Rework lockfile= file lock handling
[fio.git] / mutex.c
diff --git a/mutex.c b/mutex.c
index 94e11884424cd203cd1f3357fef33250ffbd94eb..332e9f9ea871c04cc09c4661d252fec31cbf0dc5 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -17,8 +17,6 @@
 #include "time.h"
 #include "gettime.h"
 
-static clockid_t fio_clk_id = CLOCK_REALTIME;
-
 void fio_mutex_remove(struct fio_mutex *mutex)
 {
        pthread_cond_destroy(&mutex->cond);
@@ -63,9 +61,6 @@ struct fio_mutex *fio_mutex_init(int value)
        pthread_condattr_init(&cond);
 #ifdef FIO_HAVE_PSHARED_MUTEX
        pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
-#endif
-#ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK
-       pthread_condattr_setclock(&cond, fio_clk_id);
 #endif
        pthread_cond_init(&mutex->cond, &cond);
 
@@ -97,10 +92,9 @@ int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
        struct timespec t;
        int ret = 0;
 
-       fio_gettime(&tv_s, NULL);
-
-       clock_gettime(fio_clk_id, &t);
-       t.tv_sec += seconds;
+       gettimeofday(&tv_s, NULL);
+       t.tv_sec = tv_s.tv_sec + seconds;
+       t.tv_nsec = tv_s.tv_usec * 1000;
 
        pthread_mutex_lock(&mutex->lock);
 
@@ -112,10 +106,8 @@ int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
                 * way too early, double check.
                 */
                ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
-               if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds)) {
-                       pthread_mutex_lock(&mutex->lock);
+               if (ret == ETIMEDOUT && !mutex_timed_out(&tv_s, seconds))
                        ret = 0;
-               }
 
                mutex->waiters--;
        }
@@ -152,66 +144,49 @@ void fio_mutex_up(struct fio_mutex *mutex)
        pthread_mutex_unlock(&mutex->lock);
 }
 
-void fio_mutex_down_write(struct fio_mutex *mutex)
+void fio_rwlock_write(struct fio_rwlock *lock)
 {
-       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);
+       pthread_rwlock_wrlock(&lock->lock);
 }
 
-void fio_mutex_down_read(struct fio_mutex *mutex)
+void fio_rwlock_read(struct fio_rwlock *lock)
 {
-       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);
+       pthread_rwlock_rdlock(&lock->lock);
 }
 
-void fio_mutex_up_read(struct fio_mutex *mutex)
+void fio_rwlock_unlock(struct fio_rwlock *lock)
 {
-       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);
+       pthread_rwlock_unlock(&lock->lock);
 }
 
-void fio_mutex_up_write(struct fio_mutex *mutex)
+void fio_rwlock_remove(struct fio_rwlock *lock)
 {
-       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);
+       munmap((void *) lock, sizeof(*lock));
 }
 
-static void fio_init fio_mutex_global_init(void)
+struct fio_rwlock *fio_rwlock_init(void)
 {
-#ifdef FIO_HAVE_PTHREAD_CONDATTR_SETCLOCK
-#ifdef FIO_HAVE_CLOCK_MONOTONIC
-       pthread_condattr_t cond;
+       struct fio_rwlock *lock;
+       int ret;
 
-       pthread_condattr_init(&cond);
+       lock = (void *) mmap(NULL, sizeof(struct fio_rwlock),
+                               PROT_READ | PROT_WRITE,
+                               OS_MAP_ANON | MAP_SHARED, -1, 0);
+       if (lock == MAP_FAILED) {
+               perror("mmap rwlock");
+               lock = NULL;
+               goto err;
+       }
 
-       if (!pthread_condattr_setclock(&cond, CLOCK_MONOTONIC))
-               fio_clk_id = CLOCK_MONOTONIC;
+       ret = pthread_rwlock_init(&lock->lock, NULL);
+       if (ret) {
+               log_err("pthread_rwlock_init: %s\n", strerror(ret));
+               goto err;
+       }
 
-       pthread_condattr_destroy(&cond);
-#endif
-#endif
+       return lock;
+err:
+       if (lock)
+               fio_rwlock_remove(lock);
+       return NULL;
 }