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