|
|
d8307d |
commit 3d265911c2aac65d978f679101594f9071024874
|
|
|
d8307d |
Author: Andreas Schwab <schwab@suse.de>
|
|
|
d8307d |
Date: Mon Nov 12 11:11:40 2018 +0100
|
|
|
d8307d |
|
|
|
d8307d |
Reindent nptl/pthread_rwlock_common.c
|
|
|
d8307d |
|
|
|
d8307d |
diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c
|
|
|
d8307d |
index a290d08332b802a5..5dd534271aed6b41 100644
|
|
|
d8307d |
--- a/nptl/pthread_rwlock_common.c
|
|
|
d8307d |
+++ b/nptl/pthread_rwlock_common.c
|
|
|
d8307d |
@@ -34,7 +34,7 @@
|
|
|
d8307d |
|
|
|
d8307d |
A thread is allowed to acquire a read lock recursively (i.e., have rdlock
|
|
|
d8307d |
critical sections that overlap in sequenced-before) unless the kind of the
|
|
|
d8307d |
- rwlock is set to PTHREAD_RWLOCK_PREFER_WRITERS_NONRECURSIVE_NP.
|
|
|
d8307d |
+ rwlock is set to PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP.
|
|
|
d8307d |
|
|
|
d8307d |
This lock is built so that workloads of mostly readers can be executed with
|
|
|
d8307d |
low runtime overheads. This matches that the default kind of the lock is
|
|
|
d8307d |
@@ -46,7 +46,7 @@
|
|
|
d8307d |
An uncontended write lock acquisition is as fast as for a normal
|
|
|
d8307d |
exclusive mutex but writer contention is somewhat more costly due to
|
|
|
d8307d |
keeping track of the exact number of writers. If the rwlock kind requests
|
|
|
d8307d |
- writers to be preferred (i.e., PTHREAD_RWLOCK_PREFER_WRITERS_NP or the
|
|
|
d8307d |
+ writers to be preferred (i.e., PTHREAD_RWLOCK_PREFER_WRITER_NP or the
|
|
|
d8307d |
no-recursive-readers variant of it), then writer--to--writer lock ownership
|
|
|
d8307d |
hand-over is fairly fast and bypasses lock acquisition attempts by readers.
|
|
|
d8307d |
The costs of lock ownership transfer between readers and writers vary. If
|
|
|
d8307d |
@@ -251,7 +251,7 @@ __pthread_rwlock_rdunlock (pthread_rwlock_t *rwlock)
|
|
|
d8307d |
the first reader's store to __wrphase_futex (or a later value) if
|
|
|
d8307d |
the writer observes that a write phase has been started. */
|
|
|
d8307d |
if (atomic_compare_exchange_weak_release (&rwlock->__data.__readers,
|
|
|
d8307d |
- &r, rnew))
|
|
|
d8307d |
+ &r, rnew))
|
|
|
d8307d |
break;
|
|
|
d8307d |
/* TODO Back-off. */
|
|
|
d8307d |
}
|
|
|
d8307d |
@@ -285,7 +285,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* Make sure we are not holding the rwlock as a writer. This is a deadlock
|
|
|
d8307d |
situation we recognize and report. */
|
|
|
d8307d |
if (__glibc_unlikely (atomic_load_relaxed (&rwlock->__data.__cur_writer)
|
|
|
d8307d |
- == THREAD_GETMEM (THREAD_SELF, tid)))
|
|
|
d8307d |
+ == THREAD_GETMEM (THREAD_SELF, tid)))
|
|
|
d8307d |
return EDEADLK;
|
|
|
d8307d |
|
|
|
d8307d |
/* If we prefer writers, recursive rdlock is disallowed, we are in a read
|
|
|
d8307d |
@@ -299,9 +299,9 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
if (rwlock->__data.__flags == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)
|
|
|
d8307d |
{
|
|
|
d8307d |
r = atomic_load_relaxed (&rwlock->__data.__readers);
|
|
|
d8307d |
- while (((r & PTHREAD_RWLOCK_WRPHASE) == 0)
|
|
|
d8307d |
- && ((r & PTHREAD_RWLOCK_WRLOCKED) != 0)
|
|
|
d8307d |
- && ((r >> PTHREAD_RWLOCK_READER_SHIFT) > 0))
|
|
|
d8307d |
+ while ((r & PTHREAD_RWLOCK_WRPHASE) == 0
|
|
|
d8307d |
+ && (r & PTHREAD_RWLOCK_WRLOCKED) != 0
|
|
|
d8307d |
+ && (r >> PTHREAD_RWLOCK_READER_SHIFT) > 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* TODO Spin first. */
|
|
|
d8307d |
/* Try setting the flag signaling that we are waiting without having
|
|
|
d8307d |
@@ -315,11 +315,11 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
__readers, and all threads set the flag under the same
|
|
|
d8307d |
conditions. */
|
|
|
d8307d |
while ((atomic_load_relaxed (&rwlock->__data.__readers)
|
|
|
d8307d |
- & PTHREAD_RWLOCK_RWAITING) != 0)
|
|
|
d8307d |
+ & PTHREAD_RWLOCK_RWAITING) != 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
int private = __pthread_rwlock_get_private (rwlock);
|
|
|
d8307d |
int err = futex_abstimed_wait (&rwlock->__data.__readers,
|
|
|
d8307d |
- r, abstime, private);
|
|
|
d8307d |
+ r, abstime, private);
|
|
|
d8307d |
/* We ignore EAGAIN and EINTR. On time-outs, we can just
|
|
|
d8307d |
return because we don't need to clean up anything. */
|
|
|
d8307d |
if (err == ETIMEDOUT)
|
|
|
d8307d |
@@ -338,8 +338,9 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
expected value for future operations. Acquire MO so we synchronize with
|
|
|
d8307d |
prior writers as well as the last reader of the previous read phase (see
|
|
|
d8307d |
below). */
|
|
|
d8307d |
- r = atomic_fetch_add_acquire (&rwlock->__data.__readers,
|
|
|
d8307d |
- (1 << PTHREAD_RWLOCK_READER_SHIFT)) + (1 << PTHREAD_RWLOCK_READER_SHIFT);
|
|
|
d8307d |
+ r = (atomic_fetch_add_acquire (&rwlock->__data.__readers,
|
|
|
d8307d |
+ (1 << PTHREAD_RWLOCK_READER_SHIFT))
|
|
|
d8307d |
+ + (1 << PTHREAD_RWLOCK_READER_SHIFT));
|
|
|
d8307d |
|
|
|
d8307d |
/* Check whether there is an overflow in the number of readers. We assume
|
|
|
d8307d |
that the total number of threads is less than half the maximum number
|
|
|
d8307d |
@@ -359,8 +360,9 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* Relaxed MO is okay because we just want to undo our registration and
|
|
|
d8307d |
cannot have changed the rwlock state substantially if the CAS
|
|
|
d8307d |
succeeds. */
|
|
|
d8307d |
- if (atomic_compare_exchange_weak_relaxed (&rwlock->__data.__readers, &r,
|
|
|
d8307d |
- r - (1 << PTHREAD_RWLOCK_READER_SHIFT)))
|
|
|
d8307d |
+ if (atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
+ (&rwlock->__data.__readers,
|
|
|
d8307d |
+ &r, r - (1 << PTHREAD_RWLOCK_READER_SHIFT)))
|
|
|
d8307d |
return EAGAIN;
|
|
|
d8307d |
}
|
|
|
d8307d |
|
|
|
d8307d |
@@ -378,15 +380,15 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* Otherwise, if we were in a write phase (states #6 or #8), we must wait
|
|
|
d8307d |
for explicit hand-over of the read phase; the only exception is if we
|
|
|
d8307d |
can start a read phase if there is no primary writer currently. */
|
|
|
d8307d |
- while (((r & PTHREAD_RWLOCK_WRPHASE) != 0)
|
|
|
d8307d |
- && ((r & PTHREAD_RWLOCK_WRLOCKED) == 0))
|
|
|
d8307d |
+ while ((r & PTHREAD_RWLOCK_WRPHASE) != 0
|
|
|
d8307d |
+ && (r & PTHREAD_RWLOCK_WRLOCKED) == 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
- /* Try to enter a read phase: If the CAS below succeeds, we have
|
|
|
d8307d |
+ /* Try to enter a read phase: If the CAS below succeeds, we have
|
|
|
d8307d |
ownership; if it fails, we will simply retry and reassess the
|
|
|
d8307d |
situation.
|
|
|
d8307d |
Acquire MO so we synchronize with prior writers. */
|
|
|
d8307d |
if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers, &r,
|
|
|
d8307d |
- r ^ PTHREAD_RWLOCK_WRPHASE))
|
|
|
d8307d |
+ r ^ PTHREAD_RWLOCK_WRPHASE))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* We started the read phase, so we are also responsible for
|
|
|
d8307d |
updating the write-phase futex. Relaxed MO is sufficient.
|
|
|
d8307d |
@@ -397,7 +399,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
(but we can pretend to do the setting and unsetting of WRLOCKED
|
|
|
d8307d |
atomically, and thus can skip this step). */
|
|
|
d8307d |
if ((atomic_exchange_relaxed (&rwlock->__data.__wrphase_futex, 0)
|
|
|
d8307d |
- & PTHREAD_RWLOCK_FUTEX_USED) != 0)
|
|
|
d8307d |
+ & PTHREAD_RWLOCK_FUTEX_USED) != 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
int private = __pthread_rwlock_get_private (rwlock);
|
|
|
d8307d |
futex_wake (&rwlock->__data.__wrphase_futex, INT_MAX, private);
|
|
|
d8307d |
@@ -435,16 +437,17 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
for (;;)
|
|
|
d8307d |
{
|
|
|
d8307d |
while (((wpf = atomic_load_relaxed (&rwlock->__data.__wrphase_futex))
|
|
|
d8307d |
- | PTHREAD_RWLOCK_FUTEX_USED) == (1 | PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
+ | PTHREAD_RWLOCK_FUTEX_USED) == (1 | PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
{
|
|
|
d8307d |
int private = __pthread_rwlock_get_private (rwlock);
|
|
|
d8307d |
if (((wpf & PTHREAD_RWLOCK_FUTEX_USED) == 0)
|
|
|
d8307d |
- && !atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
+ && (!atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
(&rwlock->__data.__wrphase_futex,
|
|
|
d8307d |
- &wpf, wpf | PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
+ &wpf, wpf | PTHREAD_RWLOCK_FUTEX_USED)))
|
|
|
d8307d |
continue;
|
|
|
d8307d |
int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex,
|
|
|
d8307d |
- 1 | PTHREAD_RWLOCK_FUTEX_USED, abstime, private);
|
|
|
d8307d |
+ 1 | PTHREAD_RWLOCK_FUTEX_USED,
|
|
|
d8307d |
+ abstime, private);
|
|
|
d8307d |
if (err == ETIMEDOUT)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* If we timed out, we need to unregister. If no read phase
|
|
|
d8307d |
@@ -477,8 +480,8 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
in this case and thus make the spin-waiting we need
|
|
|
d8307d |
unnecessarily expensive. */
|
|
|
d8307d |
while ((atomic_load_relaxed (&rwlock->__data.__wrphase_futex)
|
|
|
d8307d |
- | PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
- == (1 | PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
+ | PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
+ == (1 | PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* TODO Back-off? */
|
|
|
d8307d |
}
|
|
|
d8307d |
@@ -495,7 +498,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
release of the writer, and so that we observe a recent value of
|
|
|
d8307d |
__wrphase_futex (see below). */
|
|
|
d8307d |
if ((atomic_load_acquire (&rwlock->__data.__readers)
|
|
|
d8307d |
- & PTHREAD_RWLOCK_WRPHASE) == 0)
|
|
|
d8307d |
+ & PTHREAD_RWLOCK_WRPHASE) == 0)
|
|
|
d8307d |
/* We are in a read phase now, so the least recent modification of
|
|
|
d8307d |
__wrphase_futex we can read from is the store by the writer
|
|
|
d8307d |
with value 1. Thus, only now we can assume that if we observe
|
|
|
d8307d |
@@ -516,8 +519,9 @@ __pthread_rwlock_wrunlock (pthread_rwlock_t *rwlock)
|
|
|
d8307d |
atomic_store_relaxed (&rwlock->__data.__cur_writer, 0);
|
|
|
d8307d |
/* Disable waiting by writers. We will wake up after we decided how to
|
|
|
d8307d |
proceed. */
|
|
|
d8307d |
- bool wake_writers = ((atomic_exchange_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers_futex, 0) & PTHREAD_RWLOCK_FUTEX_USED) != 0);
|
|
|
d8307d |
+ bool wake_writers
|
|
|
d8307d |
+ = ((atomic_exchange_relaxed (&rwlock->__data.__writers_futex, 0)
|
|
|
d8307d |
+ & PTHREAD_RWLOCK_FUTEX_USED) != 0);
|
|
|
d8307d |
|
|
|
d8307d |
if (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP)
|
|
|
d8307d |
{
|
|
|
d8307d |
@@ -529,8 +533,8 @@ __pthread_rwlock_wrunlock (pthread_rwlock_t *rwlock)
|
|
|
d8307d |
synchronize with us and thus can take over our view of
|
|
|
d8307d |
__readers (including, for example, whether we are in a write
|
|
|
d8307d |
phase or not). */
|
|
|
d8307d |
- if (atomic_compare_exchange_weak_release (&rwlock->__data.__writers,
|
|
|
d8307d |
- &w, w | PTHREAD_RWLOCK_WRHANDOVER))
|
|
|
d8307d |
+ if (atomic_compare_exchange_weak_release
|
|
|
d8307d |
+ (&rwlock->__data.__writers, &w, w | PTHREAD_RWLOCK_WRHANDOVER))
|
|
|
d8307d |
/* Another writer will take over. */
|
|
|
d8307d |
goto done;
|
|
|
d8307d |
/* TODO Back-off. */
|
|
|
d8307d |
@@ -543,9 +547,10 @@ __pthread_rwlock_wrunlock (pthread_rwlock_t *rwlock)
|
|
|
d8307d |
unsigned int r = atomic_load_relaxed (&rwlock->__data.__readers);
|
|
|
d8307d |
/* Release MO so that subsequent readers or writers synchronize with us. */
|
|
|
d8307d |
while (!atomic_compare_exchange_weak_release
|
|
|
d8307d |
- (&rwlock->__data.__readers, &r, (r ^ PTHREAD_RWLOCK_WRLOCKED)
|
|
|
d8307d |
- ^ ((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0 ? 0
|
|
|
d8307d |
- : PTHREAD_RWLOCK_WRPHASE)))
|
|
|
d8307d |
+ (&rwlock->__data.__readers, &r,
|
|
|
d8307d |
+ ((r ^ PTHREAD_RWLOCK_WRLOCKED)
|
|
|
d8307d |
+ ^ ((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0 ? 0
|
|
|
d8307d |
+ : PTHREAD_RWLOCK_WRPHASE))))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* TODO Back-off. */
|
|
|
d8307d |
}
|
|
|
d8307d |
@@ -574,7 +579,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* Make sure we are not holding the rwlock as a writer. This is a deadlock
|
|
|
d8307d |
situation we recognize and report. */
|
|
|
d8307d |
if (__glibc_unlikely (atomic_load_relaxed (&rwlock->__data.__cur_writer)
|
|
|
d8307d |
- == THREAD_GETMEM (THREAD_SELF, tid)))
|
|
|
d8307d |
+ == THREAD_GETMEM (THREAD_SELF, tid)))
|
|
|
d8307d |
return EDEADLK;
|
|
|
d8307d |
|
|
|
d8307d |
/* First we try to acquire the role of primary writer by setting WRLOCKED;
|
|
|
d8307d |
@@ -593,12 +598,12 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
this could be less scalable if readers arrive and leave frequently. */
|
|
|
d8307d |
bool may_share_futex_used_flag = false;
|
|
|
d8307d |
unsigned int r = atomic_fetch_or_acquire (&rwlock->__data.__readers,
|
|
|
d8307d |
- PTHREAD_RWLOCK_WRLOCKED);
|
|
|
d8307d |
+ PTHREAD_RWLOCK_WRLOCKED);
|
|
|
d8307d |
if (__glibc_unlikely ((r & PTHREAD_RWLOCK_WRLOCKED) != 0))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* There is another primary writer. */
|
|
|
d8307d |
- bool prefer_writer =
|
|
|
d8307d |
- (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP);
|
|
|
d8307d |
+ bool prefer_writer
|
|
|
d8307d |
+ = (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP);
|
|
|
d8307d |
if (prefer_writer)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* We register as a waiting writer, so that we can make use of
|
|
|
d8307d |
@@ -617,8 +622,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* Try to become the primary writer or retry. Acquire MO as in
|
|
|
d8307d |
the fetch_or above. */
|
|
|
d8307d |
if (atomic_compare_exchange_weak_acquire
|
|
|
d8307d |
- (&rwlock->__data.__readers, &r,
|
|
|
d8307d |
- r | PTHREAD_RWLOCK_WRLOCKED))
|
|
|
d8307d |
+ (&rwlock->__data.__readers, &r, r | PTHREAD_RWLOCK_WRLOCKED))
|
|
|
d8307d |
{
|
|
|
d8307d |
if (prefer_writer)
|
|
|
d8307d |
{
|
|
|
d8307d |
@@ -633,8 +637,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
__writers).
|
|
|
d8307d |
??? Perhaps this is not strictly necessary for
|
|
|
d8307d |
reasons we do not yet know of. */
|
|
|
d8307d |
- atomic_fetch_add_relaxed (&rwlock->__data.__writers,
|
|
|
d8307d |
- -1);
|
|
|
d8307d |
+ atomic_fetch_add_relaxed (&rwlock->__data.__writers, -1);
|
|
|
d8307d |
}
|
|
|
d8307d |
break;
|
|
|
d8307d |
}
|
|
|
d8307d |
@@ -646,8 +649,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
succeed, we own WRLOCKED. */
|
|
|
d8307d |
if (prefer_writer)
|
|
|
d8307d |
{
|
|
|
d8307d |
- unsigned int w = atomic_load_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers);
|
|
|
d8307d |
+ unsigned int w = atomic_load_relaxed (&rwlock->__data.__writers);
|
|
|
d8307d |
if ((w & PTHREAD_RWLOCK_WRHANDOVER) != 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* Acquire MO is required here so that we synchronize with
|
|
|
d8307d |
@@ -677,13 +679,13 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* We did not acquire WRLOCKED nor were able to use writer--writer
|
|
|
d8307d |
hand-over, so we block on __writers_futex. */
|
|
|
d8307d |
int private = __pthread_rwlock_get_private (rwlock);
|
|
|
d8307d |
- unsigned int wf = atomic_load_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers_futex);
|
|
|
d8307d |
+ unsigned int wf
|
|
|
d8307d |
+ = atomic_load_relaxed (&rwlock->__data.__writers_futex);
|
|
|
d8307d |
if (((wf & ~(unsigned int) PTHREAD_RWLOCK_FUTEX_USED) != 1)
|
|
|
d8307d |
|| ((wf != (1 | PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
- && !atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
+ && (!atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
(&rwlock->__data.__writers_futex, &wf,
|
|
|
d8307d |
- 1 | PTHREAD_RWLOCK_FUTEX_USED)))
|
|
|
d8307d |
+ 1 | PTHREAD_RWLOCK_FUTEX_USED))))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* If we cannot block on __writers_futex because there is no
|
|
|
d8307d |
primary writer, or we cannot set PTHREAD_RWLOCK_FUTEX_USED,
|
|
|
d8307d |
@@ -704,7 +706,8 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
in this group. */
|
|
|
d8307d |
may_share_futex_used_flag = true;
|
|
|
d8307d |
int err = futex_abstimed_wait (&rwlock->__data.__writers_futex,
|
|
|
d8307d |
- 1 | PTHREAD_RWLOCK_FUTEX_USED, abstime, private);
|
|
|
d8307d |
+ 1 | PTHREAD_RWLOCK_FUTEX_USED,
|
|
|
d8307d |
+ abstime, private);
|
|
|
d8307d |
if (err == ETIMEDOUT)
|
|
|
d8307d |
{
|
|
|
d8307d |
if (prefer_writer)
|
|
|
d8307d |
@@ -716,10 +719,10 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
that this happened before the timeout; see
|
|
|
d8307d |
pthread_rwlock_rdlock_full for the full reasoning.)
|
|
|
d8307d |
Also see the similar code above. */
|
|
|
d8307d |
- unsigned int w = atomic_load_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers);
|
|
|
d8307d |
+ unsigned int w
|
|
|
d8307d |
+ = atomic_load_relaxed (&rwlock->__data.__writers);
|
|
|
d8307d |
while (!atomic_compare_exchange_weak_acquire
|
|
|
d8307d |
- (&rwlock->__data.__writers, &w,
|
|
|
d8307d |
+ (&rwlock->__data.__writers, &w,
|
|
|
d8307d |
(w == PTHREAD_RWLOCK_WRHANDOVER + 1 ? 0 : w - 1)))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* TODO Back-off. */
|
|
|
d8307d |
@@ -751,7 +754,8 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
modifications of __readers ensures that this store happens after the
|
|
|
d8307d |
store of value 0 by the previous primary writer. */
|
|
|
d8307d |
atomic_store_relaxed (&rwlock->__data.__writers_futex,
|
|
|
d8307d |
- 1 | (may_share_futex_used_flag ? PTHREAD_RWLOCK_FUTEX_USED : 0));
|
|
|
d8307d |
+ 1 | (may_share_futex_used_flag
|
|
|
d8307d |
+ ? PTHREAD_RWLOCK_FUTEX_USED : 0));
|
|
|
d8307d |
|
|
|
d8307d |
/* If we are in a write phase, we have acquired the lock. */
|
|
|
d8307d |
if ((r & PTHREAD_RWLOCK_WRPHASE) != 0)
|
|
|
d8307d |
@@ -759,15 +763,15 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
|
|
|
d8307d |
/* If we are in a read phase and there are no readers, try to start a write
|
|
|
d8307d |
phase. */
|
|
|
d8307d |
- while (((r & PTHREAD_RWLOCK_WRPHASE) == 0)
|
|
|
d8307d |
- && ((r >> PTHREAD_RWLOCK_READER_SHIFT) == 0))
|
|
|
d8307d |
+ while ((r & PTHREAD_RWLOCK_WRPHASE) == 0
|
|
|
d8307d |
+ && (r >> PTHREAD_RWLOCK_READER_SHIFT) == 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* Acquire MO so that we synchronize with prior writers and do
|
|
|
d8307d |
not interfere with their updates to __writers_futex, as well
|
|
|
d8307d |
as regarding prior readers and their updates to __wrphase_futex,
|
|
|
d8307d |
respectively. */
|
|
|
d8307d |
if (atomic_compare_exchange_weak_acquire (&rwlock->__data.__readers,
|
|
|
d8307d |
- &r, r | PTHREAD_RWLOCK_WRPHASE))
|
|
|
d8307d |
+ &r, r | PTHREAD_RWLOCK_WRPHASE))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* We have started a write phase, so need to enable readers to wait.
|
|
|
d8307d |
See the similar case in __pthread_rwlock_rdlock_full. Unlike in
|
|
|
d8307d |
@@ -792,24 +796,24 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
for (;;)
|
|
|
d8307d |
{
|
|
|
d8307d |
while (((wpf = atomic_load_relaxed (&rwlock->__data.__wrphase_futex))
|
|
|
d8307d |
- | PTHREAD_RWLOCK_FUTEX_USED) == PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
+ | PTHREAD_RWLOCK_FUTEX_USED) == PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
{
|
|
|
d8307d |
int private = __pthread_rwlock_get_private (rwlock);
|
|
|
d8307d |
- if (((wpf & PTHREAD_RWLOCK_FUTEX_USED) == 0)
|
|
|
d8307d |
- && !atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
+ if ((wpf & PTHREAD_RWLOCK_FUTEX_USED) == 0
|
|
|
d8307d |
+ && (!atomic_compare_exchange_weak_relaxed
|
|
|
d8307d |
(&rwlock->__data.__wrphase_futex, &wpf,
|
|
|
d8307d |
- PTHREAD_RWLOCK_FUTEX_USED))
|
|
|
d8307d |
+ PTHREAD_RWLOCK_FUTEX_USED)))
|
|
|
d8307d |
continue;
|
|
|
d8307d |
int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex,
|
|
|
d8307d |
- PTHREAD_RWLOCK_FUTEX_USED, abstime, private);
|
|
|
d8307d |
+ PTHREAD_RWLOCK_FUTEX_USED,
|
|
|
d8307d |
+ abstime, private);
|
|
|
d8307d |
if (err == ETIMEDOUT)
|
|
|
d8307d |
{
|
|
|
d8307d |
- if (rwlock->__data.__flags
|
|
|
d8307d |
- != PTHREAD_RWLOCK_PREFER_READER_NP)
|
|
|
d8307d |
+ if (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* We try writer--writer hand-over. */
|
|
|
d8307d |
- unsigned int w = atomic_load_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers);
|
|
|
d8307d |
+ unsigned int w
|
|
|
d8307d |
+ = atomic_load_relaxed (&rwlock->__data.__writers);
|
|
|
d8307d |
if (w != 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* We are about to hand over WRLOCKED, so we must
|
|
|
d8307d |
@@ -823,13 +827,13 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
Release MO so that another writer that gets
|
|
|
d8307d |
WRLOCKED from us can take over our view of
|
|
|
d8307d |
__readers. */
|
|
|
d8307d |
- unsigned int wf = atomic_exchange_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers_futex, 0);
|
|
|
d8307d |
+ unsigned int wf
|
|
|
d8307d |
+ = atomic_exchange_relaxed (&rwlock->__data.__writers_futex, 0);
|
|
|
d8307d |
while (w != 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
if (atomic_compare_exchange_weak_release
|
|
|
d8307d |
(&rwlock->__data.__writers, &w,
|
|
|
d8307d |
- w | PTHREAD_RWLOCK_WRHANDOVER))
|
|
|
d8307d |
+ w | PTHREAD_RWLOCK_WRHANDOVER))
|
|
|
d8307d |
{
|
|
|
d8307d |
/* Wake other writers. */
|
|
|
d8307d |
if ((wf & PTHREAD_RWLOCK_FUTEX_USED) != 0)
|
|
|
d8307d |
@@ -844,8 +848,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
again. Make sure we don't loose the flag that
|
|
|
d8307d |
signals whether there are threads waiting on
|
|
|
d8307d |
this futex. */
|
|
|
d8307d |
- atomic_store_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers_futex, wf);
|
|
|
d8307d |
+ atomic_store_relaxed (&rwlock->__data.__writers_futex, wf);
|
|
|
d8307d |
}
|
|
|
d8307d |
}
|
|
|
d8307d |
/* If we timed out and we are not in a write phase, we can
|
|
|
d8307d |
@@ -857,8 +860,8 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* We are about to release WRLOCKED, so we must release
|
|
|
d8307d |
__writers_futex too; see the handling of
|
|
|
d8307d |
writer--writer hand-over above. */
|
|
|
d8307d |
- unsigned int wf = atomic_exchange_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__writers_futex, 0);
|
|
|
d8307d |
+ unsigned int wf
|
|
|
d8307d |
+ = atomic_exchange_relaxed (&rwlock->__data.__writers_futex, 0);
|
|
|
d8307d |
while ((r & PTHREAD_RWLOCK_WRPHASE) == 0)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* While we don't need to make anything from a
|
|
|
d8307d |
@@ -877,11 +880,11 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
/* Wake other writers. */
|
|
|
d8307d |
if ((wf & PTHREAD_RWLOCK_FUTEX_USED) != 0)
|
|
|
d8307d |
futex_wake (&rwlock->__data.__writers_futex,
|
|
|
d8307d |
- 1, private);
|
|
|
d8307d |
+ 1, private);
|
|
|
d8307d |
/* Wake waiting readers. */
|
|
|
d8307d |
if ((r & PTHREAD_RWLOCK_RWAITING) != 0)
|
|
|
d8307d |
futex_wake (&rwlock->__data.__readers,
|
|
|
d8307d |
- INT_MAX, private);
|
|
|
d8307d |
+ INT_MAX, private);
|
|
|
d8307d |
return ETIMEDOUT;
|
|
|
d8307d |
}
|
|
|
d8307d |
}
|
|
|
d8307d |
@@ -898,10 +901,9 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
atomic_thread_fence_acquire ();
|
|
|
d8307d |
/* We still need to wait for explicit hand-over, but we must
|
|
|
d8307d |
not use futex_wait anymore. */
|
|
|
d8307d |
- while ((atomic_load_relaxed
|
|
|
d8307d |
- (&rwlock->__data.__wrphase_futex)
|
|
|
d8307d |
- | PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
- == PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
+ while ((atomic_load_relaxed (&rwlock->__data.__wrphase_futex)
|
|
|
d8307d |
+ | PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
+ == PTHREAD_RWLOCK_FUTEX_USED)
|
|
|
d8307d |
{
|
|
|
d8307d |
/* TODO Back-off. */
|
|
|
d8307d |
}
|
|
|
d8307d |
@@ -915,12 +917,12 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock,
|
|
|
d8307d |
if (ready)
|
|
|
d8307d |
break;
|
|
|
d8307d |
if ((atomic_load_acquire (&rwlock->__data.__readers)
|
|
|
d8307d |
- & PTHREAD_RWLOCK_WRPHASE) != 0)
|
|
|
d8307d |
+ & PTHREAD_RWLOCK_WRPHASE) != 0)
|
|
|
d8307d |
ready = true;
|
|
|
d8307d |
}
|
|
|
d8307d |
|
|
|
d8307d |
done:
|
|
|
d8307d |
atomic_store_relaxed (&rwlock->__data.__cur_writer,
|
|
|
d8307d |
- THREAD_GETMEM (THREAD_SELF, tid));
|
|
|
d8307d |
+ THREAD_GETMEM (THREAD_SELF, tid));
|
|
|
d8307d |
return 0;
|
|
|
d8307d |
}
|