12 #include "arch/arch.h"
16 void fio_mutex_remove(struct fio_mutex *mutex)
18 close(mutex->mutex_fd);
19 munmap((void *) mutex, sizeof(*mutex));
22 struct fio_mutex *fio_mutex_init(int value)
24 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
25 struct fio_mutex *mutex = NULL;
26 pthread_mutexattr_t attr;
27 pthread_condattr_t cond;
30 fd = mkstemp(mutex_name);
36 #ifdef FIO_HAVE_FALLOCATE
37 posix_fallocate(fd, 0, sizeof(struct fio_mutex));
40 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
41 perror("ftruncate mutex");
45 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
46 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
47 if (mutex == MAP_FAILED) {
59 * Not all platforms support process shared mutexes (FreeBSD)
61 #ifdef FIO_HAVE_PSHARED_MUTEX
62 mflag = PTHREAD_PROCESS_SHARED;
64 mflag = PTHREAD_PROCESS_PRIVATE;
67 ret = pthread_mutexattr_init(&attr);
69 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
72 #ifdef FIO_HAVE_PSHARED_MUTEX
73 ret = pthread_mutexattr_setpshared(&attr, mflag);
75 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
80 pthread_condattr_init(&cond);
81 #ifdef FIO_HAVE_PSHARED_MUTEX
82 pthread_condattr_setpshared(&cond, mflag);
84 pthread_cond_init(&mutex->cond, &cond);
86 ret = pthread_mutex_init(&mutex->lock, &attr);
88 log_err("pthread_mutex_init: %s\n", strerror(ret));
92 pthread_condattr_destroy(&cond);
93 pthread_mutexattr_destroy(&attr);
98 fio_mutex_remove(mutex);
104 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
109 clock_gettime(CLOCK_REALTIME, &t);
112 pthread_mutex_lock(&mutex->lock);
114 while (!mutex->value && !ret) {
116 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
122 pthread_mutex_unlock(&mutex->lock);
128 void fio_mutex_down(struct fio_mutex *mutex)
130 pthread_mutex_lock(&mutex->lock);
132 while (!mutex->value) {
134 pthread_cond_wait(&mutex->cond, &mutex->lock);
139 pthread_mutex_unlock(&mutex->lock);
142 void fio_mutex_up(struct fio_mutex *mutex)
144 pthread_mutex_lock(&mutex->lock);
146 if (!mutex->value && mutex->waiters)
147 pthread_cond_signal(&mutex->cond);
149 pthread_mutex_unlock(&mutex->lock);
152 void fio_mutex_down_write(struct fio_mutex *mutex)
154 pthread_mutex_lock(&mutex->lock);
156 while (mutex->value != 0) {
158 pthread_cond_wait(&mutex->cond, &mutex->lock);
163 pthread_mutex_unlock(&mutex->lock);
166 void fio_mutex_down_read(struct fio_mutex *mutex)
168 pthread_mutex_lock(&mutex->lock);
170 while (mutex->value < 0) {
172 pthread_cond_wait(&mutex->cond, &mutex->lock);
177 pthread_mutex_unlock(&mutex->lock);
180 void fio_mutex_up_read(struct fio_mutex *mutex)
182 pthread_mutex_lock(&mutex->lock);
185 if (mutex->value >= 0 && mutex->waiters)
186 pthread_cond_signal(&mutex->cond);
187 pthread_mutex_unlock(&mutex->lock);
190 void fio_mutex_up_write(struct fio_mutex *mutex)
192 pthread_mutex_lock(&mutex->lock);
195 if (mutex->value >= 0 && mutex->waiters)
196 pthread_cond_signal(&mutex->cond);
197 pthread_mutex_unlock(&mutex->lock);