Skip to content

Commit acb9d5d

Browse files
author
Commitfest Bot
committed
[CF 6009] v2 - make LWLockCounter a global variable
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6009 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/aLG8jlpEZAseM-wk@nathan Author(s): Nathan Bossart
2 parents da9f9f7 + 0baaf85 commit acb9d5d

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/backend/postmaster/launch_backend.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ typedef struct
102102
#endif
103103
int NamedLWLockTrancheRequests;
104104
NamedLWLockTranche *NamedLWLockTrancheArray;
105+
int *LWLockCounter;
105106
LWLockPadded *MainLWLockArray;
106107
slock_t *ProcStructLock;
107108
PROC_HDR *ProcGlobal;
@@ -761,6 +762,7 @@ save_backend_variables(BackendParameters *param,
761762

762763
param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
763764
param->NamedLWLockTrancheArray = NamedLWLockTrancheArray;
765+
param->LWLockCounter = LWLockCounter;
764766
param->MainLWLockArray = MainLWLockArray;
765767
param->ProcStructLock = ProcStructLock;
766768
param->ProcGlobal = ProcGlobal;
@@ -1021,6 +1023,7 @@ restore_backend_variables(BackendParameters *param)
10211023

10221024
NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
10231025
NamedLWLockTrancheArray = param->NamedLWLockTrancheArray;
1026+
LWLockCounter = param->LWLockCounter;
10241027
MainLWLockArray = param->MainLWLockArray;
10251028
ProcStructLock = param->ProcStructLock;
10261029
ProcGlobal = param->ProcGlobal;

src/backend/storage/lmgr/lwlock.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ int NamedLWLockTrancheRequests = 0;
196196

197197
/* points to data in shared memory: */
198198
NamedLWLockTranche *NamedLWLockTrancheArray = NULL;
199+
int *LWLockCounter = NULL;
199200

200201
static void InitializeLWLocks(void);
201202
static inline void LWLockReportWaitStart(LWLock *lock);
@@ -397,11 +398,11 @@ LWLockShmemSize(void)
397398
/* Calculate total number of locks needed in the main array. */
398399
numLocks += NumLWLocksForNamedTranches();
399400

400-
/* Space for the LWLock array. */
401-
size = mul_size(numLocks, sizeof(LWLockPadded));
402-
403401
/* Space for dynamic allocation counter, plus room for alignment. */
404-
size = add_size(size, sizeof(int) + LWLOCK_PADDED_SIZE);
402+
size = sizeof(int) + LWLOCK_PADDED_SIZE;
403+
404+
/* Space for the LWLock array. */
405+
size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded)));
405406

406407
/* space for named tranches. */
407408
size = add_size(size, mul_size(NamedLWLockTrancheRequests, sizeof(NamedLWLockTranche)));
@@ -423,27 +424,20 @@ CreateLWLocks(void)
423424
if (!IsUnderPostmaster)
424425
{
425426
Size spaceLocks = LWLockShmemSize();
426-
int *LWLockCounter;
427427
char *ptr;
428428

429429
/* Allocate space */
430430
ptr = (char *) ShmemAlloc(spaceLocks);
431431

432-
/* Leave room for dynamic allocation of tranches */
432+
/* Initialize the dynamic-allocation counter for tranches */
433+
LWLockCounter = (int *) ptr;
434+
*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
433435
ptr += sizeof(int);
434436

435437
/* Ensure desired alignment of LWLock array */
436438
ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
437-
438439
MainLWLockArray = (LWLockPadded *) ptr;
439440

440-
/*
441-
* Initialize the dynamic-allocation counter for tranches, which is
442-
* stored just before the first LWLock.
443-
*/
444-
LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
445-
*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
446-
447441
/* Initialize all LWLocks */
448442
InitializeLWLocks();
449443
}
@@ -574,9 +568,7 @@ int
574568
LWLockNewTrancheId(void)
575569
{
576570
int result;
577-
int *LWLockCounter;
578571

579-
LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
580572
/* We use the ShmemLock spinlock to protect LWLockCounter */
581573
SpinLockAcquire(ShmemLock);
582574
result = (*LWLockCounter)++;

src/include/storage/lwlock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typedef struct NamedLWLockTranche
8282

8383
extern PGDLLIMPORT NamedLWLockTranche *NamedLWLockTrancheArray;
8484
extern PGDLLIMPORT int NamedLWLockTrancheRequests;
85+
extern PGDLLIMPORT int *LWLockCounter;
8586

8687
/*
8788
* It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS

0 commit comments

Comments
 (0)