blob: 3a01f98fca5273bd68cb62d7b85b3d3f4e5d84f4 [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 Axboee721c572012-02-09 20:55:29 +010026 int ret;
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 Axboe4fa6d0f2009-01-05 09:45:13 +010039 ret = pthread_mutexattr_init(&attr);
40 if (ret) {
41 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010042 goto err;
43 }
Jens Axboee721c572012-02-09 20:55:29 +010044
45 /*
46 * Not all platforms support process shared mutexes (FreeBSD)
47 */
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020048#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010049 ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010050 if (ret) {
51 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010052 goto err;
53 }
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020054#endif
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010055
56 pthread_condattr_init(&cond);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020057#ifdef FIO_HAVE_PSHARED_MUTEX
Jens Axboee721c572012-02-09 20:55:29 +010058 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020059#endif
Jens Axboecdd18ad2008-02-27 18:58:00 +010060 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010061
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010062 ret = pthread_mutex_init(&mutex->lock, &attr);
63 if (ret) {
64 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010065 goto err;
66 }
67
Bruce Cran03e20d62011-01-02 20:14:54 +010068 pthread_condattr_destroy(&cond);
69 pthread_mutexattr_destroy(&attr);
70
Jens Axboecdd18ad2008-02-27 18:58:00 +010071 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010072err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010073 if (mutex)
74 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010075
Jens Axboe07739b52007-03-08 20:25:46 +010076 return NULL;
77}
78
Jens Axboe656b1392009-07-01 23:02:10 +020079int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
80{
81 struct timespec t;
82 int ret = 0;
83
Jens Axboed481e002009-12-04 09:56:32 +010084 clock_gettime(CLOCK_REALTIME, &t);
Jens Axboe656b1392009-07-01 23:02:10 +020085 t.tv_sec += seconds;
86
87 pthread_mutex_lock(&mutex->lock);
88
89 while (!mutex->value && !ret) {
90 mutex->waiters++;
91 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
92 mutex->waiters--;
93 }
94
95 if (!ret) {
96 mutex->value--;
97 pthread_mutex_unlock(&mutex->lock);
98 }
99
100 return ret;
101}
102
Jens Axboecdd18ad2008-02-27 18:58:00 +0100103void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100104{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100105 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100106
107 while (!mutex->value) {
108 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100109 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100110 mutex->waiters--;
111 }
112
Jens Axboecdd18ad2008-02-27 18:58:00 +0100113 mutex->value--;
114 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100115}
116
Jens Axboecdd18ad2008-02-27 18:58:00 +0100117void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100118{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100119 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100120 read_barrier();
121 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100122 pthread_cond_signal(&mutex->cond);
123 mutex->value++;
124 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100125}
Jens Axboe64d4d312008-03-03 10:36:27 +0100126
127void fio_mutex_down_write(struct fio_mutex *mutex)
128{
129 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100130
131 while (mutex->value != 0) {
132 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100133 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100134 mutex->waiters--;
135 }
136
Jens Axboe64d4d312008-03-03 10:36:27 +0100137 mutex->value--;
138 pthread_mutex_unlock(&mutex->lock);
139}
140
141void fio_mutex_down_read(struct fio_mutex *mutex)
142{
143 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100144
145 while (mutex->value < 0) {
146 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100147 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100148 mutex->waiters--;
149 }
150
Jens Axboe64d4d312008-03-03 10:36:27 +0100151 mutex->value++;
152 pthread_mutex_unlock(&mutex->lock);
153}
154
155void fio_mutex_up_read(struct fio_mutex *mutex)
156{
157 pthread_mutex_lock(&mutex->lock);
158 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100159 read_barrier();
160 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100161 pthread_cond_signal(&mutex->cond);
162 pthread_mutex_unlock(&mutex->lock);
163}
164
165void fio_mutex_up_write(struct fio_mutex *mutex)
166{
167 pthread_mutex_lock(&mutex->lock);
168 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100169 read_barrier();
170 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100171 pthread_cond_signal(&mutex->cond);
172 pthread_mutex_unlock(&mutex->lock);
173}