blob: bbbcccbd0587a7139b247ac4eb38350337fcd912 [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 Axboe07739b52007-03-08 20:25:46 +01007#include <pthread.h>
8#include <sys/mman.h>
9
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010010#include "log.h"
Jens Axboe07739b52007-03-08 20:25:46 +010011#include "mutex.h"
Jens Axboe4d4e80f2008-03-04 10:18:56 +010012#include "arch/arch.h"
Jens Axboe3c2d93e2009-01-05 19:04:15 +010013#include "os/os.h"
Jens Axboe3b2e1462009-12-15 08:58:10 +010014#include "helpers.h"
Jens Axboe07739b52007-03-08 20:25:46 +010015
Jens Axboecdd18ad2008-02-27 18:58:00 +010016void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010017{
Jens Axboe5921e802008-05-30 15:02:38 +020018 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010019}
20
Jens Axboecdd18ad2008-02-27 18:58:00 +010021struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010022{
Jens Axboecdd18ad2008-02-27 18:58:00 +010023 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010024 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010025 pthread_condattr_t cond;
Jens Axboeb6f6abc2011-05-02 10:10:56 -060026 int ret, mflag;
Jens Axboe07739b52007-03-08 20:25:46 +010027
Jens Axboe5921e802008-05-30 15:02:38 +020028 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
Jens Axboeb6f6abc2011-05-02 10:10:56 -060029 PROT_READ | PROT_WRITE,
30 OS_MAP_ANON | MAP_SHARED, -1, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010031 if (mutex == MAP_FAILED) {
32 perror("mmap mutex");
Jens Axboecdd18ad2008-02-27 18:58:00 +010033 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010034 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010035 }
36
Jens Axboecdd18ad2008-02-27 18:58:00 +010037 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010038
Jens Axboef356d012009-01-05 09:56:29 +010039 /*
40 * Not all platforms support process shared mutexes (FreeBSD)
41 */
42#ifdef FIO_HAVE_PSHARED_MUTEX
43 mflag = PTHREAD_PROCESS_SHARED;
44#else
45 mflag = PTHREAD_PROCESS_PRIVATE;
46#endif
47
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010048 ret = pthread_mutexattr_init(&attr);
49 if (ret) {
50 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010051 goto err;
52 }
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020053#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboef356d012009-01-05 09:56:29 +010054 ret = pthread_mutexattr_setpshared(&attr, mflag);
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 Axboef356d012009-01-05 09:56:29 +010063 pthread_condattr_setpshared(&cond, mflag);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020064#endif
Jens Axboecdd18ad2008-02-27 18:58:00 +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 Axboe656b1392009-07-01 23:02:10 +020084int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
85{
86 struct timespec t;
87 int ret = 0;
88
Jens Axboed481e002009-12-04 09:56:32 +010089 clock_gettime(CLOCK_REALTIME, &t);
Jens Axboe656b1392009-07-01 23:02:10 +020090 t.tv_sec += seconds;
91
92 pthread_mutex_lock(&mutex->lock);
93
94 while (!mutex->value && !ret) {
95 mutex->waiters++;
96 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
97 mutex->waiters--;
98 }
99
100 if (!ret) {
101 mutex->value--;
102 pthread_mutex_unlock(&mutex->lock);
103 }
104
105 return ret;
106}
107
Jens Axboecdd18ad2008-02-27 18:58:00 +0100108void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100109{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100110 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100111
112 while (!mutex->value) {
113 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100114 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100115 mutex->waiters--;
116 }
117
Jens Axboecdd18ad2008-02-27 18:58:00 +0100118 mutex->value--;
119 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100120}
121
Jens Axboecdd18ad2008-02-27 18:58:00 +0100122void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100123{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100124 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100125 read_barrier();
126 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100127 pthread_cond_signal(&mutex->cond);
128 mutex->value++;
129 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100130}
Jens Axboe64d4d312008-03-03 10:36:27 +0100131
132void fio_mutex_down_write(struct fio_mutex *mutex)
133{
134 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100135
136 while (mutex->value != 0) {
137 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100138 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100139 mutex->waiters--;
140 }
141
Jens Axboe64d4d312008-03-03 10:36:27 +0100142 mutex->value--;
143 pthread_mutex_unlock(&mutex->lock);
144}
145
146void fio_mutex_down_read(struct fio_mutex *mutex)
147{
148 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100149
150 while (mutex->value < 0) {
151 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100152 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100153 mutex->waiters--;
154 }
155
Jens Axboe64d4d312008-03-03 10:36:27 +0100156 mutex->value++;
157 pthread_mutex_unlock(&mutex->lock);
158}
159
160void fio_mutex_up_read(struct fio_mutex *mutex)
161{
162 pthread_mutex_lock(&mutex->lock);
163 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100164 read_barrier();
165 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100166 pthread_cond_signal(&mutex->cond);
167 pthread_mutex_unlock(&mutex->lock);
168}
169
170void fio_mutex_up_write(struct fio_mutex *mutex)
171{
172 pthread_mutex_lock(&mutex->lock);
173 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100174 read_barrier();
175 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100176 pthread_cond_signal(&mutex->cond);
177 pthread_mutex_unlock(&mutex->lock);
178}