From 802748b611cbc86d29d767e5197f5a76e1803bc8 Mon Sep 17 00:00:00 2001 From: Derick Eddington <124489-kcired@users.noreply.gitlab.redox-os.org> Date: Sat, 6 Jul 2024 01:13:23 -0700 Subject: [PATCH] Fix `Rwlock::acquire_write_lock` to avoid blocking indefinitely. Without this fix, `tests/bins_static/pthread/rwlock_randtest` would sometimes get stuck blocking forever with only 1+ threads all doing `pthread_rwlock_wrlock()` but all the other threads having finished. --- src/sync/rwlock.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/sync/rwlock.rs b/src/sync/rwlock.rs index f5ecaf12..b787a8e8 100644 --- a/src/sync/rwlock.rs +++ b/src/sync/rwlock.rs @@ -45,7 +45,15 @@ impl Rwlock { }; waiting_wr = expected & WAITING_WR; - let _ = crate::sync::futex_wait(&self.state, expected, deadline); + if actual & COUNT_MASK > 0 { + let _ = crate::sync::futex_wait(&self.state, expected, deadline); + } else { + // We must avoid blocking indefinitely in our `futex_wait()`, in this case + // where it's possible that `self.state == expected` but our futex might + // never be woken again, because it's possible that all other threads + // already did their `futex_wake()` before we would've done our + // `futex_wait()`. + } } } } -- GitLab