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); |
108fcc11 | 61 | |
4fa6d0f8 JA |
62 | ret = pthread_mutex_init(&mutex->lock, &attr); |
63 | if (ret) { | |
64 | log_err("pthread_mutex_init: %s\n", strerror(ret)); | |
07739b57 JA |
65 | goto err; |
66 | } | |
67 | ||
03e20d68 BC |
68 | pthread_condattr_destroy(&cond); |
69 | pthread_mutexattr_destroy(&attr); | |
70 | ||
cdd18ad8 | 71 | return mutex; |
07739b57 | 72 | err: |
cdd18ad8 JA |
73 | if (mutex) |
74 | fio_mutex_remove(mutex); | |
f7c9e00e | 75 | |
07739b57 JA |
76 | return NULL; |
77 | } | |
78 | ||
656b1393 JA |
79 | int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds) |
80 | { | |
81 | struct timespec t; | |
82 | int ret = 0; | |
83 | ||
d481e006 | 84 | clock_gettime(CLOCK_REALTIME, &t); |
656b1393 JA |
85 | t.tv_sec += seconds; |
86 | ||
87 | pthread_mutex_lock(&mutex->lock); | |
88 | ||
89 | while (!mutex->value && !ret) { | |
90 | mutex->waiters++; | |
91 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); | |
92 | mutex->waiters--; | |
93 | } | |
94 | ||
95 | if (!ret) { | |
96 | mutex->value--; | |
97 | pthread_mutex_unlock(&mutex->lock); | |
98 | } | |
99 | ||
100 | return ret; | |
101 | } | |
102 | ||
cdd18ad8 | 103 | void fio_mutex_down(struct fio_mutex *mutex) |
07739b57 | 104 | { |
cdd18ad8 | 105 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
106 | |
107 | while (!mutex->value) { | |
108 | mutex->waiters++; | |
cdd18ad8 | 109 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
110 | mutex->waiters--; |
111 | } | |
112 | ||
cdd18ad8 JA |
113 | mutex->value--; |
114 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 JA |
115 | } |
116 | ||
cdd18ad8 | 117 | void fio_mutex_up(struct fio_mutex *mutex) |
07739b57 | 118 | { |
cdd18ad8 | 119 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
120 | read_barrier(); |
121 | if (!mutex->value && mutex->waiters) | |
cdd18ad8 JA |
122 | pthread_cond_signal(&mutex->cond); |
123 | mutex->value++; | |
124 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 | 125 | } |
64d4d313 JA |
126 | |
127 | void fio_mutex_down_write(struct fio_mutex *mutex) | |
128 | { | |
129 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
130 | |
131 | while (mutex->value != 0) { | |
132 | mutex->waiters++; | |
64d4d313 | 133 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
134 | mutex->waiters--; |
135 | } | |
136 | ||
64d4d313 JA |
137 | mutex->value--; |
138 | pthread_mutex_unlock(&mutex->lock); | |
139 | } | |
140 | ||
141 | void fio_mutex_down_read(struct fio_mutex *mutex) | |
142 | { | |
143 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
144 | |
145 | while (mutex->value < 0) { | |
146 | mutex->waiters++; | |
64d4d313 | 147 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
148 | mutex->waiters--; |
149 | } | |
150 | ||
64d4d313 JA |
151 | mutex->value++; |
152 | pthread_mutex_unlock(&mutex->lock); | |
153 | } | |
154 | ||
155 | void fio_mutex_up_read(struct fio_mutex *mutex) | |
156 | { | |
157 | pthread_mutex_lock(&mutex->lock); | |
158 | mutex->value--; | |
4d4e80f2 JA |
159 | read_barrier(); |
160 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
161 | pthread_cond_signal(&mutex->cond); |
162 | pthread_mutex_unlock(&mutex->lock); | |
163 | } | |
164 | ||
165 | void fio_mutex_up_write(struct fio_mutex *mutex) | |
166 | { | |
167 | pthread_mutex_lock(&mutex->lock); | |
168 | mutex->value++; | |
4d4e80f2 JA |
169 | read_barrier(); |
170 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
171 | pthread_cond_signal(&mutex->cond); |
172 | pthread_mutex_unlock(&mutex->lock); | |
173 | } |