Use read-writer locks in smalloc
[fio.git] / mutex.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <pthread.h>
7 #include <sys/mman.h>
8
9 #include "mutex.h"
10
11 void fio_mutex_remove(struct fio_mutex *mutex)
12 {
13         close(mutex->mutex_fd);
14         munmap(mutex, sizeof(*mutex));
15 }
16
17 struct fio_mutex *fio_mutex_init(int value)
18 {
19         char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
20         struct fio_mutex *mutex = NULL;
21         pthread_mutexattr_t attr;
22         pthread_condattr_t cond;
23         int fd;
24
25         fd = mkstemp(mutex_name);
26         if (fd < 0) {
27                 perror("open mutex");
28                 return NULL;
29         }
30
31         if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
32                 perror("ftruncate mutex");
33                 goto err;
34         }
35
36         mutex = mmap(NULL, sizeof(struct fio_mutex), PROT_READ | PROT_WRITE,
37                         MAP_SHARED, fd, 0);
38         if (mutex == MAP_FAILED) {
39                 perror("mmap mutex");
40                 close(fd);
41                 mutex = NULL;
42                 goto err;
43         }
44
45         unlink(mutex_name);
46         mutex->mutex_fd = fd;
47         mutex->value = value;
48
49         if (pthread_mutexattr_init(&attr)) {
50                 perror("pthread_mutexattr_init");
51                 goto err;
52         }
53         if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
54                 perror("pthread_mutexattr_setpshared");
55                 goto err;
56         }
57
58         pthread_condattr_init(&cond);
59         pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
60         pthread_cond_init(&mutex->cond, &cond);
61
62         if (pthread_mutex_init(&mutex->lock, &attr)) {
63                 perror("pthread_mutex_init");
64                 goto err;
65         }
66
67         return mutex;
68 err:
69         if (mutex)
70                 fio_mutex_remove(mutex);
71
72         unlink(mutex_name);
73         return NULL;
74 }
75
76 void fio_mutex_down(struct fio_mutex *mutex)
77 {
78         pthread_mutex_lock(&mutex->lock);
79         while (mutex->value == 0)
80                 pthread_cond_wait(&mutex->cond, &mutex->lock);
81         mutex->value--;
82         pthread_mutex_unlock(&mutex->lock);
83 }
84
85 void fio_mutex_up(struct fio_mutex *mutex)
86 {
87         pthread_mutex_lock(&mutex->lock);
88         if (!mutex->value)
89                 pthread_cond_signal(&mutex->cond);
90         mutex->value++;
91         pthread_mutex_unlock(&mutex->lock);
92 }
93
94 void fio_mutex_down_write(struct fio_mutex *mutex)
95 {
96         pthread_mutex_lock(&mutex->lock);
97         while (mutex->value != 0)
98                 pthread_cond_wait(&mutex->cond, &mutex->lock);
99         mutex->value--;
100         pthread_mutex_unlock(&mutex->lock);
101 }
102
103 void fio_mutex_down_read(struct fio_mutex *mutex)
104 {
105         pthread_mutex_lock(&mutex->lock);
106         while (mutex->value < 0)
107                 pthread_cond_wait(&mutex->cond, &mutex->lock);
108         mutex->value++;
109         pthread_mutex_unlock(&mutex->lock);
110 }
111
112 void fio_mutex_up_read(struct fio_mutex *mutex)
113 {
114         pthread_mutex_lock(&mutex->lock);
115         mutex->value--;
116         if (mutex->value >= 0)
117                 pthread_cond_signal(&mutex->cond);
118         pthread_mutex_unlock(&mutex->lock);
119 }
120
121 void fio_mutex_up_write(struct fio_mutex *mutex)
122 {
123         pthread_mutex_lock(&mutex->lock);
124         mutex->value++;
125         if (mutex->value >= 0)
126                 pthread_cond_signal(&mutex->cond);
127         pthread_mutex_unlock(&mutex->lock);
128 }