@@ -152,7 +152,7 @@ SimpleLruShmemSize(int nslots, int nlsns)
152
152
sz += MAXALIGN (nslots * sizeof (bool )); /* page_dirty[] */
153
153
sz += MAXALIGN (nslots * sizeof (int )); /* page_number[] */
154
154
sz += MAXALIGN (nslots * sizeof (int )); /* page_lru_count[] */
155
- sz += MAXALIGN (nslots * sizeof (LWLock * )); /* buffer_locks[] */
155
+ sz += MAXALIGN (nslots * sizeof (LWLockPadded )); /* buffer_locks[] */
156
156
157
157
if (nlsns > 0 )
158
158
sz += MAXALIGN (nslots * nlsns * sizeof (XLogRecPtr )); /* group_lsn[] */
@@ -203,29 +203,42 @@ SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
203
203
offset += MAXALIGN (nslots * sizeof (int ));
204
204
shared -> page_lru_count = (int * ) (ptr + offset );
205
205
offset += MAXALIGN (nslots * sizeof (int ));
206
- shared -> buffer_locks = (LWLock * * ) (ptr + offset );
207
- offset += MAXALIGN (nslots * sizeof (LWLock * ));
208
206
209
207
if (nlsns > 0 )
210
208
{
211
209
shared -> group_lsn = (XLogRecPtr * ) (ptr + offset );
212
210
offset += MAXALIGN (nslots * nlsns * sizeof (XLogRecPtr ));
213
211
}
214
212
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
+
215
223
ptr += BUFFERALIGN (offset );
216
224
for (slotno = 0 ; slotno < nslots ; slotno ++ )
217
225
{
226
+ LWLockInitialize (& shared -> buffer_locks [slotno ].lock ,
227
+ shared -> lwlock_tranche_id );
228
+
218
229
shared -> page_buffer [slotno ] = ptr ;
219
230
shared -> page_status [slotno ] = SLRU_PAGE_EMPTY ;
220
231
shared -> page_dirty [slotno ] = false;
221
232
shared -> page_lru_count [slotno ] = 0 ;
222
- shared -> buffer_locks [slotno ] = LWLockAssign ();
223
233
ptr += BLCKSZ ;
224
234
}
225
235
}
226
236
else
227
237
Assert (found );
228
238
239
+ /* Register SLRU tranche in the main tranches array */
240
+ LWLockRegisterTranche (shared -> lwlock_tranche_id , & shared -> lwlock_tranche );
241
+
229
242
/*
230
243
* Initialize the unshared control struct, including directory path. We
231
244
* assume caller set PagePrecedes.
@@ -308,8 +321,8 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
308
321
309
322
/* See notes at top of file */
310
323
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 );
313
326
LWLockAcquire (shared -> ControlLock , LW_EXCLUSIVE );
314
327
315
328
/*
@@ -323,7 +336,7 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
323
336
if (shared -> page_status [slotno ] == SLRU_PAGE_READ_IN_PROGRESS ||
324
337
shared -> page_status [slotno ] == SLRU_PAGE_WRITE_IN_PROGRESS )
325
338
{
326
- if (LWLockConditionalAcquire (shared -> buffer_locks [slotno ], LW_SHARED ))
339
+ if (LWLockConditionalAcquire (& shared -> buffer_locks [slotno ]. lock , LW_SHARED ))
327
340
{
328
341
/* indeed, the I/O must have failed */
329
342
if (shared -> page_status [slotno ] == SLRU_PAGE_READ_IN_PROGRESS )
@@ -333,7 +346,7 @@ SimpleLruWaitIO(SlruCtl ctl, int slotno)
333
346
shared -> page_status [slotno ] = SLRU_PAGE_VALID ;
334
347
shared -> page_dirty [slotno ] = true;
335
348
}
336
- LWLockRelease (shared -> buffer_locks [slotno ]);
349
+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
337
350
}
338
351
}
339
352
}
@@ -402,7 +415,7 @@ SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
402
415
shared -> page_dirty [slotno ] = false;
403
416
404
417
/* 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 );
406
419
407
420
/* Release control lock while doing I/O */
408
421
LWLockRelease (shared -> ControlLock );
@@ -422,7 +435,7 @@ SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
422
435
423
436
shared -> page_status [slotno ] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY ;
424
437
425
- LWLockRelease (shared -> buffer_locks [slotno ]);
438
+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
426
439
427
440
/* Now it's okay to ereport if we failed */
428
441
if (!ok )
@@ -518,7 +531,7 @@ SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
518
531
shared -> page_dirty [slotno ] = false;
519
532
520
533
/* 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 );
522
535
523
536
/* Release control lock while doing I/O */
524
537
LWLockRelease (shared -> ControlLock );
@@ -547,7 +560,7 @@ SlruInternalWritePage(SlruCtl ctl, int slotno, SlruFlush fdata)
547
560
548
561
shared -> page_status [slotno ] = SLRU_PAGE_VALID ;
549
562
550
- LWLockRelease (shared -> buffer_locks [slotno ]);
563
+ LWLockRelease (& shared -> buffer_locks [slotno ]. lock );
551
564
552
565
/* Now it's okay to ereport if we failed */
553
566
if (!ok )
0 commit comments