blob: 5beaa08f0b921868d36598a6030c23867495cf42 [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 Axboe07739b52007-03-08 20:25:46 +010012
Jens Axboecdd18ad2008-02-27 18:58:00 +010013void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010014{
Jens Axboecdd18ad2008-02-27 18:58:00 +010015 close(mutex->mutex_fd);
Jens Axboe5921e802008-05-30 15:02:38 +020016 munmap((void *) mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010017}
18
Jens Axboecdd18ad2008-02-27 18:58:00 +010019struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010020{
Jens Axboecdd18ad2008-02-27 18:58:00 +010021 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
22 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010023 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010024 pthread_condattr_t cond;
Jens Axboef356d012009-01-05 09:56:29 +010025 int fd, ret, mflag;
Jens Axboe07739b52007-03-08 20:25:46 +010026
Jens Axboecdd18ad2008-02-27 18:58:00 +010027 fd = mkstemp(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010028 if (fd < 0) {
Jens Axboecdd18ad2008-02-27 18:58:00 +010029 perror("open mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010030 return NULL;
31 }
32
Jens Axboecdd18ad2008-02-27 18:58:00 +010033 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
34 perror("ftruncate mutex");
Jens Axboee53bd0b2007-03-08 20:29:11 +010035 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010036 }
37
Jens Axboe5921e802008-05-30 15:02:38 +020038 mutex = (void *) mmap(NULL, sizeof(struct fio_mutex),
39 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010040 if (mutex == MAP_FAILED) {
41 perror("mmap mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010042 close(fd);
Jens Axboecdd18ad2008-02-27 18:58:00 +010043 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010044 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010045 }
46
Jens Axboecdd18ad2008-02-27 18:58:00 +010047 unlink(mutex_name);
48 mutex->mutex_fd = fd;
49 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010050
Jens Axboef356d012009-01-05 09:56:29 +010051 /*
52 * Not all platforms support process shared mutexes (FreeBSD)
53 */
54#ifdef FIO_HAVE_PSHARED_MUTEX
55 mflag = PTHREAD_PROCESS_SHARED;
56#else
57 mflag = PTHREAD_PROCESS_PRIVATE;
58#endif
59
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010060 ret = pthread_mutexattr_init(&attr);
61 if (ret) {
62 log_err("pthread_mutexattr_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010063 goto err;
64 }
Jens Axboef356d012009-01-05 09:56:29 +010065 ret = pthread_mutexattr_setpshared(&attr, mflag);
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010066 if (ret) {
67 log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010068 goto err;
69 }
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010070
71 pthread_condattr_init(&cond);
Jens Axboef356d012009-01-05 09:56:29 +010072 pthread_condattr_setpshared(&cond, mflag);
Jens Axboecdd18ad2008-02-27 18:58:00 +010073 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010074
Jens Axboe4fa6d0f2009-01-05 09:45:13 +010075 ret = pthread_mutex_init(&mutex->lock, &attr);
76 if (ret) {
77 log_err("pthread_mutex_init: %s\n", strerror(ret));
Jens Axboe07739b52007-03-08 20:25:46 +010078 goto err;
79 }
80
Jens Axboecdd18ad2008-02-27 18:58:00 +010081 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010082err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010083 if (mutex)
84 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010085
Jens Axboecdd18ad2008-02-27 18:58:00 +010086 unlink(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010087 return NULL;
88}
89
Jens Axboecdd18ad2008-02-27 18:58:00 +010090void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010091{
Jens Axboecdd18ad2008-02-27 18:58:00 +010092 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +010093
94 while (!mutex->value) {
95 mutex->waiters++;
Jens Axboecdd18ad2008-02-27 18:58:00 +010096 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +010097 mutex->waiters--;
98 }
99
Jens Axboecdd18ad2008-02-27 18:58:00 +0100100 mutex->value--;
101 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100102}
103
Jens Axboecdd18ad2008-02-27 18:58:00 +0100104void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +0100105{
Jens Axboecdd18ad2008-02-27 18:58:00 +0100106 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100107 read_barrier();
108 if (!mutex->value && mutex->waiters)
Jens Axboecdd18ad2008-02-27 18:58:00 +0100109 pthread_cond_signal(&mutex->cond);
110 mutex->value++;
111 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +0100112}
Jens Axboe64d4d312008-03-03 10:36:27 +0100113
114void fio_mutex_down_write(struct fio_mutex *mutex)
115{
116 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100117
118 while (mutex->value != 0) {
119 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100120 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100121 mutex->waiters--;
122 }
123
Jens Axboe64d4d312008-03-03 10:36:27 +0100124 mutex->value--;
125 pthread_mutex_unlock(&mutex->lock);
126}
127
128void fio_mutex_down_read(struct fio_mutex *mutex)
129{
130 pthread_mutex_lock(&mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100131
132 while (mutex->value < 0) {
133 mutex->waiters++;
Jens Axboe64d4d312008-03-03 10:36:27 +0100134 pthread_cond_wait(&mutex->cond, &mutex->lock);
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100135 mutex->waiters--;
136 }
137
Jens Axboe64d4d312008-03-03 10:36:27 +0100138 mutex->value++;
139 pthread_mutex_unlock(&mutex->lock);
140}
141
142void fio_mutex_up_read(struct fio_mutex *mutex)
143{
144 pthread_mutex_lock(&mutex->lock);
145 mutex->value--;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100146 read_barrier();
147 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100148 pthread_cond_signal(&mutex->cond);
149 pthread_mutex_unlock(&mutex->lock);
150}
151
152void fio_mutex_up_write(struct fio_mutex *mutex)
153{
154 pthread_mutex_lock(&mutex->lock);
155 mutex->value++;
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100156 read_barrier();
157 if (mutex->value >= 0 && mutex->waiters)
Jens Axboe64d4d312008-03-03 10:36:27 +0100158 pthread_cond_signal(&mutex->cond);
159 pthread_mutex_unlock(&mutex->lock);
160}