mirror of
https://github.com/postgres/postgres.git
synced 2025-11-18 02:02:55 +03:00
Make LWLockCounter a global variable.
Using the LWLockCounter requires first calculating its address in
shared memory like this:
LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
Commit 82e861fbe1 started this trend in order to fix EXEC_BACKEND
builds, but it could also be fixed by adding it to the
BackendParameters struct. The current approach is somewhat
difficult to follow, so this commit switches to the latter. While
at it, swap around the code in LWLockShmemSize() to match the order
of assignments in CreateLWLocks() for added readability.
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Discussion: https://postgr.es/m/aLDLnan9gNCS9fHx%40nathan
This commit is contained in:
@@ -102,6 +102,7 @@ typedef struct
|
||||
#endif
|
||||
int NamedLWLockTrancheRequests;
|
||||
NamedLWLockTranche *NamedLWLockTrancheArray;
|
||||
int *LWLockCounter;
|
||||
LWLockPadded *MainLWLockArray;
|
||||
slock_t *ProcStructLock;
|
||||
PROC_HDR *ProcGlobal;
|
||||
@@ -761,6 +762,7 @@ save_backend_variables(BackendParameters *param,
|
||||
|
||||
param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
|
||||
param->NamedLWLockTrancheArray = NamedLWLockTrancheArray;
|
||||
param->LWLockCounter = LWLockCounter;
|
||||
param->MainLWLockArray = MainLWLockArray;
|
||||
param->ProcStructLock = ProcStructLock;
|
||||
param->ProcGlobal = ProcGlobal;
|
||||
@@ -1021,6 +1023,7 @@ restore_backend_variables(BackendParameters *param)
|
||||
|
||||
NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
|
||||
NamedLWLockTrancheArray = param->NamedLWLockTrancheArray;
|
||||
LWLockCounter = param->LWLockCounter;
|
||||
MainLWLockArray = param->MainLWLockArray;
|
||||
ProcStructLock = param->ProcStructLock;
|
||||
ProcGlobal = param->ProcGlobal;
|
||||
|
||||
@@ -196,6 +196,7 @@ int NamedLWLockTrancheRequests = 0;
|
||||
|
||||
/* points to data in shared memory: */
|
||||
NamedLWLockTranche *NamedLWLockTrancheArray = NULL;
|
||||
int *LWLockCounter = NULL;
|
||||
|
||||
static void InitializeLWLocks(void);
|
||||
static inline void LWLockReportWaitStart(LWLock *lock);
|
||||
@@ -397,11 +398,11 @@ LWLockShmemSize(void)
|
||||
/* Calculate total number of locks needed in the main array. */
|
||||
numLocks += NumLWLocksForNamedTranches();
|
||||
|
||||
/* Space for the LWLock array. */
|
||||
size = mul_size(numLocks, sizeof(LWLockPadded));
|
||||
|
||||
/* Space for dynamic allocation counter, plus room for alignment. */
|
||||
size = add_size(size, sizeof(int) + LWLOCK_PADDED_SIZE);
|
||||
size = sizeof(int) + LWLOCK_PADDED_SIZE;
|
||||
|
||||
/* Space for the LWLock array. */
|
||||
size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded)));
|
||||
|
||||
/* space for named tranches. */
|
||||
size = add_size(size, mul_size(NamedLWLockTrancheRequests, sizeof(NamedLWLockTranche)));
|
||||
@@ -423,27 +424,20 @@ CreateLWLocks(void)
|
||||
if (!IsUnderPostmaster)
|
||||
{
|
||||
Size spaceLocks = LWLockShmemSize();
|
||||
int *LWLockCounter;
|
||||
char *ptr;
|
||||
|
||||
/* Allocate space */
|
||||
ptr = (char *) ShmemAlloc(spaceLocks);
|
||||
|
||||
/* Leave room for dynamic allocation of tranches */
|
||||
/* Initialize the dynamic-allocation counter for tranches */
|
||||
LWLockCounter = (int *) ptr;
|
||||
*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
|
||||
ptr += sizeof(int);
|
||||
|
||||
/* Ensure desired alignment of LWLock array */
|
||||
ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
|
||||
|
||||
MainLWLockArray = (LWLockPadded *) ptr;
|
||||
|
||||
/*
|
||||
* Initialize the dynamic-allocation counter for tranches, which is
|
||||
* stored just before the first LWLock.
|
||||
*/
|
||||
LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
|
||||
*LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
|
||||
|
||||
/* Initialize all LWLocks */
|
||||
InitializeLWLocks();
|
||||
}
|
||||
@@ -574,9 +568,7 @@ int
|
||||
LWLockNewTrancheId(void)
|
||||
{
|
||||
int result;
|
||||
int *LWLockCounter;
|
||||
|
||||
LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
|
||||
/* We use the ShmemLock spinlock to protect LWLockCounter */
|
||||
SpinLockAcquire(ShmemLock);
|
||||
result = (*LWLockCounter)++;
|
||||
|
||||
Reference in New Issue
Block a user