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