12 #include "arch/arch.h"
16 void fio_mutex_remove(struct fio_mutex *mutex)
18 munmap((void *) mutex, sizeof(*mutex));
21 struct fio_mutex *fio_mutex_init(int value)
23 struct fio_mutex *mutex = NULL;
24 pthread_mutexattr_t attr;
25 pthread_condattr_t cond;
28 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
29 PROT_READ | PROT_WRITE,
30 OS_MAP_ANON | MAP_SHARED, -1, 0);
31 if (mutex == MAP_FAILED) {
40 * Not all platforms support process shared mutexes (FreeBSD)
42 #ifdef FIO_HAVE_PSHARED_MUTEX
43 mflag = PTHREAD_PROCESS_SHARED;
45 mflag = PTHREAD_PROCESS_PRIVATE;
48 ret = pthread_mutexattr_init(&attr);
50 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
53 #ifdef FIO_HAVE_PSHARED_MUTEX
54 ret = pthread_mutexattr_setpshared(&attr, mflag);
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, mflag);
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 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
89 clock_gettime(CLOCK_REALTIME, &t);
92 pthread_mutex_lock(&mutex->lock);
94 while (!mutex->value && !ret) {
96 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
102 pthread_mutex_unlock(&mutex->lock);
108 void fio_mutex_down(struct fio_mutex *mutex)
110 pthread_mutex_lock(&mutex->lock);
112 while (!mutex->value) {
114 pthread_cond_wait(&mutex->cond, &mutex->lock);
119 pthread_mutex_unlock(&mutex->lock);
122 void fio_mutex_up(struct fio_mutex *mutex)
124 pthread_mutex_lock(&mutex->lock);
126 if (!mutex->value && mutex->waiters)
127 pthread_cond_signal(&mutex->cond);
129 pthread_mutex_unlock(&mutex->lock);
132 void fio_mutex_down_write(struct fio_mutex *mutex)
134 pthread_mutex_lock(&mutex->lock);
136 while (mutex->value != 0) {
138 pthread_cond_wait(&mutex->cond, &mutex->lock);
143 pthread_mutex_unlock(&mutex->lock);
146 void fio_mutex_down_read(struct fio_mutex *mutex)
148 pthread_mutex_lock(&mutex->lock);
150 while (mutex->value < 0) {
152 pthread_cond_wait(&mutex->cond, &mutex->lock);
157 pthread_mutex_unlock(&mutex->lock);
160 void fio_mutex_up_read(struct fio_mutex *mutex)
162 pthread_mutex_lock(&mutex->lock);
165 if (mutex->value >= 0 && mutex->waiters)
166 pthread_cond_signal(&mutex->cond);
167 pthread_mutex_unlock(&mutex->lock);
170 void fio_mutex_up_write(struct fio_mutex *mutex)
172 pthread_mutex_lock(&mutex->lock);
175 if (mutex->value >= 0 && mutex->waiters)
176 pthread_cond_signal(&mutex->cond);
177 pthread_mutex_unlock(&mutex->lock);