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