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