Correct basename usage for verify dump
[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 #ifdef FIO_HAVE_FALLOCATE
37         posix_fallocate(fd, 0, sizeof(struct fio_mutex));
38 #endif
39
40         if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
41                 perror("ftruncate mutex");
42                 goto err;
43         }
44
45         mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
46                                 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
47         if (mutex == MAP_FAILED) {
48                 perror("mmap mutex");
49                 close(fd);
50                 mutex = NULL;
51                 goto err;
52         }
53
54         unlink(mutex_name);
55         mutex->mutex_fd = fd;
56         mutex->value = value;
57
58         /*
59          * Not all platforms support process shared mutexes (FreeBSD)
60          */
61 #ifdef FIO_HAVE_PSHARED_MUTEX
62         mflag = PTHREAD_PROCESS_SHARED;
63 #else
64         mflag = PTHREAD_PROCESS_PRIVATE;
65 #endif
66
67         ret = pthread_mutexattr_init(&attr);
68         if (ret) {
69                 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
70                 goto err;
71         }
72 #ifdef FIO_HAVE_PSHARED_MUTEX
73         ret = pthread_mutexattr_setpshared(&attr, mflag);
74         if (ret) {
75                 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
76                 goto err;
77         }
78 #endif
79
80         pthread_condattr_init(&cond);
81 #ifdef FIO_HAVE_PSHARED_MUTEX
82         pthread_condattr_setpshared(&cond, mflag);
83 #endif
84         pthread_cond_init(&mutex->cond, &cond);
85
86         ret = pthread_mutex_init(&mutex->lock, &attr);
87         if (ret) {
88                 log_err("pthread_mutex_init: %s\n", strerror(ret));
89                 goto err;
90         }
91
92         pthread_condattr_destroy(&cond);
93         pthread_mutexattr_destroy(&attr);
94
95         return mutex;
96 err:
97         if (mutex)
98                 fio_mutex_remove(mutex);
99
100         unlink(mutex_name);
101         return NULL;
102 }
103
104 int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
105 {
106         struct timespec t;
107         int ret = 0;
108
109         clock_gettime(CLOCK_REALTIME, &t);
110         t.tv_sec += seconds;
111
112         pthread_mutex_lock(&mutex->lock);
113
114         while (!mutex->value && !ret) {
115                 mutex->waiters++;
116                 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
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         pthread_mutex_lock(&mutex->lock);
131
132         while (!mutex->value) {
133                 mutex->waiters++;
134                 pthread_cond_wait(&mutex->cond, &mutex->lock);
135                 mutex->waiters--;
136         }
137
138         mutex->value--;
139         pthread_mutex_unlock(&mutex->lock);
140 }
141
142 void fio_mutex_up(struct fio_mutex *mutex)
143 {
144         pthread_mutex_lock(&mutex->lock);
145         read_barrier();
146         if (!mutex->value && mutex->waiters)
147                 pthread_cond_signal(&mutex->cond);
148         mutex->value++;
149         pthread_mutex_unlock(&mutex->lock);
150 }
151
152 void fio_mutex_down_write(struct fio_mutex *mutex)
153 {
154         pthread_mutex_lock(&mutex->lock);
155
156         while (mutex->value != 0) {
157                 mutex->waiters++;
158                 pthread_cond_wait(&mutex->cond, &mutex->lock);
159                 mutex->waiters--;
160         }
161
162         mutex->value--;
163         pthread_mutex_unlock(&mutex->lock);
164 }
165
166 void fio_mutex_down_read(struct fio_mutex *mutex)
167 {
168         pthread_mutex_lock(&mutex->lock);
169
170         while (mutex->value < 0) {
171                 mutex->waiters++;
172                 pthread_cond_wait(&mutex->cond, &mutex->lock);
173                 mutex->waiters--;
174         }
175
176         mutex->value++;
177         pthread_mutex_unlock(&mutex->lock);
178 }
179
180 void fio_mutex_up_read(struct fio_mutex *mutex)
181 {
182         pthread_mutex_lock(&mutex->lock);
183         mutex->value--;
184         read_barrier();
185         if (mutex->value >= 0 && mutex->waiters)
186                 pthread_cond_signal(&mutex->cond);
187         pthread_mutex_unlock(&mutex->lock);
188 }
189
190 void fio_mutex_up_write(struct fio_mutex *mutex)
191 {
192         pthread_mutex_lock(&mutex->lock);
193         mutex->value++;
194         read_barrier();
195         if (mutex->value >= 0 && mutex->waiters)
196                 pthread_cond_signal(&mutex->cond);
197         pthread_mutex_unlock(&mutex->lock);
198 }