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> |
| 10 | |
Jens Axboe | b4c1fb3 | 2012-02-16 22:22:46 +0100 | [diff] [blame] | 11 | #include "fio.h" |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 12 | #include "log.h" |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 13 | #include "mutex.h" |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 14 | #include "arch/arch.h" |
Jens Axboe | 3c2d93e | 2009-01-05 19:04:15 +0100 | [diff] [blame] | 15 | #include "os/os.h" |
Jens Axboe | 3b2e146 | 2009-12-15 08:58:10 +0100 | [diff] [blame] | 16 | #include "helpers.h" |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 17 | #include "time.h" |
| 18 | #include "gettime.h" |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 19 | |
Jens Axboe | b4c1fb3 | 2012-02-16 22:22:46 +0100 | [diff] [blame] | 20 | static clockid_t fio_clk_id = CLOCK_REALTIME; |
| 21 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 22 | void fio_mutex_remove(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 23 | { |
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 | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 28 | struct fio_mutex *fio_mutex_init(int value) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 29 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 30 | struct fio_mutex *mutex = NULL; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 31 | pthread_mutexattr_t attr; |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 32 | pthread_condattr_t cond; |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 33 | int ret; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 34 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 35 | mutex = (void *) mmap(NULL, sizeof(struct fio_mutex), |
Jens Axboe | b6f6abc | 2011-05-02 10:10:56 -0600 | [diff] [blame] | 36 | PROT_READ | PROT_WRITE, |
| 37 | OS_MAP_ANON | MAP_SHARED, -1, 0); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 38 | if (mutex == MAP_FAILED) { |
| 39 | perror("mmap mutex"); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 40 | mutex = NULL; |
Jens Axboe | e53bd0b | 2007-03-08 20:29:11 +0100 | [diff] [blame] | 41 | goto err; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 44 | mutex->value = value; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 45 | |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 46 | ret = pthread_mutexattr_init(&attr); |
| 47 | if (ret) { |
| 48 | log_err("pthread_mutexattr_init: %s\n", strerror(ret)); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 49 | goto err; |
| 50 | } |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * Not all platforms support process shared mutexes (FreeBSD) |
| 54 | */ |
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 | ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 57 | if (ret) { |
| 58 | log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 59 | goto err; |
| 60 | } |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 61 | #endif |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 62 | |
| 63 | pthread_condattr_init(&cond); |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 64 | #ifdef FIO_HAVE_PSHARED_MUTEX |
Jens Axboe | e721c57 | 2012-02-09 20:55:29 +0100 | [diff] [blame] | 65 | pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 66 | #endif |
Jens Axboe | 23a2a8e | 2012-02-17 13:48:08 +0100 | [diff] [blame] | 67 | #ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK |
Jens Axboe | b4c1fb3 | 2012-02-16 22:22:46 +0100 | [diff] [blame] | 68 | pthread_condattr_setclock(&cond, fio_clk_id); |
Jens Axboe | ceab2ea | 2012-02-15 09:56:51 +0100 | [diff] [blame] | 69 | #endif |
Jens Axboe | 58a157d | 2012-02-15 22:20:26 +0100 | [diff] [blame] | 70 | pthread_cond_init(&mutex->cond, &cond); |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 71 | |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 72 | ret = pthread_mutex_init(&mutex->lock, &attr); |
| 73 | if (ret) { |
| 74 | log_err("pthread_mutex_init: %s\n", strerror(ret)); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 75 | goto err; |
| 76 | } |
| 77 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 78 | pthread_condattr_destroy(&cond); |
| 79 | pthread_mutexattr_destroy(&attr); |
| 80 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 81 | return mutex; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 82 | err: |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 83 | if (mutex) |
| 84 | fio_mutex_remove(mutex); |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 85 | |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
| 88 | |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 89 | static int mutex_timed_out(struct timeval *t, unsigned int seconds) |
| 90 | { |
| 91 | return mtime_since_now(t) >= seconds * 1000; |
| 92 | } |
| 93 | |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 94 | int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds) |
| 95 | { |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 96 | struct timeval tv_s; |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 97 | struct timespec t; |
| 98 | int ret = 0; |
| 99 | |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 100 | fio_gettime(&tv_s, NULL); |
| 101 | |
Jens Axboe | b4c1fb3 | 2012-02-16 22:22:46 +0100 | [diff] [blame] | 102 | clock_gettime(fio_clk_id, &t); |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 103 | t.tv_sec += seconds; |
| 104 | |
| 105 | pthread_mutex_lock(&mutex->lock); |
| 106 | |
| 107 | while (!mutex->value && !ret) { |
| 108 | mutex->waiters++; |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * Some platforms (FreeBSD 9?) seems to return timed out |
| 112 | * way too early, double check. |
| 113 | */ |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 114 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); |
Jens Axboe | ef63505 | 2012-02-15 22:24:19 +0100 | [diff] [blame] | 115 | if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) { |
| 116 | pthread_mutex_lock(&mutex->lock); |
| 117 | ret = 0; |
| 118 | } |
| 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 | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 131 | void fio_mutex_down(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 132 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 133 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 134 | |
| 135 | while (!mutex->value) { |
| 136 | mutex->waiters++; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 137 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 138 | mutex->waiters--; |
| 139 | } |
| 140 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 141 | mutex->value--; |
| 142 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 145 | void fio_mutex_up(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 146 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 147 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 148 | read_barrier(); |
| 149 | if (!mutex->value && mutex->waiters) |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 150 | pthread_cond_signal(&mutex->cond); |
| 151 | mutex->value++; |
| 152 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 153 | } |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 154 | |
| 155 | void fio_mutex_down_write(struct fio_mutex *mutex) |
| 156 | { |
| 157 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 158 | |
| 159 | while (mutex->value != 0) { |
| 160 | mutex->waiters++; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 161 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 162 | mutex->waiters--; |
| 163 | } |
| 164 | |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 165 | mutex->value--; |
| 166 | pthread_mutex_unlock(&mutex->lock); |
| 167 | } |
| 168 | |
| 169 | void fio_mutex_down_read(struct fio_mutex *mutex) |
| 170 | { |
| 171 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 172 | |
| 173 | while (mutex->value < 0) { |
| 174 | mutex->waiters++; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 175 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 176 | mutex->waiters--; |
| 177 | } |
| 178 | |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 179 | mutex->value++; |
| 180 | pthread_mutex_unlock(&mutex->lock); |
| 181 | } |
| 182 | |
| 183 | void fio_mutex_up_read(struct fio_mutex *mutex) |
| 184 | { |
| 185 | pthread_mutex_lock(&mutex->lock); |
| 186 | mutex->value--; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 187 | read_barrier(); |
| 188 | if (mutex->value >= 0 && mutex->waiters) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 189 | pthread_cond_signal(&mutex->cond); |
| 190 | pthread_mutex_unlock(&mutex->lock); |
| 191 | } |
| 192 | |
| 193 | void fio_mutex_up_write(struct fio_mutex *mutex) |
| 194 | { |
| 195 | pthread_mutex_lock(&mutex->lock); |
| 196 | mutex->value++; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 197 | read_barrier(); |
| 198 | if (mutex->value >= 0 && mutex->waiters) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 199 | pthread_cond_signal(&mutex->cond); |
| 200 | pthread_mutex_unlock(&mutex->lock); |
| 201 | } |
Jens Axboe | b4c1fb3 | 2012-02-16 22:22:46 +0100 | [diff] [blame] | 202 | |
| 203 | static void fio_init fio_mutex_global_init(void) |
| 204 | { |
| 205 | #ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK |
| 206 | #ifdef FIO_HAVE_CLOCK_MONOTONIC |
| 207 | pthread_condattr_t cond; |
| 208 | |
| 209 | pthread_condattr_init(&cond); |
| 210 | |
| 211 | if (!pthread_condattr_setclock(&cond, CLOCK_MONOTONIC)) |
| 212 | fio_clk_id = CLOCK_MONOTONIC; |
| 213 | |
| 214 | pthread_condattr_destroy(&cond); |
| 215 | #endif |
| 216 | #endif |
| 217 | } |