mutex: fixup fio_mutex_down_timeout()
[fio.git] / mutex.c
CommitLineData
07739b57
JA
1#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
656b1393 6#include <time.h>
ef635057 7#include <errno.h>
07739b57
JA
8#include <pthread.h>
9#include <sys/mman.h>
8b4e954c 10#include <assert.h>
07739b57 11
b4c1fb36 12#include "fio.h"
4fa6d0f8 13#include "log.h"
07739b57 14#include "mutex.h"
4d4e80f2 15#include "arch/arch.h"
3c2d93ed 16#include "os/os.h"
3b2e1464 17#include "helpers.h"
ef3c94b6 18#include "fio_time.h"
ef635057 19#include "gettime.h"
07739b57 20
f5a42524 21void __fio_mutex_remove(struct fio_mutex *mutex)
07739b57 22{
8f801ad5 23 assert(mutex->magic == FIO_MUTEX_MAGIC);
58a157d4 24 pthread_cond_destroy(&mutex->cond);
f5a42524
JA
25}
26
27void fio_mutex_remove(struct fio_mutex *mutex)
28{
29 __fio_mutex_remove(mutex);
5921e80c 30 munmap((void *) mutex, sizeof(*mutex));
07739b57
JA
31}
32
72242057 33int __fio_mutex_init(struct fio_mutex *mutex, int value)
07739b57
JA
34{
35 pthread_mutexattr_t attr;
108fcc11 36 pthread_condattr_t cond;
e721c57f 37 int ret;
07739b57 38
cdd18ad8 39 mutex->value = value;
8b4e954c 40 mutex->magic = FIO_MUTEX_MAGIC;
07739b57 41
4fa6d0f8
JA
42 ret = pthread_mutexattr_init(&attr);
43 if (ret) {
44 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
72242057 45 return ret;
07739b57 46 }
e721c57f
JA
47
48 /*
49 * Not all platforms support process shared mutexes (FreeBSD)
50 */
7452440e 51#ifdef FIO_HAVE_PSHARED_MUTEX
e721c57f 52 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
4fa6d0f8
JA
53 if (ret) {
54 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
72242057 55 return ret;
07739b57 56 }
7452440e 57#endif
108fcc11
ZY
58
59 pthread_condattr_init(&cond);
7452440e 60#ifdef FIO_HAVE_PSHARED_MUTEX
e721c57f 61 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
ceab2ea2 62#endif
58a157d4 63 pthread_cond_init(&mutex->cond, &cond);
108fcc11 64
4fa6d0f8
JA
65 ret = pthread_mutex_init(&mutex->lock, &attr);
66 if (ret) {
67 log_err("pthread_mutex_init: %s\n", strerror(ret));
72242057 68 return ret;
07739b57
JA
69 }
70
03e20d68
BC
71 pthread_condattr_destroy(&cond);
72 pthread_mutexattr_destroy(&attr);
72242057
JA
73 return 0;
74}
75
76struct fio_mutex *fio_mutex_init(int value)
77{
78 struct fio_mutex *mutex = NULL;
79
80 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
81 PROT_READ | PROT_WRITE,
82 OS_MAP_ANON | MAP_SHARED, -1, 0);
83 if (mutex == MAP_FAILED) {
84 perror("mmap mutex");
85 return NULL;
86 }
03e20d68 87
72242057
JA
88 if (!__fio_mutex_init(mutex, value))
89 return mutex;
f7c9e00e 90
72242057 91 fio_mutex_remove(mutex);
07739b57
JA
92 return NULL;
93}
94
09400a60 95static bool mutex_timed_out(struct timeval *t, unsigned int msecs)
ef635057 96{
09400a60
JA
97 struct timeval now;
98
99 gettimeofday(&now, NULL);
100 return mtime_since(t, &now) >= msecs;
ef635057
JA
101}
102
09400a60 103int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int msecs)
656b1393 104{
ef635057 105 struct timeval tv_s;
656b1393
JA
106 struct timespec t;
107 int ret = 0;
108
8f801ad5 109 assert(mutex->magic == FIO_MUTEX_MAGIC);
8b4e954c 110
b11e4ccf 111 gettimeofday(&tv_s, NULL);
09400a60 112 t.tv_sec = tv_s.tv_sec;
b11e4ccf 113 t.tv_nsec = tv_s.tv_usec * 1000;
656b1393 114
09400a60
JA
115 t.tv_sec += msecs / 1000;
116 t.tv_nsec += ((msecs * 1000000) % 1000000000);
117 if (t.tv_nsec >= 1000000000) {
118 t.tv_nsec -= 1000000000;
119 t.tv_sec++;
120 }
121
656b1393
JA
122 pthread_mutex_lock(&mutex->lock);
123
09400a60 124 mutex->waiters++;
656b1393 125 while (!mutex->value && !ret) {
ef635057
JA
126 /*
127 * Some platforms (FreeBSD 9?) seems to return timed out
128 * way too early, double check.
129 */
656b1393 130 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
09400a60 131 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, msecs))
ef635057 132 ret = 0;
656b1393 133 }
09400a60 134 mutex->waiters--;
656b1393
JA
135
136 if (!ret) {
137 mutex->value--;
138 pthread_mutex_unlock(&mutex->lock);
139 }
140
09400a60 141 pthread_mutex_unlock(&mutex->lock);
656b1393
JA
142 return ret;
143}
144
66608372 145bool fio_mutex_down_trylock(struct fio_mutex *mutex)
72242057 146{
66608372 147 bool ret = true;
72242057
JA
148
149 assert(mutex->magic == FIO_MUTEX_MAGIC);
150
151 pthread_mutex_lock(&mutex->lock);
152 if (mutex->value) {
153 mutex->value--;
66608372 154 ret = false;
72242057
JA
155 }
156 pthread_mutex_unlock(&mutex->lock);
157
158 return ret;
159}
160
cdd18ad8 161void fio_mutex_down(struct fio_mutex *mutex)
07739b57 162{
8f801ad5 163 assert(mutex->magic == FIO_MUTEX_MAGIC);
8b4e954c 164
cdd18ad8 165 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
166
167 while (!mutex->value) {
168 mutex->waiters++;
cdd18ad8 169 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
170 mutex->waiters--;
171 }
172
cdd18ad8
JA
173 mutex->value--;
174 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
175}
176
cdd18ad8 177void fio_mutex_up(struct fio_mutex *mutex)
07739b57 178{
6899b6cb
JA
179 int do_wake = 0;
180
8f801ad5 181 assert(mutex->magic == FIO_MUTEX_MAGIC);
8b4e954c 182
cdd18ad8 183 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
184 read_barrier();
185 if (!mutex->value && mutex->waiters)
6899b6cb 186 do_wake = 1;
cdd18ad8
JA
187 mutex->value++;
188 pthread_mutex_unlock(&mutex->lock);
6899b6cb
JA
189
190 if (do_wake)
191 pthread_cond_signal(&mutex->cond);
07739b57 192}
64d4d313 193
d7df1d13 194void fio_rwlock_write(struct fio_rwlock *lock)
64d4d313 195{
8f801ad5 196 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13 197 pthread_rwlock_wrlock(&lock->lock);
64d4d313
JA
198}
199
d7df1d13 200void fio_rwlock_read(struct fio_rwlock *lock)
64d4d313 201{
8f801ad5 202 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13
JA
203 pthread_rwlock_rdlock(&lock->lock);
204}
4d4e80f2 205
d7df1d13
JA
206void fio_rwlock_unlock(struct fio_rwlock *lock)
207{
8f801ad5 208 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13 209 pthread_rwlock_unlock(&lock->lock);
64d4d313
JA
210}
211
d7df1d13 212void fio_rwlock_remove(struct fio_rwlock *lock)
64d4d313 213{
8f801ad5 214 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13 215 munmap((void *) lock, sizeof(*lock));
64d4d313
JA
216}
217
d7df1d13 218struct fio_rwlock *fio_rwlock_init(void)
64d4d313 219{
d7df1d13 220 struct fio_rwlock *lock;
33980f5d 221 pthread_rwlockattr_t attr;
d7df1d13
JA
222 int ret;
223
224 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
225 PROT_READ | PROT_WRITE,
226 OS_MAP_ANON | MAP_SHARED, -1, 0);
227 if (lock == MAP_FAILED) {
228 perror("mmap rwlock");
229 lock = NULL;
230 goto err;
231 }
232
8b4e954c
JA
233 lock->magic = FIO_RWLOCK_MAGIC;
234
33980f5d 235 ret = pthread_rwlockattr_init(&attr);
d7df1d13 236 if (ret) {
cdb57fe2 237 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
d7df1d13
JA
238 goto err;
239 }
33980f5d
SL
240#ifdef FIO_HAVE_PSHARED_MUTEX
241 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
242 if (ret) {
cdb57fe2 243 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
33980f5d
SL
244 goto destroy_attr;
245 }
33980f5d
SL
246
247 ret = pthread_rwlock_init(&lock->lock, &attr);
09f17d62
BC
248#else
249 ret = pthread_rwlock_init(&lock->lock, NULL);
250#endif
251
33980f5d
SL
252 if (ret) {
253 log_err("pthread_rwlock_init: %s\n", strerror(ret));
254 goto destroy_attr;
255 }
256
257 pthread_rwlockattr_destroy(&attr);
d7df1d13
JA
258
259 return lock;
33980f5d
SL
260destroy_attr:
261 pthread_rwlockattr_destroy(&attr);
d7df1d13
JA
262err:
263 if (lock)
264 fio_rwlock_remove(lock);
265 return NULL;
64d4d313 266}