Fio 1.20-rc3
[fio.git] / mutex.c
diff --git a/mutex.c b/mutex.c
index f3c8326663f1d8b350a4b6cc0afd484111682711..e6fb3f0d01b96f82dd789de5201362fed3ed9433 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -7,6 +7,7 @@
 #include <sys/mman.h>
 
 #include "mutex.h"
+#include "arch/arch.h"
 
 void fio_mutex_remove(struct fio_mutex *mutex)
 {
@@ -76,8 +77,13 @@ err:
 void fio_mutex_down(struct fio_mutex *mutex)
 {
        pthread_mutex_lock(&mutex->lock);
-       while (mutex->value == 0)
+
+       while (!mutex->value) {
+               mutex->waiters++;
                pthread_cond_wait(&mutex->cond, &mutex->lock);
+               mutex->waiters--;
+       }
+
        mutex->value--;
        pthread_mutex_unlock(&mutex->lock);
 }
@@ -85,8 +91,57 @@ void fio_mutex_down(struct fio_mutex *mutex)
 void fio_mutex_up(struct fio_mutex *mutex)
 {
        pthread_mutex_lock(&mutex->lock);
-       if (!mutex->value)
+       read_barrier();
+       if (!mutex->value && mutex->waiters)
                pthread_cond_signal(&mutex->cond);
        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) {
+               mutex->waiters++;
+               pthread_cond_wait(&mutex->cond, &mutex->lock);
+               mutex->waiters--;
+       }
+
+       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) {
+               mutex->waiters++;
+               pthread_cond_wait(&mutex->cond, &mutex->lock);
+               mutex->waiters--;
+       }
+
+       mutex->value++;
+       pthread_mutex_unlock(&mutex->lock);
+}
+
+void fio_mutex_up_read(struct fio_mutex *mutex)
+{
+       pthread_mutex_lock(&mutex->lock);
+       mutex->value--;
+       read_barrier();
+       if (mutex->value >= 0 && mutex->waiters)
+               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++;
+       read_barrier();
+       if (mutex->value >= 0 && mutex->waiters)
+               pthread_cond_signal(&mutex->cond);
+       pthread_mutex_unlock(&mutex->lock);
+}