Fix $(CC) override: use system compiler except on HP-UX and Solaris.
[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 <errno.h>
8 #include <pthread.h>
9 #include <sys/mman.h>
10
11 #include "fio.h"
12 #include "log.h"
13 #include "mutex.h"
14 #include "arch/arch.h"
15 #include "os/os.h"
16 #include "helpers.h"
17 #include "time.h"
18 #include "gettime.h"
19
20 void fio_mutex_remove(struct fio_mutex *mutex)
21 {
22         pthread_cond_destroy(&mutex->cond);
23         munmap((void *) mutex, sizeof(*mutex));
24 }
25
26 struct fio_mutex *fio_mutex_init(int value)
27 {
28         struct fio_mutex *mutex = NULL;
29         pthread_mutexattr_t attr;
30         pthread_condattr_t cond;
31         int ret;
32
33         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
34                                 PROT_READ | PROT_WRITE,
35                                 OS_MAP_ANON | MAP_SHARED, -1, 0);
36         if (mutex == MAP_FAILED) {
37                 perror("mmap mutex");
38                 mutex = NULL;
39                 goto err;
40         }
41
42         mutex->value = value;
43
44         ret = pthread_mutexattr_init(&attr);
45         if (ret) {
46                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
47                 goto err;
48         }
49
50         /*
51          * Not all platforms support process shared mutexes (FreeBSD)
52          */
53 #ifdef FIO_HAVE_PSHARED_MUTEX
54         ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
55         if (ret) {
56                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
57                 goto err;
58         }
59 #endif
60
61         pthread_condattr_init(&cond);
62 #ifdef FIO_HAVE_PSHARED_MUTEX
63         pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
64 #endif
65         pthread_cond_init(&mutex->cond, &cond);
66
67         ret = pthread_mutex_init(&mutex->lock, &attr);
68         if (ret) {
69                 log_err("pthread_mutex_init: %s\n", strerror(ret));
70                 goto err;
71         }
72
73         pthread_condattr_destroy(&cond);
74         pthread_mutexattr_destroy(&attr);
75
76         return mutex;
77 err:
78         if (mutex)
79                 fio_mutex_remove(mutex);
80
81         return NULL;
82 }
83
84 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
85 {
86         return mtime_since_now(t) >= seconds * 1000;
87 }
88
89 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
90 {
91         struct timeval tv_s;
92         struct timespec t;
93         int ret = 0;
94
95         gettimeofday(&tv_s, NULL);
96         t.tv_sec = tv_s.tv_sec + seconds;
97         t.tv_nsec = tv_s.tv_usec * 1000;
98
99         pthread_mutex_lock(&mutex->lock);
100
101         while (!mutex->value && !ret) {
102                 mutex->waiters++;
103
104                 /*
105                  * Some platforms (FreeBSD 9?) seems to return timed out
106                  * way too early, double check.
107                  */
108                 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
109                 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
110                         pthread_mutex_lock(&mutex->lock);
111                         ret = 0;
112                 }
113
114                 mutex->waiters--;
115         }
116
117         if (!ret) {
118                 mutex->value--;
119                 pthread_mutex_unlock(&mutex->lock);
120         }
121
122         return ret;
123 }
124
125 void fio_mutex_down(struct fio_mutex *mutex)
126 {
127         pthread_mutex_lock(&mutex->lock);
128
129         while (!mutex->value) {
130                 mutex->waiters++;
131                 pthread_cond_wait(&mutex->cond, &mutex->lock);
132                 mutex->waiters--;
133         }
134
135         mutex->value--;
136         pthread_mutex_unlock(&mutex->lock);
137 }
138
139 void fio_mutex_up(struct fio_mutex *mutex)
140 {
141         pthread_mutex_lock(&mutex->lock);
142         read_barrier();
143         if (!mutex->value && mutex->waiters)
144                 pthread_cond_signal(&mutex->cond);
145         mutex->value++;
146         pthread_mutex_unlock(&mutex->lock);
147 }
148
149 void fio_mutex_down_write(struct fio_mutex *mutex)
150 {
151         pthread_mutex_lock(&mutex->lock);
152
153         while (mutex->value != 0) {
154                 mutex->waiters++;
155                 pthread_cond_wait(&mutex->cond, &mutex->lock);
156                 mutex->waiters--;
157         }
158
159         mutex->value--;
160         pthread_mutex_unlock(&mutex->lock);
161 }
162
163 void fio_mutex_down_read(struct fio_mutex *mutex)
164 {
165         pthread_mutex_lock(&mutex->lock);
166
167         while (mutex->value < 0) {
168                 mutex->waiters++;
169                 pthread_cond_wait(&mutex->cond, &mutex->lock);
170                 mutex->waiters--;
171         }
172
173         mutex->value++;
174         pthread_mutex_unlock(&mutex->lock);
175 }
176
177 void fio_mutex_up_read(struct fio_mutex *mutex)
178 {
179         pthread_mutex_lock(&mutex->lock);
180         mutex->value--;
181         read_barrier();
182         if (mutex->value >= 0 && mutex->waiters)
183                 pthread_cond_signal(&mutex->cond);
184         pthread_mutex_unlock(&mutex->lock);
185 }
186
187 void fio_mutex_up_write(struct fio_mutex *mutex)
188 {
189         pthread_mutex_lock(&mutex->lock);
190         mutex->value++;
191         read_barrier();
192         if (mutex->value >= 0 && mutex->waiters)
193                 pthread_cond_signal(&mutex->cond);
194         pthread_mutex_unlock(&mutex->lock);
195 }