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 | b6f6abc | 2011-05-02 10:10:56 -0600 | [diff] [blame] | 26 | int ret, mflag; |
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 | f356d01 | 2009-01-05 09:56:29 +0100 | [diff] [blame] | 39 | /* |
| 40 | * Not all platforms support process shared mutexes (FreeBSD) |
| 41 | */ |
| 42 | #ifdef FIO_HAVE_PSHARED_MUTEX |
| 43 | mflag = PTHREAD_PROCESS_SHARED; |
| 44 | #else |
| 45 | mflag = PTHREAD_PROCESS_PRIVATE; |
| 46 | #endif |
| 47 | |
Jens Axboe | 4fa6d0f | 2009-01-05 09:45:13 +0100 | [diff] [blame] | 48 | ret = pthread_mutexattr_init(&attr); |
| 49 | if (ret) { |
| 50 | log_err("pthread_mutexattr_init: %s\n", strerror(ret)); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 51 | goto err; |
| 52 | } |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 53 | #ifdef FIO_HAVE_PSHARED_MUTEX |
Jens Axboe | f356d01 | 2009-01-05 09:56:29 +0100 | [diff] [blame] | 54 | ret = pthread_mutexattr_setpshared(&attr, mflag); |
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 | f356d01 | 2009-01-05 09:56:29 +0100 | [diff] [blame] | 63 | pthread_condattr_setpshared(&cond, mflag); |
YAMAMOTO Takashi | 7452440 | 2010-05-12 15:06:46 +0200 | [diff] [blame] | 64 | #endif |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +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 | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 84 | int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds) |
| 85 | { |
| 86 | struct timespec t; |
| 87 | int ret = 0; |
| 88 | |
Jens Axboe | d481e00 | 2009-12-04 09:56:32 +0100 | [diff] [blame] | 89 | clock_gettime(CLOCK_REALTIME, &t); |
Jens Axboe | 656b139 | 2009-07-01 23:02:10 +0200 | [diff] [blame] | 90 | t.tv_sec += seconds; |
| 91 | |
| 92 | pthread_mutex_lock(&mutex->lock); |
| 93 | |
| 94 | while (!mutex->value && !ret) { |
| 95 | mutex->waiters++; |
| 96 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); |
| 97 | mutex->waiters--; |
| 98 | } |
| 99 | |
| 100 | if (!ret) { |
| 101 | mutex->value--; |
| 102 | pthread_mutex_unlock(&mutex->lock); |
| 103 | } |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 108 | void fio_mutex_down(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 109 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 110 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 111 | |
| 112 | while (!mutex->value) { |
| 113 | mutex->waiters++; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 114 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 115 | mutex->waiters--; |
| 116 | } |
| 117 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 118 | mutex->value--; |
| 119 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 122 | void fio_mutex_up(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 123 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 124 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 125 | read_barrier(); |
| 126 | if (!mutex->value && mutex->waiters) |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 127 | pthread_cond_signal(&mutex->cond); |
| 128 | mutex->value++; |
| 129 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 130 | } |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 131 | |
| 132 | void fio_mutex_down_write(struct fio_mutex *mutex) |
| 133 | { |
| 134 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 135 | |
| 136 | while (mutex->value != 0) { |
| 137 | mutex->waiters++; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 138 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 139 | mutex->waiters--; |
| 140 | } |
| 141 | |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 142 | mutex->value--; |
| 143 | pthread_mutex_unlock(&mutex->lock); |
| 144 | } |
| 145 | |
| 146 | void fio_mutex_down_read(struct fio_mutex *mutex) |
| 147 | { |
| 148 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 149 | |
| 150 | while (mutex->value < 0) { |
| 151 | mutex->waiters++; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 152 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 153 | mutex->waiters--; |
| 154 | } |
| 155 | |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 156 | mutex->value++; |
| 157 | pthread_mutex_unlock(&mutex->lock); |
| 158 | } |
| 159 | |
| 160 | void fio_mutex_up_read(struct fio_mutex *mutex) |
| 161 | { |
| 162 | pthread_mutex_lock(&mutex->lock); |
| 163 | mutex->value--; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 164 | read_barrier(); |
| 165 | if (mutex->value >= 0 && mutex->waiters) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 166 | pthread_cond_signal(&mutex->cond); |
| 167 | pthread_mutex_unlock(&mutex->lock); |
| 168 | } |
| 169 | |
| 170 | void fio_mutex_up_write(struct fio_mutex *mutex) |
| 171 | { |
| 172 | pthread_mutex_lock(&mutex->lock); |
| 173 | mutex->value++; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 174 | read_barrier(); |
| 175 | if (mutex->value >= 0 && mutex->waiters) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 176 | pthread_cond_signal(&mutex->cond); |
| 177 | pthread_mutex_unlock(&mutex->lock); |
| 178 | } |