blob: 3b94beffc7342ae2f13879126e2da3433cb5bbf1 [file] [log] [blame]
Jens Axboe07739b52007-03-08 20:25:46 +01001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <stdlib.h>
5#include <fcntl.h>
Jens Axboe656b1392009-07-01 23:02:10 +02006#include <time.h>
Jens Axboeef635052012-02-15 22:24:19 +01007#include <errno.h>
Jens Axboe07739b52007-03-08 20:25:46 +01008#include <pthread.h>
9#include <sys/mman.h>
10
Jens Axboeb4c1fb32012-02-16 22:22:46 +010011#include "fio.h"
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010012#include "log.h"
Jens Axboe07739b52007-03-08 20:25:46 +010013#include "mutex.h"
Jens Axboe4d4e80f2008-03-04 10:18:56 +010014#include "arch/arch.h"
Jens Axboe3c2d93e2009-01-05 19:04:15 +010015#include "os/os.h"
Jens Axboe3b2e1462009-12-15 08:58:10 +010016#include "helpers.h"
Jens Axboeef635052012-02-15 22:24:19 +010017#include "time.h"
18#include "gettime.h"
Jens Axboe07739b52007-03-08 20:25:46 +010019
Jens Axboecdd18ad2008-02-27 18:58:00 +010020void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010021{
Jens Axboe58a157d2012-02-15 22:20:26 +010022 pthread_cond_destroy(&mutex->cond);
Jens Axboe5921e802008-05-30 15:02:38 +020023 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010024}
25
Jens Axboecdd18ad2008-02-27 18:58:00 +010026struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010027{
Jens Axboecdd18ad2008-02-27 18:58:00 +010028 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010029 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010030 pthread_condattr_t cond;
Jens Axboee721c572012-02-09 20:55:29 +010031 int ret;
Jens Axboe07739b52007-03-08 20:25:46 +010032
Jens Axboe5921e802008-05-30 15:02:38 +020033 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
Jens Axboeb6f6abc2011-05-02 10:10:56 -060034 PROT_READ | PROT_WRITE,
35 OS_MAP_ANON | MAP_SHARED, -1, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010036 if (mutex == MAP_FAILED) {
37 perror("mmap mutex");
Jens Axboecdd18ad2008-02-27 18:58:00 +010038 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010039 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010040 }
41
Jens Axboecdd18ad2008-02-27 18:58:00 +010042 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010043
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010044 ret = pthread_mutexattr_init(&attr);
45 if (ret) {
46 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010047 goto err;
48 }
Jens Axboee721c572012-02-09 20:55:29 +010049
50 /*
51 * Not all platforms support process shared mutexes (FreeBSD)
52 */
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020053#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010054 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010055 if (ret) {
56 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010057 goto err;
58 }
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020059#endif
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010060
61 pthread_condattr_init(&cond);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020062#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010063 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020064#endif
Jens Axboe58a157d2012-02-15 22:20:26 +010065 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010066
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010067 ret = pthread_mutex_init(&mutex->lock, &attr);
68 if (ret) {
69 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010070 goto err;
71 }
72
Bruce Cran03e20d62011-01-02 20:14:54 +010073 pthread_condattr_destroy(&cond);
74 pthread_mutexattr_destroy(&attr);
75
Jens Axboecdd18ad2008-02-27 18:58:00 +010076 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010077err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010078 if (mutex)
79 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010080
Jens Axboe07739b52007-03-08 20:25:46 +010081 return NULL;
82}
83
Jens Axboeef635052012-02-15 22:24:19 +010084static int mutex_timed_out(struct timeval *t, unsigned int seconds)
85{
86 return mtime_since_now(t) >= seconds * 1000;
87}
88
Jens Axboe656b1392009-07-01 23:02:10 +020089int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
90{
Jens Axboeef635052012-02-15 22:24:19 +010091 struct timeval tv_s;
Jens Axboe656b1392009-07-01 23:02:10 +020092 struct timespec t;
93 int ret = 0;
94
Jens Axboeb11e4cc2012-02-20 09:18:43 +010095 gettimeofday(&tv_s, NULL);
96 t.tv_sec = tv_s.tv_sec + seconds;
97 t.tv_nsec = tv_s.tv_usec * 1000;
Jens Axboe656b1392009-07-01 23:02:10 +020098
99 pthread_mutex_lock(&mutex->lock);
100
101 while (!mutex->value && !ret) {
102 mutex->waiters++;
Jens Axboeef635052012-02-15 22:24:19 +0100103
104 /*
105 * Some platforms (FreeBSD 9?) seems to return timed out
106 * way too early, double check.
107 */
Jens Axboe656b1392009-07-01 23:02:10 +0200108 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
Jens Axboeef635052012-02-15 22:24:19 +0100109 if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
110 pthread_mutex_lock(&mutex->lock);
111 ret = 0;
112 }
113
Jens Axboe656b1392009-07-01 23:02:10 +0200114 mutex->waiters--;
115 }
116
117 if (!ret) {
118 mutex->value--;
119 pthread_mutex_unlock(&mutex->lock);
120 }
121
122 return ret;
123}
124
Jens Axboecdd18ad2008-02-27 18:58:00 +0100125void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100126{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100127 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100128
129 while (!mutex->value) {
130 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100131 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100132 mutex->waiters--;
133 }
134
Jens Axboecdd18ad2008-02-27 18:58:00 +0100135 mutex->value--;
136 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100137}
138
Jens Axboecdd18ad2008-02-27 18:58:00 +0100139void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100140{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100141 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100142 read_barrier();
143 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100144 pthread_cond_signal(&mutex->cond);
145 mutex->value++;
146 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100147}
Jens Axboe64d4d312008-03-03 10:36:27 +0100148
149void fio_mutex_down_write(struct fio_mutex *mutex)
150{
151 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100152
153 while (mutex->value != 0) {
154 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100155 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100156 mutex->waiters--;
157 }
158
Jens Axboe64d4d312008-03-03 10:36:27 +0100159 mutex->value--;
160 pthread_mutex_unlock(&mutex->lock);
161}
162
163void fio_mutex_down_read(struct fio_mutex *mutex)
164{
165 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100166
167 while (mutex->value < 0) {
168 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100169 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100170 mutex->waiters--;
171 }
172
Jens Axboe64d4d312008-03-03 10:36:27 +0100173 mutex->value++;
174 pthread_mutex_unlock(&mutex->lock);
175}
176
177void fio_mutex_up_read(struct fio_mutex *mutex)
178{
179 pthread_mutex_lock(&mutex->lock);
180 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100181 read_barrier();
182 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100183 pthread_cond_signal(&mutex->cond);
184 pthread_mutex_unlock(&mutex->lock);
185}
186
187void fio_mutex_up_write(struct fio_mutex *mutex)
188{
189 pthread_mutex_lock(&mutex->lock);
190 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100191 read_barrier();
192 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100193 pthread_cond_signal(&mutex->cond);
194 pthread_mutex_unlock(&mutex->lock);
195}