Skip to content

Commit f9baaf9

Browse files
committed
Simplify coding in slru.c
New code in 53c2a97 uses direct array access to shared->bank_locks[bankno].lock which can be made a little bit more legible by using the SimpleLruGetBankLock helper function. Nothing terribly serious, but let's add some clarity. Discussion: https://postgr.es/m/202403041517.3a35jw53os65@alvherre.pgsql
1 parent c8a61e3 commit f9baaf9

File tree

1 file changed

+9
-9
lines changed
  • src/backend/access/transam

1 file changed

+9
-9
lines changed

src/backend/access/transam/slru.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,14 @@ SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
489489
TransactionId xid)
490490
{
491491
SlruShared shared = ctl->shared;
492+
LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
492493

493-
Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(ctl, pageno), LW_EXCLUSIVE));
494+
Assert(LWLockHeldByMeInMode(banklock, LW_EXCLUSIVE));
494495

495496
/* Outer loop handles restart if we must wait for someone else's I/O */
496497
for (;;)
497498
{
498499
int slotno;
499-
int bankno;
500500
bool ok;
501501

502502
/* See if page already is in memory; if not, pick victim slot */
@@ -539,10 +539,9 @@ SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
539539

540540
/* Acquire per-buffer lock (cannot deadlock, see notes at top) */
541541
LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
542-
bankno = SlotGetBankNumber(slotno);
543542

544543
/* Release bank lock while doing I/O */
545-
LWLockRelease(&shared->bank_locks[bankno].lock);
544+
LWLockRelease(banklock);
546545

547546
/* Do the read */
548547
ok = SlruPhysicalReadPage(ctl, pageno, slotno);
@@ -551,7 +550,7 @@ SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
551550
SimpleLruZeroLSNs(ctl, slotno);
552551

553552
/* Re-acquire bank control lock and update page state */
554-
LWLockAcquire(&shared->bank_locks[bankno].lock, LW_EXCLUSIVE);
553+
LWLockAcquire(banklock, LW_EXCLUSIVE);
555554

556555
Assert(shared->page_number[slotno] == pageno &&
557556
shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
@@ -592,12 +591,13 @@ int
592591
SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
593592
{
594593
SlruShared shared = ctl->shared;
594+
LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
595595
int bankno = pageno & ctl->bank_mask;
596596
int bankstart = bankno * SLRU_BANK_SIZE;
597597
int bankend = bankstart + SLRU_BANK_SIZE;
598598

599599
/* Try to find the page while holding only shared lock */
600-
LWLockAcquire(&shared->bank_locks[bankno].lock, LW_SHARED);
600+
LWLockAcquire(banklock, LW_SHARED);
601601

602602
/* See if page is already in a buffer */
603603
for (int slotno = bankstart; slotno < bankend; slotno++)
@@ -617,8 +617,8 @@ SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
617617
}
618618

619619
/* No luck, so switch to normal exclusive lock and do regular read */
620-
LWLockRelease(&shared->bank_locks[bankno].lock);
621-
LWLockAcquire(&shared->bank_locks[bankno].lock, LW_EXCLUSIVE);
620+
LWLockRelease(banklock);
621+
LWLockAcquire(banklock, LW_EXCLUSIVE);
622622

623623
return SimpleLruReadPage(ctl, pageno, true, xid);
624624
}
@@ -1167,7 +1167,7 @@ SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
11671167
int bankstart = bankno * SLRU_BANK_SIZE;
11681168
int bankend = bankstart + SLRU_BANK_SIZE;
11691169

1170-
Assert(LWLockHeldByMe(&shared->bank_locks[bankno].lock));
1170+
Assert(LWLockHeldByMe(SimpleLruGetBankLock(ctl, pageno)));
11711171

11721172
/* See if page already has a buffer assigned */
11731173
for (int slotno = 0; slotno < shared->num_slots; slotno++)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy