Missed int to pointer conversion check for ->ratemin
[fio.git] / mutex.c
CommitLineData
07739b57
JA
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
4fa6d0f8 9#include "log.h"
07739b57 10#include "mutex.h"
4d4e80f2 11#include "arch/arch.h"
3c2d93ed 12#include "os/os.h"
07739b57 13
cdd18ad8 14void fio_mutex_remove(struct fio_mutex *mutex)
07739b57 15{
cdd18ad8 16 close(mutex->mutex_fd);
5921e80c 17 munmap((void *) mutex, sizeof(*mutex));
07739b57
JA
18}
19
cdd18ad8 20struct fio_mutex *fio_mutex_init(int value)
07739b57 21{
cdd18ad8
JA
22 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
23 struct fio_mutex *mutex = NULL;
07739b57 24 pthread_mutexattr_t attr;
108fcc11 25 pthread_condattr_t cond;
f356d01d 26 int fd, ret, mflag;
07739b57 27
cdd18ad8 28 fd = mkstemp(mutex_name);
07739b57 29 if (fd < 0) {
cdd18ad8 30 perror("open mutex");
07739b57
JA
31 return NULL;
32 }
33
cdd18ad8
JA
34 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
35 perror("ftruncate mutex");
e53bd0b3 36 goto err;
07739b57
JA
37 }
38
5921e80c
JA
39 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
40 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
cdd18ad8
JA
41 if (mutex == MAP_FAILED) {
42 perror("mmap mutex");
07739b57 43 close(fd);
cdd18ad8 44 mutex = NULL;
e53bd0b3 45 goto err;
07739b57
JA
46 }
47
cdd18ad8
JA
48 unlink(mutex_name);
49 mutex->mutex_fd = fd;
50 mutex->value = value;
07739b57 51
f356d01d
JA
52 /*
53 * Not all platforms support process shared mutexes (FreeBSD)
54 */
55#ifdef FIO_HAVE_PSHARED_MUTEX
56 mflag = PTHREAD_PROCESS_SHARED;
57#else
58 mflag = PTHREAD_PROCESS_PRIVATE;
59#endif
60
4fa6d0f8
JA
61 ret = pthread_mutexattr_init(&attr);
62 if (ret) {
63 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
07739b57
JA
64 goto err;
65 }
f356d01d 66 ret = pthread_mutexattr_setpshared(&attr, mflag);
4fa6d0f8
JA
67 if (ret) {
68 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
07739b57
JA
69 goto err;
70 }
108fcc11
ZY
71
72 pthread_condattr_init(&cond);
f356d01d 73 pthread_condattr_setpshared(&cond, mflag);
cdd18ad8 74 pthread_cond_init(&mutex->cond, &cond);
108fcc11 75
4fa6d0f8
JA
76 ret = pthread_mutex_init(&mutex->lock, &attr);
77 if (ret) {
78 log_err("pthread_mutex_init: %s\n", strerror(ret));
07739b57
JA
79 goto err;
80 }
81
cdd18ad8 82 return mutex;
07739b57 83err:
cdd18ad8
JA
84 if (mutex)
85 fio_mutex_remove(mutex);
f7c9e00e 86
cdd18ad8 87 unlink(mutex_name);
07739b57
JA
88 return NULL;
89}
90
cdd18ad8 91void fio_mutex_down(struct fio_mutex *mutex)
07739b57 92{
cdd18ad8 93 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
94
95 while (!mutex->value) {
96 mutex->waiters++;
cdd18ad8 97 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
98 mutex->waiters--;
99 }
100
cdd18ad8
JA
101 mutex->value--;
102 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
103}
104
cdd18ad8 105void fio_mutex_up(struct fio_mutex *mutex)
07739b57 106{
cdd18ad8 107 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
108 read_barrier();
109 if (!mutex->value && mutex->waiters)
cdd18ad8
JA
110 pthread_cond_signal(&mutex->cond);
111 mutex->value++;
112 pthread_mutex_unlock(&mutex->lock);
07739b57 113}
64d4d313
JA
114
115void fio_mutex_down_write(struct fio_mutex *mutex)
116{
117 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
118
119 while (mutex->value != 0) {
120 mutex->waiters++;
64d4d313 121 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
122 mutex->waiters--;
123 }
124
64d4d313
JA
125 mutex->value--;
126 pthread_mutex_unlock(&mutex->lock);
127}
128
129void fio_mutex_down_read(struct fio_mutex *mutex)
130{
131 pthread_mutex_lock(&mutex->lock);
4d4e80f2
JA
132
133 while (mutex->value < 0) {
134 mutex->waiters++;
64d4d313 135 pthread_cond_wait(&mutex->cond, &mutex->lock);
4d4e80f2
JA
136 mutex->waiters--;
137 }
138
64d4d313
JA
139 mutex->value++;
140 pthread_mutex_unlock(&mutex->lock);
141}
142
143void fio_mutex_up_read(struct fio_mutex *mutex)
144{
145 pthread_mutex_lock(&mutex->lock);
146 mutex->value--;
4d4e80f2
JA
147 read_barrier();
148 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
149 pthread_cond_signal(&mutex->cond);
150 pthread_mutex_unlock(&mutex->lock);
151}
152
153void fio_mutex_up_write(struct fio_mutex *mutex)
154{
155 pthread_mutex_lock(&mutex->lock);
156 mutex->value++;
4d4e80f2
JA
157 read_barrier();
158 if (mutex->value >= 0 && mutex->waiters)
64d4d313
JA
159 pthread_cond_signal(&mutex->cond);
160 pthread_mutex_unlock(&mutex->lock);
161}