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