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