14 #include "arch/arch.h"
20 void fio_mutex_remove(struct fio_mutex *mutex)
22 pthread_cond_destroy(&mutex->cond);
23 munmap((void *) mutex, sizeof(*mutex));
26 struct fio_mutex *fio_mutex_init(int value)
28 struct fio_mutex *mutex = NULL;
29 pthread_mutexattr_t attr;
30 pthread_condattr_t cond;
33 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
34 PROT_READ | PROT_WRITE,
35 OS_MAP_ANON | MAP_SHARED, -1, 0);
36 if (mutex == MAP_FAILED) {
44 ret = pthread_mutexattr_init(&attr);
46 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
51 * Not all platforms support process shared mutexes (FreeBSD)
53 #ifdef FIO_HAVE_PSHARED_MUTEX
54 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
56 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
61 pthread_condattr_init(&cond);
62 #ifdef FIO_HAVE_PSHARED_MUTEX
63 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
65 pthread_cond_init(&mutex->cond, &cond);
67 ret = pthread_mutex_init(&mutex->lock, &attr);
69 log_err("pthread_mutex_init: %s\n", strerror(ret));
73 pthread_condattr_destroy(&cond);
74 pthread_mutexattr_destroy(&attr);
79 fio_mutex_remove(mutex);
84 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
86 return mtime_since_now(t) >= seconds * 1000;
89 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
95 gettimeofday(&tv_s, NULL);
96 t.tv_sec = tv_s.tv_sec + seconds;
97 t.tv_nsec = tv_s.tv_usec * 1000;
99 pthread_mutex_lock(&mutex->lock);
101 while (!mutex->value && !ret) {
105 * Some platforms (FreeBSD 9?) seems to return timed out
106 * way too early, double check.
108 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
109 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
110 pthread_mutex_lock(&mutex->lock);
119 pthread_mutex_unlock(&mutex->lock);
125 void fio_mutex_down(struct fio_mutex *mutex)
127 pthread_mutex_lock(&mutex->lock);
129 while (!mutex->value) {
131 pthread_cond_wait(&mutex->cond, &mutex->lock);
136 pthread_mutex_unlock(&mutex->lock);
139 void fio_mutex_up(struct fio_mutex *mutex)
141 pthread_mutex_lock(&mutex->lock);
143 if (!mutex->value && mutex->waiters)
144 pthread_cond_signal(&mutex->cond);
146 pthread_mutex_unlock(&mutex->lock);
149 void fio_mutex_down_write(struct fio_mutex *mutex)
151 pthread_mutex_lock(&mutex->lock);
153 while (mutex->value != 0) {
155 pthread_cond_wait(&mutex->cond, &mutex->lock);
160 pthread_mutex_unlock(&mutex->lock);
163 void fio_mutex_down_read(struct fio_mutex *mutex)
165 pthread_mutex_lock(&mutex->lock);
167 while (mutex->value < 0) {
169 pthread_cond_wait(&mutex->cond, &mutex->lock);
174 pthread_mutex_unlock(&mutex->lock);
177 void fio_mutex_up_read(struct fio_mutex *mutex)
179 pthread_mutex_lock(&mutex->lock);
182 if (mutex->value >= 0 && mutex->waiters)
183 pthread_cond_signal(&mutex->cond);
184 pthread_mutex_unlock(&mutex->lock);
187 void fio_mutex_up_write(struct fio_mutex *mutex)
189 pthread_mutex_lock(&mutex->lock);
192 if (mutex->value >= 0 && mutex->waiters)
193 pthread_cond_signal(&mutex->cond);
194 pthread_mutex_unlock(&mutex->lock);