mutex: ensure that fio_mutex_up() holds mutex lock during wakeup
authorJens Axboe <axboe@kernel.dk>
Fri, 9 Mar 2018 14:55:40 +0000 (07:55 -0700)
committerJens Axboe <axboe@kernel.dk>
Fri, 9 Mar 2018 14:55:40 +0000 (07:55 -0700)
Some implementations mandate that you hold the lock while
issuing the wakeup, as not to lose a wakeup. This seems to
be the case for Windows. See the lengthy thread on the topic
here:

https://www.spinics.net/lists/fio/msg06772.html

Thanks to Sitsofe Wheeler for heroic debugging.

Reported-by: Rebecca Cran <rebecca@bluestop.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
mutex.c

diff --git a/mutex.c b/mutex.c
index 63229eda09d62084f2b3e7a4cb076fcdf2e0dc88..acc88dc33b98f0b7bb015df7676a15de6e16eb28 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -240,10 +240,11 @@ void fio_mutex_up(struct fio_mutex *mutex)
        if (!mutex->value && mutex->waiters)
                do_wake = 1;
        mutex->value++;
-       pthread_mutex_unlock(&mutex->lock);
 
        if (do_wake)
                pthread_cond_signal(&mutex->cond);
+
+       pthread_mutex_unlock(&mutex->lock);
 }
 
 void fio_rwlock_write(struct fio_rwlock *lock)