Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <fcntl.h> |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 6 | #include <time.h> |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 7 | #include <errno.h> |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 8 | #include <pthread.h> |
| 9 | #include <sys/mman.h> |
Jens Axboe | 8b4e954 | 2013-03-21 10:05:07 -0600 | [diff] [blame] | 10 | #include <assert.h> |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 11 | |
Jens Axboe | b4c1fb3 | 2012-02-16 22:22:46 +0100 | [diff] [blame] | 12 | #include "fio.h" |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 13 | #include "log.h" |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 14 | #include "mutex.h" |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 15 | #include "arch/arch.h" |
Jens Axboe | 3c2d93e | 2009-01-05 19:04:15 +0100 | [diff] [blame] | 16 | #include "os/os.h" |
Jens Axboe | 3b2e146 | 2009-12-15 08:58:10 +0100 | [diff] [blame] | 17 | #include "helpers.h" |
Daniel Gollub | ef3c94b | 2014-04-30 11:25:04 +0200 | [diff] [blame] | 18 | #include "fio_time.h" |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 19 | #include "gettime.h" |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 20 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 21 | void fio_mutex_remove(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 22 | { |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 23 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
Jens Axboe | 58a157d | 2012-02-15 22:20:26 +0100 | [diff] [blame] | 24 | pthread_cond_destroy(&mutex->cond); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 25 | munmap((void *) mutex, sizeof(*mutex)); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 26 | } |
| 27 | |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 28 | int __fio_mutex_init(struct fio_mutex *mutex, int value) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 29 | { |
| 30 | pthread_mutexattr_t attr; |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 31 | pthread_condattr_t cond; |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 32 | int ret; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 33 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 34 | mutex->value = value; |
Jens Axboe | 8b4e954 | 2013-03-21 10:05:07 -0600 | [diff] [blame] | 35 | mutex->magic = FIO_MUTEX_MAGIC; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 36 | |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 37 | ret = pthread_mutexattr_init(&attr); |
| 38 | if (ret) { |
| 39 | log_err("pthread_mutexattr_init: %s\n", strerror(ret)); |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 40 | return ret; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 41 | } |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Not all platforms support process shared mutexes (FreeBSD) |
| 45 | */ |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 46 | #ifdef FIO_HAVE_PSHARED_MUTEX |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 47 | ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 48 | if (ret) { |
| 49 | log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 50 | return ret; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 51 | } |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 52 | #endif |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 53 | |
| 54 | pthread_condattr_init(&cond); |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 55 | #ifdef FIO_HAVE_PSHARED_MUTEX |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 56 | pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 57 | #endif |
Jens Axboe | 58a157d | 2012-02-15 22:20:26 +0100 | [diff] [blame] | 58 | pthread_cond_init(&mutex->cond, &cond); |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 59 | |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 60 | ret = pthread_mutex_init(&mutex->lock, &attr); |
| 61 | if (ret) { |
| 62 | log_err("pthread_mutex_init: %s\n", strerror(ret)); |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 63 | return ret; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 66 | pthread_condattr_destroy(&cond); |
| 67 | pthread_mutexattr_destroy(&attr); |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 68 | return 0; |
| 69 | } |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 70 | |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 71 | struct fio_mutex *fio_mutex_init(int value) |
| 72 | { |
| 73 | struct fio_mutex *mutex = NULL; |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 74 | |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 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) { |
| 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 Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 90 | static int mutex_timed_out(struct timeval *t, unsigned int seconds) |
| 91 | { |
| 92 | return mtime_since_now(t) >= seconds * 1000; |
| 93 | } |
| 94 | |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 95 | int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds) |
| 96 | { |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 97 | struct timeval tv_s; |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 98 | struct timespec t; |
| 99 | int ret = 0; |
| 100 | |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 101 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
Jens Axboe | 8b4e954 | 2013-03-21 10:05:07 -0600 | [diff] [blame] | 102 | |
Jens Axboe | b11e4cc | 2012-02-20 09:18:43 +0100 | [diff] [blame] | 103 | gettimeofday(&tv_s, NULL); |
| 104 | t.tv_sec = tv_s.tv_sec + seconds; |
| 105 | t.tv_nsec = tv_s.tv_usec * 1000; |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 106 | |
| 107 | pthread_mutex_lock(&mutex->lock); |
| 108 | |
| 109 | while (!mutex->value && !ret) { |
| 110 | mutex->waiters++; |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Some platforms (FreeBSD 9?) seems to return timed out |
| 114 | * way too early, double check. |
| 115 | */ |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 116 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 117 | if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 118 | ret = 0; |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 119 | |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 120 | mutex->waiters--; |
| 121 | } |
| 122 | |
| 123 | if (!ret) { |
| 124 | mutex->value--; |
| 125 | pthread_mutex_unlock(&mutex->lock); |
| 126 | } |
| 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
Jens Axboe | 7224205 | 2014-04-02 15:45:25 -0600 | [diff] [blame] | 131 | int 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 Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 147 | void fio_mutex_down(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 148 | { |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 149 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
Jens Axboe | 8b4e954 | 2013-03-21 10:05:07 -0600 | [diff] [blame] | 150 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 151 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 152 | |
| 153 | while (!mutex->value) { |
| 154 | mutex->waiters++; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 155 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 156 | mutex->waiters--; |
| 157 | } |
| 158 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 159 | mutex->value--; |
| 160 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 163 | void fio_mutex_up(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 164 | { |
Jens Axboe | f5fc4b4 | 2014-07-08 09:46:37 +0200 | [diff] [blame] | 165 | int do_wake = 0; |
| 166 | |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 167 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
Jens Axboe | 8b4e954 | 2013-03-21 10:05:07 -0600 | [diff] [blame] | 168 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 169 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 170 | read_barrier(); |
| 171 | if (!mutex->value && mutex->waiters) |
Jens Axboe | f5fc4b4 | 2014-07-08 09:46:37 +0200 | [diff] [blame] | 172 | do_wake = 1; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 173 | mutex->value++; |
| 174 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | f5fc4b4 | 2014-07-08 09:46:37 +0200 | [diff] [blame] | 175 | |
| 176 | if (do_wake) |
| 177 | pthread_cond_signal(&mutex->cond); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 178 | } |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 179 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 180 | void fio_rwlock_write(struct fio_rwlock *lock) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 181 | { |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 182 | assert(lock->magic == FIO_RWLOCK_MAGIC); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 183 | pthread_rwlock_wrlock(&lock->lock); |
| 184 | } |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 185 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 186 | void fio_rwlock_read(struct fio_rwlock *lock) |
| 187 | { |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 188 | assert(lock->magic == FIO_RWLOCK_MAGIC); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 189 | pthread_rwlock_rdlock(&lock->lock); |
| 190 | } |
| 191 | |
| 192 | void fio_rwlock_unlock(struct fio_rwlock *lock) |
| 193 | { |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 194 | assert(lock->magic == FIO_RWLOCK_MAGIC); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 195 | pthread_rwlock_unlock(&lock->lock); |
| 196 | } |
| 197 | |
| 198 | void fio_rwlock_remove(struct fio_rwlock *lock) |
| 199 | { |
Jianpeng Ma | 8f801ad | 2013-03-22 07:16:32 -0600 | [diff] [blame] | 200 | assert(lock->magic == FIO_RWLOCK_MAGIC); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 201 | munmap((void *) lock, sizeof(*lock)); |
| 202 | } |
| 203 | |
| 204 | struct fio_rwlock *fio_rwlock_init(void) |
| 205 | { |
| 206 | struct fio_rwlock *lock; |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 207 | pthread_rwlockattr_t attr; |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 208 | int ret; |
| 209 | |
| 210 | lock = (void *) mmap(NULL, sizeof(struct fio_rwlock), |
| 211 | PROT_READ | PROT_WRITE, |
| 212 | OS_MAP_ANON | MAP_SHARED, -1, 0); |
| 213 | if (lock == MAP_FAILED) { |
| 214 | perror("mmap rwlock"); |
| 215 | lock = NULL; |
| 216 | goto err; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Jens Axboe | 8b4e954 | 2013-03-21 10:05:07 -0600 | [diff] [blame] | 219 | lock->magic = FIO_RWLOCK_MAGIC; |
| 220 | |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 221 | ret = pthread_rwlockattr_init(&attr); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 222 | if (ret) { |
Bruce Cran | cdb57fe | 2013-03-29 16:02:53 +0000 | [diff] [blame] | 223 | log_err("pthread_rwlockattr_init: %s\n", strerror(ret)); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 224 | goto err; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 225 | } |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 226 | #ifdef FIO_HAVE_PSHARED_MUTEX |
| 227 | ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
| 228 | if (ret) { |
Bruce Cran | cdb57fe | 2013-03-29 16:02:53 +0000 | [diff] [blame] | 229 | log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret)); |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 230 | goto destroy_attr; |
| 231 | } |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 232 | |
| 233 | ret = pthread_rwlock_init(&lock->lock, &attr); |
Bruce Cran | 09f17d6 | 2013-03-29 16:17:25 +0000 | [diff] [blame] | 234 | #else |
| 235 | ret = pthread_rwlock_init(&lock->lock, NULL); |
| 236 | #endif |
| 237 | |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 238 | if (ret) { |
| 239 | log_err("pthread_rwlock_init: %s\n", strerror(ret)); |
| 240 | goto destroy_attr; |
| 241 | } |
| 242 | |
| 243 | pthread_rwlockattr_destroy(&attr); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 244 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 245 | return lock; |
Shaohua Li | 33980f5 | 2013-03-28 08:28:51 -0600 | [diff] [blame] | 246 | destroy_attr: |
| 247 | pthread_rwlockattr_destroy(&attr); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 248 | err: |
| 249 | if (lock) |
| 250 | fio_rwlock_remove(lock); |
| 251 | return NULL; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 252 | } |