Add read-write mutexes
authorJens Axboe <jens.axboe@oracle.com>
Mon, 3 Mar 2008 09:36:27 +0000 (10:36 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Mon, 3 Mar 2008 09:36:27 +0000 (10:36 +0100)
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
mutex.c
mutex.h

diff --git a/mutex.c b/mutex.c
index f3c8326663f1d8b350a4b6cc0afd484111682711..bcc37ae648c0bf440949603d762463a1ea74a7f8 100644 (file)
--- 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);
 }
        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 0c862dba08998cc9dab9aa6f33849baa2a71f717..74515936e759f504e98399c71d51ef8e0823c8e6 100644 (file)
--- a/mutex.h
+++ b/mutex.h
@@ -6,7 +6,7 @@
 struct fio_mutex {
        pthread_mutex_t lock;
        pthread_cond_t cond;
 struct fio_mutex {
        pthread_mutex_t lock;
        pthread_cond_t cond;
-       unsigned int value;
+       int value;
 
        int mutex_fd;
 };
 
        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 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(struct fio_mutex *);
+extern void fio_mutex_up_read(struct fio_mutex *);
+extern void fio_mutex_up_write(struct fio_mutex *);
 
 #endif
 
 #endif