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 ret = posix_fallocate(fd, 0, sizeof(struct fio_mutex));
39 fprintf(stderr, "posix_fallocate mutex failed: %s\n", strerror(ret));
44 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
45 perror("ftruncate mutex");
49 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
50 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
51 if (mutex == MAP_FAILED) {
63 * Not all platforms support process shared mutexes (FreeBSD)
65 #ifdef FIO_HAVE_PSHARED_MUTEX
66 mflag = PTHREAD_PROCESS_SHARED;
68 mflag = PTHREAD_PROCESS_PRIVATE;
71 ret = pthread_mutexattr_init(&attr);
73 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
76 #ifdef FIO_HAVE_PSHARED_MUTEX
77 ret = pthread_mutexattr_setpshared(&attr, mflag);
79 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
84 pthread_condattr_init(&cond);
85 #ifdef FIO_HAVE_PSHARED_MUTEX
86 pthread_condattr_setpshared(&cond, mflag);
88 pthread_cond_init(&mutex->cond, &cond);
90 ret = pthread_mutex_init(&mutex->lock, &attr);
92 log_err("pthread_mutex_init: %s\n", strerror(ret));
99 fio_mutex_remove(mutex);
105 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
110 clock_gettime(CLOCK_REALTIME, &t);
113 pthread_mutex_lock(&mutex->lock);
115 while (!mutex->value && !ret) {
117 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
123 pthread_mutex_unlock(&mutex->lock);
129 void fio_mutex_down(struct fio_mutex *mutex)
131 pthread_mutex_lock(&mutex->lock);
133 while (!mutex->value) {
135 pthread_cond_wait(&mutex->cond, &mutex->lock);
140 pthread_mutex_unlock(&mutex->lock);
143 void fio_mutex_up(struct fio_mutex *mutex)
145 pthread_mutex_lock(&mutex->lock);
147 if (!mutex->value && mutex->waiters)
148 pthread_cond_signal(&mutex->cond);
150 pthread_mutex_unlock(&mutex->lock);
153 void fio_mutex_down_write(struct fio_mutex *mutex)
155 pthread_mutex_lock(&mutex->lock);
157 while (mutex->value != 0) {
159 pthread_cond_wait(&mutex->cond, &mutex->lock);
164 pthread_mutex_unlock(&mutex->lock);
167 void fio_mutex_down_read(struct fio_mutex *mutex)
169 pthread_mutex_lock(&mutex->lock);
171 while (mutex->value < 0) {
173 pthread_cond_wait(&mutex->cond, &mutex->lock);
178 pthread_mutex_unlock(&mutex->lock);
181 void fio_mutex_up_read(struct fio_mutex *mutex)
183 pthread_mutex_lock(&mutex->lock);
186 if (mutex->value >= 0 && mutex->waiters)
187 pthread_cond_signal(&mutex->cond);
188 pthread_mutex_unlock(&mutex->lock);
191 void fio_mutex_up_write(struct fio_mutex *mutex)
193 pthread_mutex_lock(&mutex->lock);
196 if (mutex->value >= 0 && mutex->waiters)
197 pthread_cond_signal(&mutex->cond);
198 pthread_mutex_unlock(&mutex->lock);