mutex: set pshared attributes on rw semaphore
[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>
ef635057 7#include <errno.h>
07739b57
JA
8#include <pthread.h>
9#include <sys/mman.h>
8b4e954c 10#include <assert.h>
07739b57 11
b4c1fb36 12#include "fio.h"
4fa6d0f8 13#include "log.h"
07739b57 14#include "mutex.h"
4d4e80f2 15#include "arch/arch.h"
3c2d93ed 16#include "os/os.h"
3b2e1464 17#include "helpers.h"
ef635057
JA
18#include "time.h"
19#include "gettime.h"
07739b57 20
cdd18ad8 21void fio_mutex_remove(struct fio_mutex *mutex)
07739b57 22{
8f801ad5 23 assert(mutex->magic == FIO_MUTEX_MAGIC);
58a157d4 24 pthread_cond_destroy(&mutex->cond);
5921e80c 25 munmap((void *) mutex, sizeof(*mutex));
07739b57
JA
26}
27
cdd18ad8 28struct fio_mutex *fio_mutex_init(int value)
07739b57 29{
cdd18ad8 30 struct fio_mutex *mutex = NULL;
07739b57 31 pthread_mutexattr_t attr;
108fcc11 32 pthread_condattr_t cond;
e721c57f 33 int ret;
07739b57 34
5921e80c 35 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
b6f6abc6
JA
36 PROT_READ | PROT_WRITE,
37 OS_MAP_ANON | MAP_SHARED, -1, 0);
cdd18ad8
JA
38 if (mutex == MAP_FAILED) {
39 perror("mmap mutex");
cdd18ad8 40 mutex = NULL;
e53bd0b3 41 goto err;
07739b57
JA
42 }
43
cdd18ad8 44 mutex->value = value;
8b4e954c 45 mutex->magic = FIO_MUTEX_MAGIC;
07739b57 46
4fa6d0f8
JA
47 ret = pthread_mutexattr_init(&attr);
48 if (ret) {
49 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
07739b57
JA
50 goto err;
51 }
e721c57f
JA
52
53 /*
54 * Not all platforms support process shared mutexes (FreeBSD)
55 */
7452440e 56#ifdef FIO_HAVE_PSHARED_MUTEX
e721c57f 57 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
4fa6d0f8
JA
58 if (ret) {
59 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
07739b57
JA
60 goto err;
61 }
7452440e 62#endif
108fcc11
ZY
63
64 pthread_condattr_init(&cond);
7452440e 65#ifdef FIO_HAVE_PSHARED_MUTEX
e721c57f 66 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
ceab2ea2 67#endif
58a157d4 68 pthread_cond_init(&mutex->cond, &cond);
108fcc11 69
4fa6d0f8
JA
70 ret = pthread_mutex_init(&mutex->lock, &attr);
71 if (ret) {
72 log_err("pthread_mutex_init: %s\n", strerror(ret));
07739b57
JA
73 goto err;
74 }
75
03e20d68
BC
76 pthread_condattr_destroy(&cond);
77 pthread_mutexattr_destroy(&attr);
78
cdd18ad8 79 return mutex;
07739b57 80err:
cdd18ad8
JA
81 if (mutex)
82 fio_mutex_remove(mutex);
f7c9e00e 83
07739b57
JA
84 return NULL;
85}
86
ef635057
JA
87static int mutex_timed_out(struct timeval *t, unsigned int seconds)
88{
89 return mtime_since_now(t) >= seconds * 1000;
90}
91
656b1393
JA
92int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
93{
ef635057 94 struct timeval tv_s;
656b1393
JA
95 struct timespec t;
96 int ret = 0;
97
8f801ad5 98 assert(mutex->magic == FIO_MUTEX_MAGIC);
8b4e954c 99
b11e4ccf
JA
100 gettimeofday(&tv_s, NULL);
101 t.tv_sec = tv_s.tv_sec + seconds;
102 t.tv_nsec = tv_s.tv_usec * 1000;
656b1393
JA
103
104 pthread_mutex_lock(&mutex->lock);
105
106 while (!mutex->value && !ret) {
107 mutex->waiters++;
ef635057
JA
108
109 /*
110 * Some platforms (FreeBSD 9?) seems to return timed out
111 * way too early, double check.
112 */
656b1393 113 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
d7df1d13 114 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
ef635057 115 ret = 0;
ef635057 116
656b1393
JA
117 mutex->waiters--;
118 }
119
120 if (!ret) {
121 mutex->value--;
122 pthread_mutex_unlock(&mutex->lock);
123 }
124
125 return ret;
126}
127
cdd18ad8 128void fio_mutex_down(struct fio_mutex *mutex)
07739b57 129{
8f801ad5 130 assert(mutex->magic == FIO_MUTEX_MAGIC);
8b4e954c 131
cdd18ad8 132 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
133
134 while (!mutex->value) {
135 mutex->waiters++;
cdd18ad8 136 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
137 mutex->waiters--;
138 }
139
cdd18ad8
JA
140 mutex->value--;
141 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
142}
143
cdd18ad8 144void fio_mutex_up(struct fio_mutex *mutex)
07739b57 145{
8f801ad5 146 assert(mutex->magic == FIO_MUTEX_MAGIC);
8b4e954c 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 155
d7df1d13 156void fio_rwlock_write(struct fio_rwlock *lock)
64d4d313 157{
8f801ad5 158 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13 159 pthread_rwlock_wrlock(&lock->lock);
64d4d313
JA
160}
161
d7df1d13 162void fio_rwlock_read(struct fio_rwlock *lock)
64d4d313 163{
8f801ad5 164 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13
JA
165 pthread_rwlock_rdlock(&lock->lock);
166}
4d4e80f2 167
d7df1d13
JA
168void fio_rwlock_unlock(struct fio_rwlock *lock)
169{
8f801ad5 170 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13 171 pthread_rwlock_unlock(&lock->lock);
64d4d313
JA
172}
173
d7df1d13 174void fio_rwlock_remove(struct fio_rwlock *lock)
64d4d313 175{
8f801ad5 176 assert(lock->magic == FIO_RWLOCK_MAGIC);
d7df1d13 177 munmap((void *) lock, sizeof(*lock));
64d4d313
JA
178}
179
d7df1d13 180struct fio_rwlock *fio_rwlock_init(void)
64d4d313 181{
d7df1d13 182 struct fio_rwlock *lock;
33980f5d 183 pthread_rwlockattr_t attr;
d7df1d13
JA
184 int ret;
185
186 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
187 PROT_READ | PROT_WRITE,
188 OS_MAP_ANON | MAP_SHARED, -1, 0);
189 if (lock == MAP_FAILED) {
190 perror("mmap rwlock");
191 lock = NULL;
192 goto err;
193 }
194
8b4e954c
JA
195 lock->magic = FIO_RWLOCK_MAGIC;
196
33980f5d 197 ret = pthread_rwlockattr_init(&attr);
d7df1d13
JA
198 if (ret) {
199 log_err("pthread_rwlock_init: %s\n", strerror(ret));
200 goto err;
201 }
33980f5d
SL
202#ifdef FIO_HAVE_PSHARED_MUTEX
203 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
204 if (ret) {
205 log_err("pthread_rwlock_init: %s\n", strerror(ret));
206 goto destroy_attr;
207 }
208#endif
209
210 ret = pthread_rwlock_init(&lock->lock, &attr);
211 if (ret) {
212 log_err("pthread_rwlock_init: %s\n", strerror(ret));
213 goto destroy_attr;
214 }
215
216 pthread_rwlockattr_destroy(&attr);
d7df1d13
JA
217
218 return lock;
33980f5d
SL
219destroy_attr:
220 pthread_rwlockattr_destroy(&attr);
d7df1d13
JA
221err:
222 if (lock)
223 fio_rwlock_remove(lock);
224 return NULL;
64d4d313 225}