Fixup handling of pthread_mutex*() error returns
[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 <pthread.h>
7 #include <sys/mman.h>
8
9 #include "log.h"
10 #include "mutex.h"
11 #include "arch/arch.h"
12
13 void fio_mutex_remove(struct fio_mutex *mutex)
14 {
15         close(mutex->mutex_fd);
16         munmap((void *) mutex, sizeof(*mutex));
17 }
18
19 struct fio_mutex *fio_mutex_init(int value)
20 {
21         char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
22         struct fio_mutex *mutex = NULL;
23         pthread_mutexattr_t attr;
24         pthread_condattr_t cond;
25         int fd, ret;
26
27         fd = mkstemp(mutex_name);
28         if (fd < 0) {
29                 perror("open mutex");
30                 return NULL;
31         }
32
33         if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
34                 perror("ftruncate mutex");
35                 goto err;
36         }
37
38         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
39                                 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
40         if (mutex == MAP_FAILED) {
41                 perror("mmap mutex");
42                 close(fd);
43                 mutex = NULL;
44                 goto err;
45         }
46
47         unlink(mutex_name);
48         mutex->mutex_fd = fd;
49         mutex->value = value;
50
51         ret = pthread_mutexattr_init(&attr);
52         if (ret) {
53                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
54                 goto err;
55         }
56         ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
57         if (ret) {
58                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
59                 goto err;
60         }
61
62         pthread_condattr_init(&cond);
63         pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
64         pthread_cond_init(&mutex->cond, &cond);
65
66         ret = pthread_mutex_init(&mutex->lock, &attr);
67         if (ret) {
68                 log_err("pthread_mutex_init: %s\n", strerror(ret));
69                 goto err;
70         }
71
72         return mutex;
73 err:
74         if (mutex)
75                 fio_mutex_remove(mutex);
76
77         unlink(mutex_name);
78         return NULL;
79 }
80
81 void fio_mutex_down(struct fio_mutex *mutex)
82 {
83         pthread_mutex_lock(&mutex->lock);
84
85         while (!mutex->value) {
86                 mutex->waiters++;
87                 pthread_cond_wait(&mutex->cond, &mutex->lock);
88                 mutex->waiters--;
89         }
90
91         mutex->value--;
92         pthread_mutex_unlock(&mutex->lock);
93 }
94
95 void fio_mutex_up(struct fio_mutex *mutex)
96 {
97         pthread_mutex_lock(&mutex->lock);
98         read_barrier();
99         if (!mutex->value && mutex->waiters)
100                 pthread_cond_signal(&mutex->cond);
101         mutex->value++;
102         pthread_mutex_unlock(&mutex->lock);
103 }
104
105 void fio_mutex_down_write(struct fio_mutex *mutex)
106 {
107         pthread_mutex_lock(&mutex->lock);
108
109         while (mutex->value != 0) {
110                 mutex->waiters++;
111                 pthread_cond_wait(&mutex->cond, &mutex->lock);
112                 mutex->waiters--;
113         }
114
115         mutex->value--;
116         pthread_mutex_unlock(&mutex->lock);
117 }
118
119 void fio_mutex_down_read(struct fio_mutex *mutex)
120 {
121         pthread_mutex_lock(&mutex->lock);
122
123         while (mutex->value < 0) {
124                 mutex->waiters++;
125                 pthread_cond_wait(&mutex->cond, &mutex->lock);
126                 mutex->waiters--;
127         }
128
129         mutex->value++;
130         pthread_mutex_unlock(&mutex->lock);
131 }
132
133 void fio_mutex_up_read(struct fio_mutex *mutex)
134 {
135         pthread_mutex_lock(&mutex->lock);
136         mutex->value--;
137         read_barrier();
138         if (mutex->value >= 0 && mutex->waiters)
139                 pthread_cond_signal(&mutex->cond);
140         pthread_mutex_unlock(&mutex->lock);
141 }
142
143 void fio_mutex_up_write(struct fio_mutex *mutex)
144 {
145         pthread_mutex_lock(&mutex->lock);
146         mutex->value++;
147         read_barrier();
148         if (mutex->value >= 0 && mutex->waiters)
149                 pthread_cond_signal(&mutex->cond);
150         pthread_mutex_unlock(&mutex->lock);
151 }