blob: 332e9f9ea871c04cc09c4661d252fec31cbf0dc5 [file] [log] [blame]
Jens Axboe07739b52007-03-08 20:25:46 +01001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
Jens Axboe656b1392009-07-01 23:02:10 +02006#include <time.h>
Jens Axboeef635052012-02-15 22:24:19 +01007#include <errno.h>
Jens Axboe07739b52007-03-08 20:25:46 +01008#include <pthread.h>
9#include <sys/mman.h>
10
Jens Axboeb4c1fb32012-02-16 22:22:46 +010011#include "fio.h"
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010012#include "log.h"
Jens Axboe07739b52007-03-08 20:25:46 +010013#include "mutex.h"
Jens Axboe4d4e80f2008-03-04 10:18:56 +010014#include "arch/arch.h"
Jens Axboe3c2d93e2009-01-05 19:04:15 +010015#include "os/os.h"
Jens Axboe3b2e1462009-12-15 08:58:10 +010016#include "helpers.h"
Jens Axboeef635052012-02-15 22:24:19 +010017#include "time.h"
18#include "gettime.h"
Jens Axboe07739b52007-03-08 20:25:46 +010019
Jens Axboecdd18ad2008-02-27 18:58:00 +010020void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010021{
Jens Axboe58a157d2012-02-15 22:20:26 +010022 pthread_cond_destroy(&mutex->cond);
Jens Axboe5921e802008-05-30 15:02:38 +020023 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010024}
25
Jens Axboecdd18ad2008-02-27 18:58:00 +010026struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010027{
Jens Axboecdd18ad2008-02-27 18:58:00 +010028 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010029 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010030 pthread_condattr_t cond;
Jens Axboee721c572012-02-09 20:55:29 +010031 int ret;
Jens Axboe07739b52007-03-08 20:25:46 +010032
Jens Axboe5921e802008-05-30 15:02:38 +020033 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
Jens Axboeb6f6abc2011-05-02 10:10:56 -060034 PROT_READ | PROT_WRITE,
35 OS_MAP_ANON | MAP_SHARED, -1, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010036 if (mutex == MAP_FAILED) {
37 perror("mmap mutex");
Jens Axboecdd18ad2008-02-27 18:58:00 +010038 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010039 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010040 }
41
Jens Axboecdd18ad2008-02-27 18:58:00 +010042 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010043
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010044 ret = pthread_mutexattr_init(&attr);
45 if (ret) {
46 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010047 goto err;
48 }
Jens Axboee721c572012-02-09 20:55:29 +010049
50 /*
51 * Not all platforms support process shared mutexes (FreeBSD)
52 */
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020053#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010054 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010055 if (ret) {
56 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010057 goto err;
58 }
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020059#endif
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010060
61 pthread_condattr_init(&cond);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020062#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010063 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020064#endif
Jens Axboe58a157d2012-02-15 22:20:26 +010065 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010066
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010067 ret = pthread_mutex_init(&mutex->lock, &attr);
68 if (ret) {
69 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010070 goto err;
71 }
72
Bruce Cran03e20d62011-01-02 20:14:54 +010073 pthread_condattr_destroy(&cond);
74 pthread_mutexattr_destroy(&attr);
75
Jens Axboecdd18ad2008-02-27 18:58:00 +010076 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010077err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010078 if (mutex)
79 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010080
Jens Axboe07739b52007-03-08 20:25:46 +010081 return NULL;
82}
83
Jens Axboeef635052012-02-15 22:24:19 +010084static int mutex_timed_out(struct timeval *t, unsigned int seconds)
85{
86 return mtime_since_now(t) >= seconds * 1000;
87}
88
Jens Axboe656b1392009-07-01 23:02:10 +020089int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
90{
Jens Axboeef635052012-02-15 22:24:19 +010091 struct timeval tv_s;
Jens Axboe656b1392009-07-01 23:02:10 +020092 struct timespec t;
93 int ret = 0;
94
Jens Axboeb11e4cc2012-02-20 09:18:43 +010095 gettimeofday(&tv_s, NULL);
96 t.tv_sec = tv_s.tv_sec + seconds;
97 t.tv_nsec = tv_s.tv_usec * 1000;
Jens Axboe656b1392009-07-01 23:02:10 +020098
99 pthread_mutex_lock(&mutex->lock);
100
101 while (!mutex->value && !ret) {
102 mutex->waiters++;
Jens Axboeef635052012-02-15 22:24:19 +0100103
104 /*
105 * Some platforms (FreeBSD 9?) seems to return timed out
106 * way too early, double check.
107 */
Jens Axboe656b1392009-07-01 23:02:10 +0200108 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
Jens Axboed7df1d12013-03-20 19:57:01 -0600109 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
Jens Axboeef635052012-02-15 22:24:19 +0100110 ret = 0;
Jens Axboeef635052012-02-15 22:24:19 +0100111
Jens Axboe656b1392009-07-01 23:02:10 +0200112 mutex->waiters--;
113 }
114
115 if (!ret) {
116 mutex->value--;
117 pthread_mutex_unlock(&mutex->lock);
118 }
119
120 return ret;
121}
122
Jens Axboecdd18ad2008-02-27 18:58:00 +0100123void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100124{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100125 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100126
127 while (!mutex->value) {
128 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100129 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100130 mutex->waiters--;
131 }
132
Jens Axboecdd18ad2008-02-27 18:58:00 +0100133 mutex->value--;
134 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100135}
136
Jens Axboecdd18ad2008-02-27 18:58:00 +0100137void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100138{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100139 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100140 read_barrier();
141 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100142 pthread_cond_signal(&mutex->cond);
143 mutex->value++;
144 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100145}
Jens Axboe64d4d312008-03-03 10:36:27 +0100146
Jens Axboed7df1d12013-03-20 19:57:01 -0600147void fio_rwlock_write(struct fio_rwlock *lock)
Jens Axboe64d4d312008-03-03 10:36:27 +0100148{
Jens Axboed7df1d12013-03-20 19:57:01 -0600149 pthread_rwlock_wrlock(&lock->lock);
150}
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100151
Jens Axboed7df1d12013-03-20 19:57:01 -0600152void fio_rwlock_read(struct fio_rwlock *lock)
153{
154 pthread_rwlock_rdlock(&lock->lock);
155}
156
157void fio_rwlock_unlock(struct fio_rwlock *lock)
158{
159 pthread_rwlock_unlock(&lock->lock);
160}
161
162void fio_rwlock_remove(struct fio_rwlock *lock)
163{
164 munmap((void *) lock, sizeof(*lock));
165}
166
167struct fio_rwlock *fio_rwlock_init(void)
168{
169 struct fio_rwlock *lock;
170 int ret;
171
172 lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
173 PROT_READ | PROT_WRITE,
174 OS_MAP_ANON | MAP_SHARED, -1, 0);
175 if (lock == MAP_FAILED) {
176 perror("mmap rwlock");
177 lock = NULL;
178 goto err;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100179 }
180
Jens Axboed7df1d12013-03-20 19:57:01 -0600181 ret = pthread_rwlock_init(&lock->lock, NULL);
182 if (ret) {
183 log_err("pthread_rwlock_init: %s\n", strerror(ret));
184 goto err;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100185 }
186
Jens Axboed7df1d12013-03-20 19:57:01 -0600187 return lock;
188err:
189 if (lock)
190 fio_rwlock_remove(lock);
191 return NULL;
Jens Axboe64d4d312008-03-03 10:36:27 +0100192}