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 int __fio_mutex_init(struct fio_mutex *mutex, int value)
30 pthread_mutexattr_t attr;
31 pthread_condattr_t cond;
35 mutex->magic = FIO_MUTEX_MAGIC;
37 ret = pthread_mutexattr_init(&attr);
39 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
44 * Not all platforms support process shared mutexes (FreeBSD)
46 #ifdef FIO_HAVE_PSHARED_MUTEX
47 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
49 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
54 pthread_condattr_init(&cond);
55 #ifdef FIO_HAVE_PSHARED_MUTEX
56 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
58 pthread_cond_init(&mutex->cond, &cond);
60 ret = pthread_mutex_init(&mutex->lock, &attr);
62 log_err("pthread_mutex_init: %s\n", strerror(ret));
66 pthread_condattr_destroy(&cond);
67 pthread_mutexattr_destroy(&attr);
71 struct fio_mutex *fio_mutex_init(int value)
73 struct fio_mutex *mutex = NULL;
75 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
76 PROT_READ | PROT_WRITE,
77 OS_MAP_ANON | MAP_SHARED, -1, 0);
78 if (mutex == MAP_FAILED) {
83 if (!__fio_mutex_init(mutex, value))
86 fio_mutex_remove(mutex);
90 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
92 return mtime_since_now(t) >= seconds * 1000;
95 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
101 assert(mutex->magic == FIO_MUTEX_MAGIC);
103 gettimeofday(&tv_s, NULL);
104 t.tv_sec = tv_s.tv_sec + seconds;
105 t.tv_nsec = tv_s.tv_usec * 1000;
107 pthread_mutex_lock(&mutex->lock);
109 while (!mutex->value && !ret) {
113 * Some platforms (FreeBSD 9?) seems to return timed out
114 * way too early, double check.
116 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
117 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
125 pthread_mutex_unlock(&mutex->lock);
131 int fio_mutex_down_trylock(struct fio_mutex *mutex)
135 assert(mutex->magic == FIO_MUTEX_MAGIC);
137 pthread_mutex_lock(&mutex->lock);
142 pthread_mutex_unlock(&mutex->lock);
147 void fio_mutex_down(struct fio_mutex *mutex)
149 assert(mutex->magic == FIO_MUTEX_MAGIC);
151 pthread_mutex_lock(&mutex->lock);
153 while (!mutex->value) {
155 pthread_cond_wait(&mutex->cond, &mutex->lock);
160 pthread_mutex_unlock(&mutex->lock);
163 void fio_mutex_up(struct fio_mutex *mutex)
165 assert(mutex->magic == FIO_MUTEX_MAGIC);
167 pthread_mutex_lock(&mutex->lock);
169 if (!mutex->value && mutex->waiters)
170 pthread_cond_signal(&mutex->cond);
172 pthread_mutex_unlock(&mutex->lock);
175 void fio_rwlock_write(struct fio_rwlock *lock)
177 assert(lock->magic == FIO_RWLOCK_MAGIC);
178 pthread_rwlock_wrlock(&lock->lock);
181 void fio_rwlock_read(struct fio_rwlock *lock)
183 assert(lock->magic == FIO_RWLOCK_MAGIC);
184 pthread_rwlock_rdlock(&lock->lock);
187 void fio_rwlock_unlock(struct fio_rwlock *lock)
189 assert(lock->magic == FIO_RWLOCK_MAGIC);
190 pthread_rwlock_unlock(&lock->lock);
193 void fio_rwlock_remove(struct fio_rwlock *lock)
195 assert(lock->magic == FIO_RWLOCK_MAGIC);
196 munmap((void *) lock, sizeof(*lock));
199 struct fio_rwlock *fio_rwlock_init(void)
201 struct fio_rwlock *lock;
202 pthread_rwlockattr_t attr;
205 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
206 PROT_READ | PROT_WRITE,
207 OS_MAP_ANON | MAP_SHARED, -1, 0);
208 if (lock == MAP_FAILED) {
209 perror("mmap rwlock");
214 lock->magic = FIO_RWLOCK_MAGIC;
216 ret = pthread_rwlockattr_init(&attr);
218 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
221 #ifdef FIO_HAVE_PSHARED_MUTEX
222 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
224 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
228 ret = pthread_rwlock_init(&lock->lock, &attr);
230 ret = pthread_rwlock_init(&lock->lock, NULL);
234 log_err("pthread_rwlock_init: %s\n", strerror(ret));
238 pthread_rwlockattr_destroy(&attr);
242 pthread_rwlockattr_destroy(&attr);
245 fio_rwlock_remove(lock);