PostgreSQL Source Code git master
Loading...
Searching...
No Matches
lwlock.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "storage/proc.h"
#include "storage/proclist.h"
#include "storage/procnumber.h"
#include "storage/spin.h"
#include "utils/memutils.h"
#include "utils/wait_event.h"
#include "storage/lwlocklist.h"
Include dependency graph for lwlock.c:

Go to the source code of this file.

Data Structures

struct  LWLockHandle
 
struct  NamedLWLockTrancheRequest
 

Macros

#define LW_FLAG_HAS_WAITERS   ((uint32) 1 << 31)
 
#define LW_FLAG_WAKE_IN_PROGRESS   ((uint32) 1 << 30)
 
#define LW_FLAG_LOCKED   ((uint32) 1 << 29)
 
#define LW_FLAG_BITS   3
 
#define LW_FLAG_MASK   (((1<<LW_FLAG_BITS)-1)<<(32-LW_FLAG_BITS))
 
#define LW_VAL_EXCLUSIVE   (MAX_BACKENDS + 1)
 
#define LW_VAL_SHARED   1
 
#define LW_SHARED_MASK   MAX_BACKENDS
 
#define LW_LOCK_MASK   (MAX_BACKENDS | LW_VAL_EXCLUSIVE)
 
#define PG_LWLOCK(id, lockname)   [id] = CppAsString(lockname),
 
#define PG_LWLOCKTRANCHE(id, lockname)   [LWTRANCHE_##id] = CppAsString(lockname),
 
#define MAX_SIMUL_LWLOCKS   200
 
#define MAX_NAMED_TRANCHES   256
 
#define T_NAME(lock)    GetLWTrancheName((lock)->tranche)
 
#define PRINT_LWDEBUG(a, b, c)   ((void)0)
 
#define LOG_LWDEBUG(a, b, c)   ((void)0)
 

Typedefs

typedef struct LWLockHandle LWLockHandle
 
typedef struct NamedLWLockTrancheRequest NamedLWLockTrancheRequest
 

Functions

 StaticAssertDecl (((MAX_BACKENDS+1) &MAX_BACKENDS)==0, "MAX_BACKENDS + 1 needs to be a power of 2")
 
 StaticAssertDecl ((MAX_BACKENDS &LW_FLAG_MASK)==0, "MAX_BACKENDS and LW_FLAG_MASK overlap")
 
 StaticAssertDecl ((LW_VAL_EXCLUSIVE &LW_FLAG_MASK)==0, "LW_VAL_EXCLUSIVE and LW_FLAG_MASK overlap")
 
 StaticAssertDecl (lengthof(BuiltinTrancheNames)==LWTRANCHE_FIRST_USER_DEFINED, "missing entries in BuiltinTrancheNames[]")
 
static void InitializeLWLocks (void)
 
static void LWLockReportWaitStart (LWLock *lock)
 
static void LWLockReportWaitEnd (void)
 
static const charGetLWTrancheName (uint16 trancheId)
 
static int NumLWLocksForNamedTranches (void)
 
Size LWLockShmemSize (void)
 
void CreateLWLocks (void)
 
void InitLWLockAccess (void)
 
LWLockPaddedGetNamedLWLockTranche (const char *tranche_name)
 
int LWLockNewTrancheId (const char *name)
 
void RequestNamedLWLockTranche (const char *tranche_name, int num_lwlocks)
 
void LWLockInitialize (LWLock *lock, int tranche_id)
 
const charGetLWLockIdentifier (uint32 classId, uint16 eventId)
 
static bool LWLockAttemptLock (LWLock *lock, LWLockMode mode)
 
static void LWLockWaitListLock (LWLock *lock)
 
static void LWLockWaitListUnlock (LWLock *lock)
 
static void LWLockWakeup (LWLock *lock)
 
static void LWLockQueueSelf (LWLock *lock, LWLockMode mode)
 
static void LWLockDequeueSelf (LWLock *lock)
 
bool LWLockAcquire (LWLock *lock, LWLockMode mode)
 
bool LWLockConditionalAcquire (LWLock *lock, LWLockMode mode)
 
bool LWLockAcquireOrWait (LWLock *lock, LWLockMode mode)
 
static bool LWLockConflictsWithVar (LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval, bool *result)
 
bool LWLockWaitForVar (LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
 
void LWLockUpdateVar (LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
 
void LWLockRelease (LWLock *lock)
 
void LWLockReleaseClearVar (LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
 
void LWLockReleaseAll (void)
 
bool LWLockHeldByMe (LWLock *lock)
 
bool LWLockAnyHeldByMe (LWLock *lock, int nlocks, size_t stride)
 
bool LWLockHeldByMeInMode (LWLock *lock, LWLockMode mode)
 

Variables

static const char *const BuiltinTrancheNames []
 
char ** LWLockTrancheNames = NULL
 
LWLockPaddedMainLWLockArray = NULL
 
static int num_held_lwlocks = 0
 
static LWLockHandle held_lwlocks [MAX_SIMUL_LWLOCKS]
 
int NamedLWLockTrancheRequests = 0
 
NamedLWLockTrancheRequestNamedLWLockTrancheRequestArray = NULL
 
static NamedLWLockTrancheRequestLocalNamedLWLockTrancheRequestArray = NULL
 
intLWLockCounter = NULL
 
static int LocalLWLockCounter
 

Macro Definition Documentation

◆ LOG_LWDEBUG

#define LOG_LWDEBUG (   a,
  b,
  c 
)    ((void)0)

Definition at line 277 of file lwlock.c.

◆ LW_FLAG_BITS

#define LW_FLAG_BITS   3

Definition at line 98 of file lwlock.c.

◆ LW_FLAG_HAS_WAITERS

#define LW_FLAG_HAS_WAITERS   ((uint32) 1 << 31)

Definition at line 95 of file lwlock.c.

◆ LW_FLAG_LOCKED

#define LW_FLAG_LOCKED   ((uint32) 1 << 29)

Definition at line 97 of file lwlock.c.

◆ LW_FLAG_MASK

#define LW_FLAG_MASK   (((1<<LW_FLAG_BITS)-1)<<(32-LW_FLAG_BITS))

Definition at line 99 of file lwlock.c.

◆ LW_FLAG_WAKE_IN_PROGRESS

#define LW_FLAG_WAKE_IN_PROGRESS   ((uint32) 1 << 30)

Definition at line 96 of file lwlock.c.

◆ LW_LOCK_MASK

#define LW_LOCK_MASK   (MAX_BACKENDS | LW_VAL_EXCLUSIVE)

Definition at line 107 of file lwlock.c.

◆ LW_SHARED_MASK

#define LW_SHARED_MASK   MAX_BACKENDS

Definition at line 106 of file lwlock.c.

◆ LW_VAL_EXCLUSIVE

#define LW_VAL_EXCLUSIVE   (MAX_BACKENDS + 1)

Definition at line 102 of file lwlock.c.

◆ LW_VAL_SHARED

#define LW_VAL_SHARED   1

Definition at line 103 of file lwlock.c.

◆ MAX_NAMED_TRANCHES

#define MAX_NAMED_TRANCHES   256

Definition at line 205 of file lwlock.c.

◆ MAX_SIMUL_LWLOCKS

#define MAX_SIMUL_LWLOCKS   200

Definition at line 169 of file lwlock.c.

◆ PG_LWLOCK

#define PG_LWLOCK (   id,
  lockname 
)    [id] = CppAsString(lockname),

◆ PG_LWLOCKTRANCHE

#define PG_LWLOCKTRANCHE (   id,
  lockname 
)    [LWTRANCHE_##id] = CppAsString(lockname),

◆ PRINT_LWDEBUG

#define PRINT_LWDEBUG (   a,
  b,
  c 
)    ((void)0)

Definition at line 276 of file lwlock.c.

◆ T_NAME

#define T_NAME (   lock)     GetLWTrancheName((lock)->tranche)

Definition at line 212 of file lwlock.c.

