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);
25 munmap((void *) mutex, sizeof(*mutex));
28 struct fio_mutex *fio_mutex_init(int value)
30 struct fio_mutex *mutex = NULL;
31 pthread_mutexattr_t attr;
32 pthread_condattr_t cond;
35 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
36 PROT_READ | PROT_WRITE,
37 OS_MAP_ANON | MAP_SHARED, -1, 0);
38 if (mutex == MAP_FAILED) {
45 mutex->magic = FIO_MUTEX_MAGIC;
47 ret = pthread_mutexattr_init(&attr);
49 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
54 * Not all platforms support process shared mutexes (FreeBSD)
56 #ifdef FIO_HAVE_PSHARED_MUTEX
57 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
59 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
64 pthread_condattr_init(&cond);
65 #ifdef FIO_HAVE_PSHARED_MUTEX
66 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
68 pthread_cond_init(&mutex->cond, &cond);
70 ret = pthread_mutex_init(&mutex->lock, &attr);
72 log_err("pthread_mutex_init: %s\n", strerror(ret));
76 pthread_condattr_destroy(&cond);
77 pthread_mutexattr_destroy(&attr);
82 fio_mutex_remove(mutex);
87 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
89 return mtime_since_now(t) >= seconds * 1000;
92 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
98 assert(mutex->magic == FIO_MUTEX_MAGIC);
100 gettimeofday(&tv_s, NULL);
101 t.tv_sec = tv_s.tv_sec + seconds;
102 t.tv_nsec = tv_s.tv_usec * 1000;
104 pthread_mutex_lock(&mutex->lock);
106 while (!mutex->value && !ret) {
110 * Some platforms (FreeBSD 9?) seems to return timed out
111 * way too early, double check.
113 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
114 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
122 pthread_mutex_unlock(&mutex->lock);
128 void fio_mutex_down(struct fio_mutex *mutex)
130 assert(mutex->magic == FIO_MUTEX_MAGIC);
132 pthread_mutex_lock(&mutex->lock);
134 while (!mutex->value) {
136 pthread_cond_wait(&mutex->cond, &mutex->lock);
141 pthread_mutex_unlock(&mutex->lock);
144 void fio_mutex_up(struct fio_mutex *mutex)
146 assert(mutex->magic == FIO_MUTEX_MAGIC);
148 pthread_mutex_lock(&mutex->lock);
150 if (!mutex->value && mutex->waiters)
151 pthread_cond_signal(&mutex->cond);
153 pthread_mutex_unlock(&mutex->lock);
156 void fio_rwlock_write(struct fio_rwlock *lock)
158 assert(lock->magic == FIO_RWLOCK_MAGIC);
159 pthread_rwlock_wrlock(&lock->lock);
162 void fio_rwlock_read(struct fio_rwlock *lock)
164 assert(lock->magic == FIO_RWLOCK_MAGIC);
165 pthread_rwlock_rdlock(&lock->lock);
168 void fio_rwlock_unlock(struct fio_rwlock *lock)
170 assert(lock->magic == FIO_RWLOCK_MAGIC);
171 pthread_rwlock_unlock(&lock->lock);
174 void fio_rwlock_remove(struct fio_rwlock *lock)
176 assert(lock->magic == FIO_RWLOCK_MAGIC);
177 munmap((void *) lock, sizeof(*lock));
180 struct fio_rwlock *fio_rwlock_init(void)
182 struct fio_rwlock *lock;
183 pthread_rwlockattr_t attr;
186 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
187 PROT_READ | PROT_WRITE,
188 OS_MAP_ANON | MAP_SHARED, -1, 0);
189 if (lock == MAP_FAILED) {
190 perror("mmap rwlock");
195 lock->magic = FIO_RWLOCK_MAGIC;
197 ret = pthread_rwlockattr_init(&attr);
199 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
202 #ifdef FIO_HAVE_PSHARED_MUTEX
203 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
205 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
209 ret = pthread_rwlock_init(&lock->lock, &attr);
211 ret = pthread_rwlock_init(&lock->lock, NULL);
215 log_err("pthread_rwlock_init: %s\n", strerror(ret));
219 pthread_rwlockattr_destroy(&attr);
223 pthread_rwlockattr_destroy(&attr);
226 fio_rwlock_remove(lock);