Commit | Line | Data |
---|---|---|
07739b57 JA |
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" | |
4d4e80f2 | 10 | #include "arch/arch.h" |
07739b57 | 11 | |
cdd18ad8 | 12 | void fio_mutex_remove(struct fio_mutex *mutex) |
07739b57 | 13 | { |
cdd18ad8 | 14 | close(mutex->mutex_fd); |
5921e80c | 15 | munmap((void *) mutex, sizeof(*mutex)); |
07739b57 JA |
16 | } |
17 | ||
cdd18ad8 | 18 | struct fio_mutex *fio_mutex_init(int value) |
07739b57 | 19 | { |
cdd18ad8 JA |
20 | char mutex_name[] = "/tmp/.fio_mutex.XXXXXX"; |
21 | struct fio_mutex *mutex = NULL; | |
07739b57 | 22 | pthread_mutexattr_t attr; |
108fcc11 | 23 | pthread_condattr_t cond; |
07739b57 JA |
24 | int fd; |
25 | ||
cdd18ad8 | 26 | fd = mkstemp(mutex_name); |
07739b57 | 27 | if (fd < 0) { |
cdd18ad8 | 28 | perror("open mutex"); |
07739b57 JA |
29 | return NULL; |
30 | } | |
31 | ||
cdd18ad8 JA |
32 | if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) { |
33 | perror("ftruncate mutex"); | |
e53bd0b3 | 34 | goto err; |
07739b57 JA |
35 | } |
36 | ||
5921e80c JA |
37 | mutex = (void *) mmap(NULL, sizeof(struct fio_mutex), |
38 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
cdd18ad8 JA |
39 | if (mutex == MAP_FAILED) { |
40 | perror("mmap mutex"); | |
07739b57 | 41 | close(fd); |
cdd18ad8 | 42 | mutex = NULL; |
e53bd0b3 | 43 | goto err; |
07739b57 JA |
44 | } |
45 | ||
cdd18ad8 JA |
46 | unlink(mutex_name); |
47 | mutex->mutex_fd = fd; | |
48 | mutex->value = value; | |
07739b57 | 49 | |
42124d37 | 50 | if (pthread_mutexattr_init(&attr) < 0) { |
07739b57 JA |
51 | perror("pthread_mutexattr_init"); |
52 | goto err; | |
53 | } | |
42124d37 | 54 | if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) < 0) { |
07739b57 JA |
55 | perror("pthread_mutexattr_setpshared"); |
56 | goto err; | |
57 | } | |
108fcc11 ZY |
58 | |
59 | pthread_condattr_init(&cond); | |
60 | pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); | |
cdd18ad8 | 61 | pthread_cond_init(&mutex->cond, &cond); |
108fcc11 | 62 | |
42124d37 | 63 | if (pthread_mutex_init(&mutex->lock, &attr) < 0) { |
07739b57 JA |
64 | perror("pthread_mutex_init"); |
65 | goto err; | |
66 | } | |
67 | ||
cdd18ad8 | 68 | return mutex; |
07739b57 | 69 | err: |
cdd18ad8 JA |
70 | if (mutex) |
71 | fio_mutex_remove(mutex); | |
f7c9e00e | 72 | |
cdd18ad8 | 73 | unlink(mutex_name); |
07739b57 JA |
74 | return NULL; |
75 | } | |
76 | ||
cdd18ad8 | 77 | void fio_mutex_down(struct fio_mutex *mutex) |
07739b57 | 78 | { |
cdd18ad8 | 79 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
80 | |
81 | while (!mutex->value) { | |
82 | mutex->waiters++; | |
cdd18ad8 | 83 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
84 | mutex->waiters--; |
85 | } | |
86 | ||
cdd18ad8 JA |
87 | mutex->value--; |
88 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 JA |
89 | } |
90 | ||
cdd18ad8 | 91 | void fio_mutex_up(struct fio_mutex *mutex) |
07739b57 | 92 | { |
cdd18ad8 | 93 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
94 | read_barrier(); |
95 | if (!mutex->value && mutex->waiters) | |
cdd18ad8 JA |
96 | pthread_cond_signal(&mutex->cond); |
97 | mutex->value++; | |
98 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 | 99 | } |
64d4d313 JA |
100 | |
101 | void fio_mutex_down_write(struct fio_mutex *mutex) | |
102 | { | |
103 | pthread_mutex_lock(&mutex->lock); | |
4d4e80f2 JA |
104 | |
105 | while (mutex->value != 0) { | |
106 | mutex->waiters++; | |
64d4d313 | 107 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
108 | mutex->waiters--; |
109 | } | |
110 | ||
64d4d313 JA |
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); | |
4d4e80f2 JA |
118 | |
119 | while (mutex->value < 0) { | |
120 | mutex->waiters++; | |
64d4d313 | 121 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
122 | mutex->waiters--; |
123 | } | |
124 | ||
64d4d313 JA |
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--; | |
4d4e80f2 JA |
133 | read_barrier(); |
134 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
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++; | |
4d4e80f2 JA |
143 | read_barrier(); |
144 | if (mutex->value >= 0 && mutex->waiters) | |
64d4d313 JA |
145 | pthread_cond_signal(&mutex->cond); |
146 | pthread_mutex_unlock(&mutex->lock); | |
147 | } |