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));
96 pthread_condattr_destroy(&cond);
97 pthread_mutexattr_destroy(&attr);
102 fio_mutex_remove(mutex);
108 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
113 clock_gettime(CLOCK_REALTIME, &t);
116 pthread_mutex_lock(&mutex->lock);
118 while (!mutex->value && !ret) {
120 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
126 pthread_mutex_unlock(&mutex->lock);
132 void fio_mutex_down(struct fio_mutex *mutex)
134 pthread_mutex_lock(&mutex->lock);
136 while (!mutex->value) {
138 pthread_cond_wait(&mutex->cond, &mutex->lock);
143 pthread_mutex_unlock(&mutex->lock);
146 void fio_mutex_up(struct fio_mutex *mutex)
148 pthread_mutex_lock(&mutex->lock);
150 if (!mutex->value && mutex->waiters)
151 pthread_cond_signal(&mutex->cond);
153 pthread_mutex_unlock(&mutex->lock);
156 void fio_mutex_down_write(struct fio_mutex *mutex)
158 pthread_mutex_lock(&mutex->lock);
160 while (mutex->value != 0) {
162 pthread_cond_wait(&mutex->cond, &mutex->lock);
167 pthread_mutex_unlock(&mutex->lock);
170 void fio_mutex_down_read(struct fio_mutex *mutex)
172 pthread_mutex_lock(&mutex->lock);
174 while (mutex->value < 0) {
176 pthread_cond_wait(&mutex->cond, &mutex->lock);
181 pthread_mutex_unlock(&mutex->lock);
184 void fio_mutex_up_read(struct fio_mutex *mutex)
186 pthread_mutex_lock(&mutex->lock);
189 if (mutex->value >= 0 && mutex->waiters)
190 pthread_cond_signal(&mutex->cond);
191 pthread_mutex_unlock(&mutex->lock);
194 void fio_mutex_up_write(struct fio_mutex *mutex)
196 pthread_mutex_lock(&mutex->lock);
199 if (mutex->value >= 0 && mutex->waiters)
200 pthread_cond_signal(&mutex->cond);
201 pthread_mutex_unlock(&mutex->lock);