From b634486d57a14b53f1cfcf739e41ddf826e51977 Mon Sep 17 00:00:00 2001 From: Torvald Riegel Date: Tue, 28 Apr 2015 23:24:36 +0200 Subject: Fix missing wake-ups in pthread_rwlock_rdlock. This adds wake-ups that would be missing if assuming that for a non-writer-preferring rwlock, if one thread has acquired a rdlock and does not release it, another thread will eventually acquire a rdlock too despite concurrent write lock acquisition attempts. BZ 14958 is about supporting this assumption. Strictly speaking, this isn't a valid test case, but nonetheless worth supporting (see comment 7 of BZ 14958). --- nptl/pthread_rwlock_rdlock.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'nptl/pthread_rwlock_rdlock.c') diff --git a/nptl/pthread_rwlock_rdlock.c b/nptl/pthread_rwlock_rdlock.c index 0edca654a3..004a386fd5 100644 --- a/nptl/pthread_rwlock_rdlock.c +++ b/nptl/pthread_rwlock_rdlock.c @@ -23,6 +23,7 @@ #include #include #include +#include /* Acquire read lock for RWLOCK. Slow path. */ @@ -30,6 +31,7 @@ static int __attribute__((noinline)) __pthread_rwlock_rdlock_slow (pthread_rwlock_t *rwlock) { int result = 0; + bool wake = false; /* Lock is taken in caller. */ @@ -81,7 +83,17 @@ __pthread_rwlock_rdlock_slow (pthread_rwlock_t *rwlock) result = EAGAIN; } else - LIBC_PROBE (rdlock_acquire_read, 1, rwlock); + { + LIBC_PROBE (rdlock_acquire_read, 1, rwlock); + /* See pthread_rwlock_rdlock. */ + if (rwlock->__data.__nr_readers == 1 + && rwlock->__data.__nr_readers_queued > 0 + && rwlock->__data.__nr_writers_queued > 0) + { + ++rwlock->__data.__readers_wakeup; + wake = true; + } + } break; } @@ -90,6 +102,10 @@ __pthread_rwlock_rdlock_slow (pthread_rwlock_t *rwlock) /* We are done, free the lock. */ lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared); + if (wake) + lll_futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, + rwlock->__data.__shared); + return result; } @@ -100,6 +116,7 @@ int __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) { int result = 0; + bool wake = false; LIBC_PROBE (rdlock_entry, 1, rwlock); @@ -126,11 +143,30 @@ __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) result = EAGAIN; } else - LIBC_PROBE (rdlock_acquire_read, 1, rwlock); + { + LIBC_PROBE (rdlock_acquire_read, 1, rwlock); + /* If we are the first reader, and there are blocked readers and + writers (which we don't prefer, see above), then it can be the + case that we stole the lock from a writer that was already woken + to acquire it. That means that we need to take over the writer's + responsibility to wake all readers (see pthread_rwlock_unlock). + Thus, wake all readers in this case. */ + if (rwlock->__data.__nr_readers == 1 + && rwlock->__data.__nr_readers_queued > 0 + && rwlock->__data.__nr_writers_queued > 0) + { + ++rwlock->__data.__readers_wakeup; + wake = true; + } + } /* We are done, free the lock. */ lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared); + if (wake) + lll_futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, + rwlock->__data.__shared); + return result; } -- cgit v1.2.3