blob: 1538f62c4b1a286ebe015c299031ffede126e2c4 [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>
6#include <pthread.h>
7#include <sys/mman.h>
8
Jens Axboe4fa6d0f2009-01-05 09:45:13 +01009#include "log.h"
Jens Axboe07739b52007-03-08 20:25:46 +010010#include "mutex.h"
Jens Axboe4d4e80f2008-03-04 10:18:56 +010011#include "arch/arch.h"
Jens Axboe3c2d93e2009-01-05 19:04:15 +010012#include "os/os.h"
Jens Axboe07739b52007-03-08 20:25:46 +010013
Jens Axboecdd18ad2008-02-27 18:58:00 +010014void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010015{
Jens Axboecdd18ad2008-02-27 18:58:00 +010016 close(mutex->mutex_fd);
Jens Axboe5921e802008-05-30 15:02:38 +020017 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010018}
19
Jens Axboecdd18ad2008-02-27 18:58:00 +010020struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010021{
Jens Axboecdd18ad2008-02-27 18:58:00 +010022 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
23 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 Axboef356d012009-01-05 09:56:29 +010026 int fd, ret, mflag;
Jens Axboe07739b52007-03-08 20:25:46 +010027
Jens Axboecdd18ad2008-02-27 18:58:00 +010028 fd = mkstemp(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010029 if (fd < 0) {
Jens Axboecdd18ad2008-02-27 18:58:00 +010030 perror("open mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010031 return NULL;
32 }
33
Jens Axboecdd18ad2008-02-27 18:58:00 +010034 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
35 perror("ftruncate mutex");
Jens Axboee53bd0b2007-03-08 20:29:11 +010036 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010037 }
38
Jens Axboe5921e802008-05-30 15:02:38 +020039 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
40 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010041 if (mutex == MAP_FAILED) {
42 perror("mmap mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010043 close(fd);
Jens Axboecdd18ad2008-02-27 18:58:00 +010044 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010045 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010046 }
47
Jens Axboecdd18ad2008-02-27 18:58:00 +010048 unlink(mutex_name);
49 mutex->mutex_fd = fd;
50 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010051
Jens Axboef356d012009-01-05 09:56:29 +010052 /*
53 * Not all platforms support process shared mutexes (FreeBSD)
54 */
55#ifdef FIO_HAVE_PSHARED_MUTEX
56 mflag = PTHREAD_PROCESS_SHARED;
57#else
58 mflag = PTHREAD_PROCESS_PRIVATE;
59#endif
60
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010061 ret = pthread_mutexattr_init(&attr);
62 if (ret) {
63 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010064 goto err;
65 }
Jens Axboef356d012009-01-05 09:56:29 +010066 ret = pthread_mutexattr_setpshared(&attr, mflag);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010067 if (ret) {
68 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010069 goto err;
70 }
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010071
72 pthread_condattr_init(&cond);
Jens Axboef356d012009-01-05 09:56:29 +010073 pthread_condattr_setpshared(&cond, mflag);
Jens Axboecdd18ad2008-02-27 18:58:00 +010074 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010075
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010076 ret = pthread_mutex_init(&mutex->lock, &attr);
77 if (ret) {
78 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010079 goto err;
80 }
81
Jens Axboecdd18ad2008-02-27 18:58:00 +010082 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010083err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010084 if (mutex)
85 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010086
Jens Axboecdd18ad2008-02-27 18:58:00 +010087 unlink(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010088 return NULL;
89}
90
Jens Axboecdd18ad2008-02-27 18:58:00 +010091void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010092{
Jens Axboecdd18ad2008-02-27 18:58:00 +010093 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +010094
95 while (!mutex->value) {
96 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +010097 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +010098 mutex->waiters--;
99 }
100
Jens Axboecdd18ad2008-02-27 18:58:00 +0100101 mutex->value--;
102 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100103}
104
Jens Axboecdd18ad2008-02-27 18:58:00 +0100105void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100106{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100107 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100108 read_barrier();
109 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100110 pthread_cond_signal(&mutex->cond);
111 mutex->value++;
112 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100113}
Jens Axboe64d4d312008-03-03 10:36:27 +0100114
115void fio_mutex_down_write(struct fio_mutex *mutex)
116{
117 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100118
119 while (mutex->value != 0) {
120 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100121 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100122 mutex->waiters--;
123 }
124
Jens Axboe64d4d312008-03-03 10:36:27 +0100125 mutex->value--;
126 pthread_mutex_unlock(&mutex->lock);
127}
128
129void fio_mutex_down_read(struct fio_mutex *mutex)
130{
131 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100132
133 while (mutex->value < 0) {
134 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100135 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100136 mutex->waiters--;
137 }
138
Jens Axboe64d4d312008-03-03 10:36:27 +0100139 mutex->value++;
140 pthread_mutex_unlock(&mutex->lock);
141}
142
143void fio_mutex_up_read(struct fio_mutex *mutex)
144{
145 pthread_mutex_lock(&mutex->lock);
146 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100147 read_barrier();
148 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100149 pthread_cond_signal(&mutex->cond);
150 pthread_mutex_unlock(&mutex->lock);
151}
152
153void fio_mutex_up_write(struct fio_mutex *mutex)
154{
155 pthread_mutex_lock(&mutex->lock);
156 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100157 read_barrier();
158 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100159 pthread_cond_signal(&mutex->cond);
160 pthread_mutex_unlock(&mutex->lock);
161}