@@ -152,7 +152,7 @@ SimpleLruShmemSize(int nslots, int nlsns)
152152 sz += MAXALIGN (nslots * sizeof (bool )); /* page_dirty[] */
153153 sz += MAXALIGN (nslots * sizeof (int )); /* page_number[] */
154154 sz += MAXALIGN (nslots * sizeof (int )); /* page_lru_count[] */
155- sz += MAXALIGN (nslots * sizeof (LWLock * )); /* buffer_locks[] */
155+ sz += MAXALIGN (nslots * sizeof (LWLockPadded )); /* buffer_locks[] */
156156
157157 if (nlsns > 0 )
158158 sz += MAXALIGN (nslots * nlsns * sizeof (XLogRecPtr )); /* group_lsn[] */
@@ -203,29 +203,42 @@ SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
203203 offset += MAXALIGN (nslots * sizeof (int ));
204204 shared -> page_lru_count = (int * ) (ptr + offset );
205205 offset += MAXALIGN (nslots * sizeof (int ));
206- shared -> buffer_locks = (LWLock * * ) (ptr + offset );
207- offset += MAXALIGN (nslots * sizeof (LWLock * ));
208206
209207 if (nlsns > 0 )
210208 {
211209 shared -> group_lsn = (XLogRecPtr * ) (ptr + offset );
212210 offset += MAXALIGN (nslots * nlsns * sizeof (XLogRecPtr ));
213211 }
214212
213+ /* Initialize LWLocks */
214+ shared -> buffer_locks = (LWLockPadded * ) ShmemAlloc (sizeof (LWLockPadded ) * nslots );
215+
216+ Assert (strlen (name ) + 1 < SLRU_MAX_NAME_LENGTH );
217+ strlcpy (shared -> lwlock_tranche_name , name , SLRU_MAX_NAME_LENGTH );
218+ shared -> lwlock_tranche_id = LWLockNewTrancheId ();
219+ shared -> lwlock_tranche .name = shared -> lwlock_tranche_name ;
220+ shared -> lwlock_tranche .array_base = shared -> buffer_locks ;
221+ shared -> lwlock_tranche .array_stride = sizeof (LWLockPadded );
222+
215223 ptr += BUFFERALIGN (offset );
216224 for (slotno = 0 ; slotno < nslots ; slotno ++ )
217225 {
226+ LWLockInitialize (& shared -> buffer_locks [slotno ].lock ,
227+ shared -> lwlock_tranche_id );
228+
218229 shared -> page_buffer [slotno ] = ptr ;
219230 shared -> page_status [slotno ] = SLRU_PAGE_EMPTY ;
220231 shared -> page_dirty [slotno ] = false;
221232 shared -> page_lru_count [slotno ] = 0 ;
222- shared -> buffer_locks [slotno ] = LWLockAssign ();
223233 ptr += BLCKSZ ;
224234 }
225235 }
226236 else
227237 Assert (found );
228238
239+ /* Register SLRU tranche in the main tranches array */
240+ LWLockRegisterTranche (shared -> lwlock_tranche_id , & shared -> lwlock_tranche );
241+
229242 /*
230243 * Initialize the unshared control struct, including directory path. We
231244 * assume caller set PagePrecedes.
@@ -308,8 +321,8 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
308321
309322 /* See notes at top of file */
310323 LWLockRelease (shared -> ControlLock );
311- LWLockAcquire (shared -> buffer_locks [slotno ], LW_SHARED );
312- LWLockRelease (shared -> buffer_locks [slotno ]);
324+ LWLockAcquire (& shared -> buffer_locks [slotno ]. lock , LW_SHARED );
325+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
313326 LWLockAcquire (shared -> ControlLock , LW_EXCLUSIVE );
314327
315328 /*
@@ -323,7 +336,7 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
323336 if (shared -> page_status [slotno ] == SLRU_PAGE_READ_IN_PROGRESS ||
324337 shared -> page_status [slotno ] == SLRU_PAGE_WRITE_IN_PROGRESS )
325338 {
326- if (LWLockConditionalAcquire (shared -> buffer_locks [slotno ], LW_SHARED ))
339+ if (LWLockConditionalAcquire (& shared -> buffer_locks [slotno ]. lock , LW_SHARED ))
327340 {
328341 /* indeed, the I/O must have failed */
329342 if (shared -> page_status [slotno ] == SLRU_PAGE_READ_IN_PROGRESS )
@@ -333,7 +346,7 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
333346 shared -> page_status [slotno ] = SLRU_PAGE_VALID ;
334347 shared -> page_dirty [slotno ] = true;
335348 }
336- LWLockRelease (shared -> buffer_locks [slotno ]);
349+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
337350 }
338351 }
339352}
@@ -402,7 +415,7 @@ SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
402415 shared -> page_dirty [slotno ] = false;
403416
404417 /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
405- LWLockAcquire (shared -> buffer_locks [slotno ], LW_EXCLUSIVE );
418+ LWLockAcquire (& shared -> buffer_locks [slotno ]. lock , LW_EXCLUSIVE );
406419
407420 /* Release control lock while doing I/O */
408421 LWLockRelease (shared -> ControlLock );
@@ -422,7 +435,7 @@ SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
422435
423436 shared -> page_status [slotno ] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY ;
424437
425- LWLockRelease (shared -> buffer_locks [slotno ]);
438+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
426439
427440 /* Now it's okay to ereport if we failed */
428441 if (!ok )
@@ -518,7 +531,7 @@ SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
518531 shared -> page_dirty [slotno ] = false;
519532
520533 /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
521- LWLockAcquire (shared -> buffer_locks [slotno ], LW_EXCLUSIVE );
534+ LWLockAcquire (& shared -> buffer_locks [slotno ]. lock , LW_EXCLUSIVE );
522535
523536 /* Release control lock while doing I/O */
524537 LWLockRelease (shared -> ControlLock );
@@ -547,7 +560,7 @@ SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
547560
548561 shared -> page_status [slotno ] = SLRU_PAGE_VALID ;
549562
550- LWLockRelease (shared -> buffer_locks [slotno ]);
563+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
551564
552565 /* Now it's okay to ereport if we failed */
553566 if (!ok )
0 commit comments