216{
217 int tranche;
218 void *instance;
220
221typedef struct lwlock_stats
222{
226 int block_count;
230
231static HTAB *lwlock_stats_htab;
233#endif
234
235#ifdef LOCK_DEBUG
236bool Trace_lwlocks = false;
237
238inline static void
239PRINT_LWDEBUG(const char *where, LWLock *lock, LWLockMode mode)
240{
241 /* hide statement & context here, otherwise the log is just too verbose */
242 if (Trace_lwlocks)
243 {
245
246 ereport(LOG,
247 (errhidestmt(true),
248 errhidecontext(true),
249 errmsg_internal("%d: %s(%s %p): excl %u shared %u haswaiters %u waiters %u waking %d",
250 MyProcPid,
251 where, T_NAME(lock), lock,
252 (state & LW_VAL_EXCLUSIVE) != 0,
254 (state & LW_FLAG_HAS_WAITERS) != 0,
255 pg_atomic_read_u32(&lock->nwaiters),
257 }
258}
259
260inline static void
261LOG_LWDEBUG(const char *where, LWLock *lock, const char *msg)
262{
263 /* hide statement & context here, otherwise the log is just too verbose */
264 if (Trace_lwlocks)
265 {
266 ereport(LOG,
267 (errhidestmt(true),
268 errhidecontext(true),
269 errmsg_internal("%s(%s %p): %s", where,
270 T_NAME(lock), lock, msg)));
271 }
272}
273
274#else /* not LOCK_DEBUG */
275#define PRINT_LWDEBUG(a,b,c) ((void)0)
276#define LOG_LWDEBUG(a,b,c) ((void)0)
277#endif /* LOCK_DEBUG */
278
279#ifdef LWLOCK_STATS
280
281static void init_lwlock_stats(void);
282static void print_lwlock_stats(int code, Datum arg);
284
285static void
287{
288 HASHCTL ctl;
290 static bool exit_registered = false;
291
292 if (lwlock_stats_cxt != NULL)
294
295 /*
296 * The LWLock stats will be updated within a critical section, which
297 * requires allocating new hash entries. Allocations within a critical
298 * section are normally not allowed because running out of memory would
299 * lead to a PANIC, but LWLOCK_STATS is debugging code that's not normally
300 * turned on in production, so that's an acceptable risk. The hash entries
301 * are small, so the risk of running out of memory is minimal in practice.
302 */
304 "LWLock stats",
307
308 ctl.keysize = sizeof(lwlock_stats_key);
309 ctl.entrysize = sizeof(lwlock_stats);
310 ctl.hcxt = lwlock_stats_cxt;
311 lwlock_stats_htab = hash_create("lwlock stats", 16384, &ctl,
313 if (!exit_registered)
314 {
316 exit_registered = true;
317 }
318}
319
320static void
322{
323 HASH_SEQ_STATUS scan;
325
327
328 /* Grab an LWLock to keep different backends from mixing reports */
330
331 while ((lwstats = (lwlock_stats *) hash_seq_search(&scan)) != NULL)
332 {
334 "PID %d lwlock %s %p: shacq %u exacq %u blk %u spindelay %u dequeue self %u\n",
335 MyProcPid, GetLWTrancheName(lwstats->key.tranche),
336 lwstats->key.instance, lwstats->sh_acquire_count,
337 lwstats->ex_acquire_count, lwstats->block_count,
338 lwstats->spin_delay_count, lwstats->dequeue_self_count);
339 }
340
342}
343
344static lwlock_stats *
346{
349 bool found;
350
351 /*
352 * During shared memory initialization, the hash table doesn't exist yet.
353 * Stats of that phase aren't very interesting, so just collect operations
354 * on all locks in a single dummy entry.
355 */
356 if (lwlock_stats_htab == NULL)
357 return &lwlock_stats_dummy;
358
359 /* Fetch or create the entry. */
360 MemSet(&key, 0, sizeof(key));
361 key.tranche = lock->tranche;
362 key.instance = lock;
364 if (!found)
365 {
366 lwstats->sh_acquire_count = 0;
367 lwstats->ex_acquire_count = 0;
368 lwstats->block_count = 0;
369 lwstats->dequeue_self_count = 0;
370 lwstats->spin_delay_count = 0;
371 }
372 return lwstats;
373}
374#endif /* LWLOCK_STATS */
375
376
377/*
378 * Compute number of LWLocks required by named tranches. These will be
379 * allocated in the main array.
380 */
381static int
383{
384 int numLocks = 0;
385 int i;
386
387 for (i = 0; i < NamedLWLockTrancheRequests; i++)
388 numLocks += NamedLWLockTrancheRequestArray[i].num_lwlocks;
389
390 return numLocks;
391}
392
393/*
394 * Compute shmem space needed for LWLocks and named tranches.
395 */
396Size
397LWLockShmemSize(void)
398{
399 Size size;
400 int numLocks = NUM_FIXED_LWLOCKS;
401
402 /*
403 * If re-initializing shared memory, the request array will no longer be
404 * accessible, so switch to the copy in postmaster's local memory. We'll
405 * copy it back into shared memory later when CreateLWLocks() is called
406 * again.
407 */
410
411 /* Calculate total number of locks needed in the main array. */
412 numLocks += NumLWLocksForNamedTranches();
413
414 /* Space for dynamic allocation counter. */
415 size = MAXALIGN(sizeof(int));
416
417 /* Space for named tranches. */
418 size = add_size(size, mul_size(MAX_NAMED_TRANCHES, sizeof(char *)));
420
421 /*
422 * Make space for named tranche requests. This is done for the benefit of
423 * EXEC_BACKEND builds, which otherwise wouldn't be able to call
424 * GetNamedLWLockTranche() outside postmaster.
425 */
428
429 /* Space for the LWLock array, plus room for cache line alignment. */
430 size = add_size(size, LWLOCK_PADDED_SIZE);
431 size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded)));
432
433 return size;
434}
435
436/*
437 * Allocate shmem space for the main LWLock array and all tranches and
438 * initialize it.
439 */
440void
441CreateLWLocks(void)
442{
444 {
446 char *ptr;
447
448 /* Allocate space */
449 ptr = (char *) ShmemAlloc(spaceLocks);
450
451 /* Initialize the dynamic-allocation counter for tranches */
452 LWLockCounter = (int *) ptr;
454 ptr += MAXALIGN(sizeof(int));
455
456 /* Initialize tranche names */
457 LWLockTrancheNames = (char **) ptr;
458 ptr += MAX_NAMED_TRANCHES * sizeof(char *);
459 for (int i = 0; i < MAX_NAMED_TRANCHES; i++)
460 {
461 LWLockTrancheNames[i] = ptr;
462 ptr += NAMEDATALEN;
463 }
464
465 /*
466 * Move named tranche requests to shared memory. This is done for the
467 * benefit of EXEC_BACKEND builds, which otherwise wouldn't be able to
468 * call GetNamedLWLockTranche() outside postmaster.
469 */
471 {
472 /*
473 * Save the pointer to the request array in postmaster's local
474 * memory. We'll need it if we ever need to re-initialize shared
475 * memory after a crash.
476 */
478
483 }
484
485 /* Ensure desired alignment of LWLock array */
488
489 /* Initialize all LWLocks */
491 }
492}
493
494/*
495 * Initialize LWLocks that are fixed and those belonging to named tranches.
496 */
497static void
499{
500 int id;
501 int i;
502 int j;
503 LWLockPadded *lock;
504
505 /* Initialize all individual LWLocks in main array */
506 for (id = 0, lock = MainLWLockArray; id < NUM_INDIVIDUAL_LWLOCKS; id++, lock++)
507 LWLockInitialize(&lock->lock, id);
508
509 /* Initialize buffer mapping LWLocks in main array */
511 for (id = 0; id < NUM_BUFFER_PARTITIONS; id++, lock++)
513
514 /* Initialize lmgrs' LWLocks in main array */
516 for (id = 0; id < NUM_LOCK_PARTITIONS; id++, lock++)
518
519 /* Initialize predicate lmgrs' LWLocks in main array */
521 for (id = 0; id < NUM_PREDICATELOCK_PARTITIONS; id++, lock++)
523
524 /*
525 * Copy the info about any named tranches into shared memory (so that
526 * other processes can see it), and initialize the requested LWLocks.
527 */
529 {
531
532 for (i = 0; i < NamedLWLockTrancheRequests; i++)
533 {
535 int tranche;
536
538 tranche = LWLockNewTrancheId(request->tranche_name);
539
540 for (j = 0; j < request->num_lwlocks; j++, lock++)
541 LWLockInitialize(&lock->lock, tranche);
542 }
543 }
544}
545
546/*
547 * InitLWLockAccess - initialize backend-local state needed to hold LWLocks
548 */
549void
551{
552#ifdef LWLOCK_STATS
554#endif
555}
556
557/*
558 * GetNamedLWLockTranche - returns the base address of LWLock from the
559 * specified tranche.
560 *
561 * Caller needs to retrieve the requested number of LWLocks starting from
562 * the base lock address returned by this API. This can be used for
563 * tranches that are requested by using RequestNamedLWLockTranche() API.
564 */
566GetNamedLWLockTranche(const char *tranche_name)
567{
568 int lock_pos;
569 int i;
570
571 /*
572 * Obtain the position of base address of LWLock belonging to requested
573 * tranche_name in MainLWLockArray. LWLocks for named tranches are placed
574 * in MainLWLockArray after fixed locks.
575 */
577 for (i = 0; i < NamedLWLockTrancheRequests; i++)
578 {
579 if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name,
580 tranche_name) == 0)
581 return &MainLWLockArray[lock_pos];
582
584 }
585
586 elog(ERROR, "requested tranche is not registered");
587
588 /* just to keep compiler quiet */
589 return NULL;
590}
591
592/*
593 * Allocate a new tranche ID with the provided name.
594 */
595int
596LWLockNewTrancheId(const char *name)
597{
598 int result;
599
600 if (!name)
603 errmsg("tranche name cannot be NULL")));
604
605 if (strlen(name) >= NAMEDATALEN)
608 errmsg("tranche name too long"),
609 errdetail("LWLock tranche names must be no longer than %d bytes.",
610 NAMEDATALEN - 1)));
611
612 /*
613 * We use the ShmemLock spinlock to protect LWLockCounter and
614 * LWLockTrancheNames.
615 */
617
619 {
622 (errmsg("maximum number of tranches already registered"),
623 errdetail("No more than %d tranches may be registered.",
625 }
626
627 result = (*LWLockCounter)++;
630
632
633 return result;
634}
635
636/*
637 * RequestNamedLWLockTranche
638 * Request that extra LWLocks be allocated during postmaster
639 * startup.
640 *
641 * This may only be called via the shmem_request_hook of a library that is
642 * loaded into the postmaster via shared_preload_libraries. Calls from
643 * elsewhere will fail.
644 *
645 * The tranche name will be user-visible as a wait event name, so try to
646 * use a name that fits the style for those.
647 */
648void
649RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
650{
653
655 elog(FATAL, "cannot request additional LWLocks outside shmem_request_hook");
656
657 if (!tranche_name)
660 errmsg("tranche name cannot be NULL")));
661
662 if (strlen(tranche_name) >= NAMEDATALEN)
665 errmsg("tranche name too long"),
666 errdetail("LWLock tranche names must be no longer than %d bytes.",
667 NAMEDATALEN - 1)));
668
670 {
675 * sizeof(NamedLWLockTrancheRequest));
676 }
677
679 {
681
684 i * sizeof(NamedLWLockTrancheRequest));
686 }
687
689 strlcpy(request->tranche_name, tranche_name, NAMEDATALEN);
690 request->num_lwlocks = num_lwlocks;
692}
693
694/*
695 * LWLockInitialize - initialize a new lwlock; it's initially unlocked
696 */
697void
698LWLockInitialize(LWLock *lock, int tranche_id)
699{
700 /* verify the tranche_id is valid */
701 (void) GetLWTrancheName(tranche_id);
702
703 pg_atomic_init_u32(&lock->state, 0);
704#ifdef LOCK_DEBUG
705 pg_atomic_init_u32(&lock->nwaiters, 0);
706#endif
707 lock->tranche = tranche_id;
708 proclist_init(&lock->waiters);
709}
710
711/*
712 * Report start of wait event for light-weight locks.
713 *
714 * This function will be used by all the light-weight lock calls which
715 * needs to wait to acquire the lock. This function distinguishes wait
716 * event based on tranche and lock id.
717 */
718static inline void
720{
722}
723
724/*
725 * Report end of wait event for light-weight locks.
726 */
727static inline void
729{
731}
732
733/*
734 * Return the name of an LWLock tranche.
735 */
736static const char *
738{
739 /* Built-in tranche or individual LWLock? */
742
743 /*
744 * We only ever add new entries to LWLockTrancheNames, so most lookups can
745 * avoid taking the spinlock as long as the backend-local counter
746 * (LocalLWLockCounter) is greater than the requested tranche ID. Else,
747 * we need to first update the backend-local counter with ShmemLock held
748 * before attempting the lookup again. In practice, the latter case is
749 * probably rare.
750 */
752 {
756
758 elog(ERROR, "tranche %d is not registered", trancheId);
759 }
760
761 /*
762 * It's an extension tranche, so look in LWLockTrancheNames.
763 */
765
767}
768
769/*
770 * Return an identifier for an LWLock based on the wait class and event.
771 */
772const char *
774{
775 Assert(classId == PG_WAIT_LWLOCK);
776 /* The event IDs are just tranche numbers. */
778}
779
780/*
781 * Internal function that tries to atomically acquire the lwlock in the passed
782 * in mode.
783 *
784 * This function will not block waiting for a lock to become free - that's the
785 * caller's job.
786 *
787 * Returns true if the lock isn't free and we need to wait.
788 */
789static bool
791{
793
795
796 /*
797 * Read once outside the loop, later iterations will get the newer value
798 * via compare & exchange.
799 */
801
802 /* loop until we've determined whether we could acquire the lock or not */
803 while (true)
804 {
806 bool lock_free;
807
809
810 if (mode == LW_EXCLUSIVE)
811 {
813 if (lock_free)
815 }
816 else
817 {
819 if (lock_free)
821 }
822
823 /*
824 * Attempt to swap in the state we are expecting. If we didn't see
825 * lock to be free, that's just the old value. If we saw it as free,
826 * we'll attempt to mark it acquired. The reason that we always swap
827 * in the value is that this doubles as a memory barrier. We could try
828 * to be smarter and only swap in values if we saw the lock as free,
829 * but benchmark haven't shown it as beneficial so far.
830 *
831 * Retry if the value changed since we last looked at it.
832 */
835 {
836 if (lock_free)
837 {
838 /* Great! Got the lock. */
839#ifdef LOCK_DEBUG
840 if (mode == LW_EXCLUSIVE)
841 lock->owner = MyProc;
842#endif
843 return false;
844 }
845 else
846 return true; /* somebody else has the lock */
847 }
848 }
850}
851
852/*
853 * Lock the LWLock's wait list against concurrent activity.
854 *
855 * NB: even though the wait list is locked, non-conflicting lock operations
856 * may still happen concurrently.
857 *
858 * Time spent holding mutex should be short!
859 */
860static void
862{
864#ifdef LWLOCK_STATS
866 uint32 delays = 0;
867
869#endif
870
871 while (true)
872 {
873 /*
874 * Always try once to acquire the lock directly, without setting up
875 * the spin-delay infrastructure. The work necessary for that shows up
876 * in profiles and is rarely necessary.
877 */
880 break; /* got lock */
881
882 /* and then spin without atomic operations until lock is released */
883 {
885
887
888 while (old_state & LW_FLAG_LOCKED)
889 {
892 }
893#ifdef LWLOCK_STATS
894 delays += delayStatus.delays;
895#endif
897 }
898
899 /*
900 * Retry. The lock might obviously already be re-acquired by the time
901 * we're attempting to get it again.
902 */
903 }
904
905#ifdef LWLOCK_STATS
906 lwstats->spin_delay_count += delays;
907#endif
908}
909
910/*
911 * Unlock the LWLock's wait list.
912 *
913 * Note that it can be more efficient to manipulate flags and release the
914 * locks in a single atomic operation.
915 */
916static void
918{
920
922
924}
925
926/*
927 * Wakeup all the lockers that currently have a chance to acquire the lock.
928 */
929static void
930LWLockWakeup(LWLock *lock)
931{
932 bool new_wake_in_progress = false;
933 bool wokeup_somebody = false;
936
938
939 /* lock wait list while collecting backends to wake up */
940 LWLockWaitListLock(lock);
941
942 proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
943 {
944 PGPROC *waiter = GetPGProcByNumber(iter.cur);
945
946 if (wokeup_somebody && waiter->lwWaitMode == LW_EXCLUSIVE)
947 continue;
948
949 proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
950 proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
951
952 if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
953 {
954 /*
955 * Prevent additional wakeups until retryer gets to run. Backends
956 * that are just waiting for the lock to become free don't retry
957 * automatically.
958 */
960
961 /*
962 * Don't wakeup (further) exclusive locks.
963 */
964 wokeup_somebody = true;
965 }
966
967 /*
968 * Signal that the process isn't on the wait list anymore. This allows
969 * LWLockDequeueSelf() to remove itself of the waitlist with a
970 * proclist_delete(), rather than having to check if it has been
971 * removed from the list.
972 */
973 Assert(waiter->lwWaiting == LW_WS_WAITING);
975
976 /*
977 * Once we've woken up an exclusive lock, there's no point in waking
978 * up anybody else.
979 */
980 if (waiter->lwWaitMode == LW_EXCLUSIVE)
981 break;
982 }
983
985
986 /* unset required flags, and release lock, in one fell swoop */
987 {
990
992 while (true)
993 {
995
996 /* compute desired flags */
997
1000 else
1002
1003 if (proclist_is_empty(&lock->waiters))
1005
1006 desired_state &= ~LW_FLAG_LOCKED; /* release lock */
1007
1010 break;
1011 }
1012 }
1013
1014 /* Awaken any waiters I removed from the queue. */
1015 proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1016 {
1017 PGPROC *waiter = GetPGProcByNumber(iter.cur);
1018
1019 LOG_LWDEBUG("LWLockRelease", lock, "release waiter");
1020 proclist_delete(&wakeup, iter.cur, lwWaitLink);
1021
1022 /*
1023 * Guarantee that lwWaiting being unset only becomes visible once the
1024 * unlink from the link has completed. Otherwise the target backend
1025 * could be woken up for other reason and enqueue for a new lock - if
1026 * that happens before the list unlink happens, the list would end up
1027 * being corrupted.
1028 *
1029 * The barrier pairs with the LWLockWaitListLock() when enqueuing for
1030 * another lock.
1031 */
1033 waiter->lwWaiting = LW_WS_NOT_WAITING;
1034 PGSemaphoreUnlock(waiter->sem);
1035 }
1036}
1037
1038/*
1039 * Add ourselves to the end of the queue.
1040 *
1041 * NB: Mode can be LW_WAIT_UNTIL_FREE here!
1042 */
1043static void
1045{
1046 /*
1047 * If we don't have a PGPROC structure, there's no way to wait. This
1048 * should never occur, since MyProc should only be null during shared
1049 * memory initialization.
1050 */
1051 if (MyProc == NULL)
1052 elog(PANIC, "cannot wait without a PGPROC structure");
1053
1055 elog(PANIC, "queueing for lock while waiting on another one");
1056
1057 LWLockWaitListLock(lock);
1058
1059 /* setting the flag is protected by the spinlock */
1061
1064
1065 /* LW_WAIT_UNTIL_FREE waiters are always at the front of the queue */
1066 if (mode == LW_WAIT_UNTIL_FREE)
1067 proclist_push_head(&lock->waiters, MyProcNumber, lwWaitLink);
1068 else
1069 proclist_push_tail(&lock->waiters, MyProcNumber, lwWaitLink);
1070
1071 /* Can release the mutex now */
1073
1074#ifdef LOCK_DEBUG
1075 pg_atomic_fetch_add_u32(&lock->nwaiters, 1);
1076#endif
1077}
1078
1079/*
1080 * Remove ourselves from the waitlist.
1081 *
1082 * This is used if we queued ourselves because we thought we needed to sleep
1083 * but, after further checking, we discovered that we don't actually need to
1084 * do so.
1085 */
1086static void
1088{
1089 bool on_waitlist;
1090
1091#ifdef LWLOCK_STATS
1093
1095
1096 lwstats->dequeue_self_count++;
1097#endif
1098
1099 LWLockWaitListLock(lock);
1100
1101 /*
1102 * Remove ourselves from the waitlist, unless we've already been removed.
1103 * The removal happens with the wait list lock held, so there's no race in
1104 * this check.
1105 */
1107 if (on_waitlist)
1108 proclist_delete(&lock->waiters, MyProcNumber, lwWaitLink);
1109
1110 if (proclist_is_empty(&lock->waiters) &&
1112 {
1114 }
1115
1116 /* XXX: combine with fetch_and above? */
1118
1119 /* clear waiting state again, nice for debugging */
1120 if (on_waitlist)
1122 else
1123 {
1124 int extraWaits = 0;
1125
1126 /*
1127 * Somebody else dequeued us and has or will wake us up. Deal with the
1128 * superfluous absorption of a wakeup.
1129 */
1130
1131 /*
1132 * Clear LW_FLAG_WAKE_IN_PROGRESS if somebody woke us before we
1133 * removed ourselves - they'll have set it.
1134 */
1136
1137 /*
1138 * Now wait for the scheduled wakeup, otherwise our ->lwWaiting would
1139 * get reset at some inconvenient point later. Most of the time this
1140 * will immediately return.
1141 */
1142 for (;;)
1143 {
1146 break;
1147 extraWaits++;
1148 }
1149
1150 /*
1151 * Fix the process wait semaphore's count for any absorbed wakeups.
1152 */
1153 while (extraWaits-- > 0)
1155 }
1156
1157#ifdef LOCK_DEBUG
1158 {
1159 /* not waiting anymore */
1161
1163 }
1164#endif
1165}
1166
1167/*
1168 * LWLockAcquire - acquire a lightweight lock in the specified mode
1169 *
1170 * If the lock is not available, sleep until it is. Returns true if the lock
1171 * was available immediately, false if we had to sleep.
1172 *
1173 * Side effect: cancel/die interrupts are held off until lock release.
1174 */
1175bool
1177{
1178 PGPROC *proc = MyProc;
1179 bool result = true;
1180 int extraWaits = 0;
1181#ifdef LWLOCK_STATS
1183
1185#endif
1186
1188
1189 PRINT_LWDEBUG("LWLockAcquire", lock, mode);
1190
1191#ifdef LWLOCK_STATS
1192 /* Count lock acquisition attempts */
1193 if (mode == LW_EXCLUSIVE)
1194 lwstats->ex_acquire_count++;
1195 else
1196 lwstats->sh_acquire_count++;
1197#endif /* LWLOCK_STATS */
1198
1199 /*
1200 * We can't wait if we haven't got a PGPROC. This should only occur
1201 * during bootstrap or shared memory initialization. Put an Assert here
1202 * to catch unsafe coding practices.
1203 */
1204 Assert(!(proc == NULL && IsUnderPostmaster));
1205
1206 /* Ensure we will have room to remember the lock */
1208 elog(ERROR, "too many LWLocks taken");
1209
1210 /*
1211 * Lock out cancel/die interrupts until we exit the code section protected
1212 * by the LWLock. This ensures that interrupts will not interfere with
1213 * manipulations of data structures in shared memory.
1214 */
1216
1217 /*
1218 * Loop here to try to acquire lock after each time we are signaled by
1219 * LWLockRelease.
1220 *
1221 * NOTE: it might seem better to have LWLockRelease actually grant us the
1222 * lock, rather than retrying and possibly having to go back to sleep. But
1223 * in practice that is no good because it means a process swap for every
1224 * lock acquisition when two or more processes are contending for the same
1225 * lock. Since LWLocks are normally used to protect not-very-long
1226 * sections of computation, a process needs to be able to acquire and
1227 * release the same lock many times during a single CPU time slice, even
1228 * in the presence of contention. The efficiency of being able to do that
1229 * outweighs the inefficiency of sometimes wasting a process dispatch
1230 * cycle because the lock is not free when a released waiter finally gets
1231 * to run. See pgsql-hackers archives for 29-Dec-01.
1232 */
1233 for (;;)
1234 {
1235 bool mustwait;
1236
1237 /*
1238 * Try to grab the lock the first time, we're not in the waitqueue
1239 * yet/anymore.
1240 */
1242
1243 if (!mustwait)
1244 {
1245 LOG_LWDEBUG("LWLockAcquire", lock, "immediately acquired lock");
1246 break; /* got the lock */
1247 }
1248
1249 /*
1250 * Ok, at this point we couldn't grab the lock on the first try. We
1251 * cannot simply queue ourselves to the end of the list and wait to be
1252 * woken up because by now the lock could long have been released.
1253 * Instead add us to the queue and try to grab the lock again. If we
1254 * succeed we need to revert the queuing and be happy, otherwise we
1255 * recheck the lock. If we still couldn't grab it, we know that the
1256 * other locker will see our queue entries when releasing since they
1257 * existed before we checked for the lock.
1258 */
1259
1260 /* add to the queue */
1261 LWLockQueueSelf(lock, mode);
1262
1263 /* we're now guaranteed to be woken up if necessary */
1265
1266 /* ok, grabbed the lock the second time round, need to undo queueing */
1267 if (!mustwait)
1268 {
1269 LOG_LWDEBUG("LWLockAcquire", lock, "acquired, undoing queue");
1270
1271 LWLockDequeueSelf(lock);
1272 break;
1273 }
1274
1275 /*
1276 * Wait until awakened.
1277 *
1278 * It is possible that we get awakened for a reason other than being
1279 * signaled by LWLockRelease. If so, loop back and wait again. Once
1280 * we've gotten the LWLock, re-increment the sema by the number of
1281 * additional signals received.
1282 */
1283 LOG_LWDEBUG("LWLockAcquire", lock, "waiting");
1284
1285#ifdef LWLOCK_STATS
1286 lwstats->block_count++;
1287#endif
1288
1292
1293 for (;;)
1294 {
1295 PGSemaphoreLock(proc->sem);
1296 if (proc->lwWaiting == LW_WS_NOT_WAITING)
1297 break;
1298 extraWaits++;
1299 }
1300
1301 /* Retrying, allow LWLockRelease to release waiters again. */
1303
1304#ifdef LOCK_DEBUG
1305 {
1306 /* not waiting anymore */
1308
1310 }
1311#endif
1312
1316
1317 LOG_LWDEBUG("LWLockAcquire", lock, "awakened");
1318
1319 /* Now loop back and try to acquire lock again. */
1320 result = false;
1321 }
1322
1325
1326 /* Add lock to list of locks held by this backend */
1329
1330 /*
1331 * Fix the process wait semaphore's count for any absorbed wakeups.
1332 */
1333 while (extraWaits-- > 0)
1334 PGSemaphoreUnlock(proc->sem);
1335
1336 return result;
1337}
1338
1339/*
1340 * LWLockConditionalAcquire - acquire a lightweight lock in the specified mode
1341 *
1342 * If the lock is not available, return false with no side-effects.
1343 *
1344 * If successful, cancel/die interrupts are held off until lock release.
1345 */
1346bool
1348{
1349 bool mustwait;
1350
1352
1353 PRINT_LWDEBUG("LWLockConditionalAcquire", lock, mode);
1354
1355 /* Ensure we will have room to remember the lock */
1357 elog(ERROR, "too many LWLocks taken");
1358
1359 /*
1360 * Lock out cancel/die interrupts until we exit the code section protected
1361 * by the LWLock. This ensures that interrupts will not interfere with
1362 * manipulations of data structures in shared memory.
1363 */
1365
1366 /* Check for the lock */
1368
1369 if (mustwait)
1370 {
1371 /* Failed to get lock, so release interrupt holdoff */
1373
1374 LOG_LWDEBUG("LWLockConditionalAcquire", lock, "failed");
1377 }
1378 else
1379 {
1380 /* Add lock to list of locks held by this backend */
1385 }
1386 return !mustwait;
1387}
1388
1389/*
1390 * LWLockAcquireOrWait - Acquire lock, or wait until it's free
1391 *
1392 * The semantics of this function are a bit funky. If the lock is currently
1393 * free, it is acquired in the given mode, and the function returns true. If
1394 * the lock isn't immediately free, the function waits until it is released
1395 * and returns false, but does not acquire the lock.
1396 *
1397 * This is currently used for WALWriteLock: when a backend flushes the WAL,
1398 * holding WALWriteLock, it can flush the commit records of many other
1399 * backends as a side-effect. Those other backends need to wait until the
1400 * flush finishes, but don't need to acquire the lock anymore. They can just
1401 * wake up, observe that their records have already been flushed, and return.
1402 */
1403bool
1405{
1406 PGPROC *proc = MyProc;
1407 bool mustwait;
1408 int extraWaits = 0;
1409#ifdef LWLOCK_STATS
1411
1413#endif
1414
1416
1417 PRINT_LWDEBUG("LWLockAcquireOrWait", lock, mode);
1418
1419 /* Ensure we will have room to remember the lock */
1421 elog(ERROR, "too many LWLocks taken");
1422
1423 /*
1424 * Lock out cancel/die interrupts until we exit the code section protected
1425 * by the LWLock. This ensures that interrupts will not interfere with
1426 * manipulations of data structures in shared memory.
1427 */
1429
1430 /*
1431 * NB: We're using nearly the same twice-in-a-row lock acquisition
1432 * protocol as LWLockAcquire(). Check its comments for details.
1433 */
1435
1436 if (mustwait)
1437 {
1439
1441
1442 if (mustwait)
1443 {
1444 /*
1445 * Wait until awakened. Like in LWLockAcquire, be prepared for
1446 * bogus wakeups.
1447 */
1448 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "waiting");
1449
1450#ifdef LWLOCK_STATS
1451 lwstats->block_count++;
1452#endif
1453
1457
1458 for (;;)
1459 {
1460 PGSemaphoreLock(proc->sem);
1461 if (proc->lwWaiting == LW_WS_NOT_WAITING)
1462 break;
1463 extraWaits++;
1464 }
1465
1466#ifdef LOCK_DEBUG
1467 {
1468 /* not waiting anymore */
1470
1472 }
1473#endif
1477
1478 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "awakened");
1479 }
1480 else
1481 {
1482 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "acquired, undoing queue");
1483
1484 /*
1485 * Got lock in the second attempt, undo queueing. We need to treat
1486 * this as having successfully acquired the lock, otherwise we'd
1487 * not necessarily wake up people we've prevented from acquiring
1488 * the lock.
1489 */
1490 LWLockDequeueSelf(lock);
1491 }
1492 }
1493
1494 /*
1495 * Fix the process wait semaphore's count for any absorbed wakeups.
1496 */
1497 while (extraWaits-- > 0)
1498 PGSemaphoreUnlock(proc->sem);
1499
1500 if (mustwait)
1501 {
1502 /* Failed to get lock, so release interrupt holdoff */
1504 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "failed");
1507 }
1508 else
1509 {
1510 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "succeeded");
1511 /* Add lock to list of locks held by this backend */
1516 }
1517
1518 return !mustwait;
1519}
1520
1521/*
1522 * Does the lwlock in its current state need to wait for the variable value to
1523 * change?
1524 *
1525 * If we don't need to wait, and it's because the value of the variable has
1526 * changed, store the current value in newval.
1527 *
1528 * *result is set to true if the lock was free, and false otherwise.
1529 */
1530static bool
1532 uint64 *newval, bool *result)
1533{
1534 bool mustwait;
1535 uint64 value;
1536
1537 /*
1538 * Test first to see if it the slot is free right now.
1539 *
1540 * XXX: the unique caller of this routine, WaitXLogInsertionsToFinish()
1541 * via LWLockWaitForVar(), uses an implied barrier with a spinlock before
1542 * this, so we don't need a memory barrier here as far as the current
1543 * usage is concerned. But that might not be safe in general.
1544 */
1546
1547 if (!mustwait)
1548 {
1549 *result = true;
1550 return false;
1551 }
1552
1553 *result = false;
1554
1555 /*
1556 * Reading this value atomically is safe even on platforms where uint64
1557 * cannot be read without observing a torn value.
1558 */
1560
1561 if (value != oldval)
1562 {
1563 mustwait = false;
1564 *newval = value;
1565 }
1566 else
1567 {
1568 mustwait = true;
1569 }
1570
1571 return mustwait;
1572}
1573
1574/*
1575 * LWLockWaitForVar - Wait until lock is free, or a variable is updated.
1576 *
1577 * If the lock is held and *valptr equals oldval, waits until the lock is
1578 * either freed, or the lock holder updates *valptr by calling
1579 * LWLockUpdateVar. If the lock is free on exit (immediately or after
1580 * waiting), returns true. If the lock is still held, but *valptr no longer
1581 * matches oldval, returns false and sets *newval to the current value in
1582 * *valptr.
1583 *
1584 * Note: this function ignores shared lock holders; if the lock is held
1585 * in shared mode, returns 'true'.
1586 *
1587 * Be aware that LWLockConflictsWithVar() does not include a memory barrier,
1588 * hence the caller of this function may want to rely on an explicit barrier or
1589 * an implied barrier via spinlock or LWLock to avoid memory ordering issues.
1590 */
1591bool
1593 uint64 *newval)
1594{
1595 PGPROC *proc = MyProc;
1596 int extraWaits = 0;
1597 bool result = false;
1598#ifdef LWLOCK_STATS
1600
1602#endif
1603
1604 PRINT_LWDEBUG("LWLockWaitForVar", lock, LW_WAIT_UNTIL_FREE);
1605
1606 /*
1607 * Lock out cancel/die interrupts while we sleep on the lock. There is no
1608 * cleanup mechanism to remove us from the wait queue if we got
1609 * interrupted.
1610 */
1612
1613 /*
1614 * Loop here to check the lock's status after each time we are signaled.
1615 */
1616 for (;;)
1617 {
1618 bool mustwait;
1619
1621 &result);
1622
1623 if (!mustwait)
1624 break; /* the lock was free or value didn't match */
1625
1626 /*
1627 * Add myself to wait queue. Note that this is racy, somebody else
1628 * could wakeup before we're finished queuing. NB: We're using nearly
1629 * the same twice-in-a-row lock acquisition protocol as
1630 * LWLockAcquire(). Check its comments for details. The only
1631 * difference is that we also have to check the variable's values when
1632 * checking the state of the lock.
1633 */
1635
1636 /*
1637 * Clear LW_FLAG_WAKE_IN_PROGRESS flag, to make sure we get woken up
1638 * as soon as the lock is released.
1639 */
1641
1642 /*
1643 * We're now guaranteed to be woken up if necessary. Recheck the lock
1644 * and variables state.
1645 */
1647 &result);
1648
1649 /* Ok, no conflict after we queued ourselves. Undo queueing. */
1650 if (!mustwait)
1651 {
1652 LOG_LWDEBUG("LWLockWaitForVar", lock, "free, undoing queue");
1653
1654 LWLockDequeueSelf(lock);
1655 break;
1656 }
1657
1658 /*
1659 * Wait until awakened.
1660 *
1661 * It is possible that we get awakened for a reason other than being
1662 * signaled by LWLockRelease. If so, loop back and wait again. Once
1663 * we've gotten the LWLock, re-increment the sema by the number of
1664 * additional signals received.
1665 */
1666 LOG_LWDEBUG("LWLockWaitForVar", lock, "waiting");
1667
1668#ifdef LWLOCK_STATS
1669 lwstats->block_count++;
1670#endif
1671
1675
1676 for (;;)
1677 {
1678 PGSemaphoreLock(proc->sem);
1679 if (proc->lwWaiting == LW_WS_NOT_WAITING)
1680 break;
1681 extraWaits++;
1682 }
1683
1684#ifdef LOCK_DEBUG
1685 {
1686 /* not waiting anymore */
1688
1690 }
1691#endif
1692
1696
1697 LOG_LWDEBUG("LWLockWaitForVar", lock, "awakened");
1698
1699 /* Now loop back and check the status of the lock again. */
1700 }
1701
1702 /*
1703 * Fix the process wait semaphore's count for any absorbed wakeups.
1704 */
1705 while (extraWaits-- > 0)
1706 PGSemaphoreUnlock(proc->sem);
1707
1708 /*
1709 * Now okay to allow cancel/die interrupts.
1710 */
1712
1713 return result;
1714}
1715
1716
1717/*
1718 * LWLockUpdateVar - Update a variable and wake up waiters atomically
1719 *
1720 * Sets *valptr to 'val', and wakes up all processes waiting for us with
1721 * LWLockWaitForVar(). It first sets the value atomically and then wakes up
1722 * waiting processes so that any process calling LWLockWaitForVar() on the same
1723 * lock is guaranteed to see the new value, and act accordingly.
1724 *
1725 * The caller must be holding the lock in exclusive mode.
1726 */
1727void
1729{
1732
1733 PRINT_LWDEBUG("LWLockUpdateVar", lock, LW_EXCLUSIVE);
1734
1735 /*
1736 * Note that pg_atomic_exchange_u64 is a full barrier, so we're guaranteed
1737 * that the variable is updated before waking up waiters.
1738 */
1740
1742
1743 LWLockWaitListLock(lock);
1744
1746
1747 /*
1748 * See if there are any LW_WAIT_UNTIL_FREE waiters that need to be woken
1749 * up. They are always in the front of the queue.
1750 */
1751 proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
1752 {
1753 PGPROC *waiter = GetPGProcByNumber(iter.cur);
1754
1755 if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
1756 break;
1757
1758 proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
1759 proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
1760
1761 /* see LWLockWakeup() */
1762 Assert(waiter->lwWaiting == LW_WS_WAITING);
1764 }
1765
1766 /* We are done updating shared state of the lock itself. */
1768
1769 /*
1770 * Awaken any waiters I removed from the queue.
1771 */
1772 proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1773 {
1774 PGPROC *waiter = GetPGProcByNumber(iter.cur);
1775
1776 proclist_delete(&wakeup, iter.cur, lwWaitLink);
1777 /* check comment in LWLockWakeup() about this barrier */
1779 waiter->lwWaiting = LW_WS_NOT_WAITING;
1780 PGSemaphoreUnlock(waiter->sem);
1781 }
1782}
1783
1784
1785/*
1786 * LWLockRelease - release a previously acquired lock
1787 *
1788 * NB: This will leave lock->owner pointing to the current backend (if
1789 * LOCK_DEBUG is set). This is somewhat intentional, as it makes it easier to
1790 * debug cases of missing wakeups during lock release.
1791 */
1792void
1793LWLockRelease(LWLock *lock)
1794{
1797 bool check_waiters;
1798 int i;
1799
1800 /*
1801 * Remove lock from list of locks held. Usually, but not always, it will
1802 * be the latest-acquired lock; so search array backwards.
1803 */
1804 for (i = num_held_lwlocks; --i >= 0;)
1805 if (lock == held_lwlocks[i].lock)
1806 break;
1807
1808 if (i < 0)
1809 elog(ERROR, "lock %s is not held", T_NAME(lock));
1810
1812
1814 for (; i < num_held_lwlocks; i++)
1815 held_lwlocks[i] = held_lwlocks[i + 1];
1816
1817 PRINT_LWDEBUG("LWLockRelease", lock, mode);
1818
1819 /*
1820 * Release my hold on lock, after that it can immediately be acquired by
1821 * others, even if we still have to wakeup other waiters.
1822 */
1823 if (mode == LW_EXCLUSIVE)
1825 else
1827
1828 /* nobody else can have that kind of lock */
1830
1833
1834 /*
1835 * Check if we're still waiting for backends to get scheduled, if so,
1836 * don't wake them up again.
1837 */
1838 if ((oldstate & LW_FLAG_HAS_WAITERS) &&
1840 (oldstate & LW_LOCK_MASK) == 0)
1841 check_waiters = true;
1842 else
1843 check_waiters = false;
1844
1845 /*
1846 * As waking up waiters requires the spinlock to be acquired, only do so
1847 * if necessary.
1848 */
1849 if (check_waiters)
1850 {
1851 /* XXX: remove before commit? */
1852 LOG_LWDEBUG("LWLockRelease", lock, "releasing waiters");
1853 LWLockWakeup(lock);
1854 }
1855
1856 /*
1857 * Now okay to allow cancel/die interrupts.
1858 */
1860}
1861
1862/*
1863 * LWLockReleaseClearVar - release a previously acquired lock, reset variable
1864 */
1865void
1867{
1868 /*
1869 * Note that pg_atomic_exchange_u64 is a full barrier, so we're guaranteed
1870 * that the variable is updated before releasing the lock.
1871 */
1873
1874 LWLockRelease(lock);
1875}
1876
1877
1878/*
1879 * LWLockReleaseAll - release all currently-held locks
1880 *
1881 * Used to clean up after ereport(ERROR). An important difference between this
1882 * function and retail LWLockRelease calls is that InterruptHoldoffCount is
1883 * unchanged by this operation. This is necessary since InterruptHoldoffCount
1884 * has been set to an appropriate level earlier in error recovery. We could
1885 * decrement it below zero if we allow it to drop for each released lock!
1886 *
1887 * Note that this function must be safe to call even before the LWLock
1888 * subsystem has been initialized (e.g., during early startup failures).
1889 * In that case, num_held_lwlocks will be 0 and we do nothing.
1890 */
1891void
1892LWLockReleaseAll(void)
1893{
1894 while (num_held_lwlocks > 0)
1895 {
1896 HOLD_INTERRUPTS(); /* match the upcoming RESUME_INTERRUPTS */
1897
1899 }
1900
1902}
1903
1904
1905/*
1906 * LWLockHeldByMe - test whether my process holds a lock in any mode
1907 *
1908 * This is meant as debug support only.
1909 */
1910bool
1912{
1913 int i;
1914
1915 for (i = 0; i < num_held_lwlocks; i++)
1916 {
1917 if (held_lwlocks[i].lock == lock)
1918 return true;
1919 }
1920 return false;
1921}
1922
1923/*
1924 * LWLockAnyHeldByMe - test whether my process holds any of an array of locks
1925 *
1926 * This is meant as debug support only.
1927 */
1928bool
1929LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
1930{
1931 char *held_lock_addr;
1932 char *begin;
1933 char *end;
1934 int i;
1935
1936 begin = (char *) lock;
1937 end = begin + nlocks * stride;
1938 for (i = 0; i < num_held_lwlocks; i++)
1939 {
1940 held_lock_addr = (char *) held_lwlocks[i].lock;
1941 if (held_lock_addr >= begin &&
1942 held_lock_addr < end &&
1943 (held_lock_addr - begin) % stride == 0)
1944 return true;
1945 }
1946 return false;
1947}
1948
1949/*
1950 * LWLockHeldByMeInMode - test whether my process holds a lock in given mode
1951 *
1952 * This is meant as debug support only.
1953 */
1954bool
1956{
1957 int i;
1958
1959 for (i = 0; i < num_held_lwlocks; i++)
1960 {
1961 if (held_lwlocks[i].lock == lock && held_lwlocks[i].mode == mode)
1962 return true;
1963 }
1964 return false;
1965}
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
Definition atomics.h:396
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition atomics.h:349
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition atomics.h:410
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition atomics.h:439
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition atomics.h:381
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:219
#define pg_write_barrier()
Definition atomics.h:155
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition atomics.h:366
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
Definition atomics.h:513
#define likely(x)
Definition c.h:431
#define MAXALIGN(LEN)
Definition c.h:898
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:243
#define Assert(condition)
Definition c.h:945
uint64_t uint64
Definition c.h:619
uint16_t uint16
Definition c.h:617
#define pg_unreachable()
Definition c.h:361
uint32_t uint32
Definition c.h:618
#define MemSet(start, val, len)
Definition c.h:1109
size_t Size
Definition c.h:691
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
Datum arg
Definition elog.c:1322
int errcode(int sqlerrcode)
Definition elog.c:874
int int errhidestmt(bool hide_stmt)
#define LOG
Definition elog.h:31
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define FATAL
Definition elog.h:41
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define PANIC
Definition elog.h:42
#define ERROR
Definition elog.h:39
int errhidecontext(bool hide_ctx)
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
int MyProcPid
Definition globals.c:47
ProcNumber MyProcNumber
Definition globals.c:90
bool IsUnderPostmaster
Definition globals.c:120
#define newval
@ HASH_ENTER
Definition hsearch.h:114
#define HASH_CONTEXT
Definition hsearch.h:102
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
long val
Definition informix.c:689
static struct @174 value
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
int j
Definition isn.c:78
int i
Definition isn.c:77
#define LW_VAL_EXCLUSIVE
Definition lwlock.c:102
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition lwlock.c:1729
static void LWLockWakeup(LWLock *lock)
Definition lwlock.c:931
#define LW_FLAG_LOCKED
Definition lwlock.c:97
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1912
static int LocalLWLockCounter
Definition lwlock.c:203
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition lwlock.c:179
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition lwlock.c:1867
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
void CreateLWLocks(void)
Definition lwlock.c:442
int LWLockNewTrancheId(const char *name)
Definition lwlock.c:597
#define LW_VAL_SHARED
Definition lwlock.c:103
static bool LWLockAttemptLock(LWLock *lock, LWLockMode mode)
Definition lwlock.c:791
static void LWLockWaitListLock(LWLock *lock)
Definition lwlock.c:862
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition lwlock.c:567
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1956
static void LWLockReportWaitEnd(void)
Definition lwlock.c:729
char ** LWLockTrancheNames
Definition lwlock.c:155
bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
Definition lwlock.c:1593
static const char * GetLWTrancheName(uint16 trancheId)
Definition lwlock.c:738
#define LW_LOCK_MASK
Definition lwlock.c:107
int NamedLWLockTrancheRequests
Definition lwlock.c:193
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition lwlock.c:650
#define LW_FLAG_HAS_WAITERS
Definition lwlock.c:95
#define MAX_SIMUL_LWLOCKS
Definition lwlock.c:169
static int NumLWLocksForNamedTranches(void)
Definition lwlock.c:383
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
static NamedLWLockTrancheRequest * LocalNamedLWLockTrancheRequestArray
Definition lwlock.c:197
#define T_NAME(lock)
Definition lwlock.c:212
static int num_held_lwlocks
Definition lwlock.c:178
void LWLockReleaseAll(void)
Definition lwlock.c:1893
static void InitializeLWLocks(void)
Definition lwlock.c:499
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:699
static const char *const BuiltinTrancheNames[]
Definition lwlock.c:136
NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition lwlock.c:194
static void LWLockWaitListUnlock(LWLock *lock)
Definition lwlock.c:918
#define LOG_LWDEBUG(a, b, c)
Definition lwlock.c:277
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1348
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1405
static void LWLockQueueSelf(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1045
#define PRINT_LWDEBUG(a, b, c)
Definition lwlock.c:276
static void LWLockReportWaitStart(LWLock *lock)
Definition lwlock.c:720
LWLockPadded * MainLWLockArray
Definition lwlock.c:162
#define LW_FLAG_WAKE_IN_PROGRESS
Definition lwlock.c:96
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition lwlock.c:774
static void LWLockDequeueSelf(LWLock *lock)
Definition lwlock.c:1088
int * LWLockCounter
Definition lwlock.c:200
Size LWLockShmemSize(void)
Definition lwlock.c:398
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
Definition lwlock.c:1930
#define MAX_NAMED_TRANCHES
Definition lwlock.c:205
#define LW_SHARED_MASK
Definition lwlock.c:106
static bool LWLockConflictsWithVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval, bool *result)
Definition lwlock.c:1532
void InitLWLockAccess(void)
Definition lwlock.c:551
@ LW_WS_NOT_WAITING
Definition lwlock.h:30
@ LW_WS_WAITING
Definition lwlock.h:31
@ LW_WS_PENDING_WAKEUP
Definition lwlock.h:32
#define LWLOCK_PADDED_SIZE
Definition lwlock.h:62
#define BUFFER_MAPPING_LWLOCK_OFFSET
Definition lwlock.h:102
#define NUM_LOCK_PARTITIONS
Definition lwlock.h:95
@ LWTRANCHE_FIRST_USER_DEFINED
Definition lwlock.h:182
#define LOCK_MANAGER_LWLOCK_OFFSET
Definition lwlock.h:103
#define NUM_BUFFER_PARTITIONS
Definition lwlock.h:91
#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET
Definition lwlock.h:105
#define NUM_FIXED_LWLOCKS
Definition lwlock.h:107
LWLockMode
Definition lwlock.h:111
@ LW_SHARED
Definition lwlock.h:113
@ LW_WAIT_UNTIL_FREE
Definition lwlock.h:114
@ LW_EXCLUSIVE
Definition lwlock.h:112
#define NUM_PREDICATELOCK_PARTITIONS
Definition lwlock.h:99
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
MemoryContext TopMemoryContext
Definition mcxt.c:166
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
Definition mcxt.c:743
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
bool process_shmem_requests_in_progress
Definition miscinit.c:1791
static char * errmsg
static uint32 pg_nextpower2_32(uint32 num)
static PgChecksumMode mode
#define NAMEDATALEN
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
void PGSemaphoreUnlock(PGSemaphore sema)
Definition posix_sema.c:335
void PGSemaphoreLock(PGSemaphore sema)
Definition posix_sema.c:315
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
#define GetPGProcByNumber(n)
Definition proc.h:501
#define proclist_delete(list, procno, link_member)
Definition proclist.h:187
static void proclist_init(proclist_head *list)
Definition proclist.h:29
#define proclist_push_tail(list, procno, link_member)
Definition proclist.h:191
#define proclist_push_head(list, procno, link_member)
Definition proclist.h:189
#define proclist_foreach_modify(iter, lhead, link_member)
Definition proclist.h:206
static bool proclist_is_empty(const proclist_head *list)
Definition proclist.h:38
#define MAX_BACKENDS
Definition procnumber.h:39
tree ctl
Definition radixtree.h:1838
void perform_spin_delay(SpinDelayStatus *status)
Definition s_lock.c:126
void finish_spin_delay(SpinDelayStatus *status)
Definition s_lock.c:186
#define init_local_spin_delay(status)
Definition s_lock.h:749
Size add_size(Size s1, Size s2)
Definition shmem.c:485
Size mul_size(Size s1, Size s2)
Definition shmem.c:500
void * ShmemAlloc(Size size)
Definition shmem.c:185
slock_t * ShmemLock
Definition shmem.c:108
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
PGPROC * MyProc
Definition proc.c:68
LWLockMode mode
Definition lwlock.c:175
LWLock * lock
Definition lwlock.c:174
pg_atomic_uint32 state
Definition lwlock.h:44
uint16 tranche
Definition lwlock.h:43
proclist_head waiters
Definition lwlock.h:45
Definition proc.h:176
uint8 lwWaitMode
Definition proc.h:281
PGSemaphore sem
Definition proc.h:255
uint8 lwWaiting
Definition proc.h:280
LWLock lock
Definition lwlock.h:70
#define PG_WAIT_LWLOCK
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
const char * name
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]

