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