blob: e1fbb607f1a416fb06a25a611f0f4233c9814357 [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>
Jens Axboe8b4e9542013-03-21 10:05:07 -060010#include <assert.h>
Jens Axboe07739b52007-03-08 20:25:46 +010011
Jens Axboeb4c1fb32012-02-16 22:22:46 +010012#include "fio.h"
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010013#include "log.h"
Jens Axboe07739b52007-03-08 20:25:46 +010014#include "mutex.h"
Jens Axboe4d4e80f2008-03-04 10:18:56 +010015#include "arch/arch.h"
Jens Axboe3c2d93e2009-01-05 19:04:15 +010016#include "os/os.h"
Jens Axboe3b2e1462009-12-15 08:58:10 +010017#include "helpers.h"
Jens Axboeef635052012-02-15 22:24:19 +010018#include "time.h"
19#include "gettime.h"
Jens Axboe07739b52007-03-08 20:25:46 +010020
Jens Axboecdd18ad2008-02-27 18:58:00 +010021void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010022{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -060023 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe58a157d2012-02-15 22:20:26 +010024 pthread_cond_destroy(&mutex->cond);
Jens Axboe5921e802008-05-30 15:02:38 +020025 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010026}
27
Jens Axboecdd18ad2008-02-27 18:58:00 +010028struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010029{
Jens Axboecdd18ad2008-02-27 18:58:00 +010030 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010031 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010032 pthread_condattr_t cond;
Jens Axboee721c572012-02-09 20:55:29 +010033 int ret;
Jens Axboe07739b52007-03-08 20:25:46 +010034
Jens Axboe5921e802008-05-30 15:02:38 +020035 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
Jens Axboeb6f6abc2011-05-02 10:10:56 -060036 PROT_READ | PROT_WRITE,
37 OS_MAP_ANON | MAP_SHARED, -1, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010038 if (mutex == MAP_FAILED) {
39 perror("mmap mutex");
Jens Axboecdd18ad2008-02-27 18:58:00 +010040 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010041 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010042 }
43
Jens Axboecdd18ad2008-02-27 18:58:00 +010044 mutex->value = value;
Jens Axboe8b4e9542013-03-21 10:05:07 -060045 mutex->magic = FIO_MUTEX_MAGIC;
Jens Axboe07739b52007-03-08 20:25:46 +010046
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010047 ret = pthread_mutexattr_init(&attr);
48 if (ret) {
49 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010050 goto err;
51 }
Jens Axboee721c572012-02-09 20:55:29 +010052
53 /*
54 * Not all platforms support process shared mutexes (FreeBSD)
55 */
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020056#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010057 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010058 if (ret) {
59 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010060 goto err;
61 }
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020062#endif
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010063
64 pthread_condattr_init(&cond);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020065#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010066 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020067#endif
Jens Axboe58a157d2012-02-15 22:20:26 +010068 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010069
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010070 ret = pthread_mutex_init(&mutex->lock, &attr);
71 if (ret) {
72 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010073 goto err;
74 }
75
Bruce Cran03e20d62011-01-02 20:14:54 +010076 pthread_condattr_destroy(&cond);
77 pthread_mutexattr_destroy(&attr);
78
Jens Axboecdd18ad2008-02-27 18:58:00 +010079 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010080err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010081 if (mutex)
82 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010083
Jens Axboe07739b52007-03-08 20:25:46 +010084 return NULL;
85}
86
Jens Axboeef635052012-02-15 22:24:19 +010087static int mutex_timed_out(struct timeval *t, unsigned int seconds)
88{
89 return mtime_since_now(t) >= seconds * 1000;
90}
91
Jens Axboe656b1392009-07-01 23:02:10 +020092int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
93{
Jens Axboeef635052012-02-15 22:24:19 +010094 struct timeval tv_s;
Jens Axboe656b1392009-07-01 23:02:10 +020095 struct timespec t;
96 int ret = 0;
97
Jianpeng Ma8f801ad2013-03-22 07:16:32 -060098 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe8b4e9542013-03-21 10:05:07 -060099
Jens Axboeb11e4cc2012-02-20 09:18:43 +0100100 gettimeofday(&tv_s, NULL);
101 t.tv_sec = tv_s.tv_sec + seconds;
102 t.tv_nsec = tv_s.tv_usec * 1000;
Jens Axboe656b1392009-07-01 23:02:10 +0200103
104 pthread_mutex_lock(&mutex->lock);
105
106 while (!mutex->value && !ret) {
107 mutex->waiters++;
Jens Axboeef635052012-02-15 22:24:19 +0100108
109 /*
110 * Some platforms (FreeBSD 9?) seems to return timed out
111 * way too early, double check.
112 */
Jens Axboe656b1392009-07-01 23:02:10 +0200113 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
Jens Axboed7df1d12013-03-20 19:57:01 -0600114 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
Jens Axboeef635052012-02-15 22:24:19 +0100115 ret = 0;
Jens Axboeef635052012-02-15 22:24:19 +0100116
Jens Axboe656b1392009-07-01 23:02:10 +0200117 mutex->waiters--;
118 }
119
120 if (!ret) {
121 mutex->value--;
122 pthread_mutex_unlock(&mutex->lock);
123 }
124
125 return ret;
126}
127
Jens Axboecdd18ad2008-02-27 18:58:00 +0100128void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100129{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600130 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe8b4e9542013-03-21 10:05:07 -0600131
Jens Axboecdd18ad2008-02-27 18:58:00 +0100132 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100133
134 while (!mutex->value) {
135 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100136 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100137 mutex->waiters--;
138 }
139
Jens Axboecdd18ad2008-02-27 18:58:00 +0100140 mutex->value--;
141 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100142}
143
Jens Axboecdd18ad2008-02-27 18:58:00 +0100144void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100145{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600146 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe8b4e9542013-03-21 10:05:07 -0600147
Jens Axboecdd18ad2008-02-27 18:58:00 +0100148 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100149 read_barrier();
150 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100151 pthread_cond_signal(&mutex->cond);
152 mutex->value++;
153 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100154}
Jens Axboe64d4d312008-03-03 10:36:27 +0100155
Jens Axboed7df1d12013-03-20 19:57:01 -0600156void fio_rwlock_write(struct fio_rwlock *lock)
Jens Axboe64d4d312008-03-03 10:36:27 +0100157{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600158 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600159 pthread_rwlock_wrlock(&lock->lock);
160}
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100161
Jens Axboed7df1d12013-03-20 19:57:01 -0600162void fio_rwlock_read(struct fio_rwlock *lock)
163{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600164 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600165 pthread_rwlock_rdlock(&lock->lock);
166}
167
168void fio_rwlock_unlock(struct fio_rwlock *lock)
169{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600170 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600171 pthread_rwlock_unlock(&lock->lock);
172}
173
174void fio_rwlock_remove(struct fio_rwlock *lock)
175{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600176 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600177 munmap((void *) lock, sizeof(*lock));
178}
179
180struct fio_rwlock *fio_rwlock_init(void)
181{
182 struct fio_rwlock *lock;
Shaohua Li33980f52013-03-28 08:28:51 -0600183 pthread_rwlockattr_t attr;
Jens Axboed7df1d12013-03-20 19:57:01 -0600184 int ret;
185
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");
191 lock = NULL;
192 goto err;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100193 }
194
Jens Axboe8b4e9542013-03-21 10:05:07 -0600195 lock->magic = FIO_RWLOCK_MAGIC;
196
Shaohua Li33980f52013-03-28 08:28:51 -0600197 ret = pthread_rwlockattr_init(&attr);
Jens Axboed7df1d12013-03-20 19:57:01 -0600198 if (ret) {
Bruce Crancdb57fe2013-03-29 16:02:53 +0000199 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
Jens Axboed7df1d12013-03-20 19:57:01 -0600200 goto err;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100201 }
Shaohua Li33980f52013-03-28 08:28:51 -0600202#ifdef FIO_HAVE_PSHARED_MUTEX
203 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
204 if (ret) {
Bruce Crancdb57fe2013-03-29 16:02:53 +0000205 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
Shaohua Li33980f52013-03-28 08:28:51 -0600206 goto destroy_attr;
207 }
Shaohua Li33980f52013-03-28 08:28:51 -0600208
209 ret = pthread_rwlock_init(&lock->lock, &attr);
Bruce Cran09f17d62013-03-29 16:17:25 +0000210#else
211 ret = pthread_rwlock_init(&lock->lock, NULL);
212#endif
213
Shaohua Li33980f52013-03-28 08:28:51 -0600214 if (ret) {
215 log_err("pthread_rwlock_init: %s\n", strerror(ret));
216 goto destroy_attr;
217 }
218
219 pthread_rwlockattr_destroy(&attr);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100220
Jens Axboed7df1d12013-03-20 19:57:01 -0600221 return lock;
Shaohua Li33980f52013-03-28 08:28:51 -0600222destroy_attr:
223 pthread_rwlockattr_destroy(&attr);
Jens Axboed7df1d12013-03-20 19:57:01 -0600224err:
225 if (lock)
226 fio_rwlock_remove(lock);
227 return NULL;
Jens Axboe64d4d312008-03-03 10:36:27 +0100228}