14 #include "arch/arch.h"
20 static clockid_t fio_clk_id = CLOCK_REALTIME;
22 void fio_mutex_remove(struct fio_mutex *mutex)
24 pthread_cond_destroy(&mutex->cond);
25 munmap((void *) mutex, sizeof(*mutex));
28 struct fio_mutex *fio_mutex_init(int value)
30 struct fio_mutex *mutex = NULL;
31 pthread_mutexattr_t attr;
32 pthread_condattr_t cond;
35 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
36 PROT_READ | PROT_WRITE,
37 OS_MAP_ANON | MAP_SHARED, -1, 0);
38 if (mutex == MAP_FAILED) {
46 ret = pthread_mutexattr_init(&attr);
48 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
53 * Not all platforms support process shared mutexes (FreeBSD)
55 #ifdef FIO_HAVE_PSHARED_MUTEX
56 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
58 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
63 pthread_condattr_init(&cond);
64 #ifdef FIO_HAVE_PSHARED_MUTEX
65 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
67 #ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK
68 pthread_condattr_setclock(&cond, fio_clk_id);
70 pthread_cond_init(&mutex->cond, &cond);
72 ret = pthread_mutex_init(&mutex->lock, &attr);
74 log_err("pthread_mutex_init: %s\n", strerror(ret));
78 pthread_condattr_destroy(&cond);
79 pthread_mutexattr_destroy(&attr);
84 fio_mutex_remove(mutex);
89 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
91 return mtime_since_now(t) >= seconds * 1000;
94 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
100 fio_gettime(&tv_s, NULL);
102 clock_gettime(fio_clk_id, &t);
105 pthread_mutex_lock(&mutex->lock);
107 while (!mutex->value && !ret) {
111 * Some platforms (FreeBSD 9?) seems to return timed out
112 * way too early, double check.
114 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
115 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
116 pthread_mutex_lock(&mutex->lock);
125 pthread_mutex_unlock(&mutex->lock);
131 void fio_mutex_down(struct fio_mutex *mutex)
133 pthread_mutex_lock(&mutex->lock);
135 while (!mutex->value) {
137 pthread_cond_wait(&mutex->cond, &mutex->lock);
142 pthread_mutex_unlock(&mutex->lock);
145 void fio_mutex_up(struct fio_mutex *mutex)
147 pthread_mutex_lock(&mutex->lock);
149 if (!mutex->value && mutex->waiters)
150 pthread_cond_signal(&mutex->cond);
152 pthread_mutex_unlock(&mutex->lock);
155 void fio_mutex_down_write(struct fio_mutex *mutex)
157 pthread_mutex_lock(&mutex->lock);
159 while (mutex->value != 0) {
161 pthread_cond_wait(&mutex->cond, &mutex->lock);
166 pthread_mutex_unlock(&mutex->lock);
169 void fio_mutex_down_read(struct fio_mutex *mutex)
171 pthread_mutex_lock(&mutex->lock);
173 while (mutex->value < 0) {
175 pthread_cond_wait(&mutex->cond, &mutex->lock);
180 pthread_mutex_unlock(&mutex->lock);
183 void fio_mutex_up_read(struct fio_mutex *mutex)
185 pthread_mutex_lock(&mutex->lock);
188 if (mutex->value >= 0 && mutex->waiters)
189 pthread_cond_signal(&mutex->cond);
190 pthread_mutex_unlock(&mutex->lock);
193 void fio_mutex_up_write(struct fio_mutex *mutex)
195 pthread_mutex_lock(&mutex->lock);
198 if (mutex->value >= 0 && mutex->waiters)
199 pthread_cond_signal(&mutex->cond);
200 pthread_mutex_unlock(&mutex->lock);
203 static void fio_init fio_mutex_global_init(void)
205 #ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK
206 #ifdef FIO_HAVE_CLOCK_MONOTONIC
207 pthread_condattr_t cond;
209 pthread_condattr_init(&cond);
211 if (!pthread_condattr_setclock(&cond, CLOCK_MONOTONIC))
212 fio_clk_id = CLOCK_MONOTONIC;
214 pthread_condattr_destroy(&cond);