Revamp file locking

Get rid of the semaphore implementation, no need to carry both.
Add different locking modes (exclusive and readwrite) to enable
a wider range of testing. Also combine lockfile and lockfile_batch,
the latter is now a postfix option to the former.

So to enable readers-excluding-writers locking mode with a lock batch
count of 4, you would write:

lockfile=readwrite:4

instead.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/mutex.c b/mutex.c
index bcc37ae..e6fb3f0 100644
--- 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 @@
 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,7 +91,8 @@
 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);
@@ -94,8 +101,13 @@
 void fio_mutex_down_write(struct fio_mutex *mutex)
 {
 	pthread_mutex_lock(&mutex->lock);
-	while (mutex->value != 0)
+
+	while (mutex->value != 0) {
+		mutex->waiters++;
 		pthread_cond_wait(&mutex->cond, &mutex->lock);
+		mutex->waiters--;
+	}
+
 	mutex->value--;
 	pthread_mutex_unlock(&mutex->lock);
 }
@@ -103,8 +115,13 @@
 void fio_mutex_down_read(struct fio_mutex *mutex)
 {
 	pthread_mutex_lock(&mutex->lock);
-	while (mutex->value < 0)
+
+	while (mutex->value < 0) {
+		mutex->waiters++;
 		pthread_cond_wait(&mutex->cond, &mutex->lock);
+		mutex->waiters--;
+	}
+
 	mutex->value++;
 	pthread_mutex_unlock(&mutex->lock);
 }
@@ -113,7 +130,8 @@
 {
 	pthread_mutex_lock(&mutex->lock);
 	mutex->value--;
-	if (mutex->value >= 0)
+	read_barrier();
+	if (mutex->value >= 0 && mutex->waiters)
 		pthread_cond_signal(&mutex->cond);
 	pthread_mutex_unlock(&mutex->lock);
 }
@@ -122,7 +140,8 @@
 {
 	pthread_mutex_lock(&mutex->lock);
 	mutex->value++;
-	if (mutex->value >= 0)
+	read_barrier();
+	if (mutex->value >= 0 && mutex->waiters)
 		pthread_cond_signal(&mutex->cond);
 	pthread_mutex_unlock(&mutex->lock);
 }