91
91
#endif
92
92
93
93
94
- /* We use the ShmemLock spinlock to protect LWLockAssign */
94
+ /* We use the ShmemLock spinlock to protect LWLockCounter */
95
95
extern slock_t * ShmemLock ;
96
96
97
97
#define LW_FLAG_HAS_WAITERS ((uint32) 1 << 30)
@@ -363,30 +363,6 @@ NumLWLocksByNamedTranches(void)
363
363
return numLocks ;
364
364
}
365
365
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
-
390
366
/*
391
367
* Compute shmem space needed for LWLocks and named tranches.
392
368
*/
@@ -395,15 +371,15 @@ LWLockShmemSize(void)
395
371
{
396
372
Size size ;
397
373
int i ;
398
- int numLocks = NumLWLocks () ;
374
+ int numLocks = NUM_FIXED_LWLOCKS ;
399
375
400
376
numLocks += NumLWLocksByNamedTranches ();
401
377
402
378
/* Space for the LWLock array. */
403
379
size = mul_size (numLocks , sizeof (LWLockPadded ));
404
380
405
381
/* 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 );
407
383
408
384
/* space for named tranches. */
409
385
size = add_size (size , mul_size (NamedLWLockTrancheRequests , sizeof (NamedLWLockTranche )));
@@ -412,6 +388,9 @@ LWLockShmemSize(void)
412
388
for (i = 0 ; i < NamedLWLockTrancheRequests ; i ++ )
413
389
size = add_size (size , strlen (NamedLWLockTrancheRequestArray [i ].tranche_name ) + 1 );
414
390
391
+ /* Disallow named LWLocks' requests after startup */
392
+ lock_named_request_allowed = false;
393
+
415
394
return size ;
416
395
}
417
396
@@ -433,7 +412,7 @@ CreateLWLocks(void)
433
412
434
413
if (!IsUnderPostmaster )
435
414
{
436
- int numLocks = NumLWLocks () ;
415
+ int numLocks = NUM_FIXED_LWLOCKS ;
437
416
int numNamedLocks = NumLWLocksByNamedTranches ();
438
417
Size spaceLocks = LWLockShmemSize ();
439
418
LWLockPadded * lock ;
@@ -445,8 +424,8 @@ CreateLWLocks(void)
445
424
/* Allocate space */
446
425
ptr = (char * ) ShmemAlloc (spaceLocks );
447
426
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 );
450
429
451
430
/* Ensure desired alignment of LWLock array */
452
431
ptr += LWLOCK_PADDED_SIZE - ((uintptr_t ) ptr ) % LWLOCK_PADDED_SIZE ;
@@ -458,16 +437,11 @@ CreateLWLocks(void)
458
437
LWLockInitialize (& lock -> lock , LWTRANCHE_MAIN );
459
438
460
439
/*
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.
466
442
*/
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 ;
471
445
472
446
/* Initialize named tranches. */
473
447
if (NamedLWLockTrancheRequests > 0 )
@@ -535,32 +509,6 @@ InitLWLockAccess(void)
535
509
#endif
536
510
}
537
511
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
-
564
512
/*
565
513
* GetNamedLWLockTranche - returns the base address of LWLock from the
566
514
* specified tranche.
@@ -574,16 +522,13 @@ GetNamedLWLockTranche(const char *tranche_name)
574
522
{
575
523
int lock_pos ;
576
524
int i ;
577
- int * LWLockCounter ;
578
-
579
- LWLockCounter = (int * ) ((char * ) MainLWLockArray - 3 * sizeof (int ));
580
525
581
526
/*
582
527
* Obtain the position of base address of LWLock belonging to requested
583
528
* 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 .
585
530
*/
586
- lock_pos = LWLockCounter [ 1 ] ;
531
+ lock_pos = NUM_FIXED_LWLOCKS ;
587
532
for (i = 0 ; i < NamedLWLockTrancheRequests ; i ++ )
588
533
{
589
534
if (strcmp (NamedLWLockTrancheRequestArray [i ].tranche_name ,
@@ -609,9 +554,9 @@ LWLockNewTrancheId(void)
609
554
int result ;
610
555
int * LWLockCounter ;
611
556
612
- LWLockCounter = (int * ) ((char * ) MainLWLockArray - 3 * sizeof (int ));
557
+ LWLockCounter = (int * ) ((char * ) MainLWLockArray - sizeof (int ));
613
558
SpinLockAcquire (ShmemLock );
614
- result = LWLockCounter [ 2 ] ++ ;
559
+ result = ( * LWLockCounter ) ++ ;
615
560
SpinLockRelease (ShmemLock );
616
561
617
562
return result ;
0 commit comments