Typedef Documentation

◆ LWLockHandle

◆ NamedLWLockTrancheRequest

Function Documentation

◆ CreateLWLocks()

void CreateLWLocks ( void  )

Definition at line 442 of file lwlock.c.

443{
445 {
447 char *ptr;
448
449 /* Allocate space */
450 ptr = (char *) ShmemAlloc(spaceLocks);
451
452 /* Initialize the dynamic-allocation counter for tranches */
453 LWLockCounter = (int *) ptr;
455 ptr += MAXALIGN(sizeof(int));
456
457 /* Initialize tranche names */
458 LWLockTrancheNames = (char **) ptr;
459 ptr += MAX_NAMED_TRANCHES * sizeof(char *);
460 for (int i = 0; i < MAX_NAMED_TRANCHES; i++)
461 {
462 LWLockTrancheNames[i] = ptr;
463 ptr += NAMEDATALEN;
464 }
465
466 /*
467 * Move named tranche requests to shared memory. This is done for the
468 * benefit of EXEC_BACKEND builds, which otherwise wouldn't be able to
469 * call GetNamedLWLockTranche() outside postmaster.
470 */
472 {
473 /*
474 * Save the pointer to the request array in postmaster's local
475 * memory. We'll need it if we ever need to re-initialize shared
476 * memory after a crash.
477 */
479
484 }
485
486 /* Ensure desired alignment of LWLock array */
489
490 /* Initialize all LWLocks */
492 }
493}

