Fix some issues with the OSX port
[fio.git] / mutex.c
diff --git a/mutex.c b/mutex.c
index 1538f62c4b1a286ebe015c299031ffede126e2c4..e148430b618e5cae1c1fd93c1134f47910198ec6 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
+#include <time.h>
 #include <pthread.h>
 #include <sys/mman.h>
 
@@ -10,6 +11,7 @@
 #include "mutex.h"
 #include "arch/arch.h"
 #include "os/os.h"
+#include "helpers.h"
 
 void fio_mutex_remove(struct fio_mutex *mutex)
 {
@@ -88,6 +90,30 @@ err:
        return NULL;
 }
 
+int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
+{
+       struct timespec t;
+       int ret = 0;
+
+       clock_gettime(CLOCK_REALTIME, &t);
+       t.tv_sec += seconds;
+
+       pthread_mutex_lock(&mutex->lock);
+
+       while (!mutex->value && !ret) {
+               mutex->waiters++;
+               ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
+               mutex->waiters--;
+       }
+
+       if (!ret) {
+               mutex->value--;
+               pthread_mutex_unlock(&mutex->lock);
+       }
+
+       return ret;
+}
+
 void fio_mutex_down(struct fio_mutex *mutex)
 {
        pthread_mutex_lock(&mutex->lock);