Add read-write mutexes
[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
9#include "mutex.h"
10
cdd18ad8 11void fio_mutex_remove(struct fio_mutex *mutex)
07739b57 12{
cdd18ad8
JA
13 close(mutex->mutex_fd);
14 munmap(mutex, sizeof(*mutex));
07739b57
JA
15}
16
cdd18ad8 17struct fio_mutex *fio_mutex_init(int value)
07739b57 18{
cdd18ad8
JA
19 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
20 struct fio_mutex *mutex = NULL;
07739b57 21 pthread_mutexattr_t attr;
108fcc11 22 pthread_condattr_t cond;
07739b57
JA
23 int fd;
24
cdd18ad8 25 fd = mkstemp(mutex_name);
07739b57 26 if (fd < 0) {
cdd18ad8 27 perror("open mutex");
07739b57
JA
28 return NULL;
29 }
30
cdd18ad8
JA
31 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
32 perror("ftruncate mutex");
e53bd0b3 33 goto err;
07739b57
JA
34 }
35
cdd18ad8 36 mutex = mmap(NULL, sizeof(struct fio_mutex), PROT_READ | PROT_WRITE,
07739b57 37 MAP_SHARED, fd, 0);
cdd18ad8
JA
38 if (mutex == MAP_FAILED) {
39 perror("mmap mutex");
07739b57 40 close(fd);
cdd18ad8 41 mutex = NULL;
e53bd0b3 42 goto err;
07739b57
JA
43 }
44
cdd18ad8
JA
45 unlink(mutex_name);
46 mutex->mutex_fd = fd;
47 mutex->value = value;
07739b57
JA
48
49 if (pthread_mutexattr_init(&attr)) {
50 perror("pthread_mutexattr_init");
51 goto err;
52 }
53 if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
54 perror("pthread_mutexattr_setpshared");
55 goto err;
56 }
108fcc11
ZY
57
58 pthread_condattr_init(&cond);
59 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
cdd18ad8 60 pthread_cond_init(&mutex->cond, &cond);
108fcc11 61
cdd18ad8 62 if (pthread_mutex_init(&mutex->lock, &attr)) {
07739b57
JA
63 perror("pthread_mutex_init");
64 goto err;
65 }
66
cdd18ad8 67 return mutex;
07739b57 68err:
cdd18ad8
JA
69 if (mutex)
70 fio_mutex_remove(mutex);
f7c9e00e 71
cdd18ad8 72 unlink(mutex_name);
07739b57
JA
73 return NULL;
74}
75
cdd18ad8 76void fio_mutex_down(struct fio_mutex *mutex)
07739b57 77{
cdd18ad8
JA
78 pthread_mutex_lock(&mutex->lock);
79 while (mutex->value == 0)
80 pthread_cond_wait(&mutex->cond, &mutex->lock);
81 mutex->value--;
82 pthread_mutex_unlock(&mutex->lock);
07739b57
JA
83}
84
cdd18ad8 85void fio_mutex_up(struct fio_mutex *mutex)
07739b57 86{
cdd18ad8
JA
87 pthread_mutex_lock(&mutex->lock);
88 if (!mutex->value)
89 pthread_cond_signal(&mutex->cond);
90 mutex->value++;
91 pthread_mutex_unlock(&mutex->lock);
07739b57 92}
64d4d313
JA
93
94void fio_mutex_down_write(struct fio_mutex *mutex)
95{
96 pthread_mutex_lock(&mutex->lock);
97 while (mutex->value != 0)
98 pthread_cond_wait(&mutex->cond, &mutex->lock);
99 mutex->value--;
100 pthread_mutex_unlock(&mutex->lock);
101}
102
103void fio_mutex_down_read(struct fio_mutex *mutex)
104{
105 pthread_mutex_lock(&mutex->lock);
106 while (mutex->value < 0)
107 pthread_cond_wait(&mutex->cond, &mutex->lock);
108 mutex->value++;
109 pthread_mutex_unlock(&mutex->lock);
110}
111
112void fio_mutex_up_read(struct fio_mutex *mutex)
113{
114 pthread_mutex_lock(&mutex->lock);
115 mutex->value--;
116 if (mutex->value >= 0)
117 pthread_cond_signal(&mutex->cond);
118 pthread_mutex_unlock(&mutex->lock);
119}
120
121void fio_mutex_up_write(struct fio_mutex *mutex)
122{
123 pthread_mutex_lock(&mutex->lock);
124 mutex->value++;
125 if (mutex->value >= 0)
126 pthread_cond_signal(&mutex->cond);
127 pthread_mutex_unlock(&mutex->lock);
128}