13 #include "arch/arch.h"
19 void fio_mutex_remove(struct fio_mutex *mutex)
21 pthread_cond_destroy(&mutex->cond);
22 munmap((void *) mutex, sizeof(*mutex));
25 struct fio_mutex *fio_mutex_init(int value)
27 struct fio_mutex *mutex = NULL;
28 pthread_mutexattr_t attr;
29 pthread_condattr_t cond;
32 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
33 PROT_READ | PROT_WRITE,
34 OS_MAP_ANON | MAP_SHARED, -1, 0);
35 if (mutex == MAP_FAILED) {
43 ret = pthread_mutexattr_init(&attr);
45 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
50 * Not all platforms support process shared mutexes (FreeBSD)
52 #ifdef FIO_HAVE_PSHARED_MUTEX
53 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
55 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
60 pthread_condattr_init(&cond);
61 #ifdef FIO_HAVE_PSHARED_MUTEX
62 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
64 #ifdef FIO_HAVE_CLOCK_MONOTONIC
65 pthread_condattr_setclock(&cond, CLOCK_MONOTONIC);
67 pthread_condattr_setclock(&cond, CLOCK_REALTIME);
69 pthread_cond_init(&mutex->cond, &cond);
71 ret = pthread_mutex_init(&mutex->lock, &attr);
73 log_err("pthread_mutex_init: %s\n", strerror(ret));
77 pthread_condattr_destroy(&cond);
78 pthread_mutexattr_destroy(&attr);
83 fio_mutex_remove(mutex);
88 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
90 return mtime_since_now(t) >= seconds * 1000;
93 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
99 fio_gettime(&tv_s, NULL);
101 #ifdef FIO_HAVE_CLOCK_MONOTONIC
102 clock_gettime(CLOCK_MONOTONIC, &t);
104 clock_gettime(CLOCK_REALTIME, &t);
108 pthread_mutex_lock(&mutex->lock);
110 while (!mutex->value && !ret) {
114 * Some platforms (FreeBSD 9?) seems to return timed out
115 * way too early, double check.
117 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
118 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
119 pthread_mutex_lock(&mutex->lock);
128 pthread_mutex_unlock(&mutex->lock);
134 void fio_mutex_down(struct fio_mutex *mutex)
136 pthread_mutex_lock(&mutex->lock);
138 while (!mutex->value) {
140 pthread_cond_wait(&mutex->cond, &mutex->lock);
145 pthread_mutex_unlock(&mutex->lock);
148 void fio_mutex_up(struct fio_mutex *mutex)
150 pthread_mutex_lock(&mutex->lock);
152 if (!mutex->value && mutex->waiters)
153 pthread_cond_signal(&mutex->cond);
155 pthread_mutex_unlock(&mutex->lock);
158 void fio_mutex_down_write(struct fio_mutex *mutex)
160 pthread_mutex_lock(&mutex->lock);
162 while (mutex->value != 0) {
164 pthread_cond_wait(&mutex->cond, &mutex->lock);
169 pthread_mutex_unlock(&mutex->lock);
172 void fio_mutex_down_read(struct fio_mutex *mutex)
174 pthread_mutex_lock(&mutex->lock);
176 while (mutex->value < 0) {
178 pthread_cond_wait(&mutex->cond, &mutex->lock);
183 pthread_mutex_unlock(&mutex->lock);
186 void fio_mutex_up_read(struct fio_mutex *mutex)
188 pthread_mutex_lock(&mutex->lock);
191 if (mutex->value >= 0 && mutex->waiters)
192 pthread_cond_signal(&mutex->cond);
193 pthread_mutex_unlock(&mutex->lock);
196 void fio_mutex_up_write(struct fio_mutex *mutex)
198 pthread_mutex_lock(&mutex->lock);
201 if (mutex->value >= 0 && mutex->waiters)
202 pthread_cond_signal(&mutex->cond);
203 pthread_mutex_unlock(&mutex->lock);