References fb(), i, InitializeLWLocks(), IsUnderPostmaster, LocalNamedLWLockTrancheRequestArray, LWLOCK_PADDED_SIZE, LWLockCounter, LWLockShmemSize(), LWLockTrancheNames, LWTRANCHE_FIRST_USER_DEFINED, MainLWLockArray, MAX_NAMED_TRANCHES, MAXALIGN, NAMEDATALEN, NamedLWLockTrancheRequestArray, NamedLWLockTrancheRequests, and ShmemAlloc().

Referenced by CreateOrAttachShmemStructs().

◆ GetLWLockIdentifier()

const char * GetLWLockIdentifier ( uint32  classId,
uint16  eventId 
)

Definition at line 774 of file lwlock.c.

775{
776 Assert(classId == PG_WAIT_LWLOCK);
777 /* The event IDs are just tranche numbers. */
779}

References Assert, fb(), GetLWTrancheName(), and PG_WAIT_LWLOCK.

Referenced by pgstat_get_wait_event().

◆ GetLWTrancheName()

static const char * GetLWTrancheName ( uint16  trancheId)
static

Definition at line 738 of file lwlock.c.

739{
740 /* Built-in tranche or individual LWLock? */
743
744 /*
745 * We only ever add new entries to LWLockTrancheNames, so most lookups can
746 * avoid taking the spinlock as long as the backend-local counter
747 * (LocalLWLockCounter) is greater than the requested tranche ID. Else,
748 * we need to first update the backend-local counter with ShmemLock held
749 * before attempting the lookup again. In practice, the latter case is
750 * probably rare.
751 */
753 {
757
759 elog(ERROR, "tranche %d is not registered", trancheId);
760 }
761
762 /*
763 * It's an extension tranche, so look in LWLockTrancheNames.
764 */
766
768}

References BuiltinTrancheNames, elog, ERROR, fb(), LocalLWLockCounter, LWLockCounter, LWLockTrancheNames, LWTRANCHE_FIRST_USER_DEFINED, ShmemLock, SpinLockAcquire(), and SpinLockRelease().

Referenced by GetLWLockIdentifier(), and LWLockInitialize().

◆ GetNamedLWLockTranche()

LWLockPadded * GetNamedLWLockTranche ( const char tranche_name)

Definition at line 567 of file lwlock.c.

568{
569 int lock_pos;
570 int i;
571
572 /*
573 * Obtain the position of base address of LWLock belonging to requested
574 * tranche_name in MainLWLockArray. LWLocks for named tranches are placed
575 * in MainLWLockArray after fixed locks.
576 */
578 for (i = 0; i < NamedLWLockTrancheRequests; i++)
579 {
580 if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name,
581 tranche_name) == 0)
582 return &MainLWLockArray[lock_pos];
583
585 }
586
587 elog(ERROR, "requested tranche is not registered");
588
589 /* just to keep compiler quiet */
590 return NULL;
591}

References elog, ERROR, fb(), i, MainLWLockArray, NamedLWLockTrancheRequestArray, NamedLWLockTrancheRequests, NUM_FIXED_LWLOCKS, and NamedLWLockTrancheRequest::num_lwlocks.

Referenced by pgss_shmem_startup(), and test_lwlock_tranche_lookup().

◆ InitializeLWLocks()

static void InitializeLWLocks ( void  )
static

Definition at line 499 of file lwlock.c.

500{
501 int id;
502 int i;
503 int j;
504 LWLockPadded *lock;
505
506 /* Initialize all individual LWLocks in main array */
507 for (id = 0, lock = MainLWLockArray; id < NUM_INDIVIDUAL_LWLOCKS; id++, lock++)
508 LWLockInitialize(&lock->lock, id);
509
510 /* Initialize buffer mapping LWLocks in main array */
512 for (id = 0; id < NUM_BUFFER_PARTITIONS; id++, lock++)
514
515 /* Initialize lmgrs' LWLocks in main array */
517 for (id = 0; id < NUM_LOCK_PARTITIONS; id++, lock++)
519
520 /* Initialize predicate lmgrs' LWLocks in main array */
522 for (id = 0; id < NUM_PREDICATELOCK_PARTITIONS; id++, lock++)
524
525 /*
526 * Copy the info about any named tranches into shared memory (so that
527 * other processes can see it), and initialize the requested LWLocks.
528 */
530 {
532
533 for (i = 0; i < NamedLWLockTrancheRequests; i++)
534 {
536 int tranche;
537
539 tranche = LWLockNewTrancheId(request->tranche_name);
540
541 for (j = 0; j < request->num_lwlocks; j++, lock++)
542 LWLockInitialize(&lock->lock, tranche);
543 }
544 }
545}

References BUFFER_MAPPING_LWLOCK_OFFSET, fb(), i, j, LWLockPadded::lock, LOCK_MANAGER_LWLOCK_OFFSET, LWLockInitialize(), LWLockNewTrancheId(), MainLWLockArray, NamedLWLockTrancheRequestArray, NamedLWLockTrancheRequests, NUM_BUFFER_PARTITIONS, NUM_FIXED_LWLOCKS, NUM_LOCK_PARTITIONS, NUM_PREDICATELOCK_PARTITIONS, and PREDICATELOCK_MANAGER_LWLOCK_OFFSET.

Referenced by CreateLWLocks().

◆ InitLWLockAccess()

void InitLWLockAccess ( void  )

Definition at line 551 of file lwlock.c.

552{
553#ifdef LWLOCK_STATS
555#endif
556}

References fb().

Referenced by InitAuxiliaryProcess(), and InitProcess().

◆ LWLockAcquire()

bool LWLockAcquire ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1177 of file lwlock.c.

