From 5623acafc6f9729ff1ed2c539e26b00c04c23fef Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Wed, 19 Jul 2023 15:35:51 +0000 Subject: [PATCH v9] Have a quick exit for LWLockUpdateVar when there are no waiters --- src/backend/storage/lmgr/lwlock.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c index dad53591c7..bd84f63d11 100644 --- a/src/backend/storage/lmgr/lwlock.c +++ b/src/backend/storage/lmgr/lwlock.c @@ -1753,6 +1753,19 @@ LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val) */ pg_atomic_exchange_u64(valptr, val); + /* + * Quick exit when there are no waiters. + * + * The twice-in-a-row lock acquisition protocol used by LWLockWaitForVar is + * helping us out have quick exit here when there are no waiters without + * acquiring LWLock wait list lock. LWLockWaitForVar ensures that the + * waiters are added to wait queue even if LWLockUpdateVar thinks that + * there aren't any waiters actually. This avoids unnecessary LWLock wait + * list lock acquisition and release when there are no waiters at all. + */ + if ((pg_atomic_read_u32(&lock->state) & LW_FLAG_HAS_WAITERS) == 0) + return; + proclist_init(&wakeup); LWLockWaitListLock(lock); -- 2.34.1