mutex: more clock fixes
[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>
10
b4c1fb36 11#include "fio.h"
4fa6d0f8 12#include "log.h"
07739b57 13#include "mutex.h"
4d4e80f2 14#include "arch/arch.h"
3c2d93ed 15#include "os/os.h"
3b2e1464 16#include "helpers.h"
ef635057
JA
17#include "time.h"
18#include "gettime.h"
07739b57 19
b4c1fb36
JA
20static clockid_t fio_clk_id = CLOCK_REALTIME;
21
cdd18ad8 22void fio_mutex_remove(struct fio_mutex *mutex)
07739b57 23{
58a157d4 24 pthread_cond_destroy(&mutex->cond);
5921e80c 25 munmap((void *) mutex, sizeof(*mutex));
07739b57
JA
26}
27
cdd18ad8 28struct fio_mutex *fio_mutex_init(int value)
07739b57 29{
cdd18ad8 30 struct fio_mutex *mutex = NULL;
07739b57 31 pthread_mutexattr_t attr;
108fcc11 32 pthread_condattr_t cond;
e721c57f 33 int ret;
07739b57 34
5921e80c 35 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
b6f6abc6
JA
36 PROT_READ | PROT_WRITE,
37 OS_MAP_ANON | MAP_SHARED, -1, 0);
cdd18ad8
JA
38 if (mutex == MAP_FAILED) {
39 perror("mmap mutex");
cdd18ad8 40 mutex = NULL;
e53bd0b3 41 goto err;
07739b57
JA
42 }
43
cdd18ad8 44 mutex->value = value;
07739b57 45
4fa6d0f8
JA
46 ret = pthread_mutexattr_init(&attr);
47 if (ret) {
48 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
07739b57
JA
49 goto err;
50 }
e721c57f
JA
51
52 /*
53 * Not all platforms support process shared mutexes (FreeBSD)
54 */
7452440e 55#ifdef FIO_HAVE_PSHARED_MUTEX
e721c57f 56 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
4fa6d0f8
JA
57 if (ret) {
58 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
07739b57
JA
59 goto err;
60 }
7452440e 61#endif
108fcc11
ZY
62
63 pthread_condattr_init(&cond);
7452440e 64#ifdef FIO_HAVE_PSHARED_MUTEX
e721c57f 65 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
7452440e 66#endif
ceab2ea2 67#ifdef FIO_HAVE_CLOCK_MONOTONIC
b4c1fb36 68 pthread_condattr_setclock(&cond, fio_clk_id);
ceab2ea2 69#endif
58a157d4 70 pthread_cond_init(&mutex->cond, &cond);
108fcc11 71
4fa6d0f8
JA
72 ret = pthread_mutex_init(&mutex->lock, &attr);
73 if (ret) {
74 log_err("pthread_mutex_init: %s\n", strerror(ret));
07739b57
JA
75 goto err;
76 }
77
03e20d68
BC
78 pthread_condattr_destroy(&cond);
79 pthread_mutexattr_destroy(&attr);
80
cdd18ad8 81 return mutex;
07739b57 82err:
cdd18ad8
JA
83 if (mutex)
84 fio_mutex_remove(mutex);
f7c9e00e 85
07739b57
JA
86 return NULL;
87}
88
ef635057
JA
89static int mutex_timed_out(struct timeval *t, unsigned int seconds)
90{
91 return mtime_since_now(t) >= seconds * 1000;
92}
93
656b1393
JA
94int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
95{
ef635057 96 struct timeval tv_s;
656b1393
JA
97 struct timespec t;
98 int ret = 0;
99
ef635057
JA
100 fio_gettime(&tv_s, NULL);
101
b4c1fb36 102 clock_gettime(fio_clk_id, &t);
656b1393
JA
103 t.tv_sec += seconds;
104
105 pthread_mutex_lock(&mutex->lock);
106
107 while (!mutex->value && !ret) {
108 mutex->waiters++;
ef635057
JA
109
110 /*
111 * Some platforms (FreeBSD 9?) seems to return timed out
112 * way too early, double check.
113 */
656b1393 114 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
ef635057
JA
115 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
116 pthread_mutex_lock(&mutex->lock);
117 ret = 0;
118 }
119
656b1393
JA
120 mutex->waiters--;
121 }
122
123 if (!ret) {
124 mutex->value--;
125 pthread_mutex_unlock(&mutex->lock);
126 }
127
128 return ret;
129}
130
cdd18ad8 131void fio_mutex_down(struct fio_mutex *mutex)
07739b57 132{
cdd18ad8 133 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
134
135 while (!mutex->value) {
136 mutex->waiters++;
cdd18ad8 137 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
138 mutex->waiters--;
139 }
140
cdd18ad8
JA
141 mutex->value--;
142 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
143}
144
cdd18ad8 145void fio_mutex_up(struct fio_mutex *mutex)
07739b57 146{
cdd18ad8 147 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
148 read_barrier();
149 if (!mutex->value && mutex->waiters)
cdd18ad8
JA
150 pthread_cond_signal(&mutex->cond);
151 mutex->value++;
152 pthread_mutex_unlock(&mutex->lock);
07739b57 153}
64d4d313
JA
154
155void fio_mutex_down_write(struct fio_mutex *mutex)
156{
157 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
158
159 while (mutex->value != 0) {
160 mutex->waiters++;
64d4d313 161 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
162 mutex->waiters--;
163 }
164
64d4d313
JA
165 mutex->value--;
166 pthread_mutex_unlock(&mutex->lock);
167}
168
169void fio_mutex_down_read(struct fio_mutex *mutex)
170{
171 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
172
173 while (mutex->value < 0) {
174 mutex->waiters++;
64d4d313 175 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
176 mutex->waiters--;
177 }
178
64d4d313
JA
179 mutex->value++;
180 pthread_mutex_unlock(&mutex->lock);
181}
182
183void fio_mutex_up_read(struct fio_mutex *mutex)
184{
185 pthread_mutex_lock(&mutex->lock);
186 mutex->value--;
4d4e80f2
JA
187 read_barrier();
188 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
189 pthread_cond_signal(&mutex->cond);
190 pthread_mutex_unlock(&mutex->lock);
191}
192
193void fio_mutex_up_write(struct fio_mutex *mutex)
194{
195 pthread_mutex_lock(&mutex->lock);
196 mutex->value++;
4d4e80f2
JA
197 read_barrier();
198 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
199 pthread_cond_signal(&mutex->cond);
200 pthread_mutex_unlock(&mutex->lock);
201}
b4c1fb36
JA
202
203static void fio_init fio_mutex_global_init(void)
204{
205#ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK
206#ifdef FIO_HAVE_CLOCK_MONOTONIC
207 pthread_condattr_t cond;
208
209 pthread_condattr_init(&cond);
210
211 if (!pthread_condattr_setclock(&cond, CLOCK_MONOTONIC))
212 fio_clk_id = CLOCK_MONOTONIC;
213
214 pthread_condattr_destroy(&cond);
215#endif
216#endif
217}