1178{
1179 PGPROC *proc = MyProc;
1180 bool result = true;
1181 int extraWaits = 0;
1182#ifdef LWLOCK_STATS
1184
1186#endif
1187
1189
1190 PRINT_LWDEBUG("LWLockAcquire", lock, mode);
1191
1192#ifdef LWLOCK_STATS
1193 /* Count lock acquisition attempts */
1194 if (mode == LW_EXCLUSIVE)
1195 lwstats->ex_acquire_count++;
1196 else
1197 lwstats->sh_acquire_count++;
1198#endif /* LWLOCK_STATS */
1199
1200 /*
1201 * We can't wait if we haven't got a PGPROC. This should only occur
1202 * during bootstrap or shared memory initialization. Put an Assert here
1203 * to catch unsafe coding practices.
1204 */
1205 Assert(!(proc == NULL && IsUnderPostmaster));
1206
1207 /* Ensure we will have room to remember the lock */
1209 elog(ERROR, "too many LWLocks taken");
1210
1211 /*
1212 * Lock out cancel/die interrupts until we exit the code section protected
1213 * by the LWLock. This ensures that interrupts will not interfere with
1214 * manipulations of data structures in shared memory.
1215 */
1217
1218 /*
1219 * Loop here to try to acquire lock after each time we are signaled by
1220 * LWLockRelease.
1221 *
1222 * NOTE: it might seem better to have LWLockRelease actually grant us the
1223 * lock, rather than retrying and possibly having to go back to sleep. But
1224 * in practice that is no good because it means a process swap for every
1225 * lock acquisition when two or more processes are contending for the same
1226 * lock. Since LWLocks are normally used to protect not-very-long
1227 * sections of computation, a process needs to be able to acquire and
1228 * release the same lock many times during a single CPU time slice, even
1229 * in the presence of contention. The efficiency of being able to do that
1230 * outweighs the inefficiency of sometimes wasting a process dispatch
1231 * cycle because the lock is not free when a released waiter finally gets
1232 * to run. See pgsql-hackers archives for 29-Dec-01.
1233 */
1234 for (;;)
1235 {
1236 bool mustwait;
1237
1238 /*
1239 * Try to grab the lock the first time, we're not in the waitqueue
1240 * yet/anymore.
1241 */
1243
1244 if (!mustwait)
1245 {
1246 LOG_LWDEBUG("LWLockAcquire", lock, "immediately acquired lock");
1247 break; /* got the lock */
1248 }
1249
1250 /*
1251 * Ok, at this point we couldn't grab the lock on the first try. We
1252 * cannot simply queue ourselves to the end of the list and wait to be
1253 * woken up because by now the lock could long have been released.
1254 * Instead add us to the queue and try to grab the lock again. If we
1255 * succeed we need to revert the queuing and be happy, otherwise we
1256 * recheck the lock. If we still couldn't grab it, we know that the
1257 * other locker will see our queue entries when releasing since they
1258 * existed before we checked for the lock.
1259 */
1260
1261 /* add to the queue */
1262 LWLockQueueSelf(lock, mode);
1263
1264 /* we're now guaranteed to be woken up if necessary */
1266
1267 /* ok, grabbed the lock the second time round, need to undo queueing */
1268 if (!mustwait)
1269 {
1270 LOG_LWDEBUG("LWLockAcquire", lock, "acquired, undoing queue");
1271
1272 LWLockDequeueSelf(lock);
1273 break;
1274 }
1275
1276 /*
1277 * Wait until awakened.
1278 *
1279 * It is possible that we get awakened for a reason other than being
1280 * signaled by LWLockRelease. If so, loop back and wait again. Once
1281 * we've gotten the LWLock, re-increment the sema by the number of
1282 * additional signals received.
1283 */
1284 LOG_LWDEBUG("LWLockAcquire", lock, "waiting");
1285
1286#ifdef LWLOCK_STATS
1287 lwstats->block_count++;
1288#endif
1289
1293
1294 for (;;)
1295 {
1296 PGSemaphoreLock(proc->sem);
1297 if (proc->lwWaiting == LW_WS_NOT_WAITING)
1298 break;
1299 extraWaits++;
1300 }
1301
1302 /* Retrying, allow LWLockRelease to release waiters again. */
1304
1305#ifdef LOCK_DEBUG
1306 {
1307 /* not waiting anymore */
1309
1311 }
1312#endif
1313
1317
1318 LOG_LWDEBUG("LWLockAcquire", lock, "awakened");
1319
1320 /* Now loop back and try to acquire lock again. */
1321 result = false;
1322 }
1323
1326
1327 /* Add lock to list of locks held by this backend */
1330
1331 /*
1332 * Fix the process wait semaphore's count for any absorbed wakeups.
1333 */
1334 while (extraWaits-- > 0)
1335 PGSemaphoreUnlock(proc->sem);
1336
1337 return result;
1338}

References Assert, elog, ERROR, fb(), held_lwlocks, HOLD_INTERRUPTS, IsUnderPostmaster, LWLockHandle::lock, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_WAKE_IN_PROGRESS, LW_SHARED, LW_WS_NOT_WAITING, LWLockAttemptLock(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockReportWaitEnd(), LWLockReportWaitStart(), PGPROC::lwWaiting, MAX_BACKENDS, MAX_SIMUL_LWLOCKS, LWLockHandle::mode, mode, MyProc, num_held_lwlocks, pg_atomic_fetch_and_u32(), pg_atomic_fetch_sub_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), PRINT_LWDEBUG, PGPROC::sem, LWLock::state, and T_NAME.

Referenced by _bt_end_vacuum(), _bt_parallel_done(), _bt_parallel_primscan_schedule(), _bt_parallel_release(), _bt_parallel_seize(), _bt_start_vacuum(), _bt_vacuum_cycleid(), abort_logical_decoding_activation(), AbsorbSyncRequests(), ActivateCommitTs(), addLSNWaiter(), AdvanceNextFullTransactionIdPastXid(), AdvanceOldestClogXid(), AdvanceOldestCommitTsXid(), AdvanceXLInsertBuffer(), alloc_object(), AlterSystemSetConfigFile(), ApplyLauncherMain(), apw_detach_shmem(), apw_dump_now(), apw_load_buffers(), AsyncNotifyFreezeXids(), asyncQueueAddEntries(), asyncQueueAdvanceTail(), asyncQueueReadAllNotifications(), asyncQueueUnregister(), AtAbort_Twophase(), AtEOXact_LogicalRepWorkers(), AtPrepare_PredicateLocks(), attach_internal(), autoprewarm_main(), autoprewarm_start_worker(), AutoVacLauncherMain(), AutoVacuumRequestWork(), AutoVacWorkerMain(), BackendPidGetProc(), BackendXidGetPid(), BecomeLockGroupLeader(), BecomeLockGroupMember(), BecomeRegisteredListener(), btparallelrescan(), BufferAlloc(), check_for_freed_segments(), CheckDeadLock(), CheckForSerializableConflictOut(), CheckLogicalSlotExists(), CheckPointPredicate(), CheckPointRelationMap(), CheckPointReplicationOrigin(), CheckPointReplicationSlots(), CheckPointTwoPhase(), CheckTableForSerializableConflictIn(), CheckTargetForConflictsIn(), choose_next_subplan_for_leader(), choose_next_subplan_for_worker(), CleanupInvalidationState(), ClearOldPredicateLocks(), ComputeXidHorizons(), consume_xids_shortcut(), copy_replication_slot(), CountDBBackends(), CountDBConnections(), CountOtherDBBackends(), CountUserBackends(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateInitDecodingContext(), CreatePredicateLock(), CreateRestartPoint(), DeactivateCommitTs(), DeleteChildTargetLocks(), DeleteLockTarget(), deleteLSNWaiter(), destroy_superblock(), DisableLogicalDecoding(), DisableLogicalDecodingIfNecessary(), do_autovacuum(), do_pg_backup_start(), do_pg_backup_stop(), do_start_worker(), DropAllPredicateLocksFromTable(), DropTableSpace(), dsa_allocate_extended(), dsa_dump(), dsa_free(), dsa_get_total_size(), dsa_get_total_size_from_handle(), dsa_pin(), dsa_release_in_place(), dsa_set_size_limit(), dsa_trim(), dsa_unpin(), dshash_delete_key(), dshash_dump(), dshash_find(), dshash_find_or_insert_extended(), dshash_seq_next(), dsm_attach(), dsm_create(), dsm_detach(), dsm_pin_segment(), dsm_unpin_segment(), EnableLogicalDecoding(), ensure_active_superblock(), entry_reset(), ExecParallelHashMergeCounters(), ExecParallelHashPopChunkQueue(), ExecParallelHashTupleAlloc(), ExecParallelHashTuplePrealloc(), ExpireAllKnownAssignedTransactionIds(), ExpireOldKnownAssignedTransactionIds(), ExpireTreeKnownAssignedTransactionIds(), ExtendBufferedRelShared(), ExtendCLOG(), ExtendCommitTs(), ExtendMultiXactMember(), ExtendMultiXactOffset(), ExtendSUBTRANS(), FastPathGetRelationLockEntry(), FastPathTransferRelationLocks(), FindAndDropRelationBuffers(), FindDeletedTupleInLocalRel(), FinishPreparedTransaction(), ForceTransactionIdLimitUpdate(), ForwardSyncRequest(), FreeWorkerInfo(), get_local_synced_slots(), get_val_in_shmem(), get_xid_status(), GetBackgroundWorkerPid(), GetBackgroundWorkerTypeByPid(), GetBlockerStatusData(), GetConflictingVirtualXIDs(), GetCurrentVirtualXIDs(), GetLastImportantRecPtr(), GetLastSegSwitchData(), GetLatestCommitTsData(), GetLeaderApplyWorkerPid(), GetLockConflicts(), GetLockStatusData(), GetMultiXactIdMembers(), GetMultiXactInfo(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestMultiXactId(), GetOldestRestartPoint(), GetOldestSafeDecodingTransactionId(), GetOldestUnsummarizedLSN(), GetPredicateLockStatusData(), GetPreparedTransactionList(), GetRunningTransactionData(), GetRunningTransactionLocks(), GetSafeSnapshot(), GetSafeSnapshotBlockingPids(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), GetStrictOldestNonRemovableTransactionId(), GetVirtualXIDsDelayingChkpt(), GetWaitEventCustomIdentifier(), GetWaitEventCustomNames(), GetWalSummarizerState(), HaveVirtualXIDsDelayingChkpt(), init_conflict_slot_xmin(), init_dsm_registry(), initGlobalChannelTable(), InitWalSender(), injection_shmem_startup(), InjectionPointAttach(), InjectionPointDetach(), InjectionPointList(), InstallXLogFileSegment(), InvalidateBuffer(), InvalidateObsoleteReplicationSlots(), InvalidatePossiblyObsoleteSlot(), InvalidateVictimBuffer(), IoWorkerMain(), IsInstallXLogFileSegmentActive(), IsLogicalDecodingEnabled(), IsXLogLogicalInfoEnabled(), KnownAssignedXidsCompress(), KnownAssignedXidsReset(), lock_twophase_recover(), LockAcquireExtended(), LockErrorCleanup(), LockGXact(), LockHasWaiters(), LockRefindAndRelease(), LockRelease(), LockReleaseAll(), LockWaiterCount(), logicalrep_launcher_attach_dshmem(), logicalrep_pa_worker_stop(), logicalrep_reset_seqsync_start_time(), logicalrep_worker_attach(), logicalrep_worker_detach(), logicalrep_worker_launch(), logicalrep_worker_stop(), logicalrep_worker_stop_internal(), logicalrep_worker_wakeup(), logicalrep_workers_find(), LookupGXact(), LookupGXactBySubid(), MarkAsPrepared(), MarkAsPreparing(), multixact_redo(), MultiXactAdvanceNextMXact(), MultiXactGetCheckptMulti(), MultiXactIdSetOldestMember(), MultiXactIdSetOldestVisible(), MultiXactSetNextMXact(), PageIsPredicateLocked(), perform_relmap_update(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_get_replication_slots(), pg_get_shmem_allocations(), pg_get_shmem_allocations_numa(), pg_notification_queue_usage(), pg_show_replication_origin_status(), pg_stat_get_subscription(), pg_stat_statements_internal(), pg_xact_status(), pgaio_worker_die(), pgaio_worker_register(), pgss_shmem_startup(), pgss_store(), pgstat_archiver_reset_all_cb(), pgstat_archiver_snapshot_cb(), pgstat_bgwriter_reset_all_cb(), pgstat_bgwriter_snapshot_cb(), pgstat_build_snapshot(), pgstat_checkpointer_reset_all_cb(), pgstat_checkpointer_snapshot_cb(), pgstat_fetch_replslot(), pgstat_io_flush_cb(), pgstat_io_reset_all_cb(), pgstat_io_snapshot_cb(), pgstat_lock_entry(), pgstat_lock_entry_shared(), pgstat_reset_matching_entries(), pgstat_reset_replslot(), pgstat_reset_slru_counter_internal(), pgstat_slru_flush_cb(), pgstat_slru_snapshot_cb(), pgstat_wal_flush_cb(), pgstat_wal_reset_all_cb(), pgstat_wal_snapshot_cb(), PostPrepare_Locks(), PostPrepare_MultiXact(), PostPrepare_Twophase(), PreCommit_CheckForSerializationFailure(), PreCommit_Notify(), predicatelock_twophase_recover(), PredicateLockPageSplit(), PredicateLockTwoPhaseFinish(), PrefetchSharedBuffer(), PrescanPreparedTransactions(), ProcArrayAdd(), ProcArrayApplyRecoveryInfo(), ProcArrayApplyXidAssignment(), ProcArrayClearTransaction(), ProcArrayEndTransaction(), ProcArrayGetReplicationSlotXmin(), ProcArrayGroupClearXid(), ProcArrayInstallImportedXmin(), ProcArrayInstallRestoredXmin(), ProcArrayRemove(), ProcArraySetReplicationSlotXmin(), ProcessSequencesForSync(), ProcessSyncingTablesForApply(), ProcKill(), ProcNumberGetTransactionIds(), ProcSleep(), ReachedEndOfBackup(), read_relmap_file(), ReadMultiXactIdRange(), ReadNextFullTransactionId(), ReadNextMultiXactId(), ReadReplicationSlot(), RecordNewMultiXact(), RecoverPreparedTransactions(), RegisterDynamicBackgroundWorker(), RegisterPredicateLockingXid(), RelationCacheInitFilePreInvalidate(), RelationMapCopy(), RelationMapFinishBootstrap(), ReleaseOneSerializableXact(), ReleasePredicateLocks(), relmap_redo(), RemoveScratchTarget(), ReplicationSlotAcquire(), ReplicationSlotCleanup(), ReplicationSlotCreate(), ReplicationSlotDropPtr(), ReplicationSlotName(), ReplicationSlotRelease(), ReplicationSlotReserveWal(), ReplicationSlotsComputeLogicalRestartLSN(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotsCountDBSlots(), ReplicationSlotsDropDBSlots(), replorigin_advance(), replorigin_get_progress(), replorigin_session_advance(), replorigin_session_get_progress(), replorigin_session_reset_internal(), replorigin_session_setup(), replorigin_state_clear(), RequestDisableLogicalDecoding(), reserve_wal_for_local_slot(), ResetInstallXLogFileSegmentActive(), resize(), RestoreScratchTarget(), restoreTwoPhaseData(), SaveSlotToPath(), SearchNamedReplicationSlot(), SerialAdd(), SerialGetMinConflictCommitSeqNo(), SerialInit(), SerialSetActiveSerXmin(), set_indexsafe_procflags(), set_val_in_shmem(), SetCommitTsLimit(), SetInstallXLogFileSegmentActive(), SetMultiXactIdLimit(), SetNextObjectId(), SetOldestOffset(), SetTransactionIdLimit(), SetXidCommitTsInPage(), SharedInvalBackendInit(), ShmemInitStruct(), SICleanupQueue(), SIGetDataEntries(), SignalBackends(), SignalRecoveryConflict(), SignalRecoveryConflictWithDatabase(), SignalRecoveryConflictWithVirtualXID(), SIInsertDataEntries(), SimpleLruReadPage(), SimpleLruReadPage_ReadOnly(), SimpleLruTruncate(), SimpleLruWaitIO(), SimpleLruWriteAll(), SimpleLruZeroAndWritePage(), SlruDeleteSegment(), SlruInternalWritePage(), SnapBuildInitialSnapshot(), ss_get_location(), StandbyRecoverPreparedTransactions(), StandbySlotsHaveCaughtup(), StartupDecodingContext(), StartupSUBTRANS(), StartupXLOG(), sts_parallel_scan_next(), SubTransSetParent(), SummarizeOldestCommittedSxact(), SummarizeWAL(), SwitchIntoArchiveRecovery(), synchronize_one_slot(), SyncRepCancelWait(), SyncRepCleanupAtProcExit(), SyncRepReleaseWaiters(), SyncRepUpdateSyncStandbysDefined(), SyncRepWaitForLSN(), TablespaceCreateDbspace(), tbm_shared_iterate(), TerminateBackgroundWorker(), TerminateBackgroundWorkersForDatabase(), TerminateOtherDBBackends(), test_aio_shmem_startup(), test_custom_stats_fixed_reset_all_cb(), test_custom_stats_fixed_snapshot_cb(), test_custom_stats_fixed_update(), test_slru_page_exists(), test_slru_page_read(), test_slru_page_write(), TransactionGroupUpdateXidStatus(), TransactionIdGetCommitTsData(), TransactionIdIsInProgress(), TransactionIdSetPageStatus(), TransactionTreeSetCommitTsData(), TransferPredicateLocksToNewTarget(), TrimCLOG(), TrimMultiXact(), TruncateMultiXact(), TwoPhaseGetGXact(), TwoPhaseGetOldestXidInCommit(), TwoPhaseGetXidByVirtualXID(), update_cached_xid_range(), update_synced_slots_inactive_since(), UpdateLogicalDecodingStatusEndOfRecovery(), UpdateMinRecoveryPoint(), vac_truncate_clog(), vacuum_rel(), VacuumUpdateCosts(), VirtualXactLock(), VirtualXactLockTableCleanup(), VirtualXactLockTableInsert(), wait_for_table_state_change(), wait_for_worker_state_change(), WaitEventCustomNew(), WaitForReplicationWorkerAttach(), WaitForWalSummarization(), wakeupWaiters(), WakeupWalSummarizer(), WALInsertLockAcquire(), WALInsertLockAcquireExclusive(), WalSummarizerMain(), WalSummarizerShutdown(), write_relcache_init_file(), xact_redo(), XidCacheRemoveRunningXids(), xlog_redo(), XLogBackgroundFlush(), and XLogReportParameters().

◆ LWLockAcquireOrWait()

bool LWLockAcquireOrWait ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1405 of file lwlock.c.

1406{
1407 PGPROC *proc = MyProc;
1408 bool mustwait;
1409 int extraWaits = 0;
1410#ifdef LWLOCK_STATS
1412
1414#endif
1415
1417
1418 PRINT_LWDEBUG("LWLockAcquireOrWait", lock, mode);
1419
1420 /* Ensure we will have room to remember the lock */
1422 elog(ERROR, "too many LWLocks taken");
1423
1424 /*
1425 * Lock out cancel/die interrupts until we exit the code section protected
1426 * by the LWLock. This ensures that interrupts will not interfere with
1427 * manipulations of data structures in shared memory.
1428 */
1430
1431 /*
1432 * NB: We're using nearly the same twice-in-a-row lock acquisition
1433 * protocol as LWLockAcquire(). Check its comments for details.
1434 */
1436
1437 if (mustwait)
1438 {
1440
1442
1443 if (mustwait)
1444 {
1445 /*
1446 * Wait until awakened. Like in LWLockAcquire, be prepared for
1447 * bogus wakeups.
1448 */
1449 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "waiting");
1450
1451#ifdef LWLOCK_STATS
1452 lwstats->block_count++;
1453#endif
1454
1458
1459 for (;;)
1460 {
1461 PGSemaphoreLock(proc->sem);
1462 if (proc->lwWaiting == LW_WS_NOT_WAITING)
1463 break;
1464 extraWaits++;
1465 }
1466
1467#ifdef LOCK_DEBUG
1468 {
1469 /* not waiting anymore */
1471
1473 }
1474#endif
1478
1479 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "awakened");
1480 }
1481 else
1482 {
1483 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "acquired, undoing queue");
1484
1485 /*
1486 * Got lock in the second attempt, undo queueing. We need to treat
1487 * this as having successfully acquired the lock, otherwise we'd
1488 * not necessarily wake up people we've prevented from acquiring
1489 * the lock.
1490 */
1491 LWLockDequeueSelf(lock);
1492 }
1493 }
1494
1495 /*
1496 * Fix the process wait semaphore's count for any absorbed wakeups.
1497 */
1498 while (extraWaits-- > 0)
1499 PGSemaphoreUnlock(proc->sem);
1500
1501 if (mustwait)
1502 {
1503 /* Failed to get lock, so release interrupt holdoff */
1505 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "failed");
1508 }
1509 else
1510 {
1511 LOG_LWDEBUG("LWLockAcquireOrWait", lock, "succeeded");
1512 /* Add lock to list of locks held by this backend */
1517 }
1518
1519 return !mustwait;
1520}

