15 #include "arch/arch.h"
21 void __fio_mutex_remove(struct fio_mutex *mutex)
23 assert(mutex->magic == FIO_MUTEX_MAGIC);
24 pthread_cond_destroy(&mutex->cond);
27 void fio_mutex_remove(struct fio_mutex *mutex)
29 __fio_mutex_remove(mutex);
30 munmap((void *) mutex, sizeof(*mutex));
33 int __fio_mutex_init(struct fio_mutex *mutex, int value)
35 pthread_mutexattr_t attr;
36 pthread_condattr_t cond;
40 mutex->magic = FIO_MUTEX_MAGIC;
42 ret = pthread_mutexattr_init(&attr);
44 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
49 * Not all platforms support process shared mutexes (FreeBSD)
51 #ifdef FIO_HAVE_PSHARED_MUTEX
52 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
54 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
59 pthread_condattr_init(&cond);
60 #ifdef FIO_HAVE_PSHARED_MUTEX
61 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
63 pthread_cond_init(&mutex->cond, &cond);
65 ret = pthread_mutex_init(&mutex->lock, &attr);
67 log_err("pthread_mutex_init: %s\n", strerror(ret));
71 pthread_condattr_destroy(&cond);
72 pthread_mutexattr_destroy(&attr);
76 struct fio_mutex *fio_mutex_init(int value)
78 struct fio_mutex *mutex = NULL;
80 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
81 PROT_READ | PROT_WRITE,
82 OS_MAP_ANON | MAP_SHARED, -1, 0);
83 if (mutex == MAP_FAILED) {
88 if (!__fio_mutex_init(mutex, value))
91 fio_mutex_remove(mutex);
95 static bool mutex_timed_out(struct timeval *t, unsigned int msecs)
99 gettimeofday(&now, NULL);
100 return mtime_since(t, &now) >= msecs;
103 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int msecs)
109 assert(mutex->magic == FIO_MUTEX_MAGIC);
111 gettimeofday(&tv_s, NULL);
112 t.tv_sec = tv_s.tv_sec;
113 t.tv_nsec = tv_s.tv_usec * 1000;
115 t.tv_sec += msecs / 1000;
116 t.tv_nsec += ((msecs * 1000000) % 1000000000);
117 if (t.tv_nsec >= 1000000000) {
118 t.tv_nsec -= 1000000000;
122 pthread_mutex_lock(&mutex->lock);
125 while (!mutex->value && !ret) {
127 * Some platforms (FreeBSD 9?) seems to return timed out
128 * way too early, double check.
130 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
131 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, msecs))
138 pthread_mutex_unlock(&mutex->lock);
141 pthread_mutex_unlock(&mutex->lock);
145 bool fio_mutex_down_trylock(struct fio_mutex *mutex)
149 assert(mutex->magic == FIO_MUTEX_MAGIC);
151 pthread_mutex_lock(&mutex->lock);
156 pthread_mutex_unlock(&mutex->lock);
161 void fio_mutex_down(struct fio_mutex *mutex)
163 assert(mutex->magic == FIO_MUTEX_MAGIC);
165 pthread_mutex_lock(&mutex->lock);
167 while (!mutex->value) {
169 pthread_cond_wait(&mutex->cond, &mutex->lock);
174 pthread_mutex_unlock(&mutex->lock);
177 void fio_mutex_up(struct fio_mutex *mutex)
181 assert(mutex->magic == FIO_MUTEX_MAGIC);
183 pthread_mutex_lock(&mutex->lock);
185 if (!mutex->value && mutex->waiters)
188 pthread_mutex_unlock(&mutex->lock);
191 pthread_cond_signal(&mutex->cond);
194 void fio_rwlock_write(struct fio_rwlock *lock)
196 assert(lock->magic == FIO_RWLOCK_MAGIC);
197 pthread_rwlock_wrlock(&lock->lock);
200 void fio_rwlock_read(struct fio_rwlock *lock)
202 assert(lock->magic == FIO_RWLOCK_MAGIC);
203 pthread_rwlock_rdlock(&lock->lock);
206 void fio_rwlock_unlock(struct fio_rwlock *lock)
208 assert(lock->magic == FIO_RWLOCK_MAGIC);
209 pthread_rwlock_unlock(&lock->lock);
212 void fio_rwlock_remove(struct fio_rwlock *lock)
214 assert(lock->magic == FIO_RWLOCK_MAGIC);
215 munmap((void *) lock, sizeof(*lock));
218 struct fio_rwlock *fio_rwlock_init(void)
220 struct fio_rwlock *lock;
221 pthread_rwlockattr_t attr;
224 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
225 PROT_READ | PROT_WRITE,
226 OS_MAP_ANON | MAP_SHARED, -1, 0);
227 if (lock == MAP_FAILED) {
228 perror("mmap rwlock");
233 lock->magic = FIO_RWLOCK_MAGIC;
235 ret = pthread_rwlockattr_init(&attr);
237 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
240 #ifdef FIO_HAVE_PSHARED_MUTEX
241 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
243 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
247 ret = pthread_rwlock_init(&lock->lock, &attr);
249 ret = pthread_rwlock_init(&lock->lock, NULL);
253 log_err("pthread_rwlock_init: %s\n", strerror(ret));
257 pthread_rwlockattr_destroy(&attr);
261 pthread_rwlockattr_destroy(&attr);
264 fio_rwlock_remove(lock);