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