References Assert, elog, ERROR, fb(), held_lwlocks, HOLD_INTERRUPTS, LWLockHandle::lock, LOG_LWDEBUG, LW_EXCLUSIVE, LW_SHARED, LW_WAIT_UNTIL_FREE, LW_WS_NOT_WAITING, LWLockAttemptLock(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockReportWaitEnd(), LWLockReportWaitStart(), PGPROC::lwWaiting, MAX_BACKENDS, MAX_SIMUL_LWLOCKS, LWLockHandle::mode, mode, MyProc, num_held_lwlocks, pg_atomic_fetch_sub_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), PRINT_LWDEBUG, RESUME_INTERRUPTS, PGPROC::sem, and T_NAME.

Referenced by XLogFlush().

◆ LWLockAnyHeldByMe()

bool LWLockAnyHeldByMe ( LWLock lock,
int  nlocks,
size_t  stride 
)

Definition at line 1930 of file lwlock.c.

1931{
1932 char *held_lock_addr;
1933 char *begin;
1934 char *end;
1935 int i;
1936
1937 begin = (char *) lock;
1938 end = begin + nlocks * stride;
1939 for (i = 0; i < num_held_lwlocks; i++)
1940 {
1941 held_lock_addr = (char *) held_lwlocks[i].lock;
1942 if (held_lock_addr >= begin &&
1943 held_lock_addr < end &&
1944 (held_lock_addr - begin) % stride == 0)
1945 return true;
1946 }
1947 return false;
1948}

References fb(), held_lwlocks, i, and num_held_lwlocks.

◆ LWLockAttemptLock()

static bool LWLockAttemptLock ( LWLock lock,
LWLockMode  mode 
)
static

Definition at line 791 of file lwlock.c.

792{
794
796
797 /*
798 * Read once outside the loop, later iterations will get the newer value
799 * via compare & exchange.
800 */
802
803 /* loop until we've determined whether we could acquire the lock or not */
804 while (true)
805 {
807 bool lock_free;
808
810
811 if (mode == LW_EXCLUSIVE)
812 {
814 if (lock_free)
816 }
817 else
818 {
820 if (lock_free)
822 }
823
824 /*
825 * Attempt to swap in the state we are expecting. If we didn't see
826 * lock to be free, that's just the old value. If we saw it as free,
827 * we'll attempt to mark it acquired. The reason that we always swap
828 * in the value is that this doubles as a memory barrier. We could try
829 * to be smarter and only swap in values if we saw the lock as free,
830 * but benchmark haven't shown it as beneficial so far.
831 *
832 * Retry if the value changed since we last looked at it.
833 */
836 {
837 if (lock_free)
838 {
839 /* Great! Got the lock. */
840#ifdef LOCK_DEBUG
841 if (mode == LW_EXCLUSIVE)
842 lock->owner = MyProc;
843#endif
844 return false;
845 }
846 else
847 return true; /* somebody else has the lock */
848 }
849 }
851}

References Assert, fb(), LW_EXCLUSIVE, LW_LOCK_MASK, LW_SHARED, LW_VAL_EXCLUSIVE, LW_VAL_SHARED, mode, MyProc, pg_atomic_compare_exchange_u32(), pg_atomic_read_u32(), pg_unreachable, and LWLock::state.

Referenced by LWLockAcquire(), LWLockAcquireOrWait(), and LWLockConditionalAcquire().

◆ LWLockConditionalAcquire()

bool LWLockConditionalAcquire ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1348 of file lwlock.c.

1349{
1350 bool mustwait;
1351
1353
1354 PRINT_LWDEBUG("LWLockConditionalAcquire", lock, mode);
1355
1356 /* Ensure we will have room to remember the lock */
1358 elog(ERROR, "too many LWLocks taken");
1359
1360 /*
1361 * Lock out cancel/die interrupts until we exit the code section protected
1362 * by the LWLock. This ensures that interrupts will not interfere with
1363 * manipulations of data structures in shared memory.
1364 */
1366
1367 /* Check for the lock */
1369
1370 if (mustwait)
1371 {
1372 /* Failed to get lock, so release interrupt holdoff */
1374
1375 LOG_LWDEBUG("LWLockConditionalAcquire", lock, "failed");
1378 }
1379 else
1380 {
1381 /* Add lock to list of locks held by this backend */
1386 }
1387 return !mustwait;
1388}

References Assert, elog, ERROR, fb(), held_lwlocks, HOLD_INTERRUPTS, LWLockHandle::lock, LOG_LWDEBUG, LW_EXCLUSIVE, LW_SHARED, LWLockAttemptLock(), MAX_SIMUL_LWLOCKS, LWLockHandle::mode, mode, num_held_lwlocks, PRINT_LWDEBUG, RESUME_INTERRUPTS, and T_NAME.

Referenced by pgaio_worker_submit_internal(), pgstat_io_flush_cb(), pgstat_lock_entry(), pgstat_lock_entry_shared(), pgstat_slru_flush_cb(), pgstat_wal_flush_cb(), ProcArrayEndTransaction(), SimpleLruWaitIO(), ss_report_location(), TransactionIdSetPageStatus(), and XLogNeedsFlush().

◆ LWLockConflictsWithVar()

static bool LWLockConflictsWithVar ( LWLock lock,
pg_atomic_uint64 valptr,
uint64  oldval,
uint64 newval,
bool result 
)
static

Definition at line 1532 of file lwlock.c.

1534{
1535 bool mustwait;
1536 uint64 value;
1537
1538 /*
1539 * Test first to see if it the slot is free right now.
1540 *
1541 * XXX: the unique caller of this routine, WaitXLogInsertionsToFinish()
1542 * via LWLockWaitForVar(), uses an implied barrier with a spinlock before
1543 * this, so we don't need a memory barrier here as far as the current
1544 * usage is concerned. But that might not be safe in general.
1545 */
1547
1548 if (!mustwait)
1549 {
1550 *result = true;
1551 return false;
1552 }
1553
1554 *result = false;
1555
1556 /*
1557 * Reading this value atomically is safe even on platforms where uint64
1558 * cannot be read without observing a torn value.
1559 */
1561
1562 if (value != oldval)
1563 {
1564 mustwait = false;
1565 *newval = value;
1566 }
1567 else
1568 {
1569 mustwait = true;
1570 }
1571
1572 return mustwait;
1573}

References fb(), LW_VAL_EXCLUSIVE, newval, pg_atomic_read_u32(), pg_atomic_read_u64(), LWLock::state, and value.

Referenced by LWLockWaitForVar().

◆ LWLockDequeueSelf()

static void LWLockDequeueSelf ( LWLock lock)
static

Definition at line 1088 of file lwlock.c.

1089{
1090 bool on_waitlist;
1091
1092#ifdef LWLOCK_STATS
1094
1096
1097 lwstats->dequeue_self_count++;
1098#endif
1099
1100 LWLockWaitListLock(lock);
1101
1102 /*
1103 * Remove ourselves from the waitlist, unless we've already been removed.
1104 * The removal happens with the wait list lock held, so there's no race in
1105 * this check.
1106 */
1108 if (on_waitlist)
1109 proclist_delete(&lock->waiters, MyProcNumber, lwWaitLink);
1110
1111 if (proclist_is_empty(&lock->waiters) &&
1113 {
1115 }
1116
1117 /* XXX: combine with fetch_and above? */
1119
1120 /* clear waiting state again, nice for debugging */
1121 if (on_waitlist)
1123 else
1124 {
1125 int extraWaits = 0;
1126
1127 /*
1128 * Somebody else dequeued us and has or will wake us up. Deal with the
1129 * superfluous absorption of a wakeup.
1130 */
1131
1132 /*
1133 * Clear LW_FLAG_WAKE_IN_PROGRESS if somebody woke us before we
1134 * removed ourselves - they'll have set it.
1135 */
1137
1138 /*
1139 * Now wait for the scheduled wakeup, otherwise our ->lwWaiting would
1140 * get reset at some inconvenient point later. Most of the time this
1141 * will immediately return.
1142 */
1143 for (;;)
1144 {
1147 break;
1148 extraWaits++;
1149 }
1150
1151 /*
1152 * Fix the process wait semaphore's count for any absorbed wakeups.
1153 */
1154 while (extraWaits-- > 0)
1156 }
1157
1158#ifdef LOCK_DEBUG
1159 {
1160 /* not waiting anymore */
1162
1164 }
1165#endif
1166}

References Assert, fb(), LW_FLAG_HAS_WAITERS, LW_FLAG_WAKE_IN_PROGRESS, LW_WS_NOT_WAITING, LW_WS_WAITING, LWLockWaitListLock(), LWLockWaitListUnlock(), PGPROC::lwWaiting, MAX_BACKENDS, MyProc, MyProcNumber, pg_atomic_fetch_and_u32(), pg_atomic_fetch_sub_u32(), pg_atomic_read_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), proclist_delete, proclist_is_empty(), PGPROC::sem, LWLock::state, and LWLock::waiters.

Referenced by LWLockAcquire(), LWLockAcquireOrWait(), and LWLockWaitForVar().

◆ LWLockHeldByMe()

◆ LWLockHeldByMeInMode()

◆ LWLockInitialize()

◆ LWLockNewTrancheId()

int LWLockNewTrancheId ( const char name)

Definition at line 597 of file lwlock.c.

