mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-01 10:06:57 +03:00
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).
This commit is contained in:
@ -23,6 +23,7 @@
|
||||
#include <pthreadP.h>
|
||||
#include <stap-probe.h>
|
||||
#include <elide.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user