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