Fixup a few items spotted by a static code checker
[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         munmap((void *) mutex, sizeof(*mutex));
19 }
20
21 struct fio_mutex *fio_mutex_init(int value)
22 {
23         struct fio_mutex *mutex = NULL;
24         pthread_mutexattr_t attr;
25         pthread_condattr_t cond;
26         int ret;
27
28         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
29                                 PROT_READ | PROT_WRITE,
30                                 OS_MAP_ANON | MAP_SHARED, -1, 0);
31         if (mutex == MAP_FAILED) {
32                 perror("mmap mutex");
33                 mutex = NULL;
34                 goto err;
35         }
36
37         mutex->value = value;
38
39         ret = pthread_mutexattr_init(&attr);
40         if (ret) {
41                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
42                 goto err;
43         }
44
45         /*
46          * Not all platforms support process shared mutexes (FreeBSD)
47          */
48 #ifdef FIO_HAVE_PSHARED_MUTEX
49         ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
50         if (ret) {
51                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
52                 goto err;
53         }
54 #endif
55
56         pthread_condattr_init(&cond);
57 #ifdef FIO_HAVE_PSHARED_MUTEX
58         pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
59 #endif
60         pthread_cond_init(&mutex->cond, &cond);
61
62         ret = pthread_mutex_init(&mutex->lock, &attr);
63         if (ret) {
64                 log_err("pthread_mutex_init: %s\n", strerror(ret));
65                 goto err;
66         }
67
68         pthread_condattr_destroy(&cond);
69         pthread_mutexattr_destroy(&attr);
70
71         return mutex;
72 err:
73         if (mutex)
74                 fio_mutex_remove(mutex);
75
76         return NULL;
77 }
78
79 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
80 {
81         struct timespec t;
82         int ret = 0;
83
84         clock_gettime(CLOCK_REALTIME, &t);
85         t.tv_sec += seconds;
86
87         pthread_mutex_lock(&mutex->lock);
88
89         while (!mutex->value && !ret) {
90                 mutex->waiters++;
91                 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
92                 mutex->waiters--;
93         }
94
95         if (!ret) {
96                 mutex->value--;
97                 pthread_mutex_unlock(&mutex->lock);
98         }
99
100         return ret;
101 }
102
103 void fio_mutex_down(struct fio_mutex *mutex)
104 {
105         pthread_mutex_lock(&mutex->lock);
106
107         while (!mutex->value) {
108                 mutex->waiters++;
109                 pthread_cond_wait(&mutex->cond, &mutex->lock);
110                 mutex->waiters--;
111         }
112
113         mutex->value--;
114         pthread_mutex_unlock(&mutex->lock);
115 }
116
117 void fio_mutex_up(struct fio_mutex *mutex)
118 {
119         pthread_mutex_lock(&mutex->lock);
120         read_barrier();
121         if (!mutex->value && mutex->waiters)
122                 pthread_cond_signal(&mutex->cond);
123         mutex->value++;
124         pthread_mutex_unlock(&mutex->lock);
125 }
126
127 void fio_mutex_down_write(struct fio_mutex *mutex)
128 {
129         pthread_mutex_lock(&mutex->lock);
130
131         while (mutex->value != 0) {
132                 mutex->waiters++;
133                 pthread_cond_wait(&mutex->cond, &mutex->lock);
134                 mutex->waiters--;
135         }
136
137         mutex->value--;
138         pthread_mutex_unlock(&mutex->lock);
139 }
140
141 void fio_mutex_down_read(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_up_read(struct fio_mutex *mutex)
156 {
157         pthread_mutex_lock(&mutex->lock);
158         mutex->value--;
159         read_barrier();
160         if (mutex->value >= 0 && mutex->waiters)
161                 pthread_cond_signal(&mutex->cond);
162         pthread_mutex_unlock(&mutex->lock);
163 }
164
165 void fio_mutex_up_write(struct fio_mutex *mutex)
166 {
167         pthread_mutex_lock(&mutex->lock);
168         mutex->value++;
169         read_barrier();
170         if (mutex->value >= 0 && mutex->waiters)
171                 pthread_cond_signal(&mutex->cond);
172         pthread_mutex_unlock(&mutex->lock);
173 }