598{
599 int result;
600
601 if (!name)
604 errmsg("tranche name cannot be NULL")));
605
606 if (strlen(name) >= NAMEDATALEN)
609 errmsg("tranche name too long"),
610 errdetail("LWLock tranche names must be no longer than %d bytes.",
611 NAMEDATALEN - 1)));
612
613 /*
614 * We use the ShmemLock spinlock to protect LWLockCounter and
615 * LWLockTrancheNames.
616 */
618
620 {
623 (errmsg("maximum number of tranches already registered"),
624 errdetail("No more than %d tranches may be registered.",
626 }
627
628 result = (*LWLockCounter)++;
631
633
634 return result;
635}

References ereport, errcode(), errdetail(), errmsg, ERROR, fb(), LocalLWLockCounter, LWLockCounter, LWLockTrancheNames, LWTRANCHE_FIRST_USER_DEFINED, MAX_NAMED_TRANCHES, name, NAMEDATALEN, ShmemLock, SpinLockAcquire(), SpinLockRelease(), and strlcpy().

Referenced by apw_init_state(), GetNamedDSA(), GetNamedDSHash(), init_tdr_dsm(), init_tranche(), InitializeLWLocks(), test_basic(), test_create(), test_empty(), test_lwlock_tranche_creation(), test_lwlock_tranches(), test_random(), and test_slru_shmem_startup().

◆ LWLockQueueSelf()

static void LWLockQueueSelf ( LWLock lock,
LWLockMode  mode 
)
static

Definition at line 1045 of file lwlock.c.

1046{
1047 /*
1048 * If we don't have a PGPROC structure, there's no way to wait. This
1049 * should never occur, since MyProc should only be null during shared
1050 * memory initialization.
1051 */
1052 if (MyProc == NULL)
1053 elog(PANIC, "cannot wait without a PGPROC structure");
1054
1056 elog(PANIC, "queueing for lock while waiting on another one");
1057
1058 LWLockWaitListLock(lock);
1059
1060 /* setting the flag is protected by the spinlock */
1062
1065
1066 /* LW_WAIT_UNTIL_FREE waiters are always at the front of the queue */
1067 if (mode == LW_WAIT_UNTIL_FREE)
1068 proclist_push_head(&lock->waiters, MyProcNumber, lwWaitLink);
1069 else
1070 proclist_push_tail(&lock->waiters, MyProcNumber, lwWaitLink);
1071
1072 /* Can release the mutex now */
1074
1075#ifdef LOCK_DEBUG
1076 pg_atomic_fetch_add_u32(&lock->nwaiters, 1);
1077#endif
1078}

References elog, fb(), LW_FLAG_HAS_WAITERS, LW_WAIT_UNTIL_FREE, LW_WS_NOT_WAITING, LW_WS_WAITING, LWLockWaitListLock(), LWLockWaitListUnlock(), PGPROC::lwWaiting, PGPROC::lwWaitMode, mode, MyProc, MyProcNumber, PANIC, pg_atomic_fetch_add_u32(), pg_atomic_fetch_or_u32(), proclist_push_head, proclist_push_tail, LWLock::state, and LWLock::waiters.

Referenced by LWLockAcquire(), LWLockAcquireOrWait(), and LWLockWaitForVar().

◆ LWLockRelease()

void LWLockRelease ( LWLock lock)

Definition at line 1794 of file lwlock.c.

1795{
1798 bool check_waiters;
1799 int i;
1800
1801 /*
1802 * Remove lock from list of locks held. Usually, but not always, it will
1803 * be the latest-acquired lock; so search array backwards.
1804 */
1805 for (i = num_held_lwlocks; --i >= 0;)
1806 if (lock == held_lwlocks[i].lock)
1807 break;
1808
1809 if (i < 0)
1810 elog(ERROR, "lock %s is not held", T_NAME(lock));
1811
1813
1815 for (; i < num_held_lwlocks; i++)
1816 held_lwlocks[i] = held_lwlocks[i + 1];
1817
1818 PRINT_LWDEBUG("LWLockRelease", lock, mode);
1819
1820 /*
1821 * Release my hold on lock, after that it can immediately be acquired by
1822 * others, even if we still have to wakeup other waiters.
1823 */
1824 if (mode == LW_EXCLUSIVE)
1826 else
1828
1829 /* nobody else can have that kind of lock */
1831
1834
1835 /*
1836 * Check if we're still waiting for backends to get scheduled, if so,
1837 * don't wake them up again.
1838 */
1839 if ((oldstate & LW_FLAG_HAS_WAITERS) &&
1841 (oldstate & LW_LOCK_MASK) == 0)
1842 check_waiters = true;
1843 else
1844 check_waiters = false;
1845
1846 /*
1847 * As waking up waiters requires the spinlock to be acquired, only do so
1848 * if necessary.
1849 */
1850 if (check_waiters)
1851 {
1852 /* XXX: remove before commit? */
1853 LOG_LWDEBUG("LWLockRelease", lock, "releasing waiters");
1854 LWLockWakeup(lock);
1855 }
1856
1857 /*
1858 * Now okay to allow cancel/die interrupts.
1859 */
1861}

References Assert, elog, ERROR, fb(), held_lwlocks, i, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_HAS_WAITERS, LW_FLAG_WAKE_IN_PROGRESS, LW_LOCK_MASK, LW_VAL_EXCLUSIVE, LW_VAL_SHARED, LWLockWakeup(), LWLockHandle::mode, mode, num_held_lwlocks, pg_atomic_sub_fetch_u32(), PRINT_LWDEBUG, RESUME_INTERRUPTS, LWLock::state, and T_NAME.

Referenced by _bt_end_vacuum(), _bt_parallel_done(), _bt_parallel_primscan_schedule(), _bt_parallel_release(), _bt_parallel_seize(), _bt_start_vacuum(), _bt_vacuum_cycleid(), abort_logical_decoding_activation(), AbsorbSyncRequests(), ActivateCommitTs(), addLSNWaiter(), AdvanceNextFullTransactionIdPastXid(), AdvanceOldestClogXid(), AdvanceOldestCommitTsXid(), AdvanceXLInsertBuffer(), alloc_object(), AlterSystemSetConfigFile(), ApplyLauncherMain(), apw_detach_shmem(), apw_dump_now(), apw_load_buffers(), AsyncNotifyFreezeXids(), asyncQueueAddEntries(), asyncQueueAdvanceTail(), asyncQueueProcessPageEntries(), asyncQueueReadAllNotifications(), asyncQueueUnregister(), AtAbort_Twophase(), AtEOXact_LogicalRepWorkers(), AtPrepare_PredicateLocks(), attach_internal(), autoprewarm_main(), autoprewarm_start_worker(), AutoVacLauncherMain(), AutoVacuumRequestWork(), AutoVacWorkerMain(), BackendPidGetProc(), BackendXidGetPid(), BecomeLockGroupLeader(), BecomeLockGroupMember(), BecomeRegisteredListener(), btparallelrescan(), BufferAlloc(), check_for_freed_segments(), CheckDeadLock(), CheckForSerializableConflictOut(), CheckLogicalSlotExists(), CheckPointPredicate(), CheckPointRelationMap(), CheckPointReplicationOrigin(), CheckPointReplicationSlots(), CheckPointTwoPhase(), CheckTableForSerializableConflictIn(), CheckTargetForConflictsIn(), choose_next_subplan_for_leader(), choose_next_subplan_for_worker(), CleanupInvalidationState(), ClearOldPredicateLocks(), ComputeXidHorizons(), consume_xids_shortcut(), copy_replication_slot(), CountDBBackends(), CountDBConnections(), CountOtherDBBackends(), CountUserBackends(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateInitDecodingContext(), CreatePredicateLock(), CreateRestartPoint(), DeactivateCommitTs(), DeleteChildTargetLocks(), DeleteLockTarget(), deleteLSNWaiter(), destroy_superblock(), DisableLogicalDecoding(), DisableLogicalDecodingIfNecessary(), do_autovacuum(), do_pg_backup_start(), do_pg_backup_stop(), do_start_worker(), DropAllPredicateLocksFromTable(), DropTableSpace(), dsa_allocate_extended(), dsa_dump(), dsa_free(), dsa_get_total_size(), dsa_get_total_size_from_handle(), dsa_pin(), dsa_release_in_place(), dsa_set_size_limit(), dsa_trim(), dsa_unpin(), dshash_delete_entry(), dshash_delete_key(), dshash_dump(), dshash_find(), dshash_find_or_insert_extended(), dshash_release_lock(), dshash_seq_next(), dshash_seq_term(), dsm_attach(), dsm_create(), dsm_detach(), dsm_pin_segment(), dsm_unpin_segment(), EnableLogicalDecoding(), ensure_active_superblock(), entry_reset(), ExecParallelHashMergeCounters(), ExecParallelHashPopChunkQueue(), ExecParallelHashTupleAlloc(), ExecParallelHashTuplePrealloc(), ExpireAllKnownAssignedTransactionIds(), ExpireOldKnownAssignedTransactionIds(), ExpireTreeKnownAssignedTransactionIds(), ExtendBufferedRelShared(), ExtendCLOG(), ExtendCommitTs(), ExtendMultiXactMember(), ExtendMultiXactOffset(), ExtendSUBTRANS(), FastPathGetRelationLockEntry(), FastPathTransferRelationLocks(), find_multixact_start(), FindAndDropRelationBuffers(), FindDeletedTupleInLocalRel(), FinishPreparedTransaction(), ForceTransactionIdLimitUpdate(), ForwardSyncRequest(), FreeWorkerInfo(), get_local_synced_slots(), get_val_in_shmem(), get_xid_status(), GetBackgroundWorkerPid(), GetBackgroundWorkerTypeByPid(), GetBlockerStatusData(), GetConflictingVirtualXIDs(), GetCurrentVirtualXIDs(), GetLastImportantRecPtr(), GetLastSegSwitchData(), GetLatestCommitTsData(), GetLeaderApplyWorkerPid(), GetLockConflicts(), GetLockStatusData(), GetMultiXactIdMembers(), GetMultiXactInfo(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestMultiXactId(), GetOldestRestartPoint(), GetOldestSafeDecodingTransactionId(), GetOldestUnsummarizedLSN(), GetPredicateLockStatusData(), GetPreparedTransactionList(), GetRunningTransactionLocks(), GetSafeSnapshot(), GetSafeSnapshotBlockingPids(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), GetStrictOldestNonRemovableTransactionId(), GetVirtualXIDsDelayingChkpt(), GetWaitEventCustomIdentifier(), GetWaitEventCustomNames(), GetWalSummarizerState(), HaveVirtualXIDsDelayingChkpt(), init_conflict_slot_xmin(), init_dsm_registry(), initGlobalChannelTable(), InitWalSender(), injection_shmem_startup(), InjectionPointAttach(), InjectionPointDetach(), InjectionPointList(), InstallXLogFileSegment(), InvalidateBuffer(), InvalidateObsoleteReplicationSlots(), InvalidatePossiblyObsoleteSlot(), InvalidateVictimBuffer(), IoWorkerMain(), IsInstallXLogFileSegmentActive(), IsLogicalDecodingEnabled(), IsXLogLogicalInfoEnabled(), KnownAssignedXidsCompress(), KnownAssignedXidsReset(), lock_twophase_recover(), LockAcquireExtended(), LockErrorCleanup(), LockGXact(), LockHasWaiters(), LockRefindAndRelease(), LockRelease(), LockReleaseAll(), LockWaiterCount(), logicalrep_launcher_attach_dshmem(), logicalrep_pa_worker_stop(), logicalrep_reset_seqsync_start_time(), logicalrep_worker_attach(), logicalrep_worker_detach(), logicalrep_worker_launch(), logicalrep_worker_stop(), logicalrep_worker_stop_internal(), logicalrep_worker_wakeup(), logicalrep_workers_find(), LogStandbySnapshot(), LookupGXact(), LookupGXactBySubid(), LWLockReleaseAll(), LWLockReleaseClearVar(), MarkAsPrepared(), MarkAsPreparing(), multixact_redo(), MultiXactAdvanceNextMXact(), MultiXactGetCheckptMulti(), MultiXactIdSetOldestMember(), MultiXactIdSetOldestVisible(), MultiXactSetNextMXact(), OnConflict_CheckForSerializationFailure(), PageIsPredicateLocked(), perform_relmap_update(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_get_replication_slots(), pg_get_shmem_allocations(), pg_get_shmem_allocations_numa(), pg_notification_queue_usage(), pg_show_replication_origin_status(), pg_stat_get_subscription(), pg_stat_statements_internal(), pg_xact_status(), pgaio_worker_die(), pgaio_worker_register(), pgaio_worker_submit_internal(), pgss_shmem_startup(), pgss_store(), pgstat_archiver_reset_all_cb(), pgstat_archiver_snapshot_cb(), pgstat_bgwriter_reset_all_cb(), pgstat_bgwriter_snapshot_cb(), pgstat_build_snapshot(), pgstat_checkpointer_reset_all_cb(), pgstat_checkpointer_snapshot_cb(), pgstat_fetch_replslot(), pgstat_io_flush_cb(), pgstat_io_reset_all_cb(), pgstat_io_snapshot_cb(), pgstat_reset_matching_entries(), pgstat_reset_replslot(), pgstat_reset_slru_counter_internal(), pgstat_slru_flush_cb(), pgstat_slru_snapshot_cb(), pgstat_unlock_entry(), pgstat_wal_flush_cb(), pgstat_wal_reset_all_cb(), pgstat_wal_snapshot_cb(), PostPrepare_Locks(), PostPrepare_MultiXact(), PostPrepare_Twophase(), PreCommit_CheckForSerializationFailure(), PreCommit_Notify(), predicatelock_twophase_recover(), PredicateLockPageSplit(), PredicateLockTwoPhaseFinish(), PrefetchSharedBuffer(), PrescanPreparedTransactions(), ProcArrayAdd(), ProcArrayApplyRecoveryInfo(), ProcArrayApplyXidAssignment(), ProcArrayClearTransaction(), ProcArrayEndTransaction(), ProcArrayGetReplicationSlotXmin(), ProcArrayGroupClearXid(), ProcArrayInstallImportedXmin(), ProcArrayInstallRestoredXmin(), ProcArrayRemove(), ProcArraySetReplicationSlotXmin(), ProcessSequencesForSync(), ProcessSyncingTablesForApply(), ProcKill(), ProcNumberGetTransactionIds(), ProcSleep(), ReachedEndOfBackup(), read_relmap_file(), ReadMultiXactIdRange(), ReadNextFullTransactionId(), ReadNextMultiXactId(), ReadReplicationSlot(), RecordNewMultiXact(), RecoverPreparedTransactions(), RegisterDynamicBackgroundWorker(), RegisterPredicateLockingXid(), RelationCacheInitFilePostInvalidate(), RelationMapCopy(), RelationMapFinishBootstrap(), ReleaseOneSerializableXact(), ReleasePredicateLocks(), relmap_redo(), RemoveScratchTarget(), ReplicationSlotAcquire(), ReplicationSlotCleanup(), ReplicationSlotCreate(), ReplicationSlotDropPtr(), ReplicationSlotName(), ReplicationSlotRelease(), ReplicationSlotReserveWal(), ReplicationSlotsComputeLogicalRestartLSN(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotsCountDBSlots(), ReplicationSlotsDropDBSlots(), replorigin_advance(), replorigin_get_progress(), replorigin_session_advance(), replorigin_session_get_progress(), replorigin_session_reset_internal(), replorigin_session_setup(), replorigin_state_clear(), RequestDisableLogicalDecoding(), reserve_wal_for_local_slot(), ResetInstallXLogFileSegmentActive(), resize(), RestoreScratchTarget(), restoreTwoPhaseData(), SaveSlotToPath(), SearchNamedReplicationSlot(), SerialAdd(), SerialGetMinConflictCommitSeqNo(), SerialInit(), SerialSetActiveSerXmin(), set_indexsafe_procflags(), set_val_in_shmem(), SetCommitTsLimit(), SetInstallXLogFileSegmentActive(), SetMultiXactIdLimit(), SetNextObjectId(), SetOldestOffset(), SetTransactionIdLimit(), SetXidCommitTsInPage(), SharedInvalBackendInit(), ShmemInitStruct(), SICleanupQueue(), SIGetDataEntries(), SignalBackends(), SignalRecoveryConflict(), SignalRecoveryConflictWithDatabase(), SignalRecoveryConflictWithVirtualXID(), SIInsertDataEntries(), SimpleLruReadPage(), SimpleLruReadPage_ReadOnly(), SimpleLruTruncate(), SimpleLruWaitIO(), SimpleLruWriteAll(), SimpleLruZeroAndWritePage(), SlruDeleteSegment(), SlruInternalWritePage(), SnapBuildInitialSnapshot(), ss_get_location(), ss_report_location(), StandbyRecoverPreparedTransactions(), StandbySlotsHaveCaughtup(), StartupDecodingContext(), StartupSUBTRANS(), StartupXLOG(), sts_parallel_scan_next(), SubTransGetParent(), SubTransSetParent(), SummarizeOldestCommittedSxact(), SummarizeWAL(), SwitchIntoArchiveRecovery(), synchronize_one_slot(), SyncRepCancelWait(), SyncRepCleanupAtProcExit(), SyncRepReleaseWaiters(), SyncRepUpdateSyncStandbysDefined(), SyncRepWaitForLSN(), TablespaceCreateDbspace(), tbm_shared_iterate(), TerminateBackgroundWorker(), TerminateBackgroundWorkersForDatabase(), TerminateOtherDBBackends(), test_aio_shmem_startup(), test_custom_stats_fixed_reset_all_cb(), test_custom_stats_fixed_snapshot_cb(), test_custom_stats_fixed_update(), test_slru_page_exists(), test_slru_page_read(), test_slru_page_readonly(), test_slru_page_write(), TransactionGroupUpdateXidStatus(), TransactionIdGetCommitTsData(), TransactionIdGetStatus(), TransactionIdIsInProgress(), TransactionIdSetPageStatus(), TransactionTreeSetCommitTsData(), TransferPredicateLocksToNewTarget(), TrimCLOG(), TrimMultiXact(), TruncateMultiXact(), TwoPhaseGetGXact(), TwoPhaseGetOldestXidInCommit(), TwoPhaseGetXidByVirtualXID(), update_cached_xid_range(), update_synced_slots_inactive_since(), UpdateLogicalDecodingStatusEndOfRecovery(), UpdateMinRecoveryPoint(), vac_truncate_clog(), vacuum_rel(), VacuumUpdateCosts(), VirtualXactLock(), VirtualXactLockTableCleanup(), VirtualXactLockTableInsert(), wait_for_table_state_change(), wait_for_worker_state_change(), WaitEventCustomNew(), WaitForReplicationWorkerAttach(), WaitForWalSummarization(), wakeupWaiters(), WakeupWalSummarizer(), WalSummarizerMain(), WalSummarizerShutdown(), write_relcache_init_file(), xact_redo(), XidCacheRemoveRunningXids(), xlog_redo(), XLogBackgroundFlush(), XLogFlush(), XLogNeedsFlush(), and XLogReportParameters().

◆ LWLockReleaseAll()

◆ LWLockReleaseClearVar()

void LWLockReleaseClearVar ( LWLock lock,
pg_atomic_uint64 valptr,
uint64  val 
)

Definition at line 1867 of file lwlock.c.

1868{
1869 /*
1870 * Note that pg_atomic_exchange_u64 is a full barrier, so we're guaranteed
1871 * that the variable is updated before releasing the lock.
1872 */
1874
1875 LWLockRelease(lock);
1876}

References fb(), LWLockRelease(), pg_atomic_exchange_u64(), and val.

Referenced by WALInsertLockRelease().

◆ LWLockReportWaitEnd()

static void LWLockReportWaitEnd ( void  )
inlinestatic

Definition at line 729 of file lwlock.c.

730{
732}

References pgstat_report_wait_end().

Referenced by LWLockAcquire(), LWLockAcquireOrWait(), and LWLockWaitForVar().

◆ LWLockReportWaitStart()

static void LWLockReportWaitStart ( LWLock lock)
inlinestatic

◆ LWLockShmemSize()

Size LWLockShmemSize ( void  )

Definition at line 398 of file lwlock.c.

399{
400 Size size;
401 int numLocks = NUM_FIXED_LWLOCKS;
402
403 /*
404 * If re-initializing shared memory, the request array will no longer be
405 * accessible, so switch to the copy in postmaster's local memory. We'll
406 * copy it back into shared memory later when CreateLWLocks() is called
407 * again.
408 */
411
412 /* Calculate total number of locks needed in the main array. */
413 numLocks += NumLWLocksForNamedTranches();
414
415 /* Space for dynamic allocation counter. */
416 size = MAXALIGN(sizeof(int));
417
418 /* Space for named tranches. */
419 size = add_size(size, mul_size(MAX_NAMED_TRANCHES, sizeof(char *)));
421
422 /*
423 * Make space for named tranche requests. This is done for the benefit of
424 * EXEC_BACKEND builds, which otherwise wouldn't be able to call
425 * GetNamedLWLockTranche() outside postmaster.
426 */
429
430 /* Space for the LWLock array, plus room for cache line alignment. */
431 size = add_size(size, LWLOCK_PADDED_SIZE);
432 size = add_size(size, mul_size(numLocks, sizeof(LWLockPadded)));
433
434 return size;
435}

References add_size(), LocalNamedLWLockTrancheRequestArray, LWLOCK_PADDED_SIZE, MAX_NAMED_TRANCHES, MAXALIGN, mul_size(), NAMEDATALEN, NamedLWLockTrancheRequestArray, NamedLWLockTrancheRequests, NUM_FIXED_LWLOCKS, and NumLWLocksForNamedTranches().

Referenced by CalculateShmemSize(), and CreateLWLocks().

◆ LWLockUpdateVar()

void LWLockUpdateVar ( LWLock lock,
pg_atomic_uint64 valptr,
uint64  val 
)

Definition at line 1729 of file lwlock.c.

1730{
1733
1734 PRINT_LWDEBUG("LWLockUpdateVar", lock, LW_EXCLUSIVE);
1735
1736 /*
1737 * Note that pg_atomic_exchange_u64 is a full barrier, so we're guaranteed
1738 * that the variable is updated before waking up waiters.
1739 */
1741
1743
1744 LWLockWaitListLock(lock);
1745
1747
1748 /*
1749 * See if there are any LW_WAIT_UNTIL_FREE waiters that need to be woken
1750 * up. They are always in the front of the queue.
1751 */
1752 proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
1753 {
1754 PGPROC *waiter = GetPGProcByNumber(iter.cur);
1755
1756 if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
1757 break;
1758
1759 proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
1760 proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
1761
1762 /* see LWLockWakeup() */
1763 Assert(waiter->lwWaiting == LW_WS_WAITING);
1765 }
1766
1767 /* We are done updating shared state of the lock itself. */
1769
1770 /*
1771 * Awaken any waiters I removed from the queue.
1772 */
1773 proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1774 {
1775 PGPROC *waiter = GetPGProcByNumber(iter.cur);
1776
1777 proclist_delete(&wakeup, iter.cur, lwWaitLink);
1778 /* check comment in LWLockWakeup() about this barrier */
1780 waiter->lwWaiting = LW_WS_NOT_WAITING;
1781 PGSemaphoreUnlock(waiter->sem);
1782 }
1783}

References Assert, proclist_mutable_iter::cur, fb(), GetPGProcByNumber, LW_EXCLUSIVE, LW_VAL_EXCLUSIVE, LW_WAIT_UNTIL_FREE, LW_WS_NOT_WAITING, LW_WS_PENDING_WAKEUP, LW_WS_WAITING, LWLockWaitListLock(), LWLockWaitListUnlock(), PGPROC::lwWaiting, PGPROC::lwWaitMode, pg_atomic_exchange_u64(), pg_atomic_read_u32(), pg_write_barrier, PGSemaphoreUnlock(), PRINT_LWDEBUG, proclist_delete, proclist_foreach_modify, proclist_init(), proclist_push_tail, PGPROC::sem, LWLock::state, val, LWLock::waiters, and wakeup.

Referenced by WALInsertLockAcquireExclusive(), and WALInsertLockUpdateInsertingAt().

◆ LWLockWaitForVar()

bool LWLockWaitForVar ( LWLock lock,
pg_atomic_uint64 valptr,
uint64  oldval,
uint64 newval 
)

Definition at line 1593 of file lwlock.c.

1595{
1596 PGPROC *proc = MyProc;
1597 int extraWaits = 0;
1598 bool result = false;
1599#ifdef LWLOCK_STATS
1601
1603#endif
1604
1605 PRINT_LWDEBUG("LWLockWaitForVar", lock, LW_WAIT_UNTIL_FREE);
1606
1607 /*
1608 * Lock out cancel/die interrupts while we sleep on the lock. There is no
1609 * cleanup mechanism to remove us from the wait queue if we got
1610 * interrupted.
1611 */
1613
1614 /*
1615 * Loop here to check the lock's status after each time we are signaled.
1616 */
1617 for (;;)
1618 {
1619 bool mustwait;
1620
1622 &result);
1623
1624 if (!mustwait)
1625 break; /* the lock was free or value didn't match */
1626
1627 /*
1628 * Add myself to wait queue. Note that this is racy, somebody else
1629 * could wakeup before we're finished queuing. NB: We're using nearly
1630 * the same twice-in-a-row lock acquisition protocol as
1631 * LWLockAcquire(). Check its comments for details. The only
1632 * difference is that we also have to check the variable's values when
1633 * checking the state of the lock.
1634 */
1636
1637 /*
1638 * Clear LW_FLAG_WAKE_IN_PROGRESS flag, to make sure we get woken up
1639 * as soon as the lock is released.
1640 */
1642
1643 /*
1644 * We're now guaranteed to be woken up if necessary. Recheck the lock
1645 * and variables state.
1646 */
1648 &result);
1649
1650 /* Ok, no conflict after we queued ourselves. Undo queueing. */
1651 if (!mustwait)
1652 {
1653 LOG_LWDEBUG("LWLockWaitForVar", lock, "free, undoing queue");
1654
1655 LWLockDequeueSelf(lock);
1656 break;
1657 }
1658
1659 /*
1660 * Wait until awakened.
1661 *
1662 * It is possible that we get awakened for a reason other than being
1663 * signaled by LWLockRelease. If so, loop back and wait again. Once
1664 * we've gotten the LWLock, re-increment the sema by the number of
1665 * additional signals received.
1666 */
1667 LOG_LWDEBUG("LWLockWaitForVar", lock, "waiting");
1668
1669#ifdef LWLOCK_STATS
1670 lwstats->block_count++;
1671#endif
1672
1676
1677 for (;;)
1678 {
1679 PGSemaphoreLock(proc->sem);
1680 if (proc->lwWaiting == LW_WS_NOT_WAITING)
1681 break;
1682 extraWaits++;
1683 }
1684
1685#ifdef LOCK_DEBUG
1686 {
1687 /* not waiting anymore */
1689
1691 }
1692#endif
1693
1697
1698 LOG_LWDEBUG("LWLockWaitForVar", lock, "awakened");
1699
1700 /* Now loop back and check the status of the lock again. */
1701 }
1702
1703 /*
1704 * Fix the process wait semaphore's count for any absorbed wakeups.
1705 */
1706 while (extraWaits-- > 0)
1707 PGSemaphoreUnlock(proc->sem);
1708
1709 /*
1710 * Now okay to allow cancel/die interrupts.
1711 */
1713
1714 return result;
1715}

References Assert, fb(), HOLD_INTERRUPTS, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_WAKE_IN_PROGRESS, LW_WAIT_UNTIL_FREE, LW_WS_NOT_WAITING, LWLockConflictsWithVar(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockReportWaitEnd(), LWLockReportWaitStart(), PGPROC::lwWaiting, MAX_BACKENDS, MyProc, newval, pg_atomic_fetch_and_u32(), pg_atomic_fetch_sub_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), PRINT_LWDEBUG, RESUME_INTERRUPTS, PGPROC::sem, LWLock::state, and T_NAME.

Referenced by WaitXLogInsertionsToFinish().

◆ LWLockWaitListLock()

static void LWLockWaitListLock ( LWLock lock)
static

Definition at line 862 of file lwlock.c.

863{
865#ifdef LWLOCK_STATS
867 uint32 delays = 0;
868
870#endif
871
872 while (true)
873 {
874 /*
875 * Always try once to acquire the lock directly, without setting up
876 * the spin-delay infrastructure. The work necessary for that shows up
877 * in profiles and is rarely necessary.
878 */
881 break; /* got lock */
882
883 /* and then spin without atomic operations until lock is released */
884 {
886
888
889 while (old_state & LW_FLAG_LOCKED)
890 {
893 }
894#ifdef LWLOCK_STATS
895 delays += delayStatus.delays;
896#endif
898 }
899
900 /*
901 * Retry. The lock might obviously already be re-acquired by the time
902 * we're attempting to get it again.
903 */
904 }
905
906#ifdef LWLOCK_STATS
907 lwstats->spin_delay_count += delays;
908#endif
909}

References fb(), finish_spin_delay(), init_local_spin_delay, likely, LW_FLAG_LOCKED, perform_spin_delay(), pg_atomic_fetch_or_u32(), pg_atomic_read_u32(), and LWLock::state.

Referenced by LWLockDequeueSelf(), LWLockQueueSelf(), LWLockUpdateVar(), and LWLockWakeup().

◆ LWLockWaitListUnlock()

◆ LWLockWakeup()

static void LWLockWakeup ( LWLock lock)
static

Definition at line 931 of file lwlock.c.

932{
933 bool new_wake_in_progress = false;
934 bool wokeup_somebody = false;
937
939
940 /* lock wait list while collecting backends to wake up */
941 LWLockWaitListLock(lock);
942
943 proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
944 {
945 PGPROC *waiter = GetPGProcByNumber(iter.cur);
946
947 if (wokeup_somebody && waiter->lwWaitMode == LW_EXCLUSIVE)
948 continue;
949
950 proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
951 proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
952
953 if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
954 {
955 /*
956 * Prevent additional wakeups until retryer gets to run. Backends
957 * that are just waiting for the lock to become free don't retry
958 * automatically.
959 */
961
962 /*
963 * Don't wakeup (further) exclusive locks.
964 */
965 wokeup_somebody = true;
966 }
967
968 /*
969 * Signal that the process isn't on the wait list anymore. This allows
970 * LWLockDequeueSelf() to remove itself of the waitlist with a
971 * proclist_delete(), rather than having to check if it has been
972 * removed from the list.
973 */
974 Assert(waiter->lwWaiting == LW_WS_WAITING);
976
977 /*
978 * Once we've woken up an exclusive lock, there's no point in waking
979 * up anybody else.
980 */
981 if (waiter->lwWaitMode == LW_EXCLUSIVE)
982 break;
983 }
984
986
987 /* unset required flags, and release lock, in one fell swoop */
988 {
991
993 while (true)
994 {
996
997 /* compute desired flags */
998
1001 else
1003
1004 if (proclist_is_empty(&lock->waiters))
1006
1007 desired_state &= ~LW_FLAG_LOCKED; /* release lock */
1008
1011 break;
1012 }
1013 }
1014
1015 /* Awaken any waiters I removed from the queue. */
1016 proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1017 {
1018 PGPROC *waiter = GetPGProcByNumber(iter.cur);
1019
1020 LOG_LWDEBUG("LWLockRelease", lock, "release waiter");
1021 proclist_delete(&wakeup, iter.cur, lwWaitLink);
1022
1023 /*
1024 * Guarantee that lwWaiting being unset only becomes visible once the
1025 * unlink from the link has completed. Otherwise the target backend
1026 * could be woken up for other reason and enqueue for a new lock - if
1027 * that happens before the list unlink happens, the list would end up
1028 * being corrupted.
1029 *
1030 * The barrier pairs with the LWLockWaitListLock() when enqueuing for
1031 * another lock.
1032 */
1034 waiter->lwWaiting = LW_WS_NOT_WAITING;
1035 PGSemaphoreUnlock(waiter->sem);
1036 }
1037}

References Assert, proclist_mutable_iter::cur, fb(), GetPGProcByNumber, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_HAS_WAITERS, LW_FLAG_WAKE_IN_PROGRESS, LW_WAIT_UNTIL_FREE, LW_WS_NOT_WAITING, LW_WS_PENDING_WAKEUP, LW_WS_WAITING, LWLockWaitListLock(), PGPROC::lwWaiting, PGPROC::lwWaitMode, pg_atomic_compare_exchange_u32(), pg_atomic_read_u32(), pg_write_barrier, PGSemaphoreUnlock(), proclist_delete, proclist_foreach_modify, proclist_init(), proclist_is_empty(), proclist_push_tail, PGPROC::sem, LWLock::state, LWLock::waiters, and wakeup.

Referenced by LWLockRelease().

◆ NumLWLocksForNamedTranches()

static int NumLWLocksForNamedTranches ( void  )
static

Definition at line 383 of file lwlock.c.

384{
385 int numLocks = 0;
386 int i;
387
388 for (i = 0; i < NamedLWLockTrancheRequests; i++)
389 numLocks += NamedLWLockTrancheRequestArray[i].num_lwlocks;
390
391 return numLocks;
392}

References i, NamedLWLockTrancheRequestArray, and NamedLWLockTrancheRequests.

Referenced by LWLockShmemSize().

◆ RequestNamedLWLockTranche()

void RequestNamedLWLockTranche ( const char tranche_name,
int  num_lwlocks 
)

Definition at line 650 of file lwlock.c.

651{
654
656 elog(FATAL, "cannot request additional LWLocks outside shmem_request_hook");
657
658 if (!tranche_name)
661 errmsg("tranche name cannot be NULL")));
662
663 if (strlen(tranche_name) >= NAMEDATALEN)
666 errmsg("tranche name too long"),
667 errdetail("LWLock tranche names must be no longer than %d bytes.",
668 NAMEDATALEN - 1)));
669
671 {
676 * sizeof(NamedLWLockTrancheRequest));
677 }
678
680 {
682
685 i * sizeof(NamedLWLockTrancheRequest));
687 }
688
690 strlcpy(request->tranche_name, tranche_name, NAMEDATALEN);
691 request->num_lwlocks = num_lwlocks;
693}

