PostgreSQL Source Code git master
Loading...
Searching...
No Matches
lwlock.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * lwlock.c
4 * Lightweight lock manager
5 *
6 * Lightweight locks are intended primarily to provide mutual exclusion of
7 * access to shared-memory data structures. Therefore, they offer both
8 * exclusive and shared lock modes (to support read/write and read-only
9 * access to a shared object). There are few other frammishes. User-level
10 * locking should be done with the full lock manager --- which depends on
11 * LWLocks to protect its shared state.
12 *
13 * In addition to exclusive and shared modes, lightweight locks can be used to
14 * wait until a variable changes value. The variable is initially not set
15 * when the lock is acquired with LWLockAcquire, i.e. it remains set to the
16 * value it was set to when the lock was released last, and can be updated
17 * without releasing the lock by calling LWLockUpdateVar. LWLockWaitForVar
18 * waits for the variable to be updated, or until the lock is free. When
19 * releasing the lock with LWLockReleaseClearVar() the value can be set to an
20 * appropriate value for a free lock. The meaning of the variable is up to
21 * the caller, the lightweight lock code just assigns and compares it.
22 *
23 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
25 *
26 * IDENTIFICATION
27 * src/backend/storage/lmgr/lwlock.c
28 *
29 * NOTES:
30 *
31 * This used to be a pretty straight forward reader-writer lock
32 * implementation, in which the internal state was protected by a
33 * spinlock. Unfortunately the overhead of taking the spinlock proved to be
34 * too high for workloads/locks that were taken in shared mode very
35 * frequently. Often we were spinning in the (obviously exclusive) spinlock,
36 * while trying to acquire a shared lock that was actually free.
37 *
38 * Thus a new implementation was devised that provides wait-free shared lock
39 * acquisition for locks that aren't exclusively locked.
40 *
41 * The basic idea is to have a single atomic variable 'lockcount' instead of
42 * the formerly separate shared and exclusive counters and to use atomic
43 * operations to acquire the lock. That's fairly easy to do for plain
44 * rw-spinlocks, but a lot harder for something like LWLocks that want to wait
45 * in the OS.
46 *
47 * For lock acquisition we use an atomic compare-and-exchange on the lockcount
48 * variable. For exclusive lock we swap in a sentinel value
49 * (LW_VAL_EXCLUSIVE), for shared locks we count the number of holders.
50 *
51 * To release the lock we use an atomic decrement to release the lock. If the
52 * new value is zero (we get that atomically), we know we can/have to release
53 * waiters.
54 *
55 * Obviously it is important that the sentinel value for exclusive locks
56 * doesn't conflict with the maximum number of possible share lockers -
57 * luckily MAX_BACKENDS makes that easily possible.
58 *
59 *
60 * The attentive reader might have noticed that naively doing the above has a
61 * glaring race condition: We try to lock using the atomic operations and
62 * notice that we have to wait. Unfortunately by the time we have finished
63 * queuing, the former locker very well might have already finished its
64 * work. That's problematic because we're now stuck waiting inside the OS.
65
66 * To mitigate those races we use a two phased attempt at locking:
67 * Phase 1: Try to do it atomically, if we succeed, nice
68 * Phase 2: Add ourselves to the waitqueue of the lock
69 * Phase 3: Try to grab the lock again, if we succeed, remove ourselves from
70 * the queue
71 * Phase 4: Sleep till wake-up, goto Phase 1
72 *
73 * This protects us against the problem from above as nobody can release too
74 * quick, before we're queued, since after Phase 2 we're already queued.
75 * -------------------------------------------------------------------------
76 */
77#include "postgres.h"
78
79#include "miscadmin.h"
80#include "pg_trace.h"
81#include "pgstat.h"
82#include "port/pg_bitutils.h"
83#include "storage/proc.h"
84#include "storage/proclist.h"
85#include "storage/procnumber.h"
86#include "storage/spin.h"
87#include "utils/memutils.h"
88#include "utils/wait_event.h"
89
90#ifdef LWLOCK_STATS
91#include "utils/hsearch.h"
92#endif
93
94
95#define LW_FLAG_HAS_WAITERS ((uint32) 1 << 31)
96#define LW_FLAG_WAKE_IN_PROGRESS ((uint32) 1 << 30)
97#define LW_FLAG_LOCKED ((uint32) 1 << 29)
98#define LW_FLAG_BITS 3
99#define LW_FLAG_MASK (((1<<LW_FLAG_BITS)-1)<<(32-LW_FLAG_BITS))
100
101/* assumes MAX_BACKENDS is a (power of 2) - 1, checked below */
102#define LW_VAL_EXCLUSIVE (MAX_BACKENDS + 1)
103#define LW_VAL_SHARED 1
104
105/* already (power of 2)-1, i.e. suitable for a mask */
106#define LW_SHARED_MASK MAX_BACKENDS
107#define LW_LOCK_MASK (MAX_BACKENDS | LW_VAL_EXCLUSIVE)
108
109
111 "MAX_BACKENDS + 1 needs to be a power of 2");
112
114 "MAX_BACKENDS and LW_FLAG_MASK overlap");
115
117 "LW_VAL_EXCLUSIVE and LW_FLAG_MASK overlap");
118
119/*
120 * There are three sorts of LWLock "tranches":
121 *
122 * 1. The individually-named locks defined in lwlocklist.h each have their
123 * own tranche. We absorb the names of these tranches from there into
124 * BuiltinTrancheNames here.
125 *
126 * 2. There are some predefined tranches for built-in groups of locks defined
127 * in lwlocklist.h. We absorb the names of these tranches, too.
128 *
129 * 3. Extensions can create new tranches, via either RequestNamedLWLockTranche
130 * or LWLockNewTrancheId. These names are stored in shared memory and can be
131 * accessed via LWLockTrancheNames.
132 *
133 * All these names are user-visible as wait event names, so choose with care
134 * ... and do not forget to update the documentation's list of wait events.
135 */
136static const char *const BuiltinTrancheNames[] = {
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};
143
146 "missing entries in BuiltinTrancheNames[]");
147
148/*
149 * This is indexed by tranche ID minus LWTRANCHE_FIRST_USER_DEFINED, and
150 * points to the shared memory locations of the names of all
151 * dynamically-created tranches. Backends inherit the pointer by fork from the
152 * postmaster (except in the EXEC_BACKEND case, where we have special measures
153 * to pass it down).
154 */
156
157/*
158 * This points to the main array of LWLocks in shared memory. Backends inherit
159 * the pointer by fork from the postmaster (except in the EXEC_BACKEND case,
160 * where we have special measures to pass it down).
161 */
163
164/*
165 * We use this structure to keep track of locked LWLocks for release
166 * during error recovery. Normally, only a few will be held at once, but
167 * occasionally the number can be much higher.
168 */
169#define MAX_SIMUL_LWLOCKS 200
170
171/* struct representing the LWLocks we're holding */
177
178static int num_held_lwlocks = 0;
180
181/* struct representing the LWLock tranche request for named tranche */
187
188/*
189 * NamedLWLockTrancheRequests is the valid length of the request array. These
190 * variables are non-static so that launch_backend.c can copy them to child
191 * processes in EXEC_BACKEND builds.
192 */
195
196/* postmaster's local copy of the request array */
198
199/* shared memory counter of registered tranches */
201
202/* backend-local counter of registered tranches */
204
205#define MAX_NAMED_TRANCHES 256
206
207static void InitializeLWLocks(void);
208static inline void LWLockReportWaitStart(LWLock *lock);
209static inline void LWLockReportWaitEnd(void);
210static const char *GetLWTrancheName(uint16 trancheId);
211
212#define T_NAME(lock) \
213 GetLWTrancheName((lock)->tranche)
214
215#ifdef LWLOCK_STATS
216typedef struct lwlock_stats_key
217{
218 int tranche;
219 void *instance;
221
222typedef struct lwlock_stats
223{
227 int block_count;
231
232static HTAB *lwlock_stats_htab;
234#endif
235
236#ifdef LOCK_DEBUG
237bool Trace_lwlocks = false;
238
239inline static void
240PRINT_LWDEBUG(const char *where, LWLock *lock, LWLockMode mode)
241{
242 /* hide statement & context here, otherwise the log is just too verbose */
243 if (Trace_lwlocks)
244 {
246
247 ereport(LOG,
248 (errhidestmt(true),
249 errhidecontext(true),
250 errmsg_internal("%d: %s(%s %p): excl %u shared %u haswaiters %u waiters %u waking %d",
251 MyProcPid,
252 where, T_NAME(lock), lock,
253 (state & LW_VAL_EXCLUSIVE) != 0,
255 (state & LW_FLAG_HAS_WAITERS) != 0,
256 pg_atomic_read_u32(&lock->nwaiters),
258 }
259}
260
261inline static void
262LOG_LWDEBUG(const char *where, LWLock *lock, const char *msg)
263{
264 /* hide statement & context here, otherwise the log is just too verbose */
265 if (Trace_lwlocks)
266 {
267 ereport(LOG,
268 (errhidestmt(true),
269 errhidecontext(true),
270 errmsg_internal("%s(%s %p): %s", where,
271 T_NAME(lock), lock, msg)));
272 }
273}
274
275#else /* not LOCK_DEBUG */
276#define PRINT_LWDEBUG(a,b,c) ((void)0)
277#define LOG_LWDEBUG(a,b,c) ((void)0)
278#endif /* LOCK_DEBUG */
279
280#ifdef LWLOCK_STATS
281
282static void init_lwlock_stats(void);
283static void print_lwlock_stats(int code, Datum arg);
285
286static void
288{
289 HASHCTL ctl;
291 static bool exit_registered = false;
292
293 if (lwlock_stats_cxt != NULL)
295
296 /*
297 * The LWLock stats will be updated within a critical section, which
298 * requires allocating new hash entries. Allocations within a critical
299 * section are normally not allowed because running out of memory would
300 * lead to a PANIC, but LWLOCK_STATS is debugging code that's not normally
301 * turned on in production, so that's an acceptable risk. The hash entries
302 * are small, so the risk of running out of memory is minimal in practice.
303 */
305 "LWLock stats",
308
309 ctl.keysize = sizeof(lwlock_stats_key);
310 ctl.entrysize = sizeof(lwlock_stats);
311 ctl.hcxt = lwlock_stats_cxt;
312 lwlock_stats_htab = hash_create("lwlock stats", 16384, &ctl,
314 if (!exit_registered)
315 {
317 exit_registered = true;
318 }
319}
320
321static void
323{
324 HASH_SEQ_STATUS scan;
326
328
329 /* Grab an LWLock to keep different backends from mixing reports */
331
332 while ((lwstats = (lwlock_stats *) hash_seq_search(&scan)) != NULL)
333 {
335 "PID %d lwlock %s %p: shacq %u exacq %u blk %u spindelay %u dequeue self %u\n",
336 MyProcPid, GetLWTrancheName(lwstats->key.tranche),
337 lwstats->key.instance, lwstats->sh_acquire_count,
338 lwstats->ex_acquire_count, lwstats->block_count,
339 lwstats->spin_delay_count, lwstats->dequeue_self_count);
340 }
341
343}
344
345static lwlock_stats *
347{
350 bool found;
351
352 /*
353 * During shared memory initialization, the hash table doesn't exist yet.
354 * Stats of that phase aren't very interesting, so just collect operations
355 * on all locks in a single dummy entry.
356 */
357 if (lwlock_stats_htab == NULL)
358 return &lwlock_stats_dummy;
359
360 /* Fetch or create the entry. */
361 MemSet(&key, 0, sizeof(key));
362 key.tranche = lock->tranche;
363 key.instance = lock;
365 if (!found)
366 {
367 lwstats->sh_acquire_count = 0;
368 lwstats->ex_acquire_count = 0;
369 lwstats->block_count = 0;
370 lwstats->dequeue_self_count = 0;
371 lwstats->spin_delay_count = 0;
372 }
373 return lwstats;
374}
375#endif /* LWLOCK_STATS */
376
377
378/*
379 * Compute number of LWLocks required by named tranches. These will be
380 * allocated in the main array.
381 */
382static int
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}
393
394/*
395 * Compute shmem space needed for LWLocks and named tranches.
396 */
397Size
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}
436
437/*
438 * Allocate shmem space for the main LWLock array and all tranches and
439 * initialize it.
440 */
441void
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}
494
495/*
496 * Initialize LWLocks that are fixed and those belonging to named tranches.
497 */
498static void
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}
546
547/*
548 * InitLWLockAccess - initialize backend-local state needed to hold LWLocks
549 */
550void
552{
553#ifdef LWLOCK_STATS
555#endif
556}
557
558/*
559 * GetNamedLWLockTranche - returns the base address of LWLock from the
560 * specified tranche.
561 *
562 * Caller needs to retrieve the requested number of LWLocks starting from
563 * the base lock address returned by this API. This can be used for
564 * tranches that are requested by using RequestNamedLWLockTranche() API.
565 */
567GetNamedLWLockTranche(const char *tranche_name)
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}
592
593/*
594 * Allocate a new tranche ID with the provided name.
595 */
596int
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}
636
637/*
638 * RequestNamedLWLockTranche
639 * Request that extra LWLocks be allocated during postmaster
640 * startup.
641 *
642 * This may only be called via the shmem_request_hook of a library that is
643 * loaded into the postmaster via shared_preload_libraries. Calls from
644 * elsewhere will fail.
645 *
646 * The tranche name will be user-visible as a wait event name, so try to
647 * use a name that fits the style for those.
648 */
649void
650RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
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}
694
695/*
696 * LWLockInitialize - initialize a new lwlock; it's initially unlocked
697 */
698void
699LWLockInitialize(LWLock *lock, int tranche_id)
700{
701 /* verify the tranche_id is valid */
702 (void) GetLWTrancheName(tranche_id);
703
704 pg_atomic_init_u32(&lock->state, 0);
705#ifdef LOCK_DEBUG
706 pg_atomic_init_u32(&lock->nwaiters, 0);
707#endif
708 lock->tranche = tranche_id;
709 proclist_init(&lock->waiters);
710}
711
712/*
713 * Report start of wait event for light-weight locks.
714 *
715 * This function will be used by all the light-weight lock calls which
716 * needs to wait to acquire the lock. This function distinguishes wait
717 * event based on tranche and lock id.
718 */
719static inline void
724
725/*
726 * Report end of wait event for light-weight locks.
727 */
728static inline void
733
734/*
735 * Return the name of an LWLock tranche.
736 */
737static const char *
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}
769
770/*
771 * Return an identifier for an LWLock based on the wait class and event.
772 */
773const char *
775{
776 Assert(classId == PG_WAIT_LWLOCK);
777 /* The event IDs are just tranche numbers. */
779}
780
781/*
782 * Internal function that tries to atomically acquire the lwlock in the passed
783 * in mode.
784 *
785 * This function will not block waiting for a lock to become free - that's the
786 * caller's job.
787 *
788 * Returns true if the lock isn't free and we need to wait.
789 */
790static bool
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}
852
853/*
854 * Lock the LWLock's wait list against concurrent activity.
855 *
856 * NB: even though the wait list is locked, non-conflicting lock operations
857 * may still happen concurrently.
858 *
859 * Time spent holding mutex should be short!
860 */
861static void
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}
910
911/*
912 * Unlock the LWLock's wait list.
913 *
914 * Note that it can be more efficient to manipulate flags and release the
915 * locks in a single atomic operation.
916 */
917static void
926
927/*
928 * Wakeup all the lockers that currently have a chance to acquire the lock.
929 */
930static void
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}
1038
1039/*
1040 * Add ourselves to the end of the queue.
1041 *
1042 * NB: Mode can be LW_WAIT_UNTIL_FREE here!
1043 */
1044static void
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}
1079
1080/*
1081 * Remove ourselves from the waitlist.
1082 *
1083 * This is used if we queued ourselves because we thought we needed to sleep
1084 * but, after further checking, we discovered that we don't actually need to
1085 * do so.
1086 */
1087static void
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}
1167
1168/*
1169 * LWLockAcquire - acquire a lightweight lock in the specified mode
1170 *
1171 * If the lock is not available, sleep until it is. Returns true if the lock
1172 * was available immediately, false if we had to sleep.
1173 *
1174 * Side effect: cancel/die interrupts are held off until lock release.
1175 */
1176bool
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}
1339
1340/*
1341 * LWLockConditionalAcquire - acquire a lightweight lock in the specified mode
1342 *
1343 * If the lock is not available, return false with no side-effects.
1344 *
1345 * If successful, cancel/die interrupts are held off until lock release.
1346 */
1347bool
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}
1389
1390/*
1391 * LWLockAcquireOrWait - Acquire lock, or wait until it's free
1392 *
1393 * The semantics of this function are a bit funky. If the lock is currently
1394 * free, it is acquired in the given mode, and the function returns true. If
1395 * the lock isn't immediately free, the function waits until it is released
1396 * and returns false, but does not acquire the lock.
1397 *
1398 * This is currently used for WALWriteLock: when a backend flushes the WAL,
1399 * holding WALWriteLock, it can flush the commit records of many other
1400 * backends as a side-effect. Those other backends need to wait until the
1401 * flush finishes, but don't need to acquire the lock anymore. They can just
1402 * wake up, observe that their records have already been flushed, and return.
1403 */
1404bool
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}
1521
1522/*
1523 * Does the lwlock in its current state need to wait for the variable value to
1524 * change?
1525 *
1526 * If we don't need to wait, and it's because the value of the variable has
1527 * changed, store the current value in newval.
1528 *
1529 * *result is set to true if the lock was free, and false otherwise.
1530 */
1531static bool
1533 uint64 *newval, bool *result)
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}
1574
1575/*
1576 * LWLockWaitForVar - Wait until lock is free, or a variable is updated.
1577 *
1578 * If the lock is held and *valptr equals oldval, waits until the lock is
1579 * either freed, or the lock holder updates *valptr by calling
1580 * LWLockUpdateVar. If the lock is free on exit (immediately or after
1581 * waiting), returns true. If the lock is still held, but *valptr no longer
1582 * matches oldval, returns false and sets *newval to the current value in
1583 * *valptr.
1584 *
1585 * Note: this function ignores shared lock holders; if the lock is held
1586 * in shared mode, returns 'true'.
1587 *
1588 * Be aware that LWLockConflictsWithVar() does not include a memory barrier,
1589 * hence the caller of this function may want to rely on an explicit barrier or
1590 * an implied barrier via spinlock or LWLock to avoid memory ordering issues.
1591 */
1592bool
1594 uint64 *newval)
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}
1716
1717
1718/*
1719 * LWLockUpdateVar - Update a variable and wake up waiters atomically
1720 *
1721 * Sets *valptr to 'val', and wakes up all processes waiting for us with
1722 * LWLockWaitForVar(). It first sets the value atomically and then wakes up
1723 * waiting processes so that any process calling LWLockWaitForVar() on the same
1724 * lock is guaranteed to see the new value, and act accordingly.
1725 *
1726 * The caller must be holding the lock in exclusive mode.
1727 */
1728void
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}
1784
1785
1786/*
1787 * LWLockRelease - release a previously acquired lock
1788 *
1789 * NB: This will leave lock->owner pointing to the current backend (if
1790 * LOCK_DEBUG is set). This is somewhat intentional, as it makes it easier to
1791 * debug cases of missing wakeups during lock release.
1792 */
1793void
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}
1862
1863/*
1864 * LWLockReleaseClearVar - release a previously acquired lock, reset variable
1865 */
1866void
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}
1877
1878
1879/*
1880 * LWLockReleaseAll - release all currently-held locks
1881 *
1882 * Used to clean up after ereport(ERROR). An important difference between this
1883 * function and retail LWLockRelease calls is that InterruptHoldoffCount is
1884 * unchanged by this operation. This is necessary since InterruptHoldoffCount
1885 * has been set to an appropriate level earlier in error recovery. We could
1886 * decrement it below zero if we allow it to drop for each released lock!
1887 *
1888 * Note that this function must be safe to call even before the LWLock
1889 * subsystem has been initialized (e.g., during early startup failures).
1890 * In that case, num_held_lwlocks will be 0 and we do nothing.
1891 */
1892void
1894{
1895 while (num_held_lwlocks > 0)
1896 {
1897 HOLD_INTERRUPTS(); /* match the upcoming RESUME_INTERRUPTS */
1898
1900 }
1901
1903}
1904
1905
1906/*
1907 * LWLockHeldByMe - test whether my process holds a lock in any mode
1908 *
1909 * This is meant as debug support only.
1910 */
1911bool
1913{
1914 int i;
1915
1916 for (i = 0; i < num_held_lwlocks; i++)
1917 {
1918 if (held_lwlocks[i].lock == lock)
1919 return true;
1920 }
1921 return false;
1922}
1923
1924/*
1925 * LWLockAnyHeldByMe - test whether my process holds any of an array of locks
1926 *
1927 * This is meant as debug support only.
1928 */
1929bool
1930LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
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}
1949
1950/*
1951 * LWLockHeldByMeInMode - test whether my process holds a lock in given mode
1952 *
1953 * This is meant as debug support only.
1954 */
1955bool
1957{
1958 int i;
1959
1960 for (i = 0; i < num_held_lwlocks; i++)
1961 {
1962 if (held_lwlocks[i].lock == lock && held_lwlocks[i].mode == mode)
1963 return true;
1964 }
1965 return false;
1966}
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 lengthof(array)
Definition c.h:875
#define MemSet(start, val, len)
Definition c.h:1109
#define StaticAssertDecl(condition, errmessage)
Definition c.h:1010
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_MASK
Definition lwlock.c:99
#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
char tranche_name[NAMEDATALEN]
Definition lwlock.c:184
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]