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
GE
36#ifdef FIO_HAVE_FALLOCATE
37 ret = posix_fallocate(fd, 0, sizeof(struct fio_mutex));
38 if (ret > 0) {
39 fprintf(stderr, "posix_fallocate mutex failed: %s\n", strerror(ret));
40 goto err;
41 }
42#endif
43
cdd18ad8
JA
44 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
45 perror("ftruncate mutex");
e53bd0b3 46 goto err;
07739b57
JA
47 }
48
5921e80c
JA
49 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
50 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
cdd18ad8
JA
51 if (mutex == MAP_FAILED) {
52 perror("mmap mutex");
07739b57 53 close(fd);
cdd18ad8 54 mutex = NULL;
e53bd0b3 55 goto err;
07739b57
JA
56 }
57
cdd18ad8
JA
58 unlink(mutex_name);
59 mutex->mutex_fd = fd;
60 mutex->value = value;
07739b57 61
f356d01d
JA
62 /*
63 * Not all platforms support process shared mutexes (FreeBSD)
64 */
65#ifdef FIO_HAVE_PSHARED_MUTEX
66 mflag = PTHREAD_PROCESS_SHARED;
67#else
68 mflag = PTHREAD_PROCESS_PRIVATE;
69#endif
70
4fa6d0f8
JA
71 ret = pthread_mutexattr_init(&attr);
72 if (ret) {
73 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
07739b57
JA
74 goto err;
75 }
7452440e 76#ifdef FIO_HAVE_PSHARED_MUTEX
f356d01d 77 ret = pthread_mutexattr_setpshared(&attr, mflag);
4fa6d0f8
JA
78 if (ret) {
79 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
07739b57
JA
80 goto err;
81 }
7452440e 82#endif
108fcc11
ZY
83
84 pthread_condattr_init(&cond);
7452440e 85#ifdef FIO_HAVE_PSHARED_MUTEX
f356d01d 86 pthread_condattr_setpshared(&cond, mflag);
7452440e 87#endif
cdd18ad8 88 pthread_cond_init(&mutex->cond, &cond);
108fcc11 89
4fa6d0f8
JA
90 ret = pthread_mutex_init(&mutex->lock, &attr);
91 if (ret) {
92 log_err("pthread_mutex_init: %s\n", strerror(ret));
07739b57
JA
93 goto err;
94 }
95
03e20d68
BC
96 pthread_condattr_destroy(&cond);
97 pthread_mutexattr_destroy(&attr);
98
cdd18ad8 99 return mutex;
07739b57 100err:
cdd18ad8
JA
101 if (mutex)
102 fio_mutex_remove(mutex);
f7c9e00e 103
cdd18ad8 104 unlink(mutex_name);
07739b57
JA
105 return NULL;
106}
107
656b1393
JA
108int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
109{
110 struct timespec t;
111 int ret = 0;
112
d481e006 113 clock_gettime(CLOCK_REALTIME, &t);
656b1393
JA
114 t.tv_sec += seconds;
115
116 pthread_mutex_lock(&mutex->lock);
117
118 while (!mutex->value && !ret) {
119 mutex->waiters++;
120 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
121 mutex->waiters--;
122 }
123
124 if (!ret) {
125 mutex->value--;
126 pthread_mutex_unlock(&mutex->lock);
127 }
128
129 return ret;
130}
131
cdd18ad8 132void fio_mutex_down(struct fio_mutex *mutex)
07739b57 133{
cdd18ad8 134 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
135
136 while (!mutex->value) {
137 mutex->waiters++;
cdd18ad8 138 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
139 mutex->waiters--;
140 }
141
cdd18ad8
JA
142 mutex->value--;
143 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
144}
145
cdd18ad8 146void fio_mutex_up(struct fio_mutex *mutex)
07739b57 147{
cdd18ad8 148 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
149 read_barrier();
150 if (!mutex->value && mutex->waiters)
cdd18ad8
JA
151 pthread_cond_signal(&mutex->cond);
152 mutex->value++;
153 pthread_mutex_unlock(&mutex->lock);
07739b57 154}
64d4d313
JA
155
156void fio_mutex_down_write(struct fio_mutex *mutex)
157{
158 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
159
160 while (mutex->value != 0) {
161 mutex->waiters++;
64d4d313 162 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
163 mutex->waiters--;
164 }
165
64d4d313
JA
166 mutex->value--;
167 pthread_mutex_unlock(&mutex->lock);
168}
169
170void fio_mutex_down_read(struct fio_mutex *mutex)
171{
172 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
173
174 while (mutex->value < 0) {
175 mutex->waiters++;
64d4d313 176 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
177 mutex->waiters--;
178 }
179
64d4d313
JA
180 mutex->value++;
181 pthread_mutex_unlock(&mutex->lock);
182}
183
184void fio_mutex_up_read(struct fio_mutex *mutex)
185{
186 pthread_mutex_lock(&mutex->lock);
187 mutex->value--;
4d4e80f2
JA
188 read_barrier();
189 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
190 pthread_cond_signal(&mutex->cond);
191 pthread_mutex_unlock(&mutex->lock);
192}
193
194void fio_mutex_up_write(struct fio_mutex *mutex)
195{
196 pthread_mutex_lock(&mutex->lock);
197 mutex->value++;
4d4e80f2
JA
198 read_barrier();
199 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
200 pthread_cond_signal(&mutex->cond);
201 pthread_mutex_unlock(&mutex->lock);
202}