blob: a44743745ab43d18b8342a67598b4977c1e43557 [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 Axboe07739b52007-03-08 20:25:46 +010014
Jens Axboecdd18ad2008-02-27 18:58:00 +010015void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010016{
Jens Axboecdd18ad2008-02-27 18:58:00 +010017 close(mutex->mutex_fd);
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 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
24 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010025 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010026 pthread_condattr_t cond;
Jens Axboef356d012009-01-05 09:56:29 +010027 int fd, ret, mflag;
Jens Axboe07739b52007-03-08 20:25:46 +010028
Jens Axboecdd18ad2008-02-27 18:58:00 +010029 fd = mkstemp(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010030 if (fd < 0) {
Jens Axboecdd18ad2008-02-27 18:58:00 +010031 perror("open mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010032 return NULL;
33 }
34
Jens Axboecdd18ad2008-02-27 18:58:00 +010035 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
36 perror("ftruncate mutex");
Jens Axboee53bd0b2007-03-08 20:29:11 +010037 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010038 }
39
Jens Axboe5921e802008-05-30 15:02:38 +020040 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
41 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010042 if (mutex == MAP_FAILED) {
43 perror("mmap mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010044 close(fd);
Jens Axboecdd18ad2008-02-27 18:58:00 +010045 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010046 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010047 }
48
Jens Axboecdd18ad2008-02-27 18:58:00 +010049 unlink(mutex_name);
50 mutex->mutex_fd = fd;
51 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010052
Jens Axboef356d012009-01-05 09:56:29 +010053 /*
54 * Not all platforms support process shared mutexes (FreeBSD)
55 */
56#ifdef FIO_HAVE_PSHARED_MUTEX
57 mflag = PTHREAD_PROCESS_SHARED;
58#else
59 mflag = PTHREAD_PROCESS_PRIVATE;
60#endif
61
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010062 ret = pthread_mutexattr_init(&attr);
63 if (ret) {
64 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010065 goto err;
66 }
Jens Axboef356d012009-01-05 09:56:29 +010067 ret = pthread_mutexattr_setpshared(&attr, mflag);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010068 if (ret) {
69 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010070 goto err;
71 }
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010072
73 pthread_condattr_init(&cond);
Jens Axboef356d012009-01-05 09:56:29 +010074 pthread_condattr_setpshared(&cond, mflag);
Jens Axboecdd18ad2008-02-27 18:58:00 +010075 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010076
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010077 ret = pthread_mutex_init(&mutex->lock, &attr);
78 if (ret) {
79 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010080 goto err;
81 }
82
Jens Axboecdd18ad2008-02-27 18:58:00 +010083 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010084err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010085 if (mutex)
86 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010087
Jens Axboecdd18ad2008-02-27 18:58:00 +010088 unlink(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010089 return NULL;
90}
91
Jens Axboe656b1392009-07-01 23:02:10 +020092int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
93{
94 struct timespec t;
95 int ret = 0;
96
97 clock_gettime(CLOCK_REALTIME, &t);
98 t.tv_sec += seconds;
99
100 pthread_mutex_lock(&mutex->lock);
101
102 while (!mutex->value && !ret) {
103 mutex->waiters++;
104 ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
105 mutex->waiters--;
106 }
107
108 if (!ret) {
109 mutex->value--;
110 pthread_mutex_unlock(&mutex->lock);
111 }
112
113 return ret;
114}
115
Jens Axboecdd18ad2008-02-27 18:58:00 +0100116void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100117{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100118 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100119
120 while (!mutex->value) {
121 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +0100122 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100123 mutex->waiters--;
124 }
125
Jens Axboecdd18ad2008-02-27 18:58:00 +0100126 mutex->value--;
127 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100128}
129
Jens Axboecdd18ad2008-02-27 18:58:00 +0100130void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100131{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100132 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100133 read_barrier();
134 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100135 pthread_cond_signal(&mutex->cond);
136 mutex->value++;
137 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100138}
Jens Axboe64d4d312008-03-03 10:36:27 +0100139
140void fio_mutex_down_write(struct fio_mutex *mutex)
141{
142 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100143
144 while (mutex->value != 0) {
145 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100146 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100147 mutex->waiters--;
148 }
149
Jens Axboe64d4d312008-03-03 10:36:27 +0100150 mutex->value--;
151 pthread_mutex_unlock(&mutex->lock);
152}
153
154void fio_mutex_down_read(struct fio_mutex *mutex)
155{
156 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100157
158 while (mutex->value < 0) {
159 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100160 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100161 mutex->waiters--;
162 }
163
Jens Axboe64d4d312008-03-03 10:36:27 +0100164 mutex->value++;
165 pthread_mutex_unlock(&mutex->lock);
166}
167
168void fio_mutex_up_read(struct fio_mutex *mutex)
169{
170 pthread_mutex_lock(&mutex->lock);
171 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100172 read_barrier();
173 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100174 pthread_cond_signal(&mutex->cond);
175 pthread_mutex_unlock(&mutex->lock);
176}
177
178void fio_mutex_up_write(struct fio_mutex *mutex)
179{
180 pthread_mutex_lock(&mutex->lock);
181 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100182 read_barrier();
183 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100184 pthread_cond_signal(&mutex->cond);
185 pthread_mutex_unlock(&mutex->lock);
186}