fix ramp_in
[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
cdd18ad8 96 return mutex;
07739b57 97err:
cdd18ad8
JA
98 if (mutex)
99 fio_mutex_remove(mutex);
f7c9e00e 100
cdd18ad8 101 unlink(mutex_name);
07739b57
JA
102 return NULL;
103}
104
656b1393
JA
105int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
106{
107 struct timespec t;
108 int ret = 0;
109
d481e006 110 clock_gettime(CLOCK_REALTIME, &t);
656b1393
JA
111 t.tv_sec += seconds;
112
113 pthread_mutex_lock(&mutex->lock);
114
115 while (!mutex->value && !ret) {
116 mutex->waiters++;
117 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
118 mutex->waiters--;
119 }
120
121 if (!ret) {
122 mutex->value--;
123 pthread_mutex_unlock(&mutex->lock);
124 }
125
126 return ret;
127}
128
cdd18ad8 129void fio_mutex_down(struct fio_mutex *mutex)
07739b57 130{
cdd18ad8 131 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
132
133 while (!mutex->value) {
134 mutex->waiters++;
cdd18ad8 135 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
136 mutex->waiters--;
137 }
138
cdd18ad8
JA
139 mutex->value--;
140 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
141}
142
cdd18ad8 143void fio_mutex_up(struct fio_mutex *mutex)
07739b57 144{
cdd18ad8 145 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
146 read_barrier();
147 if (!mutex->value && mutex->waiters)
cdd18ad8
JA
148 pthread_cond_signal(&mutex->cond);
149 mutex->value++;
150 pthread_mutex_unlock(&mutex->lock);
07739b57 151}
64d4d313
JA
152
153void fio_mutex_down_write(struct fio_mutex *mutex)
154{
155 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
156
157 while (mutex->value != 0) {
158 mutex->waiters++;
64d4d313 159 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
160 mutex->waiters--;
161 }
162
64d4d313
JA
163 mutex->value--;
164 pthread_mutex_unlock(&mutex->lock);
165}
166
167void fio_mutex_down_read(struct fio_mutex *mutex)
168{
169 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
170
171 while (mutex->value < 0) {
172 mutex->waiters++;
64d4d313 173 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
174 mutex->waiters--;
175 }
176
64d4d313
JA
177 mutex->value++;
178 pthread_mutex_unlock(&mutex->lock);
179}
180
181void fio_mutex_up_read(struct fio_mutex *mutex)
182{
183 pthread_mutex_lock(&mutex->lock);
184 mutex->value--;
4d4e80f2
JA
185 read_barrier();
186 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
187 pthread_cond_signal(&mutex->cond);
188 pthread_mutex_unlock(&mutex->lock);
189}
190
191void fio_mutex_up_write(struct fio_mutex *mutex)
192{
193 pthread_mutex_lock(&mutex->lock);
194 mutex->value++;
4d4e80f2
JA
195 read_barrier();
196 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
197 pthread_cond_signal(&mutex->cond);
198 pthread_mutex_unlock(&mutex->lock);
199}