From c56a4ed3b865b4b240c6f01809c935b7b86d160b Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Sat, 14 May 2016 13:26:27 +0200 Subject: lib: fixed a bug in the rw_lock Locking the main mutex should happen while the counter is locked. --- src/lib/rw_lock.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib/rw_lock.c b/src/lib/rw_lock.c index cf471e60..ba285694 100644 --- a/src/lib/rw_lock.c +++ b/src/lib/rw_lock.c @@ -70,6 +70,8 @@ int rw_lock_rdlock(rw_lock_t * lock) int rw_lock_wrlock(rw_lock_t * lock) { + int ret = 0; + if (lock == NULL) return -1; @@ -86,6 +88,8 @@ int rw_lock_wrlock(rw_lock_t * lock) pthread_mutex_lock(&lock->i_lock); if (lock->i == 0) { + ++(lock->i); + ret = pthread_mutex_lock(&lock->lock); pthread_mutex_unlock(&lock->i_lock); break; } @@ -95,9 +99,7 @@ int rw_lock_wrlock(rw_lock_t * lock) sched_yield(); } - ++(lock->i); - - return pthread_mutex_lock(&lock->lock); + return ret; } int rw_lock_unlock(rw_lock_t * lock) -- cgit v1.2.3