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; |
b6f6abc6 | 26 | int ret, mflag; |
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 | |
f356d01d JA |
39 | /* |
40 | * Not all platforms support process shared mutexes (FreeBSD) | |
41 | */ | |
42 | #ifdef FIO_HAVE_PSHARED_MUTEX | |
43 | mflag = PTHREAD_PROCESS_SHARED; | |
44 | #else | |
45 | mflag = PTHREAD_PROCESS_PRIVATE; | |
46 | #endif | |
47 | ||
4fa6d0f8 JA |
48 | ret = pthread_mutexattr_init(&attr); |
49 | if (ret) { | |
50 | log_err("pthread_mutexattr_init: %s\n", strerror(ret)); | |
07739b57 JA |
51 | goto err; |
52 | } | |
7452440e | 53 | #ifdef FIO_HAVE_PSHARED_MUTEX |
f356d01d | 54 | ret = pthread_mutexattr_setpshared(&attr, mflag); |
4fa6d0f8 JA |
55 | if (ret) { |
56 | log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); | |
07739b57 JA |
57 | goto err; |
58 | } | |
7452440e | 59 | #endif |
108fcc11 ZY |
60 | |
61 | pthread_condattr_init(&cond); | |
7452440e | 62 | #ifdef FIO_HAVE_PSHARED_MUTEX |
f356d01d | 63 | pthread_condattr_setpshared(&cond, mflag); |
7452440e | 64 | #endif |
cdd18ad8 | 65 | pthread_cond_init(&mutex->cond, &cond); |
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 | ||
d481e006 | 89 | clock_gettime(CLOCK_REALTIME, &t); |
656b1393 JA |
90 | t.tv_sec += seconds; |
91 | ||
92 | pthread_mutex_lock(&mutex->lock); | |
93 | ||
94 | while (!mutex->value && !ret) { | |
95 | mutex->waiters++; | |
96 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); | |
97 | mutex->waiters--; | |
98 | } | |
99 | ||
100 | if (!ret) { | |
101 | mutex->value--; | |
102 | pthread_mutex_unlock(&mutex->lock); | |
103 | } | |
104 | ||
105 | return ret; | |
106 | } | |
107 | ||
cdd18ad8 | 108 | void fio_mutex_down(struct fio_mutex *mutex) |
07739b57 | 109 | { |
cdd18ad8 | 110 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
111 | |
112 | while (!mutex->value) { | |
113 | mutex->waiters++; | |
cdd18ad8 | 114 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
115 | mutex->waiters--; |
116 | } | |
117 | ||
cdd18ad8 JA |
118 | mutex->value--; |
119 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 JA |
120 | } |
121 | ||
cdd18ad8 | 122 | void fio_mutex_up(struct fio_mutex *mutex) |
07739b57 | 123 | { |
cdd18ad8 | 124 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
125 | read_barrier(); |
126 | if (!mutex->value && mutex->waiters) | |
cdd18ad8 JA |
127 | pthread_cond_signal(&mutex->cond); |
128 | mutex->value++; | |
129 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 | 130 | } |
64d4d313 JA |
131 | |
132 | void fio_mutex_down_write(struct fio_mutex *mutex) | |
133 | { | |
134 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
135 | |
136 | while (mutex->value != 0) { | |
137 | mutex->waiters++; | |
64d4d313 | 138 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
139 | mutex->waiters--; |
140 | } | |
141 | ||
64d4d313 JA |
142 | mutex->value--; |
143 | pthread_mutex_unlock(&mutex->lock); | |
144 | } | |
145 | ||
146 | void fio_mutex_down_read(struct fio_mutex *mutex) | |
147 | { | |
148 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
149 | |
150 | while (mutex->value < 0) { | |
151 | mutex->waiters++; | |
64d4d313 | 152 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
153 | mutex->waiters--; |
154 | } | |
155 | ||
64d4d313 JA |
156 | mutex->value++; |
157 | pthread_mutex_unlock(&mutex->lock); | |
158 | } | |
159 | ||
160 | void fio_mutex_up_read(struct fio_mutex *mutex) | |
161 | { | |
162 | pthread_mutex_lock(&mutex->lock); | |
163 | mutex->value--; | |
4d4e80f2 JA |
164 | read_barrier(); |
165 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
166 | pthread_cond_signal(&mutex->cond); |
167 | pthread_mutex_unlock(&mutex->lock); | |
168 | } | |
169 | ||
170 | void fio_mutex_up_write(struct fio_mutex *mutex) | |
171 | { | |
172 | pthread_mutex_lock(&mutex->lock); | |
173 | mutex->value++; | |
4d4e80f2 JA |
174 | read_barrier(); |
175 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
176 | pthread_cond_signal(&mutex->cond); |
177 | pthread_mutex_unlock(&mutex->lock); | |
178 | } |