Merge branch 'master' into gfio
[fio.git] / mutex.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
6#include <time.h>
7#include <errno.h>
8#include <pthread.h>
9#include <sys/mman.h>
10
11#include "fio.h"
12#include "log.h"
13#include "mutex.h"
14#include "arch/arch.h"
15#include "os/os.h"
16#include "helpers.h"
17#include "time.h"
18#include "gettime.h"
19
20void fio_mutex_remove(struct fio_mutex *mutex)
21{
22 pthread_cond_destroy(&mutex->cond);
23 munmap((void *) mutex, sizeof(*mutex));
24}
25
26struct fio_mutex *fio_mutex_init(int value)
27{
28 struct fio_mutex *mutex = NULL;
29 pthread_mutexattr_t attr;
30 pthread_condattr_t cond;
31 int ret;
32
33 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
34 PROT_READ | PROT_WRITE,
35 OS_MAP_ANON | MAP_SHARED, -1, 0);
36 if (mutex == MAP_FAILED) {
37 perror("mmap mutex");
38 mutex = NULL;
39 goto err;
40 }
41
42 mutex->value = value;
43
44 ret = pthread_mutexattr_init(&attr);
45 if (ret) {
46 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
47 goto err;
48 }
49
50 /*
51 * Not all platforms support process shared mutexes (FreeBSD)
52 */
53#ifdef FIO_HAVE_PSHARED_MUTEX
54 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
55 if (ret) {
56 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
57 goto err;
58 }
59#endif
60
61 pthread_condattr_init(&cond);
62#ifdef FIO_HAVE_PSHARED_MUTEX
63 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
64#endif
65 pthread_cond_init(&mutex->cond, &cond);
66
67 ret = pthread_mutex_init(&mutex->lock, &attr);
68 if (ret) {
69 log_err("pthread_mutex_init: %s\n", strerror(ret));
70 goto err;
71 }
72
73 pthread_condattr_destroy(&cond);
74 pthread_mutexattr_destroy(&attr);
75
76 return mutex;
77err:
78 if (mutex)
79 fio_mutex_remove(mutex);
80
81 return NULL;
82}
83
84static int mutex_timed_out(struct timeval *t, unsigned int seconds)
85{
86 return mtime_since_now(t) >= seconds * 1000;
87}
88
89int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
90{
91 struct timeval tv_s;
92 struct timespec t;
93 int ret = 0;
94
95 gettimeofday(&tv_s, NULL);
96 t.tv_sec = tv_s.tv_sec + seconds;
97 t.tv_nsec = tv_s.tv_usec * 1000;
98
99 pthread_mutex_lock(&mutex->lock);
100
101 while (!mutex->value && !ret) {
102 mutex->waiters++;
103
104 /*
105 * Some platforms (FreeBSD 9?) seems to return timed out
106 * way too early, double check.
107 */
108 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
109 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
110 ret = 0;
111
112 mutex->waiters--;
113 }
114
115 if (!ret) {
116 mutex->value--;
117 pthread_mutex_unlock(&mutex->lock);
118 }
119
120 return ret;
121}
122
123void fio_mutex_down(struct fio_mutex *mutex)
124{
125 pthread_mutex_lock(&mutex->lock);
126
127 while (!mutex->value) {
128 mutex->waiters++;
129 pthread_cond_wait(&mutex->cond, &mutex->lock);
130 mutex->waiters--;
131 }
132
133 mutex->value--;
134 pthread_mutex_unlock(&mutex->lock);
135}
136
137void fio_mutex_up(struct fio_mutex *mutex)
138{
139 pthread_mutex_lock(&mutex->lock);
140 read_barrier();
141 if (!mutex->value && mutex->waiters)
142 pthread_cond_signal(&mutex->cond);
143 mutex->value++;
144 pthread_mutex_unlock(&mutex->lock);
145}
146
147void fio_rwlock_write(struct fio_rwlock *lock)
148{
149 pthread_rwlock_wrlock(&lock->lock);
150}
151
152void fio_rwlock_read(struct fio_rwlock *lock)
153{
154 pthread_rwlock_rdlock(&lock->lock);
155}
156
157void fio_rwlock_unlock(struct fio_rwlock *lock)
158{
159 pthread_rwlock_unlock(&lock->lock);
160}
161
162void fio_rwlock_remove(struct fio_rwlock *lock)
163{
164 munmap((void *) lock, sizeof(*lock));
165}
166
167struct fio_rwlock *fio_rwlock_init(void)
168{
169 struct fio_rwlock *lock;
170 int ret;
171
172 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
173 PROT_READ | PROT_WRITE,
174 OS_MAP_ANON | MAP_SHARED, -1, 0);
175 if (lock == MAP_FAILED) {
176 perror("mmap rwlock");
177 lock = NULL;
178 goto err;
179 }
180
181 ret = pthread_rwlock_init(&lock->lock, NULL);
182 if (ret) {
183 log_err("pthread_rwlock_init: %s\n", strerror(ret));
184 goto err;
185 }
186
187 return lock;
188err:
189 if (lock)
190 fio_rwlock_remove(lock);
191 return NULL;
192}