From 64d4d313ef4f537408d28fac863cf2a2b0f00175 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Mon, 3 Mar 2008 10:36:27 +0100 Subject: [PATCH 1/1] Add read-write mutexes Signed-off-by: Jens Axboe --- mutex.c | 36 ++++++++++++++++++++++++++++++++++++ mutex.h | 6 +++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/mutex.c b/mutex.c index f3c83266..bcc37ae6 100644 --- a/mutex.c +++ b/mutex.c @@ -90,3 +90,39 @@ void fio_mutex_up(struct fio_mutex *mutex) mutex->value++; pthread_mutex_unlock(&mutex->lock); } + +void fio_mutex_down_write(struct fio_mutex *mutex) +{ + pthread_mutex_lock(&mutex->lock); + while (mutex->value != 0) + pthread_cond_wait(&mutex->cond, &mutex->lock); + mutex->value--; + pthread_mutex_unlock(&mutex->lock); +} + +void fio_mutex_down_read(struct fio_mutex *mutex) +{ + pthread_mutex_lock(&mutex->lock); + while (mutex->value < 0) + pthread_cond_wait(&mutex->cond, &mutex->lock); + mutex->value++; + pthread_mutex_unlock(&mutex->lock); +} + +void fio_mutex_up_read(struct fio_mutex *mutex) +{ + pthread_mutex_lock(&mutex->lock); + mutex->value--; + if (mutex->value >= 0) + pthread_cond_signal(&mutex->cond); + pthread_mutex_unlock(&mutex->lock); +} + +void fio_mutex_up_write(struct fio_mutex *mutex) +{ + pthread_mutex_lock(&mutex->lock); + mutex->value++; + if (mutex->value >= 0) + pthread_cond_signal(&mutex->cond); + pthread_mutex_unlock(&mutex->lock); +} diff --git a/mutex.h b/mutex.h index 0c862dba..74515936 100644 --- a/mutex.h +++ b/mutex.h @@ -6,7 +6,7 @@ struct fio_mutex { pthread_mutex_t lock; pthread_cond_t cond; - unsigned int value; + int value; int mutex_fd; }; @@ -14,6 +14,10 @@ struct fio_mutex { extern struct fio_mutex *fio_mutex_init(int); extern void fio_mutex_remove(struct fio_mutex *); extern void fio_mutex_down(struct fio_mutex *); +extern void fio_mutex_down_read(struct fio_mutex *); +extern void fio_mutex_down_write(struct fio_mutex *); extern void fio_mutex_up(struct fio_mutex *); +extern void fio_mutex_up_read(struct fio_mutex *); +extern void fio_mutex_up_write(struct fio_mutex *); #endif -- 2.25.1