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> |
| 6 | #include <pthread.h> |
| 7 | #include <sys/mman.h> |
| 8 | |
| 9 | #include "mutex.h" |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 10 | #include "arch/arch.h" |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 11 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 12 | void fio_mutex_remove(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 13 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 14 | close(mutex->mutex_fd); |
| 15 | munmap(mutex, sizeof(*mutex)); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 16 | } |
| 17 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 18 | struct fio_mutex *fio_mutex_init(int value) |
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 | char mutex_name[] = "/tmp/.fio_mutex.XXXXXX"; |
| 21 | struct fio_mutex *mutex = NULL; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 22 | pthread_mutexattr_t attr; |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 23 | pthread_condattr_t cond; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 24 | int fd; |
| 25 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 26 | fd = mkstemp(mutex_name); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 27 | if (fd < 0) { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 28 | perror("open mutex"); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 29 | return NULL; |
| 30 | } |
| 31 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 32 | if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) { |
| 33 | perror("ftruncate mutex"); |
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 = mmap(NULL, sizeof(struct fio_mutex), PROT_READ | PROT_WRITE, |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 38 | MAP_SHARED, fd, 0); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 39 | if (mutex == MAP_FAILED) { |
| 40 | perror("mmap mutex"); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 41 | close(fd); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 42 | mutex = NULL; |
Jens Axboe | e53bd0b | 2007-03-08 20:29:11 +0100 | [diff] [blame] | 43 | goto err; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 46 | unlink(mutex_name); |
| 47 | mutex->mutex_fd = fd; |
| 48 | mutex->value = value; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 49 | |
| 50 | if (pthread_mutexattr_init(&attr)) { |
| 51 | perror("pthread_mutexattr_init"); |
| 52 | goto err; |
| 53 | } |
| 54 | if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) { |
| 55 | perror("pthread_mutexattr_setpshared"); |
| 56 | goto err; |
| 57 | } |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 58 | |
| 59 | pthread_condattr_init(&cond); |
| 60 | pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 61 | pthread_cond_init(&mutex->cond, &cond); |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame] | 62 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 63 | if (pthread_mutex_init(&mutex->lock, &attr)) { |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 64 | perror("pthread_mutex_init"); |
| 65 | goto err; |
| 66 | } |
| 67 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 68 | return mutex; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 69 | err: |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 70 | if (mutex) |
| 71 | fio_mutex_remove(mutex); |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 72 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 73 | unlink(mutex_name); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 77 | void fio_mutex_down(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 78 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 79 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 80 | |
| 81 | while (!mutex->value) { |
| 82 | mutex->waiters++; |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 83 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 84 | mutex->waiters--; |
| 85 | } |
| 86 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 87 | mutex->value--; |
| 88 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 91 | void fio_mutex_up(struct fio_mutex *mutex) |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 92 | { |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 93 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 94 | read_barrier(); |
| 95 | if (!mutex->value && mutex->waiters) |
Jens Axboe | cdd18ad | 2008-02-27 18:58:00 +0100 | [diff] [blame] | 96 | pthread_cond_signal(&mutex->cond); |
| 97 | mutex->value++; |
| 98 | pthread_mutex_unlock(&mutex->lock); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 99 | } |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 100 | |
| 101 | void fio_mutex_down_write(struct fio_mutex *mutex) |
| 102 | { |
| 103 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 104 | |
| 105 | while (mutex->value != 0) { |
| 106 | mutex->waiters++; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 107 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 108 | mutex->waiters--; |
| 109 | } |
| 110 | |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 111 | mutex->value--; |
| 112 | pthread_mutex_unlock(&mutex->lock); |
| 113 | } |
| 114 | |
| 115 | void fio_mutex_down_read(struct fio_mutex *mutex) |
| 116 | { |
| 117 | pthread_mutex_lock(&mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 118 | |
| 119 | while (mutex->value < 0) { |
| 120 | mutex->waiters++; |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 121 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 122 | mutex->waiters--; |
| 123 | } |
| 124 | |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 125 | mutex->value++; |
| 126 | pthread_mutex_unlock(&mutex->lock); |
| 127 | } |
| 128 | |
| 129 | void fio_mutex_up_read(struct fio_mutex *mutex) |
| 130 | { |
| 131 | pthread_mutex_lock(&mutex->lock); |
| 132 | mutex->value--; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 133 | read_barrier(); |
| 134 | if (mutex->value >= 0 && mutex->waiters) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 135 | pthread_cond_signal(&mutex->cond); |
| 136 | pthread_mutex_unlock(&mutex->lock); |
| 137 | } |
| 138 | |
| 139 | void fio_mutex_up_write(struct fio_mutex *mutex) |
| 140 | { |
| 141 | pthread_mutex_lock(&mutex->lock); |
| 142 | mutex->value++; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 143 | read_barrier(); |
| 144 | if (mutex->value >= 0 && mutex->waiters) |
Jens Axboe | 64d4d31 | 2008-03-03 10:36:27 +0100 | [diff] [blame] | 145 | pthread_cond_signal(&mutex->cond); |
| 146 | pthread_mutex_unlock(&mutex->lock); |
| 147 | } |