Merge branch 'winfio'
[fio.git] / mutex.c
CommitLineData
07739b57
JA
1#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
656b1393 6#include <time.h>
07739b57
JA
7#include <pthread.h>
8#include <sys/mman.h>
9
4fa6d0f8 10#include "log.h"
07739b57 11#include "mutex.h"
4d4e80f2 12#include "arch/arch.h"
3c2d93ed 13#include "os/os.h"
3b2e1464 14#include "helpers.h"
07739b57 15
cdd18ad8 16void fio_mutex_remove(struct fio_mutex *mutex)
07739b57 17{
cdd18ad8 18 close(mutex->mutex_fd);
5921e80c 19 munmap((void *) mutex, sizeof(*mutex));
07739b57
JA
20}
21
cdd18ad8 22struct fio_mutex *fio_mutex_init(int value)
07739b57 23{
cdd18ad8
JA
24 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
25 struct fio_mutex *mutex = NULL;
07739b57 26 pthread_mutexattr_t attr;
108fcc11 27 pthread_condattr_t cond;
f356d01d 28 int fd, ret, mflag;
07739b57 29
cdd18ad8 30 fd = mkstemp(mutex_name);
07739b57 31 if (fd < 0) {
cdd18ad8 32 perror("open mutex");
07739b57
JA
33 return NULL;
34 }
35
3a8600b4 36#ifdef FIO_HAVE_FALLOCATE
9b836561 37 posix_fallocate(fd, 0, sizeof(struct fio_mutex));
3a8600b4
GE
38#endif
39
cdd18ad8
JA
40 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
41 perror("ftruncate mutex");
e53bd0b3 42 goto err;
07739b57
JA
43 }
44
5921e80c
JA
45 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
46 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
cdd18ad8
JA
47 if (mutex == MAP_FAILED) {
48 perror("mmap mutex");
07739b57 49 close(fd);
cdd18ad8 50 mutex = NULL;
e53bd0b3 51 goto err;
07739b57
JA
52 }
53
cdd18ad8
JA
54 unlink(mutex_name);
55 mutex->mutex_fd = fd;
56 mutex->value = value;
07739b57 57
f356d01d
JA
58 /*
59 * Not all platforms support process shared mutexes (FreeBSD)
60 */
61#ifdef FIO_HAVE_PSHARED_MUTEX
62 mflag = PTHREAD_PROCESS_SHARED;
63#else
64 mflag = PTHREAD_PROCESS_PRIVATE;
65#endif
66
4fa6d0f8
JA
67 ret = pthread_mutexattr_init(&attr);
68 if (ret) {
69 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
07739b57
JA
70 goto err;
71 }
7452440e 72#ifdef FIO_HAVE_PSHARED_MUTEX
f356d01d 73 ret = pthread_mutexattr_setpshared(&attr, mflag);
4fa6d0f8
JA
74 if (ret) {
75 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
07739b57
JA
76 goto err;
77 }
7452440e 78#endif
108fcc11
ZY
79
80 pthread_condattr_init(&cond);
7452440e 81#ifdef FIO_HAVE_PSHARED_MUTEX
f356d01d 82 pthread_condattr_setpshared(&cond, mflag);
7452440e 83#endif
cdd18ad8 84 pthread_cond_init(&mutex->cond, &cond);
108fcc11 85
4fa6d0f8
JA
86 ret = pthread_mutex_init(&mutex->lock, &attr);
87 if (ret) {
88 log_err("pthread_mutex_init: %s\n", strerror(ret));
07739b57
JA
89 goto err;
90 }
91
03e20d68
BC
92 pthread_condattr_destroy(&cond);
93 pthread_mutexattr_destroy(&attr);
94
cdd18ad8 95 return mutex;
07739b57 96err:
cdd18ad8
JA
97 if (mutex)
98 fio_mutex_remove(mutex);
f7c9e00e 99
cdd18ad8 100 unlink(mutex_name);
07739b57
JA
101 return NULL;
102}
103
656b1393
JA
104int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
105{
106 struct timespec t;
107 int ret = 0;
108
d481e006 109 clock_gettime(CLOCK_REALTIME, &t);
656b1393
JA
110 t.tv_sec += seconds;
111
112 pthread_mutex_lock(&mutex->lock);
113
114 while (!mutex->value && !ret) {
115 mutex->waiters++;
116 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
117 mutex->waiters--;
118 }
119
120 if (!ret) {
121 mutex->value--;
122 pthread_mutex_unlock(&mutex->lock);
123 }
124
125 return ret;
126}
127
cdd18ad8 128void fio_mutex_down(struct fio_mutex *mutex)
07739b57 129{
cdd18ad8 130 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
131
132 while (!mutex->value) {
133 mutex->waiters++;
cdd18ad8 134 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
135 mutex->waiters--;
136 }
137
cdd18ad8
JA
138 mutex->value--;
139 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
140}
141
cdd18ad8 142void fio_mutex_up(struct fio_mutex *mutex)
07739b57 143{
cdd18ad8 144 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
145 read_barrier();
146 if (!mutex->value && mutex->waiters)
cdd18ad8
JA
147 pthread_cond_signal(&mutex->cond);
148 mutex->value++;
149 pthread_mutex_unlock(&mutex->lock);
07739b57 150}
64d4d313
JA
151
152void fio_mutex_down_write(struct fio_mutex *mutex)
153{
154 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
155
156 while (mutex->value != 0) {
157 mutex->waiters++;
64d4d313 158 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
159 mutex->waiters--;
160 }
161
64d4d313
JA
162 mutex->value--;
163 pthread_mutex_unlock(&mutex->lock);
164}
165
166void fio_mutex_down_read(struct fio_mutex *mutex)
167{
168 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
169
170 while (mutex->value < 0) {
171 mutex->waiters++;
64d4d313 172 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
173 mutex->waiters--;
174 }
175
64d4d313
JA
176 mutex->value++;
177 pthread_mutex_unlock(&mutex->lock);
178}
179
180void fio_mutex_up_read(struct fio_mutex *mutex)
181{
182 pthread_mutex_lock(&mutex->lock);
183 mutex->value--;
4d4e80f2
JA
184 read_barrier();
185 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
186 pthread_cond_signal(&mutex->cond);
187 pthread_mutex_unlock(&mutex->lock);
188}
189
190void fio_mutex_up_write(struct fio_mutex *mutex)
191{
192 pthread_mutex_lock(&mutex->lock);
193 mutex->value++;
4d4e80f2
JA
194 read_barrier();
195 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
196 pthread_cond_signal(&mutex->cond);
197 pthread_mutex_unlock(&mutex->lock);
198}