blob: e148430b618e5cae1c1fd93c1134f47910198ec6 [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 Axboecdd18ad2008-02-27 18:58:00 +010018 close(mutex->mutex_fd);
Jens Axboe5921e802008-05-30 15:02:38 +020019 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010020}
21
Jens Axboecdd18ad2008-02-27 18:58:00 +010022struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010023{
Jens Axboecdd18ad2008-02-27 18:58:00 +010024 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
25 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010026 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010027 pthread_condattr_t cond;
Jens Axboef356d012009-01-05 09:56:29 +010028 int fd, ret, mflag;
Jens Axboe07739b52007-03-08 20:25:46 +010029
Jens Axboecdd18ad2008-02-27 18:58:00 +010030 fd = mkstemp(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010031 if (fd < 0) {
Jens Axboecdd18ad2008-02-27 18:58:00 +010032 perror("open mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010033 return NULL;
34 }
35
Jens Axboecdd18ad2008-02-27 18:58:00 +010036 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
37 perror("ftruncate mutex");
Jens Axboee53bd0b2007-03-08 20:29:11 +010038 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010039 }
40
Jens Axboe5921e802008-05-30 15:02:38 +020041 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
42 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010043 if (mutex == MAP_FAILED) {
44 perror("mmap mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010045 close(fd);
Jens Axboecdd18ad2008-02-27 18:58:00 +010046 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010047 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010048 }
49
Jens Axboecdd18ad2008-02-27 18:58:00 +010050 unlink(mutex_name);
51 mutex->mutex_fd = fd;
52 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010053
Jens Axboef356d012009-01-05 09:56:29 +010054 /*
55 * Not all platforms support process shared mutexes (FreeBSD)
56 */
57#ifdef FIO_HAVE_PSHARED_MUTEX
58 mflag = PTHREAD_PROCESS_SHARED;
59#else
60 mflag = PTHREAD_PROCESS_PRIVATE;
61#endif
62
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010063 ret = pthread_mutexattr_init(&attr);
64 if (ret) {
65 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010066 goto err;
67 }
Jens Axboef356d012009-01-05 09:56:29 +010068 ret = pthread_mutexattr_setpshared(&attr, mflag);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010069 if (ret) {
70 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010071 goto err;
72 }
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010073
74 pthread_condattr_init(&cond);
Jens Axboef356d012009-01-05 09:56:29 +010075 pthread_condattr_setpshared(&cond, mflag);
Jens Axboecdd18ad2008-02-27 18:58:00 +010076 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010077
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010078 ret = pthread_mutex_init(&mutex->lock, &attr);
79 if (ret) {
80 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010081 goto err;
82 }
83
Jens Axboecdd18ad2008-02-27 18:58:00 +010084 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010085err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010086 if (mutex)
87 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010088
Jens Axboecdd18ad2008-02-27 18:58:00 +010089 unlink(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010090 return NULL;
91}
92
Jens Axboe656b1392009-07-01 23:02:10 +020093int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
94{
95 struct timespec t;
96 int ret = 0;
97
Jens Axboed481e002009-12-04 09:56:32 +010098 clock_gettime(CLOCK_REALTIME, &t);
Jens Axboe656b1392009-07-01 23:02:10 +020099 t.tv_sec += seconds;
100
101 pthread_mutex_lock(&mutex->lock);
102
103 while (!mutex->value && !ret) {
104 mutex->waiters++;
105 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
106 mutex->waiters--;
107 }
108
109 if (!ret) {
110 mutex->value--;
111 pthread_mutex_unlock(&mutex->lock);
112 }
113
114 return ret;
115}
116
Jens Axboecdd18ad2008-02-27 18:58:00 +0100117void fio_mutex_down(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
121 while (!mutex->value) {
122 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100123 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100124 mutex->waiters--;
125 }
126
Jens Axboecdd18ad2008-02-27 18:58:00 +0100127 mutex->value--;
128 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100129}
130
Jens Axboecdd18ad2008-02-27 18:58:00 +0100131void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100132{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100133 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100134 read_barrier();
135 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100136 pthread_cond_signal(&mutex->cond);
137 mutex->value++;
138 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100139}
Jens Axboe64d4d312008-03-03 10:36:27 +0100140
141void fio_mutex_down_write(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_down_read(struct fio_mutex *mutex)
156{
157 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100158
159 while (mutex->value < 0) {
160 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100161 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100162 mutex->waiters--;
163 }
164
Jens Axboe64d4d312008-03-03 10:36:27 +0100165 mutex->value++;
166 pthread_mutex_unlock(&mutex->lock);
167}
168
169void fio_mutex_up_read(struct fio_mutex *mutex)
170{
171 pthread_mutex_lock(&mutex->lock);
172 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100173 read_barrier();
174 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100175 pthread_cond_signal(&mutex->cond);
176 pthread_mutex_unlock(&mutex->lock);
177}
178
179void fio_mutex_up_write(struct fio_mutex *mutex)
180{
181 pthread_mutex_lock(&mutex->lock);
182 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100183 read_barrier();
184 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100185 pthread_cond_signal(&mutex->cond);
186 pthread_mutex_unlock(&mutex->lock);
187}