blob: bcc37ae648c0bf440949603d762463a1ea74a7f8 [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
9#include "mutex.h"
10
Jens Axboecdd18ad2008-02-27 18:58:00 +010011void fio_mutex_remove(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010012{
Jens Axboecdd18ad2008-02-27 18:58:00 +010013 close(mutex->mutex_fd);
14 munmap(mutex, sizeof(*mutex));
Jens Axboe07739b52007-03-08 20:25:46 +010015}
16
Jens Axboecdd18ad2008-02-27 18:58:00 +010017struct fio_mutex *fio_mutex_init(int value)
Jens Axboe07739b52007-03-08 20:25:46 +010018{
Jens Axboecdd18ad2008-02-27 18:58:00 +010019 char mutex_name[] = "/tmp/.fio_mutex.XXXXXX";
20 struct fio_mutex *mutex = NULL;
Jens Axboe07739b52007-03-08 20:25:46 +010021 pthread_mutexattr_t attr;
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010022 pthread_condattr_t cond;
Jens Axboe07739b52007-03-08 20:25:46 +010023 int fd;
24
Jens Axboecdd18ad2008-02-27 18:58:00 +010025 fd = mkstemp(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010026 if (fd < 0) {
Jens Axboecdd18ad2008-02-27 18:58:00 +010027 perror("open mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010028 return NULL;
29 }
30
Jens Axboecdd18ad2008-02-27 18:58:00 +010031 if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) {
32 perror("ftruncate mutex");
Jens Axboee53bd0b2007-03-08 20:29:11 +010033 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010034 }
35
Jens Axboecdd18ad2008-02-27 18:58:00 +010036 mutex = mmap(NULL, sizeof(struct fio_mutex), PROT_READ | PROT_WRITE,
Jens Axboe07739b52007-03-08 20:25:46 +010037 MAP_SHARED, fd, 0);
Jens Axboecdd18ad2008-02-27 18:58:00 +010038 if (mutex == MAP_FAILED) {
39 perror("mmap mutex");
Jens Axboe07739b52007-03-08 20:25:46 +010040 close(fd);
Jens Axboecdd18ad2008-02-27 18:58:00 +010041 mutex = NULL;
Jens Axboee53bd0b2007-03-08 20:29:11 +010042 goto err;
Jens Axboe07739b52007-03-08 20:25:46 +010043 }
44
Jens Axboecdd18ad2008-02-27 18:58:00 +010045 unlink(mutex_name);
46 mutex->mutex_fd = fd;
47 mutex->value = value;
Jens Axboe07739b52007-03-08 20:25:46 +010048
49 if (pthread_mutexattr_init(&attr)) {
50 perror("pthread_mutexattr_init");
51 goto err;
52 }
53 if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
54 perror("pthread_mutexattr_setpshared");
55 goto err;
56 }
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010057
58 pthread_condattr_init(&cond);
59 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
Jens Axboecdd18ad2008-02-27 18:58:00 +010060 pthread_cond_init(&mutex->cond, &cond);
Zhang, Yanmin108fcc12008-02-04 09:17:52 +010061
Jens Axboecdd18ad2008-02-27 18:58:00 +010062 if (pthread_mutex_init(&mutex->lock, &attr)) {
Jens Axboe07739b52007-03-08 20:25:46 +010063 perror("pthread_mutex_init");
64 goto err;
65 }
66
Jens Axboecdd18ad2008-02-27 18:58:00 +010067 return mutex;
Jens Axboe07739b52007-03-08 20:25:46 +010068err:
Jens Axboecdd18ad2008-02-27 18:58:00 +010069 if (mutex)
70 fio_mutex_remove(mutex);
Jens Axboef7c9e002007-03-09 12:40:02 +010071
Jens Axboecdd18ad2008-02-27 18:58:00 +010072 unlink(mutex_name);
Jens Axboe07739b52007-03-08 20:25:46 +010073 return NULL;
74}
75
Jens Axboecdd18ad2008-02-27 18:58:00 +010076void fio_mutex_down(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010077{
Jens Axboecdd18ad2008-02-27 18:58:00 +010078 pthread_mutex_lock(&mutex->lock);
79 while (mutex->value == 0)
80 pthread_cond_wait(&mutex->cond, &mutex->lock);
81 mutex->value--;
82 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +010083}
84
Jens Axboecdd18ad2008-02-27 18:58:00 +010085void fio_mutex_up(struct fio_mutex *mutex)
Jens Axboe07739b52007-03-08 20:25:46 +010086{
Jens Axboecdd18ad2008-02-27 18:58:00 +010087 pthread_mutex_lock(&mutex->lock);
88 if (!mutex->value)
89 pthread_cond_signal(&mutex->cond);
90 mutex->value++;
91 pthread_mutex_unlock(&mutex->lock);
Jens Axboe07739b52007-03-08 20:25:46 +010092}
Jens Axboe64d4d312008-03-03 10:36:27 +010093
94void fio_mutex_down_write(struct fio_mutex *mutex)
95{
96 pthread_mutex_lock(&mutex->lock);
97 while (mutex->value != 0)
98 pthread_cond_wait(&mutex->cond, &mutex->lock);
99 mutex->value--;
100 pthread_mutex_unlock(&mutex->lock);
101}
102
103void fio_mutex_down_read(struct fio_mutex *mutex)
104{
105 pthread_mutex_lock(&mutex->lock);
106 while (mutex->value < 0)
107 pthread_cond_wait(&mutex->cond, &mutex->lock);
108 mutex->value++;
109 pthread_mutex_unlock(&mutex->lock);
110}
111
112void fio_mutex_up_read(struct fio_mutex *mutex)
113{
114 pthread_mutex_lock(&mutex->lock);
115 mutex->value--;
116 if (mutex->value >= 0)
117 pthread_cond_signal(&mutex->cond);
118 pthread_mutex_unlock(&mutex->lock);
119}
120
121void fio_mutex_up_write(struct fio_mutex *mutex)
122{
123 pthread_mutex_lock(&mutex->lock);
124 mutex->value++;
125 if (mutex->value >= 0)
126 pthread_cond_signal(&mutex->cond);
127 pthread_mutex_unlock(&mutex->lock);
128}