Merge branch 'master' of ssh://brick.kernel.dk/data/git/fio
[fio.git] / mutex.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
6#include <time.h>
7#include <pthread.h>
8#include <sys/mman.h>
9
10#include "log.h"
11#include "mutex.h"
12#include "arch/arch.h"
13#include "os/os.h"
14
15void fio_mutex_remove(struct fio_mutex *mutex)
16{
17 close(mutex->mutex_fd);
18 munmap((void *) mutex, sizeof(*mutex));
19}
20
21struct fio_mutex *fio_mutex_init(int value)
22{
23 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
24 struct fio_mutex *mutex = NULL;
25 pthread_mutexattr_t attr;
26 pthread_condattr_t cond;
27 int fd, ret, mflag;
28
29 fd = mkstemp(mutex_name);
30 if (fd < 0) {
31 perror("open mutex");
32 return NULL;
33 }
34
35 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
36 perror("ftruncate mutex");
37 goto err;
38 }
39
40 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
41 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
42 if (mutex == MAP_FAILED) {
43 perror("mmap mutex");
44 close(fd);
45 mutex = NULL;
46 goto err;
47 }
48
49 unlink(mutex_name);
50 mutex->mutex_fd = fd;
51 mutex->value = value;
52
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
62 ret = pthread_mutexattr_init(&attr);
63 if (ret) {
64 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
65 goto err;
66 }
67 ret = pthread_mutexattr_setpshared(&attr, mflag);
68 if (ret) {
69 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
70 goto err;
71 }
72
73 pthread_condattr_init(&cond);
74 pthread_condattr_setpshared(&cond, mflag);
75 pthread_condattr_setclock(&cond, CLOCK_MONOTONIC);
76 pthread_cond_init(&mutex->cond, &cond);
77
78 ret = pthread_mutex_init(&mutex->lock, &attr);
79 if (ret) {
80 log_err("pthread_mutex_init: %s\n", strerror(ret));
81 goto err;
82 }
83
84 return mutex;
85err:
86 if (mutex)
87 fio_mutex_remove(mutex);
88
89 unlink(mutex_name);
90 return NULL;
91}
92
93int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
94{
95 struct timespec t;
96 int ret = 0;
97
98 clock_gettime(CLOCK_MONOTONIC, &t);
99 t.tv_sec += seconds;
100
101 pthread_mutex_lock(&mutex->lock);
102
103 while (!mutex->value && !ret) {
104 mutex->waiters++;
105 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
106 mutex->waiters--;
107 }
108
109 if (!ret) {
110 mutex->value--;
111 pthread_mutex_unlock(&mutex->lock);
112 }
113
114 return ret;
115}
116
117void fio_mutex_down(struct fio_mutex *mutex)
118{
119 pthread_mutex_lock(&mutex->lock);
120
121 while (!mutex->value) {
122 mutex->waiters++;
123 pthread_cond_wait(&mutex->cond, &mutex->lock);
124 mutex->waiters--;
125 }
126
127 mutex->value--;
128 pthread_mutex_unlock(&mutex->lock);
129}
130
131void fio_mutex_up(struct fio_mutex *mutex)
132{
133 pthread_mutex_lock(&mutex->lock);
134 read_barrier();
135 if (!mutex->value && mutex->waiters)
136 pthread_cond_signal(&mutex->cond);
137 mutex->value++;
138 pthread_mutex_unlock(&mutex->lock);
139}
140
141void fio_mutex_down_write(struct fio_mutex *mutex)
142{
143 pthread_mutex_lock(&mutex->lock);
144
145 while (mutex->value != 0) {
146 mutex->waiters++;
147 pthread_cond_wait(&mutex->cond, &mutex->lock);
148 mutex->waiters--;
149 }
150
151 mutex->value--;
152 pthread_mutex_unlock(&mutex->lock);
153}
154
155void fio_mutex_down_read(struct fio_mutex *mutex)
156{
157 pthread_mutex_lock(&mutex->lock);
158
159 while (mutex->value < 0) {
160 mutex->waiters++;
161 pthread_cond_wait(&mutex->cond, &mutex->lock);
162 mutex->waiters--;
163 }
164
165 mutex->value++;
166 pthread_mutex_unlock(&mutex->lock);
167}
168
169void fio_mutex_up_read(struct fio_mutex *mutex)
170{
171 pthread_mutex_lock(&mutex->lock);
172 mutex->value--;
173 read_barrier();
174 if (mutex->value >= 0 && mutex->waiters)
175 pthread_cond_signal(&mutex->cond);
176 pthread_mutex_unlock(&mutex->lock);
177}
178
179void fio_mutex_up_write(struct fio_mutex *mutex)
180{
181 pthread_mutex_lock(&mutex->lock);
182 mutex->value++;
183 read_barrier();
184 if (mutex->value >= 0 && mutex->waiters)
185 pthread_cond_signal(&mutex->cond);
186 pthread_mutex_unlock(&mutex->lock);
187}