From 44579459ecb41585550edc5c29c2f0ec93ddae24 Mon Sep 17 00:00:00 2001 From: Vlad Khorsun Date: Tue, 12 Mar 2024 12:57:20 +0200 Subject: [PATCH] GlobalRWLock state should remain consistent in the case when fetch() returns false or throws. --- src/jrd/GlobalRWLock.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/jrd/GlobalRWLock.cpp b/src/jrd/GlobalRWLock.cpp index f7a975bbea..983dee9259 100644 --- a/src/jrd/GlobalRWLock.cpp +++ b/src/jrd/GlobalRWLock.cpp @@ -38,6 +38,7 @@ #include "Attachment.h" #include "../common/classes/rwlock.h" #include "../common/classes/condition.h" +#include "../common/classes/auto.h" #ifdef COS_DEBUG #include @@ -179,12 +180,20 @@ bool GlobalRWLock::lockWrite(thread_db* tdbb, SSHORT wait) fb_assert(!currentWriter); - currentWriter = true; + Cleanup writerFini([this]() + { + if (!currentWriter) + writerFinished.notifyAll(); + }); + + const bool ret = fetch(tdbb); + if (ret) + currentWriter = true; COS_TRACE(("(%p)->lockWrite end readers(%d), blocking(%d), pendingWriters(%d), currentWriter(%d), lck_physical(%d)", this, readers, blocking, pendingWriters, currentWriter, cachedLock->lck_physical)); - return fetch(tdbb); + return ret; } } @@ -279,12 +288,14 @@ bool GlobalRWLock::lockRead(thread_db* tdbb, SSHORT wait, const bool queueJump) { // scope 2 CheckoutLockGuard counterGuard(tdbb, counterMutex, FB_FUNCTION, true); --pendingLock; - ++readers; + const bool ret = fetch(tdbb); + if (ret) + ++readers; COS_TRACE(("(%p)->lockRead end readers(%d), blocking(%d), pendingWriters(%d), currentWriter(%d), lck_physical(%d)", this, readers, blocking, pendingWriters, currentWriter, cachedLock->lck_physical)); - return fetch(tdbb); + return ret; } }