From 6dae8511ffda6727b6a25223cc6001298e5698d6 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 9 Oct 2024 16:16:35 -0400 Subject: [PATCH] kernel/lock: Reinstate ASSERT_UNLOCKED_RW_LOCK in _rw_lock_read_lock. This reverts commit 13a5c7f91f356541faaec72888533fc7a156c3da, and adds an inline comment explaining why this actually isn't allowed, despite "working" in most circumstances. Doesn't affect anything in default builds (KDEBUG_RW_LOCK_DEBUG is not enabled under KDEBUG.) --- src/system/kernel/locks/lock.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/system/kernel/locks/lock.cpp b/src/system/kernel/locks/lock.cpp index bb2fe98df0..067b73d08b 100644 --- a/src/system/kernel/locks/lock.cpp +++ b/src/system/kernel/locks/lock.cpp @@ -512,6 +512,7 @@ _rw_lock_read_lock(rw_lock* lock) #if KDEBUG_RW_LOCK_DEBUG int32 oldCount = atomic_add(&lock->count, 1); if (oldCount < RW_LOCK_WRITER_COUNT_BASE) { + ASSERT_UNLOCKED_RW_LOCK(lock); _rw_lock_set_read_locked(lock); return B_OK; } @@ -525,6 +526,10 @@ _rw_lock_read_lock(rw_lock* lock) return B_OK; } + // If we hold a read lock already, but some other thread is waiting + // for a write lock, then trying to read-lock again will deadlock. + ASSERT_UNLOCKED_RW_LOCK(lock); + // The writer that originally had the lock when we called atomic_add() might // already have gone and another writer could have overtaken us. In this // case the original writer set pending_readers, so we know that we don't