Commit | Line | Data |
---|---|---|
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> | |
10 | ||
4fa6d0f8 | 11 | #include "log.h" |
07739b57 | 12 | #include "mutex.h" |
4d4e80f2 | 13 | #include "arch/arch.h" |
3c2d93ed | 14 | #include "os/os.h" |
3b2e1464 | 15 | #include "helpers.h" |
ef635057 JA |
16 | #include "time.h" |
17 | #include "gettime.h" | |
07739b57 | 18 | |
cdd18ad8 | 19 | void fio_mutex_remove(struct fio_mutex *mutex) |
07739b57 | 20 | { |
58a157d4 | 21 | pthread_cond_destroy(&mutex->cond); |
5921e80c | 22 | munmap((void *) mutex, sizeof(*mutex)); |
07739b57 JA |
23 | } |
24 | ||
cdd18ad8 | 25 | struct fio_mutex *fio_mutex_init(int value) |
07739b57 | 26 | { |
cdd18ad8 | 27 | struct fio_mutex *mutex = NULL; |
07739b57 | 28 | pthread_mutexattr_t attr; |
108fcc11 | 29 | pthread_condattr_t cond; |
e721c57f | 30 | int ret; |
07739b57 | 31 | |
5921e80c | 32 | mutex = (void *) mmap(NULL, sizeof(struct fio_mutex), |
b6f6abc6 JA |
33 | PROT_READ | PROT_WRITE, |
34 | OS_MAP_ANON | MAP_SHARED, -1, 0); | |
cdd18ad8 JA |
35 | if (mutex == MAP_FAILED) { |
36 | perror("mmap mutex"); | |
cdd18ad8 | 37 | mutex = NULL; |
e53bd0b3 | 38 | goto err; |
07739b57 JA |
39 | } |
40 | ||
cdd18ad8 | 41 | mutex->value = value; |
07739b57 | 42 | |
4fa6d0f8 JA |
43 | ret = pthread_mutexattr_init(&attr); |
44 | if (ret) { | |
45 | log_err("pthread_mutexattr_init: %s\n", strerror(ret)); | |
07739b57 JA |
46 | goto err; |
47 | } | |
e721c57f JA |
48 | |
49 | /* | |
50 | * Not all platforms support process shared mutexes (FreeBSD) | |
51 | */ | |
7452440e | 52 | #ifdef FIO_HAVE_PSHARED_MUTEX |
e721c57f | 53 | ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
4fa6d0f8 JA |
54 | if (ret) { |
55 | log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); | |
07739b57 JA |
56 | goto err; |
57 | } | |
7452440e | 58 | #endif |
108fcc11 ZY |
59 | |
60 | pthread_condattr_init(&cond); | |
7452440e | 61 | #ifdef FIO_HAVE_PSHARED_MUTEX |
e721c57f | 62 | pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); |
7452440e | 63 | #endif |
ceab2ea2 JA |
64 | #ifdef FIO_HAVE_CLOCK_MONOTONIC |
65 | pthread_condattr_setclock(&cond, CLOCK_MONOTONIC); | |
66 | #else | |
67 | pthread_condattr_setclock(&cond, CLOCK_REALTIME); | |
68 | #endif | |
58a157d4 | 69 | pthread_cond_init(&mutex->cond, &cond); |
108fcc11 | 70 | |
4fa6d0f8 JA |
71 | ret = pthread_mutex_init(&mutex->lock, &attr); |
72 | if (ret) { | |
73 | log_err("pthread_mutex_init: %s\n", strerror(ret)); | |
07739b57 JA |
74 | goto err; |
75 | } | |
76 | ||
03e20d68 BC |
77 | pthread_condattr_destroy(&cond); |
78 | pthread_mutexattr_destroy(&attr); | |
79 | ||
cdd18ad8 | 80 | return mutex; |
07739b57 | 81 | err: |
cdd18ad8 JA |
82 | if (mutex) |
83 | fio_mutex_remove(mutex); | |
f7c9e00e | 84 | |
07739b57 JA |
85 | return NULL; |
86 | } | |
87 | ||
ef635057 JA |
88 | static int mutex_timed_out(struct timeval *t, unsigned int seconds) |
89 | { | |
90 | return mtime_since_now(t) >= seconds * 1000; | |
91 | } | |
92 | ||
656b1393 JA |
93 | int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds) |
94 | { | |
ef635057 | 95 | struct timeval tv_s; |
656b1393 JA |
96 | struct timespec t; |
97 | int ret = 0; | |
98 | ||
ef635057 JA |
99 | fio_gettime(&tv_s, NULL); |
100 | ||
ceab2ea2 JA |
101 | #ifdef FIO_HAVE_CLOCK_MONOTONIC |
102 | clock_gettime(CLOCK_MONOTONIC, &t); | |
103 | #else | |
d481e006 | 104 | clock_gettime(CLOCK_REALTIME, &t); |
ceab2ea2 | 105 | #endif |
656b1393 JA |
106 | t.tv_sec += seconds; |
107 | ||
108 | pthread_mutex_lock(&mutex->lock); | |
109 | ||
110 | while (!mutex->value && !ret) { | |
111 | mutex->waiters++; | |
ef635057 JA |
112 | |
113 | /* | |
114 | * Some platforms (FreeBSD 9?) seems to return timed out | |
115 | * way too early, double check. | |
116 | */ | |
656b1393 | 117 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); |
ef635057 JA |
118 | if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) { |
119 | pthread_mutex_lock(&mutex->lock); | |
120 | ret = 0; | |
121 | } | |
122 | ||
656b1393 JA |
123 | mutex->waiters--; |
124 | } | |
125 | ||
126 | if (!ret) { | |
127 | mutex->value--; | |
128 | pthread_mutex_unlock(&mutex->lock); | |
129 | } | |
130 | ||
131 | return ret; | |
132 | } | |
133 | ||
cdd18ad8 | 134 | void fio_mutex_down(struct fio_mutex *mutex) |
07739b57 | 135 | { |
cdd18ad8 | 136 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
137 | |
138 | while (!mutex->value) { | |
139 | mutex->waiters++; | |
cdd18ad8 | 140 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
141 | mutex->waiters--; |
142 | } | |
143 | ||
cdd18ad8 JA |
144 | mutex->value--; |
145 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 JA |
146 | } |
147 | ||
cdd18ad8 | 148 | void fio_mutex_up(struct fio_mutex *mutex) |
07739b57 | 149 | { |
cdd18ad8 | 150 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
151 | read_barrier(); |
152 | if (!mutex->value && mutex->waiters) | |
cdd18ad8 JA |
153 | pthread_cond_signal(&mutex->cond); |
154 | mutex->value++; | |
155 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 | 156 | } |
64d4d313 JA |
157 | |
158 | void fio_mutex_down_write(struct fio_mutex *mutex) | |
159 | { | |
160 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
161 | |
162 | while (mutex->value != 0) { | |
163 | mutex->waiters++; | |
64d4d313 | 164 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
165 | mutex->waiters--; |
166 | } | |
167 | ||
64d4d313 JA |
168 | mutex->value--; |
169 | pthread_mutex_unlock(&mutex->lock); | |
170 | } | |
171 | ||
172 | void fio_mutex_down_read(struct fio_mutex *mutex) | |
173 | { | |
174 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
175 | |
176 | while (mutex->value < 0) { | |
177 | mutex->waiters++; | |
64d4d313 | 178 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
179 | mutex->waiters--; |
180 | } | |
181 | ||
64d4d313 JA |
182 | mutex->value++; |
183 | pthread_mutex_unlock(&mutex->lock); | |
184 | } | |
185 | ||
186 | void fio_mutex_up_read(struct fio_mutex *mutex) | |
187 | { | |
188 | pthread_mutex_lock(&mutex->lock); | |
189 | mutex->value--; | |
4d4e80f2 JA |
190 | read_barrier(); |
191 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
192 | pthread_cond_signal(&mutex->cond); |
193 | pthread_mutex_unlock(&mutex->lock); | |
194 | } | |
195 | ||
196 | void fio_mutex_up_write(struct fio_mutex *mutex) | |
197 | { | |
198 | pthread_mutex_lock(&mutex->lock); | |
199 | mutex->value++; | |
4d4e80f2 JA |
200 | read_barrier(); |
201 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
202 | pthread_cond_signal(&mutex->cond); |
203 | pthread_mutex_unlock(&mutex->lock); | |
204 | } |