genfio: Splitting gen_template in 2 parts
[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 #include <assert.h>
11
12 #include "fio.h"
13 #include "log.h"
14 #include "mutex.h"
15 #include "arch/arch.h"
16 #include "os/os.h"
17 #include "helpers.h"
18 #include "time.h"
19 #include "gettime.h"
20
21 void fio_mutex_remove(struct fio_mutex *mutex)
22 {
23         assert(mutex->magic == FIO_MUTEX_MAGIC);
24         pthread_cond_destroy(&mutex->cond);
25         munmap((void *) mutex, sizeof(*mutex));
26 }
27
28 struct fio_mutex *fio_mutex_init(int value)
29 {
30         struct fio_mutex *mutex = NULL;
31         pthread_mutexattr_t attr;
32         pthread_condattr_t cond;
33         int ret;
34
35         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
36                                 PROT_READ | PROT_WRITE,
37                                 OS_MAP_ANON | MAP_SHARED, -1, 0);
38         if (mutex == MAP_FAILED) {
39                 perror("mmap mutex");
40                 mutex = NULL;
41                 goto err;
42         }
43
44         mutex->value = value;
45         mutex->magic = FIO_MUTEX_MAGIC;
46
47         ret = pthread_mutexattr_init(&attr);
48         if (ret) {
49                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
50                 goto err;
51         }
52
53         /*
54          * Not all platforms support process shared mutexes (FreeBSD)
55          */
56 #ifdef FIO_HAVE_PSHARED_MUTEX
57         ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
58         if (ret) {
59                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
60                 goto err;
61         }
62 #endif
63
64         pthread_condattr_init(&cond);
65 #ifdef FIO_HAVE_PSHARED_MUTEX
66         pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
67 #endif
68         pthread_cond_init(&mutex->cond, &cond);
69
70         ret = pthread_mutex_init(&mutex->lock, &attr);
71         if (ret) {
72                 log_err("pthread_mutex_init: %s\n", strerror(ret));
73                 goto err;
74         }
75
76         pthread_condattr_destroy(&cond);
77         pthread_mutexattr_destroy(&attr);
78
79         return mutex;
80 err:
81         if (mutex)
82                 fio_mutex_remove(mutex);
83
84         return NULL;
85 }
86
87 static int mutex_timed_out(struct timeval *t, unsigned int seconds)
88 {
89         return mtime_since_now(t) >= seconds * 1000;
90 }
91
92 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
93 {
94         struct timeval tv_s;
95         struct timespec t;
96         int ret = 0;
97
98         assert(mutex->magic == FIO_MUTEX_MAGIC);
99
100         gettimeofday(&tv_s, NULL);
101         t.tv_sec = tv_s.tv_sec + seconds;
102         t.tv_nsec = tv_s.tv_usec * 1000;
103
104         pthread_mutex_lock(&mutex->lock);
105
106         while (!mutex->value && !ret) {
107                 mutex->waiters++;
108
109                 /*
110                  * Some platforms (FreeBSD 9?) seems to return timed out
111                  * way too early, double check.
112                  */
113                 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
114                 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
115                         ret = 0;
116
117                 mutex->waiters--;
118         }
119
120         if (!ret) {
121                 mutex->value--;
122                 pthread_mutex_unlock(&mutex->lock);
123         }
124
125         return ret;
126 }
127
128 void fio_mutex_down(struct fio_mutex *mutex)
129 {
130         assert(mutex->magic == FIO_MUTEX_MAGIC);
131
132         pthread_mutex_lock(&mutex->lock);
133
134         while (!mutex->value) {
135                 mutex->waiters++;
136                 pthread_cond_wait(&mutex->cond, &mutex->lock);
137                 mutex->waiters--;
138         }
139
140         mutex->value--;
141         pthread_mutex_unlock(&mutex->lock);
142 }
143
144 void fio_mutex_up(struct fio_mutex *mutex)
145 {
146         assert(mutex->magic == FIO_MUTEX_MAGIC);
147
148         pthread_mutex_lock(&mutex->lock);
149         read_barrier();
150         if (!mutex->value && mutex->waiters)
151                 pthread_cond_signal(&mutex->cond);
152         mutex->value++;
153         pthread_mutex_unlock(&mutex->lock);
154 }
155
156 void fio_rwlock_write(struct fio_rwlock *lock)
157 {
158         assert(lock->magic == FIO_RWLOCK_MAGIC);
159         pthread_rwlock_wrlock(&lock->lock);
160 }
161
162 void fio_rwlock_read(struct fio_rwlock *lock)
163 {
164         assert(lock->magic == FIO_RWLOCK_MAGIC);
165         pthread_rwlock_rdlock(&lock->lock);
166 }
167
168 void fio_rwlock_unlock(struct fio_rwlock *lock)
169 {
170         assert(lock->magic == FIO_RWLOCK_MAGIC);
171         pthread_rwlock_unlock(&lock->lock);
172 }
173
174 void fio_rwlock_remove(struct fio_rwlock *lock)
175 {
176         assert(lock->magic == FIO_RWLOCK_MAGIC);
177         munmap((void *) lock, sizeof(*lock));
178 }
179
180 struct fio_rwlock *fio_rwlock_init(void)
181 {
182         struct fio_rwlock *lock;
183         pthread_rwlockattr_t attr;
184         int ret;
185
186         lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
187                                 PROT_READ | PROT_WRITE,
188                                 OS_MAP_ANON | MAP_SHARED, -1, 0);
189         if (lock == MAP_FAILED) {
190                 perror("mmap rwlock");
191                 lock = NULL;
192                 goto err;
193         }
194
195         lock->magic = FIO_RWLOCK_MAGIC;
196
197         ret = pthread_rwlockattr_init(&attr);
198         if (ret) {
199                 log_err("pthread_rwlockattr_init: %s\n", strerror(ret));
200                 goto err;
201         }
202 #ifdef FIO_HAVE_PSHARED_MUTEX
203         ret = pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
204         if (ret) {
205                 log_err("pthread_rwlockattr_setpshared: %s\n", strerror(ret));
206                 goto destroy_attr;
207         }
208
209         ret = pthread_rwlock_init(&lock->lock, &attr);
210 #else
211         ret = pthread_rwlock_init(&lock->lock, NULL);
212 #endif
213
214         if (ret) {
215                 log_err("pthread_rwlock_init: %s\n", strerror(ret));
216                 goto destroy_attr;
217         }
218
219         pthread_rwlockattr_destroy(&attr);
220
221         return lock;
222 destroy_attr:
223         pthread_rwlockattr_destroy(&attr);
224 err:
225         if (lock)
226                 fio_rwlock_remove(lock);
227         return NULL;
228 }