Code cleanup in the wake of recent LWLock refactoring.

As of commit c1772ad922, there's no
longer any way of requesting additional LWLocks in the main tranche,
so we don't need NumLWLocks() or LWLockAssign() any more.  Also,
some of the allocation counters that we had previously aren't needed
any more either.

Amit Kapila
This commit is contained in:
Robert Haas 2016-02-10 09:58:09 -05:00
parent 019e788137
commit 79a7ff0fe5
2 changed files with 17 additions and 74 deletions

View File

@ -91,7 +91,7 @@
#endif #endif
/* We use the ShmemLock spinlock to protect LWLockAssign */ /* We use the ShmemLock spinlock to protect LWLockCounter */
extern slock_t *ShmemLock; extern slock_t *ShmemLock;
#define LW_FLAG_HAS_WAITERS ((uint32) 1 << 30) #define LW_FLAG_HAS_WAITERS ((uint32) 1 << 30)
@ -363,30 +363,6 @@ NumLWLocksByNamedTranches(void)
return numLocks; return numLocks;
} }
/*
* Compute number of LWLocks to allocate in the main array.
*/
static int
NumLWLocks(void)
{
int numLocks;
/*
* Many users of LWLocks no longer reserve space in the main array here,
* but instead allocate separate tranches. The latter approach has the
* advantage of allowing LWLOCK_STATS and LOCK_DEBUG output to produce
* more useful output.
*/
/* Predefined LWLocks */
numLocks = NUM_FIXED_LWLOCKS;
/* Disallow named LWLocks' requests after startup */
lock_named_request_allowed = false;
return numLocks;
}
/* /*
* Compute shmem space needed for LWLocks and named tranches. * Compute shmem space needed for LWLocks and named tranches.
*/ */
@ -395,7 +371,7 @@ LWLockShmemSize(void)
{ {
Size size; Size size;
int i; int i;
int numLocks = NumLWLocks(); int numLocks = NUM_FIXED_LWLOCKS;
numLocks += NumLWLocksByNamedTranches(); numLocks += NumLWLocksByNamedTranches();
@ -403,7 +379,7 @@ LWLockShmemSize(void)
size = mul_size(numLocks, sizeof(LWLockPadded)); size = mul_size(numLocks, sizeof(LWLockPadded));
/* Space for dynamic allocation counter, plus room for alignment. */ /* Space for dynamic allocation counter, plus room for alignment. */
size = add_size(size, 3 * sizeof(int) + LWLOCK_PADDED_SIZE); size = add_size(size, sizeof(int) + LWLOCK_PADDED_SIZE);
/* space for named tranches. */ /* space for named tranches. */
size = add_size(size, mul_size(NamedLWLockTrancheRequests, sizeof(NamedLWLockTranche))); size = add_size(size, mul_size(NamedLWLockTrancheRequests, sizeof(NamedLWLockTranche)));
@ -412,6 +388,9 @@ LWLockShmemSize(void)
for (i = 0; i < NamedLWLockTrancheRequests; i++) for (i = 0; i < NamedLWLockTrancheRequests; i++)
size = add_size(size, strlen(NamedLWLockTrancheRequestArray[i].tranche_name) + 1); size = add_size(size, strlen(NamedLWLockTrancheRequestArray[i].tranche_name) + 1);
/* Disallow named LWLocks' requests after startup */
lock_named_request_allowed = false;
return size; return size;
} }
@ -433,7 +412,7 @@ CreateLWLocks(void)
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
int numLocks = NumLWLocks(); int numLocks = NUM_FIXED_LWLOCKS;
int numNamedLocks = NumLWLocksByNamedTranches(); int numNamedLocks = NumLWLocksByNamedTranches();
Size spaceLocks = LWLockShmemSize(); Size spaceLocks = LWLockShmemSize();
LWLockPadded *lock; LWLockPadded *lock;
@ -445,8 +424,8 @@ CreateLWLocks(void)
/* Allocate space */ /* Allocate space */
ptr = (char *) ShmemAlloc(spaceLocks); ptr = (char *) ShmemAlloc(spaceLocks);
/* Leave room for dynamic allocation of locks and tranches */ /* Leave room for dynamic allocation of tranches */
ptr += 3 * sizeof(int); ptr += sizeof(int);
/* Ensure desired alignment of LWLock array */ /* Ensure desired alignment of LWLock array */
ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE; ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
@ -458,16 +437,11 @@ CreateLWLocks(void)
LWLockInitialize(&lock->lock, LWTRANCHE_MAIN); LWLockInitialize(&lock->lock, LWTRANCHE_MAIN);
/* /*
* Initialize the dynamic-allocation counters, which are stored just * Initialize the dynamic-allocation counter for tranches, which is
* before the first LWLock. LWLockCounter[0] is the allocation * stored just before the first LWLock.
* counter for lwlocks, LWLockCounter[1] is the maximum number that
* can be allocated from the main array, and LWLockCounter[2] is the
* allocation counter for tranches.
*/ */
LWLockCounter = (int *) ((char *) MainLWLockArray - 3 * sizeof(int)); LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
LWLockCounter[0] = NUM_FIXED_LWLOCKS; *LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
LWLockCounter[1] = numLocks;
LWLockCounter[2] = LWTRANCHE_FIRST_USER_DEFINED;
/* Initialize named tranches. */ /* Initialize named tranches. */
if (NamedLWLockTrancheRequests > 0) if (NamedLWLockTrancheRequests > 0)
@ -535,32 +509,6 @@ InitLWLockAccess(void)
#endif #endif
} }
/*
* LWLockAssign - assign a dynamically-allocated LWLock number
*
* We interlock this using the same spinlock that is used to protect
* ShmemAlloc(). Interlocking is not really necessary during postmaster
* startup, but it is needed if any user-defined code tries to allocate
* LWLocks after startup.
*/
LWLock *
LWLockAssign(void)
{
LWLock *result;
int *LWLockCounter;
LWLockCounter = (int *) ((char *) MainLWLockArray - 3 * sizeof(int));
SpinLockAcquire(ShmemLock);
if (LWLockCounter[0] >= LWLockCounter[1])
{
SpinLockRelease(ShmemLock);
elog(ERROR, "no more LWLocks available");
}
result = &MainLWLockArray[LWLockCounter[0]++].lock;
SpinLockRelease(ShmemLock);
return result;
}
/* /*
* GetNamedLWLockTranche - returns the base address of LWLock from the * GetNamedLWLockTranche - returns the base address of LWLock from the
* specified tranche. * specified tranche.
@ -574,16 +522,13 @@ GetNamedLWLockTranche(const char *tranche_name)
{ {
int lock_pos; int lock_pos;
int i; int i;
int *LWLockCounter;
LWLockCounter = (int *) ((char *) MainLWLockArray - 3 * sizeof(int));
/* /*
* Obtain the position of base address of LWLock belonging to requested * Obtain the position of base address of LWLock belonging to requested
* tranche_name in MainLWLockArray. LWLocks for named tranches are placed * tranche_name in MainLWLockArray. LWLocks for named tranches are placed
* in MainLWLockArray after LWLocks specified by LWLockCounter[1]. * in MainLWLockArray after fixed locks.
*/ */
lock_pos = LWLockCounter[1]; lock_pos = NUM_FIXED_LWLOCKS;
for (i = 0; i < NamedLWLockTrancheRequests; i++) for (i = 0; i < NamedLWLockTrancheRequests; i++)
{ {
if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name, if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name,
@ -609,9 +554,9 @@ LWLockNewTrancheId(void)
int result; int result;
int *LWLockCounter; int *LWLockCounter;
LWLockCounter = (int *) ((char *) MainLWLockArray - 3 * sizeof(int)); LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
SpinLockAcquire(ShmemLock); SpinLockAcquire(ShmemLock);
result = LWLockCounter[2]++; result = (*LWLockCounter)++;
SpinLockRelease(ShmemLock); SpinLockRelease(ShmemLock);
return result; return result;

View File

@ -196,8 +196,6 @@ extern void InitLWLockAccess(void);
extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks); extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name); extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);
extern LWLock *LWLockAssign(void);
/* /*
* There is another, more flexible method of obtaining lwlocks. First, call * There is another, more flexible method of obtaining lwlocks. First, call
* LWLockNewTrancheId just once to obtain a tranche ID; this allocates from * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from