Use log_info() more consistently
[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 }
f356d01d 68 ret = pthread_mutexattr_setpshared(&attr, mflag);
4fa6d0f8
JA
69 if (ret) {
70 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
07739b57
JA
71 goto err;
72 }
108fcc11
ZY
73
74 pthread_condattr_init(&cond);
f356d01d 75 pthread_condattr_setpshared(&cond, mflag);
cdd18ad8 76 pthread_cond_init(&mutex->cond, &cond);
108fcc11 77
4fa6d0f8
JA
78 ret = pthread_mutex_init(&mutex->lock, &attr);
79 if (ret) {
80 log_err("pthread_mutex_init: %s\n", strerror(ret));
07739b57
JA
81 goto err;
82 }
83
cdd18ad8 84 return mutex;
07739b57 85err:
cdd18ad8
JA
86 if (mutex)
87 fio_mutex_remove(mutex);
f7c9e00e 88
cdd18ad8 89 unlink(mutex_name);
07739b57
JA
90 return NULL;
91}
92
656b1393
JA
93int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
94{
95 struct timespec t;
96 int ret = 0;
97
d481e006 98 clock_gettime(CLOCK_REALTIME, &t);
656b1393
JA
99 t.tv_sec += seconds;
100
101 pthread_mutex_lock(&mutex->lock);
102
103 while (!mutex->value && !ret) {
104 mutex->waiters++;
105 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
106 mutex->waiters--;
107 }
108
109 if (!ret) {
110 mutex->value--;
111 pthread_mutex_unlock(&mutex->lock);
112 }
113
114 return ret;
115}
116
cdd18ad8 117void fio_mutex_down(struct fio_mutex *mutex)
07739b57 118{
cdd18ad8 119 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
120
121 while (!mutex->value) {
122 mutex->waiters++;
cdd18ad8 123 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
124 mutex->waiters--;
125 }
126
cdd18ad8
JA
127 mutex->value--;
128 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
129}
130
cdd18ad8 131void fio_mutex_up(struct fio_mutex *mutex)
07739b57 132{
cdd18ad8 133 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
134 read_barrier();
135 if (!mutex->value && mutex->waiters)
cdd18ad8
JA
136 pthread_cond_signal(&mutex->cond);
137 mutex->value++;
138 pthread_mutex_unlock(&mutex->lock);
07739b57 139}
64d4d313
JA
140
141void fio_mutex_down_write(struct fio_mutex *mutex)
142{
143 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
144
145 while (mutex->value != 0) {
146 mutex->waiters++;
64d4d313 147 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
148 mutex->waiters--;
149 }
150
64d4d313
JA
151 mutex->value--;
152 pthread_mutex_unlock(&mutex->lock);
153}
154
155void fio_mutex_down_read(struct fio_mutex *mutex)
156{
157 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
158
159 while (mutex->value < 0) {
160 mutex->waiters++;
64d4d313 161 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
162 mutex->waiters--;
163 }
164
64d4d313
JA
165 mutex->value++;
166 pthread_mutex_unlock(&mutex->lock);
167}
168
169void fio_mutex_up_read(struct fio_mutex *mutex)
170{
171 pthread_mutex_lock(&mutex->lock);
172 mutex->value--;
4d4e80f2
JA
173 read_barrier();
174 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
175 pthread_cond_signal(&mutex->cond);
176 pthread_mutex_unlock(&mutex->lock);
177}
178
179void fio_mutex_up_write(struct fio_mutex *mutex)
180{
181 pthread_mutex_lock(&mutex->lock);
182 mutex->value++;
4d4e80f2
JA
183 read_barrier();
184 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
185 pthread_cond_signal(&mutex->cond);
186 pthread_mutex_unlock(&mutex->lock);
187}