References elog, ereport, errcode(), errdetail(), errmsg, ERROR, FATAL, fb(), i, MemoryContextAlloc(), NAMEDATALEN, NamedLWLockTrancheRequestArray, NamedLWLockTrancheRequests, pg_nextpower2_32(), process_shmem_requests_in_progress, repalloc(), strlcpy(), and TopMemoryContext.

Referenced by pgss_shmem_request(), and test_lwlock_tranches_shmem_request().

◆ StaticAssertDecl() [1/4]

StaticAssertDecl ( ((MAX_BACKENDS+1) &MAX_BACKENDS = =0,
"MAX_BACKENDS + 1 needs to be a power of 2"   
)

◆ StaticAssertDecl() [2/4]

StaticAssertDecl ( (LW_VAL_EXCLUSIVE &LW_FLAG_MASK = =0,
"LW_VAL_EXCLUSIVE and LW_FLAG_MASK overlap"   
)

◆ StaticAssertDecl() [3/4]

StaticAssertDecl ( (MAX_BACKENDS &LW_FLAG_MASK = =0,
"MAX_BACKENDS and LW_FLAG_MASK overlap"   
)

◆ StaticAssertDecl() [4/4]

StaticAssertDecl ( lengthof(BuiltinTrancheNames = =LWTRANCHE_FIRST_USER_DEFINED,
"missing entries in BuiltinTrancheNames"  [] 
)

Variable Documentation

◆ BuiltinTrancheNames

const char* const BuiltinTrancheNames[]
static
Initial value:
= {
#define PG_LWLOCK(id, lockname)
#define PG_LWLOCKTRANCHE(id, lockname)
}

Definition at line 136 of file lwlock.c.

136 {
137#define PG_LWLOCK(id, lockname) [id] = CppAsString(lockname),
138#define PG_LWLOCKTRANCHE(id, lockname) [LWTRANCHE_##id] = CppAsString(lockname),
139#include "storage/lwlocklist.h"
140#undef PG_LWLOCK
141#undef PG_LWLOCKTRANCHE
142};

Referenced by GetLWTrancheName().

◆ held_lwlocks

◆ LocalLWLockCounter

int LocalLWLockCounter
static

Definition at line 203 of file lwlock.c.

Referenced by GetLWTrancheName(), and LWLockNewTrancheId().

◆ LocalNamedLWLockTrancheRequestArray

NamedLWLockTrancheRequest* LocalNamedLWLockTrancheRequestArray = NULL
static

Definition at line 197 of file lwlock.c.

Referenced by CreateLWLocks(), and LWLockShmemSize().

◆ LWLockCounter

int* LWLockCounter = NULL

Definition at line 200 of file lwlock.c.

Referenced by CreateLWLocks(), GetLWTrancheName(), and LWLockNewTrancheId().

◆ LWLockTrancheNames

char** LWLockTrancheNames = NULL

Definition at line 155 of file lwlock.c.

Referenced by CreateLWLocks(), GetLWTrancheName(), and LWLockNewTrancheId().

◆ MainLWLockArray

◆ NamedLWLockTrancheRequestArray

◆ NamedLWLockTrancheRequests

◆ num_held_lwlocks