blob: 9d10c2ce2fe056fefd300ebf16274f85701fa7c6 [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"
Daniel Gollubef3c94b2014-04-30 11:25:04 +020018#include "fio_time.h"
Jens Axboeef635052012-02-15 22:24:19 +010019#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 Axboe72242052014-04-02 15:45:25 -060028int __fio_mutex_init(struct fio_mutex *mutex, int value)
Jens Axboe07739b52007-03-08 20:25:46 +010029{
30 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010031 pthread_condattr_t cond;
Jens Axboee721c572012-02-09 20:55:29 +010032 int ret;
Jens Axboe07739b52007-03-08 20:25:46 +010033
Jens Axboecdd18ad2008-02-27 18:58:00 +010034 mutex->value = value;
Jens Axboe8b4e9542013-03-21 10:05:07 -060035 mutex->magic = FIO_MUTEX_MAGIC;
Jens Axboe07739b52007-03-08 20:25:46 +010036
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010037 ret = pthread_mutexattr_init(&attr);
38 if (ret) {
39 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe72242052014-04-02 15:45:25 -060040 return ret;
Jens Axboe07739b52007-03-08 20:25:46 +010041 }
Jens Axboee721c572012-02-09 20:55:29 +010042
43 /*
44 * Not all platforms support process shared mutexes (FreeBSD)
45 */
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020046#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010047 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010048 if (ret) {
49 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe72242052014-04-02 15:45:25 -060050 return ret;
Jens Axboe07739b52007-03-08 20:25:46 +010051 }
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020052#endif
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010053
54 pthread_condattr_init(&cond);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020055#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010056 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020057#endif
Jens Axboe58a157d2012-02-15 22:20:26 +010058 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010059
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010060 ret = pthread_mutex_init(&mutex->lock, &attr);
61 if (ret) {
62 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe72242052014-04-02 15:45:25 -060063 return ret;
Jens Axboe07739b52007-03-08 20:25:46 +010064 }
65
Bruce Cran03e20d62011-01-02 20:14:54 +010066 pthread_condattr_destroy(&cond);
67 pthread_mutexattr_destroy(&attr);
Jens Axboe72242052014-04-02 15:45:25 -060068 return 0;
69}
Bruce Cran03e20d62011-01-02 20:14:54 +010070
Jens Axboe72242052014-04-02 15:45:25 -060071struct fio_mutex *fio_mutex_init(int value)
72{
73 struct fio_mutex *mutex = NULL;
Jens Axboef7c9e002007-03-09 12:40:02 +010074
Jens Axboe72242052014-04-02 15:45:25 -060075 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) {
79 perror("mmap mutex");
80 return NULL;
81 }
82
83 if (!__fio_mutex_init(mutex, value))
84 return mutex;
85
86 fio_mutex_remove(mutex);
Jens Axboe07739b52007-03-08 20:25:46 +010087 return NULL;
88}
89
Jens Axboeef635052012-02-15 22:24:19 +010090static int mutex_timed_out(struct timeval *t, unsigned int seconds)
91{
92 return mtime_since_now(t) >= seconds * 1000;
93}
94
Jens Axboe656b1392009-07-01 23:02:10 +020095int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
96{
Jens Axboeef635052012-02-15 22:24:19 +010097 struct timeval tv_s;
Jens Axboe656b1392009-07-01 23:02:10 +020098 struct timespec t;
99 int ret = 0;
100
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600101 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe8b4e9542013-03-21 10:05:07 -0600102
Jens Axboeb11e4cc2012-02-20 09:18:43 +0100103 gettimeofday(&tv_s, NULL);
104 t.tv_sec = tv_s.tv_sec + seconds;
105 t.tv_nsec = tv_s.tv_usec * 1000;
Jens Axboe656b1392009-07-01 23:02:10 +0200106
107 pthread_mutex_lock(&mutex->lock);
108
109 while (!mutex->value && !ret) {
110 mutex->waiters++;
Jens Axboeef635052012-02-15 22:24:19 +0100111
112 /*
113 * Some platforms (FreeBSD 9?) seems to return timed out
114 * way too early, double check.
115 */
Jens Axboe656b1392009-07-01 23:02:10 +0200116 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
Jens Axboed7df1d12013-03-20 19:57:01 -0600117 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
Jens Axboeef635052012-02-15 22:24:19 +0100118 ret = 0;
Jens Axboeef635052012-02-15 22:24:19 +0100119
Jens Axboe656b1392009-07-01 23:02:10 +0200120 mutex->waiters--;
121 }
122
123 if (!ret) {
124 mutex->value--;
125 pthread_mutex_unlock(&mutex->lock);
126 }
127
128 return ret;
129}
130
Jens Axboe72242052014-04-02 15:45:25 -0600131int fio_mutex_down_trylock(struct fio_mutex *mutex)
132{
133 int ret = 1;
134
135 assert(mutex->magic == FIO_MUTEX_MAGIC);
136
137 pthread_mutex_lock(&mutex->lock);
138 if (mutex->value) {
139 mutex->value--;
140 ret = 0;
141 }
142 pthread_mutex_unlock(&mutex->lock);
143
144 return ret;
145}
146
Jens Axboecdd18ad2008-02-27 18:58:00 +0100147void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100148{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600149 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe8b4e9542013-03-21 10:05:07 -0600150
Jens Axboecdd18ad2008-02-27 18:58:00 +0100151 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100152
153 while (!mutex->value) {
154 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100155 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100156 mutex->waiters--;
157 }
158
Jens Axboecdd18ad2008-02-27 18:58:00 +0100159 mutex->value--;
160 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100161}
162
Jens Axboecdd18ad2008-02-27 18:58:00 +0100163void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100164{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600165 assert(mutex->magic == FIO_MUTEX_MAGIC);
Jens Axboe8b4e9542013-03-21 10:05:07 -0600166
Jens Axboecdd18ad2008-02-27 18:58:00 +0100167 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100168 read_barrier();
169 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100170 pthread_cond_signal(&mutex->cond);
171 mutex->value++;
172 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100173}
Jens Axboe64d4d312008-03-03 10:36:27 +0100174
Jens Axboed7df1d12013-03-20 19:57:01 -0600175void fio_rwlock_write(struct fio_rwlock *lock)
Jens Axboe64d4d312008-03-03 10:36:27 +0100176{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600177 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600178 pthread_rwlock_wrlock(&lock->lock);
179}
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100180
Jens Axboed7df1d12013-03-20 19:57:01 -0600181void fio_rwlock_read(struct fio_rwlock *lock)
182{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600183 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600184 pthread_rwlock_rdlock(&lock->lock);
185}
186
187void fio_rwlock_unlock(struct fio_rwlock *lock)
188{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600189 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600190 pthread_rwlock_unlock(&lock->lock);
191}
192
193void fio_rwlock_remove(struct fio_rwlock *lock)
194{
Jianpeng Ma8f801ad2013-03-22 07:16:32 -0600195 assert(lock->magic == FIO_RWLOCK_MAGIC);
Jens Axboed7df1d12013-03-20 19:57:01 -0600196 munmap((void *) lock, sizeof(*lock));
197}
198
199struct fio_rwlock *fio_rwlock_init(void)
200{
201 struct fio_rwlock *lock;
Shaohua Li33980f52013-03-28 08:28:51 -0600202 pthread_rwlockattr_t attr;
Jens Axboed7df1d12013-03-20 19:57:01 -0600203 int ret;
204
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");
210 lock = NULL;
211 goto err;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100212 }
213
Jens Axboe8b4e9542013-03-21 10:05:07 -0600214 lock->magic = FIO_RWLOCK_MAGIC;
215
Shaohua Li33980f52013-03-28 08:28:51 -0600216 ret = pthread_rwlockattr_init(&attr);
Jens Axboed7df1d12013-03-20 19:57:01 -0600217 if (ret) {
Bruce Crancdb57fe2013-03-29 16:02:53 +0000218 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
Jens Axboed7df1d12013-03-20 19:57:01 -0600219 goto err;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100220 }
Shaohua Li33980f52013-03-28 08:28:51 -0600221#ifdef FIO_HAVE_PSHARED_MUTEX
222 ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
223 if (ret) {
Bruce Crancdb57fe2013-03-29 16:02:53 +0000224 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
Shaohua Li33980f52013-03-28 08:28:51 -0600225 goto destroy_attr;
226 }
Shaohua Li33980f52013-03-28 08:28:51 -0600227
228 ret = pthread_rwlock_init(&lock->lock, &attr);
Bruce Cran09f17d62013-03-29 16:17:25 +0000229#else
230 ret = pthread_rwlock_init(&lock->lock, NULL);
231#endif
232
Shaohua Li33980f52013-03-28 08:28:51 -0600233 if (ret) {
234 log_err("pthread_rwlock_init: %s\n", strerror(ret));
235 goto destroy_attr;
236 }
237
238 pthread_rwlockattr_destroy(&attr);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100239
Jens Axboed7df1d12013-03-20 19:57:01 -0600240 return lock;
Shaohua Li33980f52013-03-28 08:28:51 -0600241destroy_attr:
242 pthread_rwlockattr_destroy(&attr);
Jens Axboed7df1d12013-03-20 19:57:01 -0600243err:
244 if (lock)
245 fio_rwlock_remove(lock);
246 return NULL;
Jens Axboe64d4d312008-03-03 10:36:27 +0100247}