add link to new list archives
[fio.git] / mutex.c
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 #include "helpers.h"
15
16 void fio_mutex_remove(struct fio_mutex *mutex)
17 {
18         close(mutex->mutex_fd);
19         munmap((void *) mutex, sizeof(*mutex));
20 }
21
22 struct fio_mutex *fio_mutex_init(int value)
23 {
24         char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
25         struct fio_mutex *mutex = NULL;
26         pthread_mutexattr_t attr;
27         pthread_condattr_t cond;
28         int fd, ret, mflag;
29
30         fd = mkstemp(mutex_name);
31         if (fd < 0) {
32                 perror("open mutex");
33                 return NULL;
34         }
35
36         if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
37                 perror("ftruncate mutex");
38                 goto err;
39         }
40
41         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
42                                 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43         if (mutex == MAP_FAILED) {
44                 perror("mmap mutex");
45                 close(fd);
46                 mutex = NULL;
47                 goto err;
48         }
49
50         unlink(mutex_name);
51         mutex->mutex_fd = fd;
52         mutex->value = value;
53
54         /*
55          * Not all platforms support process shared mutexes (FreeBSD)
56          */
57 #ifdef FIO_HAVE_PSHARED_MUTEX
58         mflag = PTHREAD_PROCESS_SHARED;
59 #else
60         mflag = PTHREAD_PROCESS_PRIVATE;
61 #endif
62
63         ret = pthread_mutexattr_init(&attr);
64         if (ret) {
65                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
66                 goto err;
67         }
68         ret = pthread_mutexattr_setpshared(&attr, mflag);
69         if (ret) {
70                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
71                 goto err;
72         }
73
74         pthread_condattr_init(&cond);
75         pthread_condattr_setpshared(&cond, mflag);
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;
85 err:
86         if (mutex)
87                 fio_mutex_remove(mutex);
88
89         unlink(mutex_name);
90         return NULL;
91 }
92
93 int 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_REALTIME, &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
117 void 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
131 void 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
141 void 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
155 void 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
169 void 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
179 void 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 }