include libgen.h for basename
[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 <time.h>
7 #include <pthread.h>
8 #include <sys/mman.h>
9
10 #include "log.h"
11 #include "mutex.h"
12 #include "arch/arch.h"
13 #include "os/os.h"
14 #include "helpers.h"
15
16 void fio_mutex_remove(struct fio_mutex *mutex)
17 {
18         close(mutex->mutex_fd);
19         munmap((void *) mutex, sizeof(*mutex));
20 }
21
22 struct fio_mutex *fio_mutex_init(int value)
23 {
24         char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
25         struct fio_mutex *mutex = NULL;
26         pthread_mutexattr_t attr;
27         pthread_condattr_t cond;
28         int fd, ret, mflag;
29
30         fd = mkstemp(mutex_name);
31         if (fd < 0) {
32                 perror("open mutex");
33                 return NULL;
34         }
35
36         if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
37                 perror("ftruncate mutex");
38                 goto err;
39         }
40
41         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
42                                 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43         if (mutex == MAP_FAILED) {
44                 perror("mmap mutex");
45                 close(fd);
46                 mutex = NULL;
47                 goto err;
48         }
49
50         unlink(mutex_name);
51         mutex->mutex_fd = fd;
52         mutex->value = value;
53
54         /*
55          * Not all platforms support process shared mutexes (FreeBSD)
56          */
57 #ifdef FIO_HAVE_PSHARED_MUTEX
58         mflag = PTHREAD_PROCESS_SHARED;
59 #else
60         mflag = PTHREAD_PROCESS_PRIVATE;
61 #endif
62
63         ret = pthread_mutexattr_init(&attr);
64         if (ret) {
65                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
66                 goto err;
67         }
68 #ifdef FIO_HAVE_PSHARED_MUTEX
69         ret = pthread_mutexattr_setpshared(&attr, mflag);
70         if (ret) {
71                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
72                 goto err;
73         }
74 #endif
75
76         pthread_condattr_init(&cond);
77 #ifdef FIO_HAVE_PSHARED_MUTEX
78         pthread_condattr_setpshared(&cond, mflag);
79 #endif
80         pthread_cond_init(&mutex->cond, &cond);
81
82         ret = pthread_mutex_init(&mutex->lock, &attr);
83         if (ret) {
84                 log_err("pthread_mutex_init: %s\n", strerror(ret));
85                 goto err;
86         }
87
88         return mutex;
89 err:
90         if (mutex)
91                 fio_mutex_remove(mutex);
92
93         unlink(mutex_name);
94         return NULL;
95 }
96
97 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
98 {
99         struct timespec t;
100         int ret = 0;
101
102         clock_gettime(CLOCK_REALTIME, &t);
103         t.tv_sec += seconds;
104
105         pthread_mutex_lock(&mutex->lock);
106
107         while (!mutex->value && !ret) {
108                 mutex->waiters++;
109                 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
110                 mutex->waiters--;
111         }
112
113         if (!ret) {
114                 mutex->value--;
115                 pthread_mutex_unlock(&mutex->lock);
116         }
117
118         return ret;
119 }
120
121 void fio_mutex_down(struct fio_mutex *mutex)
122 {
123         pthread_mutex_lock(&mutex->lock);
124
125         while (!mutex->value) {
126                 mutex->waiters++;
127                 pthread_cond_wait(&mutex->cond, &mutex->lock);
128                 mutex->waiters--;
129         }
130
131         mutex->value--;
132         pthread_mutex_unlock(&mutex->lock);
133 }
134
135 void fio_mutex_up(struct fio_mutex *mutex)
136 {
137         pthread_mutex_lock(&mutex->lock);
138         read_barrier();
139         if (!mutex->value && mutex->waiters)
140                 pthread_cond_signal(&mutex->cond);
141         mutex->value++;
142         pthread_mutex_unlock(&mutex->lock);
143 }
144
145 void fio_mutex_down_write(struct fio_mutex *mutex)
146 {
147         pthread_mutex_lock(&mutex->lock);
148
149         while (mutex->value != 0) {
150                 mutex->waiters++;
151                 pthread_cond_wait(&mutex->cond, &mutex->lock);
152                 mutex->waiters--;
153         }
154
155         mutex->value--;
156         pthread_mutex_unlock(&mutex->lock);
157 }
158
159 void fio_mutex_down_read(struct fio_mutex *mutex)
160 {
161         pthread_mutex_lock(&mutex->lock);
162
163         while (mutex->value < 0) {
164                 mutex->waiters++;
165                 pthread_cond_wait(&mutex->cond, &mutex->lock);
166                 mutex->waiters--;
167         }
168
169         mutex->value++;
170         pthread_mutex_unlock(&mutex->lock);
171 }
172
173 void fio_mutex_up_read(struct fio_mutex *mutex)
174 {
175         pthread_mutex_lock(&mutex->lock);
176         mutex->value--;
177         read_barrier();
178         if (mutex->value >= 0 && mutex->waiters)
179                 pthread_cond_signal(&mutex->cond);
180         pthread_mutex_unlock(&mutex->lock);
181 }
182
183 void fio_mutex_up_write(struct fio_mutex *mutex)
184 {
185         pthread_mutex_lock(&mutex->lock);
186         mutex->value++;
187         read_barrier();
188         if (mutex->value >= 0 && mutex->waiters)
189                 pthread_cond_signal(&mutex->cond);
190         pthread_mutex_unlock(&mutex->lock);
191 }