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