9191#endif
9292
9393
94- /* We use the ShmemLock spinlock to protect LWLockAssign */
94+ /* We use the ShmemLock spinlock to protect LWLockCounter */
9595extern slock_t * ShmemLock ;
9696
9797#define LW_FLAG_HAS_WAITERS ((uint32) 1 << 30)
@@ -363,30 +363,6 @@ NumLWLocksByNamedTranches(void)
363363 return numLocks ;
364364}
365365
366- /*
367- * Compute number of LWLocks to allocate in the main array.
368- */
369- static int
370- NumLWLocks (void )
371- {
372- int numLocks ;
373-
374- /*
375- * Many users of LWLocks no longer reserve space in the main array here,
376- * but instead allocate separate tranches. The latter approach has the
377- * advantage of allowing LWLOCK_STATS and LOCK_DEBUG output to produce
378- * more useful output.
379- */
380-
381- /* Predefined LWLocks */
382- numLocks = NUM_FIXED_LWLOCKS ;
383-
384- /* Disallow named LWLocks' requests after startup */
385- lock_named_request_allowed = false;
386-
387- return numLocks ;
388- }
389-
390366/*
391367 * Compute shmem space needed for LWLocks and named tranches.
392368 */
@@ -395,15 +371,15 @@ LWLockShmemSize(void)
395371{
396372 Size size ;
397373 int i ;
398- int numLocks = NumLWLocks () ;
374+ int numLocks = NUM_FIXED_LWLOCKS ;
399375
400376 numLocks += NumLWLocksByNamedTranches ();
401377
402378 /* Space for the LWLock array. */
403379 size = mul_size (numLocks , sizeof (LWLockPadded ));
404380
405381 /* Space for dynamic allocation counter, plus room for alignment. */
406- size = add_size (size , 3 * sizeof (int ) + LWLOCK_PADDED_SIZE );
382+ size = add_size (size , sizeof (int ) + LWLOCK_PADDED_SIZE );
407383
408384 /* space for named tranches. */
409385 size = add_size (size , mul_size (NamedLWLockTrancheRequests , sizeof (NamedLWLockTranche )));
@@ -412,6 +388,9 @@ LWLockShmemSize(void)
412388 for (i = 0 ; i < NamedLWLockTrancheRequests ; i ++ )
413389 size = add_size (size , strlen (NamedLWLockTrancheRequestArray [i ].tranche_name ) + 1 );
414390
391+ /* Disallow named LWLocks' requests after startup */
392+ lock_named_request_allowed = false;
393+
415394 return size ;
416395}
417396
@@ -433,7 +412,7 @@ CreateLWLocks(void)
433412
434413 if (!IsUnderPostmaster )
435414 {
436- int numLocks = NumLWLocks () ;
415+ int numLocks = NUM_FIXED_LWLOCKS ;
437416 int numNamedLocks = NumLWLocksByNamedTranches ();
438417 Size spaceLocks = LWLockShmemSize ();
439418 LWLockPadded * lock ;
@@ -445,8 +424,8 @@ CreateLWLocks(void)
445424 /* Allocate space */
446425 ptr = (char * ) ShmemAlloc (spaceLocks );
447426
448- /* Leave room for dynamic allocation of locks and tranches */
449- ptr += 3 * sizeof (int );
427+ /* Leave room for dynamic allocation of tranches */
428+ ptr += sizeof (int );
450429
451430 /* Ensure desired alignment of LWLock array */
452431 ptr += LWLOCK_PADDED_SIZE - ((uintptr_t ) ptr ) % LWLOCK_PADDED_SIZE ;
@@ -458,16 +437,11 @@ CreateLWLocks(void)
458437 LWLockInitialize (& lock -> lock , LWTRANCHE_MAIN );
459438
460439 /*
461- * Initialize the dynamic-allocation counters, which are stored just
462- * before the first LWLock. LWLockCounter[0] is the allocation
463- * counter for lwlocks, LWLockCounter[1] is the maximum number that
464- * can be allocated from the main array, and LWLockCounter[2] is the
465- * allocation counter for tranches.
440+ * Initialize the dynamic-allocation counter for tranches, which is
441+ * stored just before the first LWLock.
466442 */
467- LWLockCounter = (int * ) ((char * ) MainLWLockArray - 3 * sizeof (int ));
468- LWLockCounter [0 ] = NUM_FIXED_LWLOCKS ;
469- LWLockCounter [1 ] = numLocks ;
470- LWLockCounter [2 ] = LWTRANCHE_FIRST_USER_DEFINED ;
443+ LWLockCounter = (int * ) ((char * ) MainLWLockArray - sizeof (int ));
444+ * LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED ;
471445
472446 /* Initialize named tranches. */
473447 if (NamedLWLockTrancheRequests > 0 )
@@ -535,32 +509,6 @@ InitLWLockAccess(void)
535509#endif
536510}
537511
538- /*
539- * LWLockAssign - assign a dynamically-allocated LWLock number
540- *
541- * We interlock this using the same spinlock that is used to protect
542- * ShmemAlloc(). Interlocking is not really necessary during postmaster
543- * startup, but it is needed if any user-defined code tries to allocate
544- * LWLocks after startup.
545- */
546- LWLock *
547- LWLockAssign (void )
548- {
549- LWLock * result ;
550- int * LWLockCounter ;
551-
552- LWLockCounter = (int * ) ((char * ) MainLWLockArray - 3 * sizeof (int ));
553- SpinLockAcquire (ShmemLock );
554- if (LWLockCounter [0 ] >= LWLockCounter [1 ])
555- {
556- SpinLockRelease (ShmemLock );
557- elog (ERROR , "no more LWLocks available" );
558- }
559- result = & MainLWLockArray [LWLockCounter [0 ]++ ].lock ;
560- SpinLockRelease (ShmemLock );
561- return result ;
562- }
563-
564512/*
565513 * GetNamedLWLockTranche - returns the base address of LWLock from the
566514 * specified tranche.
@@ -574,16 +522,13 @@ GetNamedLWLockTranche(const char *tranche_name)
574522{
575523 int lock_pos ;
576524 int i ;
577- int * LWLockCounter ;
578-
579- LWLockCounter = (int * ) ((char * ) MainLWLockArray - 3 * sizeof (int ));
580525
581526 /*
582527 * Obtain the position of base address of LWLock belonging to requested
583528 * tranche_name in MainLWLockArray. LWLocks for named tranches are placed
584- * in MainLWLockArray after LWLocks specified by LWLockCounter[1] .
529+ * in MainLWLockArray after fixed locks .
585530 */
586- lock_pos = LWLockCounter [ 1 ] ;
531+ lock_pos = NUM_FIXED_LWLOCKS ;
587532 for (i = 0 ; i < NamedLWLockTrancheRequests ; i ++ )
588533 {
589534 if (strcmp (NamedLWLockTrancheRequestArray [i ].tranche_name ,
@@ -609,9 +554,9 @@ LWLockNewTrancheId(void)
609554 int result ;
610555 int * LWLockCounter ;
611556
612- LWLockCounter = (int * ) ((char * ) MainLWLockArray - 3 * sizeof (int ));
557+ LWLockCounter = (int * ) ((char * ) MainLWLockArray - sizeof (int ));
613558 SpinLockAcquire (ShmemLock );
614- result = LWLockCounter [ 2 ] ++ ;
559+ result = ( * LWLockCounter ) ++ ;
615560 SpinLockRelease (ShmemLock );
616561
617562 return result ;
0 commit comments