PostgreSQL Source Code  git master
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-2024, 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 it's
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 "postmaster/postmaster.h"
84 #include "storage/proc.h"
85 #include "storage/proclist.h"
86 #include "storage/spin.h"
87 #include "utils/memutils.h"
88 
89 #ifdef LWLOCK_STATS
90 #include "utils/hsearch.h"
91 #endif
92 
93 
94 /* We use the ShmemLock spinlock to protect LWLockCounter */
95 extern slock_t *ShmemLock;
96 
97 #define LW_FLAG_HAS_WAITERS ((uint32) 1 << 30)
98 #define LW_FLAG_RELEASE_OK ((uint32) 1 << 29)
99 #define LW_FLAG_LOCKED ((uint32) 1 << 28)
100 
101 #define LW_VAL_EXCLUSIVE ((uint32) 1 << 24)
102 #define LW_VAL_SHARED 1
103 
104 #define LW_LOCK_MASK ((uint32) ((1 << 25)-1))
105 /* Must be greater than MAX_BACKENDS - which is 2^23-1, so we're fine. */
106 #define LW_SHARED_MASK ((uint32) ((1 << 24)-1))
107 
109  "MAX_BACKENDS too big for lwlock.c");
110 
111 /*
112  * There are three sorts of LWLock "tranches":
113  *
114  * 1. The individually-named locks defined in lwlocknames.h each have their
115  * own tranche. The names of these tranches appear in IndividualLWLockNames[]
116  * in lwlocknames.c.
117  *
118  * 2. There are some predefined tranches for built-in groups of locks.
119  * These are listed in enum BuiltinTrancheIds in lwlock.h, and their names
120  * appear in BuiltinTrancheNames[] below.
121  *
122  * 3. Extensions can create new tranches, via either RequestNamedLWLockTranche
123  * or LWLockRegisterTranche. The names of these that are known in the current
124  * process appear in LWLockTrancheNames[].
125  *
126  * All these names are user-visible as wait event names, so choose with care
127  * ... and do not forget to update the documentation's list of wait events.
128  */
129 extern const char *const IndividualLWLockNames[]; /* in lwlocknames.c */
130 
131 static const char *const BuiltinTrancheNames[] = {
132  [LWTRANCHE_XACT_BUFFER] = "XactBuffer",
133  [LWTRANCHE_COMMITTS_BUFFER] = "CommitTsBuffer",
134  [LWTRANCHE_SUBTRANS_BUFFER] = "SubtransBuffer",
135  [LWTRANCHE_MULTIXACTOFFSET_BUFFER] = "MultiXactOffsetBuffer",
136  [LWTRANCHE_MULTIXACTMEMBER_BUFFER] = "MultiXactMemberBuffer",
137  [LWTRANCHE_NOTIFY_BUFFER] = "NotifyBuffer",
138  [LWTRANCHE_SERIAL_BUFFER] = "SerialBuffer",
139  [LWTRANCHE_WAL_INSERT] = "WALInsert",
140  [LWTRANCHE_BUFFER_CONTENT] = "BufferContent",
141  [LWTRANCHE_REPLICATION_ORIGIN_STATE] = "ReplicationOriginState",
142  [LWTRANCHE_REPLICATION_SLOT_IO] = "ReplicationSlotIO",
143  [LWTRANCHE_LOCK_FASTPATH] = "LockFastPath",
144  [LWTRANCHE_BUFFER_MAPPING] = "BufferMapping",
145  [LWTRANCHE_LOCK_MANAGER] = "LockManager",
146  [LWTRANCHE_PREDICATE_LOCK_MANAGER] = "PredicateLockManager",
147  [LWTRANCHE_PARALLEL_HASH_JOIN] = "ParallelHashJoin",
148  [LWTRANCHE_PARALLEL_QUERY_DSA] = "ParallelQueryDSA",
149  [LWTRANCHE_PER_SESSION_DSA] = "PerSessionDSA",
150  [LWTRANCHE_PER_SESSION_RECORD_TYPE] = "PerSessionRecordType",
151  [LWTRANCHE_PER_SESSION_RECORD_TYPMOD] = "PerSessionRecordTypmod",
152  [LWTRANCHE_SHARED_TUPLESTORE] = "SharedTupleStore",
153  [LWTRANCHE_SHARED_TIDBITMAP] = "SharedTidBitmap",
154  [LWTRANCHE_PARALLEL_APPEND] = "ParallelAppend",
155  [LWTRANCHE_PER_XACT_PREDICATE_LIST] = "PerXactPredicateList",
156  [LWTRANCHE_PGSTATS_DSA] = "PgStatsDSA",
157  [LWTRANCHE_PGSTATS_HASH] = "PgStatsHash",
158  [LWTRANCHE_PGSTATS_DATA] = "PgStatsData",
159  [LWTRANCHE_LAUNCHER_DSA] = "LogicalRepLauncherDSA",
160  [LWTRANCHE_LAUNCHER_HASH] = "LogicalRepLauncherHash",
161  [LWTRANCHE_DSM_REGISTRY_DSA] = "DSMRegistryDSA",
162  [LWTRANCHE_DSM_REGISTRY_HASH] = "DSMRegistryHash",
163  [LWTRANCHE_COMMITTS_SLRU] = "CommitTSSLRU",
164  [LWTRANCHE_MULTIXACTOFFSET_SLRU] = "MultixactOffsetSLRU",
165  [LWTRANCHE_MULTIXACTMEMBER_SLRU] = "MultixactMemberSLRU",
166  [LWTRANCHE_NOTIFY_SLRU] = "NotifySLRU",
167  [LWTRANCHE_SERIAL_SLRU] = "SerialSLRU",
168  [LWTRANCHE_SUBTRANS_SLRU] = "SubtransSLRU",
169  [LWTRANCHE_XACT_SLRU] = "XactSLRU",
170 };
171 
174  "missing entries in BuiltinTrancheNames[]");
175 
176 /*
177  * This is indexed by tranche ID minus LWTRANCHE_FIRST_USER_DEFINED, and
178  * stores the names of all dynamically-created tranches known to the current
179  * process. Any unused entries in the array will contain NULL.
180  */
181 static const char **LWLockTrancheNames = NULL;
183 
184 /*
185  * This points to the main array of LWLocks in shared memory. Backends inherit
186  * the pointer by fork from the postmaster (except in the EXEC_BACKEND case,
187  * where we have special measures to pass it down).
188  */
190 
191 /*
192  * We use this structure to keep track of locked LWLocks for release
193  * during error recovery. Normally, only a few will be held at once, but
194  * occasionally the number can be much higher; for example, the pg_buffercache
195  * extension locks all buffer partitions simultaneously.
196  */
197 #define MAX_SIMUL_LWLOCKS 200
198 
199 /* struct representing the LWLocks we're holding */
200 typedef struct LWLockHandle
201 {
205 
206 static int num_held_lwlocks = 0;
208 
209 /* struct representing the LWLock tranche request for named tranche */
211 {
215 
218 
219 /*
220  * NamedLWLockTrancheRequests is both the valid length of the request array,
221  * and the length of the shared-memory NamedLWLockTrancheArray later on.
222  * This variable and NamedLWLockTrancheArray are non-static so that
223  * postmaster.c can copy them to child processes in EXEC_BACKEND builds.
224  */
226 
227 /* points to data in shared memory: */
229 
230 static void InitializeLWLocks(void);
231 static inline void LWLockReportWaitStart(LWLock *lock);
232 static inline void LWLockReportWaitEnd(void);
233 static const char *GetLWTrancheName(uint16 trancheId);
234 
235 #define T_NAME(lock) \
236  GetLWTrancheName((lock)->tranche)
237 
238 #ifdef LWLOCK_STATS
239 typedef struct lwlock_stats_key
240 {
241  int tranche;
242  void *instance;
243 } lwlock_stats_key;
244 
245 typedef struct lwlock_stats
246 {
247  lwlock_stats_key key;
248  int sh_acquire_count;
249  int ex_acquire_count;
250  int block_count;
251  int dequeue_self_count;
252  int spin_delay_count;
253 } lwlock_stats;
254 
255 static HTAB *lwlock_stats_htab;
256 static lwlock_stats lwlock_stats_dummy;
257 #endif
258 
259 #ifdef LOCK_DEBUG
260 bool Trace_lwlocks = false;
261 
262 inline static void
263 PRINT_LWDEBUG(const char *where, LWLock *lock, LWLockMode mode)
264 {
265  /* hide statement & context here, otherwise the log is just too verbose */
266  if (Trace_lwlocks)
267  {
269 
270  ereport(LOG,
271  (errhidestmt(true),
272  errhidecontext(true),
273  errmsg_internal("%d: %s(%s %p): excl %u shared %u haswaiters %u waiters %u rOK %d",
274  MyProcPid,
275  where, T_NAME(lock), lock,
276  (state & LW_VAL_EXCLUSIVE) != 0,
278  (state & LW_FLAG_HAS_WAITERS) != 0,
279  pg_atomic_read_u32(&lock->nwaiters),
280  (state & LW_FLAG_RELEASE_OK) != 0)));
281  }
282 }
283 
284 inline static void
285 LOG_LWDEBUG(const char *where, LWLock *lock, const char *msg)
286 {
287  /* hide statement & context here, otherwise the log is just too verbose */
288  if (Trace_lwlocks)
289  {
290  ereport(LOG,
291  (errhidestmt(true),
292  errhidecontext(true),
293  errmsg_internal("%s(%s %p): %s", where,
294  T_NAME(lock), lock, msg)));
295  }
296 }
297 
298 #else /* not LOCK_DEBUG */
299 #define PRINT_LWDEBUG(a,b,c) ((void)0)
300 #define LOG_LWDEBUG(a,b,c) ((void)0)
301 #endif /* LOCK_DEBUG */
302 
303 #ifdef LWLOCK_STATS
304 
305 static void init_lwlock_stats(void);
306 static void print_lwlock_stats(int code, Datum arg);
307 static lwlock_stats * get_lwlock_stats_entry(LWLock *lock);
308 
309 static void
310 init_lwlock_stats(void)
311 {
312  HASHCTL ctl;
313  static MemoryContext lwlock_stats_cxt = NULL;
314  static bool exit_registered = false;
315 
316  if (lwlock_stats_cxt != NULL)
317  MemoryContextDelete(lwlock_stats_cxt);
318 
319  /*
320  * The LWLock stats will be updated within a critical section, which
321  * requires allocating new hash entries. Allocations within a critical
322  * section are normally not allowed because running out of memory would
323  * lead to a PANIC, but LWLOCK_STATS is debugging code that's not normally
324  * turned on in production, so that's an acceptable risk. The hash entries
325  * are small, so the risk of running out of memory is minimal in practice.
326  */
327  lwlock_stats_cxt = AllocSetContextCreate(TopMemoryContext,
328  "LWLock stats",
330  MemoryContextAllowInCriticalSection(lwlock_stats_cxt, true);
331 
332  ctl.keysize = sizeof(lwlock_stats_key);
333  ctl.entrysize = sizeof(lwlock_stats);
334  ctl.hcxt = lwlock_stats_cxt;
335  lwlock_stats_htab = hash_create("lwlock stats", 16384, &ctl,
337  if (!exit_registered)
338  {
339  on_shmem_exit(print_lwlock_stats, 0);
340  exit_registered = true;
341  }
342 }
343 
344 static void
345 print_lwlock_stats(int code, Datum arg)
346 {
347  HASH_SEQ_STATUS scan;
348  lwlock_stats *lwstats;
349 
350  hash_seq_init(&scan, lwlock_stats_htab);
351 
352  /* Grab an LWLock to keep different backends from mixing reports */
354 
355  while ((lwstats = (lwlock_stats *) hash_seq_search(&scan)) != NULL)
356  {
357  fprintf(stderr,
358  "PID %d lwlock %s %p: shacq %u exacq %u blk %u spindelay %u dequeue self %u\n",
359  MyProcPid, GetLWTrancheName(lwstats->key.tranche),
360  lwstats->key.instance, lwstats->sh_acquire_count,
361  lwstats->ex_acquire_count, lwstats->block_count,
362  lwstats->spin_delay_count, lwstats->dequeue_self_count);
363  }
364 
365  LWLockRelease(&MainLWLockArray[0].lock);
366 }
367 
368 static lwlock_stats *
369 get_lwlock_stats_entry(LWLock *lock)
370 {
371  lwlock_stats_key key;
372  lwlock_stats *lwstats;
373  bool found;
374 
375  /*
376  * During shared memory initialization, the hash table doesn't exist yet.
377  * Stats of that phase aren't very interesting, so just collect operations
378  * on all locks in a single dummy entry.
379  */
380  if (lwlock_stats_htab == NULL)
381  return &lwlock_stats_dummy;
382 
383  /* Fetch or create the entry. */
384  MemSet(&key, 0, sizeof(key));
385  key.tranche = lock->tranche;
386  key.instance = lock;
387  lwstats = hash_search(lwlock_stats_htab, &key, HASH_ENTER, &found);
388  if (!found)
389  {
390  lwstats->sh_acquire_count = 0;
391  lwstats->ex_acquire_count = 0;
392  lwstats->block_count = 0;
393  lwstats->dequeue_self_count = 0;
394  lwstats->spin_delay_count = 0;
395  }
396  return lwstats;
397 }
398 #endif /* LWLOCK_STATS */
399 
400 
401 /*
402  * Compute number of LWLocks required by named tranches. These will be
403  * allocated in the main array.
404  */
405 static int
407 {
408  int numLocks = 0;
409  int i;
410 
411  for (i = 0; i < NamedLWLockTrancheRequests; i++)
412  numLocks += NamedLWLockTrancheRequestArray[i].num_lwlocks;
413 
414  return numLocks;
415 }
416 
417 /*
418  * Compute shmem space needed for LWLocks and named tranches.
419  */
420 Size
422 {
423  Size size;
424  int i;
425  int numLocks = NUM_FIXED_LWLOCKS;
426 
427  /* Calculate total number of locks needed in the main array. */
428  numLocks += NumLWLocksForNamedTranches();
429 
430  /* Space for the LWLock array. */
431  size = mul_size(numLocks, sizeof(LWLockPadded));
432 
433  /* Space for dynamic allocation counter, plus room for alignment. */
434  size = add_size(size, sizeof(int) + LWLOCK_PADDED_SIZE);
435 
436  /* space for named tranches. */
438 
439  /* space for name of each tranche. */
440  for (i = 0; i < NamedLWLockTrancheRequests; i++)
441  size = add_size(size, strlen(NamedLWLockTrancheRequestArray[i].tranche_name) + 1);
442 
443  return size;
444 }
445 
446 /*
447  * Allocate shmem space for the main LWLock array and all tranches and
448  * initialize it. We also register extension LWLock tranches here.
449  */
450 void
452 {
453  if (!IsUnderPostmaster)
454  {
455  Size spaceLocks = LWLockShmemSize();
456  int *LWLockCounter;
457  char *ptr;
458 
459  /* Allocate space */
460  ptr = (char *) ShmemAlloc(spaceLocks);
461 
462  /* Leave room for dynamic allocation of tranches */
463  ptr += sizeof(int);
464 
465  /* Ensure desired alignment of LWLock array */
466  ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
467 
468  MainLWLockArray = (LWLockPadded *) ptr;
469 
470  /*
471  * Initialize the dynamic-allocation counter for tranches, which is
472  * stored just before the first LWLock.
473  */
474  LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
475  *LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
476 
477  /* Initialize all LWLocks */
479  }
480 
481  /* Register named extension LWLock tranches in the current process. */
482  for (int i = 0; i < NamedLWLockTrancheRequests; i++)
484  NamedLWLockTrancheArray[i].trancheName);
485 }
486 
487 /*
488  * Initialize LWLocks that are fixed and those belonging to named tranches.
489  */
490 static void
492 {
493  int numNamedLocks = NumLWLocksForNamedTranches();
494  int id;
495  int i;
496  int j;
497  LWLockPadded *lock;
498 
499  /* Initialize all individual LWLocks in main array */
500  for (id = 0, lock = MainLWLockArray; id < NUM_INDIVIDUAL_LWLOCKS; id++, lock++)
501  LWLockInitialize(&lock->lock, id);
502 
503  /* Initialize buffer mapping LWLocks in main array */
505  for (id = 0; id < NUM_BUFFER_PARTITIONS; id++, lock++)
507 
508  /* Initialize lmgrs' LWLocks in main array */
510  for (id = 0; id < NUM_LOCK_PARTITIONS; id++, lock++)
512 
513  /* Initialize predicate lmgrs' LWLocks in main array */
515  for (id = 0; id < NUM_PREDICATELOCK_PARTITIONS; id++, lock++)
517 
518  /*
519  * Copy the info about any named tranches into shared memory (so that
520  * other processes can see it), and initialize the requested LWLocks.
521  */
523  {
524  char *trancheNames;
525 
527  &MainLWLockArray[NUM_FIXED_LWLOCKS + numNamedLocks];
528 
529  trancheNames = (char *) NamedLWLockTrancheArray +
532 
533  for (i = 0; i < NamedLWLockTrancheRequests; i++)
534  {
535  NamedLWLockTrancheRequest *request;
536  NamedLWLockTranche *tranche;
537  char *name;
538 
539  request = &NamedLWLockTrancheRequestArray[i];
540  tranche = &NamedLWLockTrancheArray[i];
541 
542  name = trancheNames;
543  trancheNames += strlen(request->tranche_name) + 1;
544  strcpy(name, request->tranche_name);
545  tranche->trancheId = LWLockNewTrancheId();
546  tranche->trancheName = name;
547 
548  for (j = 0; j < request->num_lwlocks; j++, lock++)
549  LWLockInitialize(&lock->lock, tranche->trancheId);
550  }
551  }
552 }
553 
554 /*
555  * InitLWLockAccess - initialize backend-local state needed to hold LWLocks
556  */
557 void
559 {
560 #ifdef LWLOCK_STATS
561  init_lwlock_stats();
562 #endif
563 }
564 
565 /*
566  * GetNamedLWLockTranche - returns the base address of LWLock from the
567  * specified tranche.
568  *
569  * Caller needs to retrieve the requested number of LWLocks starting from
570  * the base lock address returned by this API. This can be used for
571  * tranches that are requested by using RequestNamedLWLockTranche() API.
572  */
573 LWLockPadded *
574 GetNamedLWLockTranche(const char *tranche_name)
575 {
576  int lock_pos;
577  int i;
578 
579  /*
580  * Obtain the position of base address of LWLock belonging to requested
581  * tranche_name in MainLWLockArray. LWLocks for named tranches are placed
582  * in MainLWLockArray after fixed locks.
583  */
584  lock_pos = NUM_FIXED_LWLOCKS;
585  for (i = 0; i < NamedLWLockTrancheRequests; i++)
586  {
587  if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name,
588  tranche_name) == 0)
589  return &MainLWLockArray[lock_pos];
590 
592  }
593 
594  elog(ERROR, "requested tranche is not registered");
595 
596  /* just to keep compiler quiet */
597  return NULL;
598 }
599 
600 /*
601  * Allocate a new tranche ID.
602  */
603 int
605 {
606  int result;
607  int *LWLockCounter;
608 
609  LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
611  result = (*LWLockCounter)++;
613 
614  return result;
615 }
616 
617 /*
618  * Register a dynamic tranche name in the lookup table of the current process.
619  *
620  * This routine will save a pointer to the tranche name passed as an argument,
621  * so the name should be allocated in a backend-lifetime context
622  * (shared memory, TopMemoryContext, static constant, or similar).
623  *
624  * The tranche name will be user-visible as a wait event name, so try to
625  * use a name that fits the style for those.
626  */
627 void
628 LWLockRegisterTranche(int tranche_id, const char *tranche_name)
629 {
630  /* This should only be called for user-defined tranches. */
631  if (tranche_id < LWTRANCHE_FIRST_USER_DEFINED)
632  return;
633 
634  /* Convert to array index. */
635  tranche_id -= LWTRANCHE_FIRST_USER_DEFINED;
636 
637  /* If necessary, create or enlarge array. */
638  if (tranche_id >= LWLockTrancheNamesAllocated)
639  {
640  int newalloc;
641 
642  newalloc = pg_nextpower2_32(Max(8, tranche_id + 1));
643 
644  if (LWLockTrancheNames == NULL)
645  LWLockTrancheNames = (const char **)
647  newalloc * sizeof(char *));
648  else
651  LWLockTrancheNamesAllocated = newalloc;
652  }
653 
654  LWLockTrancheNames[tranche_id] = tranche_name;
655 }
656 
657 /*
658  * RequestNamedLWLockTranche
659  * Request that extra LWLocks be allocated during postmaster
660  * startup.
661  *
662  * This may only be called via the shmem_request_hook of a library that is
663  * loaded into the postmaster via shared_preload_libraries. Calls from
664  * elsewhere will fail.
665  *
666  * The tranche name will be user-visible as a wait event name, so try to
667  * use a name that fits the style for those.
668  */
669 void
670 RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
671 {
672  NamedLWLockTrancheRequest *request;
673 
675  elog(FATAL, "cannot request additional LWLocks outside shmem_request_hook");
676 
677  if (NamedLWLockTrancheRequestArray == NULL)
678  {
683  * sizeof(NamedLWLockTrancheRequest));
684  }
685 
687  {
689 
692  i * sizeof(NamedLWLockTrancheRequest));
694  }
695 
697  Assert(strlen(tranche_name) + 1 <= NAMEDATALEN);
698  strlcpy(request->tranche_name, tranche_name, NAMEDATALEN);
699  request->num_lwlocks = num_lwlocks;
701 }
702 
703 /*
704  * LWLockInitialize - initialize a new lwlock; it's initially unlocked
705  */
706 void
707 LWLockInitialize(LWLock *lock, int tranche_id)
708 {
710 #ifdef LOCK_DEBUG
711  pg_atomic_init_u32(&lock->nwaiters, 0);
712 #endif
713  lock->tranche = tranche_id;
714  proclist_init(&lock->waiters);
715 }
716 
717 /*
718  * Report start of wait event for light-weight locks.
719  *
720  * This function will be used by all the light-weight lock calls which
721  * needs to wait to acquire the lock. This function distinguishes wait
722  * event based on tranche and lock id.
723  */
724 static inline void
726 {
728 }
729 
730 /*
731  * Report end of wait event for light-weight locks.
732  */
733 static inline void
735 {
737 }
738 
739 /*
740  * Return the name of an LWLock tranche.
741  */
742 static const char *
744 {
745  /* Individual LWLock? */
746  if (trancheId < NUM_INDIVIDUAL_LWLOCKS)
747  return IndividualLWLockNames[trancheId];
748 
749  /* Built-in tranche? */
750  if (trancheId < LWTRANCHE_FIRST_USER_DEFINED)
751  return BuiltinTrancheNames[trancheId];
752 
753  /*
754  * It's an extension tranche, so look in LWLockTrancheNames[]. However,
755  * it's possible that the tranche has never been registered in the current
756  * process, in which case give up and return "extension".
757  */
758  trancheId -= LWTRANCHE_FIRST_USER_DEFINED;
759 
760  if (trancheId >= LWLockTrancheNamesAllocated ||
761  LWLockTrancheNames[trancheId] == NULL)
762  return "extension";
763 
764  return LWLockTrancheNames[trancheId];
765 }
766 
767 /*
768  * Return an identifier for an LWLock based on the wait class and event.
769  */
770 const char *
772 {
773  Assert(classId == PG_WAIT_LWLOCK);
774  /* The event IDs are just tranche numbers. */
775  return GetLWTrancheName(eventId);
776 }
777 
778 /*
779  * Internal function that tries to atomically acquire the lwlock in the passed
780  * in mode.
781  *
782  * This function will not block waiting for a lock to become free - that's the
783  * caller's job.
784  *
785  * Returns true if the lock isn't free and we need to wait.
786  */
787 static bool
789 {
790  uint32 old_state;
791 
793 
794  /*
795  * Read once outside the loop, later iterations will get the newer value
796  * via compare & exchange.
797  */
798  old_state = pg_atomic_read_u32(&lock->state);
799 
800  /* loop until we've determined whether we could acquire the lock or not */
801  while (true)
802  {
803  uint32 desired_state;
804  bool lock_free;
805 
806  desired_state = old_state;
807 
808  if (mode == LW_EXCLUSIVE)
809  {
810  lock_free = (old_state & LW_LOCK_MASK) == 0;
811  if (lock_free)
812  desired_state += LW_VAL_EXCLUSIVE;
813  }
814  else
815  {
816  lock_free = (old_state & LW_VAL_EXCLUSIVE) == 0;
817  if (lock_free)
818  desired_state += LW_VAL_SHARED;
819  }
820 
821  /*
822  * Attempt to swap in the state we are expecting. If we didn't see
823  * lock to be free, that's just the old value. If we saw it as free,
824  * we'll attempt to mark it acquired. The reason that we always swap
825  * in the value is that this doubles as a memory barrier. We could try
826  * to be smarter and only swap in values if we saw the lock as free,
827  * but benchmark haven't shown it as beneficial so far.
828  *
829  * Retry if the value changed since we last looked at it.
830  */
832  &old_state, desired_state))
833  {
834  if (lock_free)
835  {
836  /* Great! Got the lock. */
837 #ifdef LOCK_DEBUG
838  if (mode == LW_EXCLUSIVE)
839  lock->owner = MyProc;
840 #endif
841  return false;
842  }
843  else
844  return true; /* somebody else has the lock */
845  }
846  }
847  pg_unreachable();
848 }
849 
850 /*
851  * Lock the LWLock's wait list against concurrent activity.
852  *
853  * NB: even though the wait list is locked, non-conflicting lock operations
854  * may still happen concurrently.
855  *
856  * Time spent holding mutex should be short!
857  */
858 static void
860 {
861  uint32 old_state;
862 #ifdef LWLOCK_STATS
863  lwlock_stats *lwstats;
864  uint32 delays = 0;
865 
866  lwstats = get_lwlock_stats_entry(lock);
867 #endif
868 
869  while (true)
870  {
871  /* always try once to acquire lock directly */
872  old_state = pg_atomic_fetch_or_u32(&lock->state, LW_FLAG_LOCKED);
873  if (!(old_state & LW_FLAG_LOCKED))
874  break; /* got lock */
875 
876  /* and then spin without atomic operations until lock is released */
877  {
878  SpinDelayStatus delayStatus;
879 
880  init_local_spin_delay(&delayStatus);
881 
882  while (old_state & LW_FLAG_LOCKED)
883  {
884  perform_spin_delay(&delayStatus);
885  old_state = pg_atomic_read_u32(&lock->state);
886  }
887 #ifdef LWLOCK_STATS
888  delays += delayStatus.delays;
889 #endif
890  finish_spin_delay(&delayStatus);
891  }
892 
893  /*
894  * Retry. The lock might obviously already be re-acquired by the time
895  * we're attempting to get it again.
896  */
897  }
898 
899 #ifdef LWLOCK_STATS
900  lwstats->spin_delay_count += delays;
901 #endif
902 }
903 
904 /*
905  * Unlock the LWLock's wait list.
906  *
907  * Note that it can be more efficient to manipulate flags and release the
908  * locks in a single atomic operation.
909  */
910 static void
912 {
914 
915  old_state = pg_atomic_fetch_and_u32(&lock->state, ~LW_FLAG_LOCKED);
916 
917  Assert(old_state & LW_FLAG_LOCKED);
918 }
919 
920 /*
921  * Wakeup all the lockers that currently have a chance to acquire the lock.
922  */
923 static void
925 {
926  bool new_release_ok;
927  bool wokeup_somebody = false;
930 
932 
933  new_release_ok = true;
934 
935  /* lock wait list while collecting backends to wake up */
936  LWLockWaitListLock(lock);
937 
938  proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
939  {
940  PGPROC *waiter = GetPGProcByNumber(iter.cur);
941 
942  if (wokeup_somebody && waiter->lwWaitMode == LW_EXCLUSIVE)
943  continue;
944 
945  proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
946  proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
947 
948  if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
949  {
950  /*
951  * Prevent additional wakeups until retryer gets to run. Backends
952  * that are just waiting for the lock to become free don't retry
953  * automatically.
954  */
955  new_release_ok = false;
956 
957  /*
958  * Don't wakeup (further) exclusive locks.
959  */
960  wokeup_somebody = true;
961  }
962 
963  /*
964  * Signal that the process isn't on the wait list anymore. This allows
965  * LWLockDequeueSelf() to remove itself of the waitlist with a
966  * proclist_delete(), rather than having to check if it has been
967  * removed from the list.
968  */
969  Assert(waiter->lwWaiting == LW_WS_WAITING);
971 
972  /*
973  * Once we've woken up an exclusive lock, there's no point in waking
974  * up anybody else.
975  */
976  if (waiter->lwWaitMode == LW_EXCLUSIVE)
977  break;
978  }
979 
981 
982  /* unset required flags, and release lock, in one fell swoop */
983  {
984  uint32 old_state;
985  uint32 desired_state;
986 
987  old_state = pg_atomic_read_u32(&lock->state);
988  while (true)
989  {
990  desired_state = old_state;
991 
992  /* compute desired flags */
993 
994  if (new_release_ok)
995  desired_state |= LW_FLAG_RELEASE_OK;
996  else
997  desired_state &= ~LW_FLAG_RELEASE_OK;
998 
1000  desired_state &= ~LW_FLAG_HAS_WAITERS;
1001 
1002  desired_state &= ~LW_FLAG_LOCKED; /* release lock */
1003 
1004  if (pg_atomic_compare_exchange_u32(&lock->state, &old_state,
1005  desired_state))
1006  break;
1007  }
1008  }
1009 
1010  /* Awaken any waiters I removed from the queue. */
1011  proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1012  {
1013  PGPROC *waiter = GetPGProcByNumber(iter.cur);
1014 
1015  LOG_LWDEBUG("LWLockRelease", lock, "release waiter");
1016  proclist_delete(&wakeup, iter.cur, lwWaitLink);
1017 
1018  /*
1019  * Guarantee that lwWaiting being unset only becomes visible once the
1020  * unlink from the link has completed. Otherwise the target backend
1021  * could be woken up for other reason and enqueue for a new lock - if
1022  * that happens before the list unlink happens, the list would end up
1023  * being corrupted.
1024  *
1025  * The barrier pairs with the LWLockWaitListLock() when enqueuing for
1026  * another lock.
1027  */
1028  pg_write_barrier();
1029  waiter->lwWaiting = LW_WS_NOT_WAITING;
1030  PGSemaphoreUnlock(waiter->sem);
1031  }
1032 }
1033 
1034 /*
1035  * Add ourselves to the end of the queue.
1036  *
1037  * NB: Mode can be LW_WAIT_UNTIL_FREE here!
1038  */
1039 static void
1041 {
1042  /*
1043  * If we don't have a PGPROC structure, there's no way to wait. This
1044  * should never occur, since MyProc should only be null during shared
1045  * memory initialization.
1046  */
1047  if (MyProc == NULL)
1048  elog(PANIC, "cannot wait without a PGPROC structure");
1049 
1051  elog(PANIC, "queueing for lock while waiting on another one");
1052 
1053  LWLockWaitListLock(lock);
1054 
1055  /* setting the flag is protected by the spinlock */
1057 
1059  MyProc->lwWaitMode = mode;
1060 
1061  /* LW_WAIT_UNTIL_FREE waiters are always at the front of the queue */
1062  if (mode == LW_WAIT_UNTIL_FREE)
1063  proclist_push_head(&lock->waiters, MyProcNumber, lwWaitLink);
1064  else
1065  proclist_push_tail(&lock->waiters, MyProcNumber, lwWaitLink);
1066 
1067  /* Can release the mutex now */
1068  LWLockWaitListUnlock(lock);
1069 
1070 #ifdef LOCK_DEBUG
1071  pg_atomic_fetch_add_u32(&lock->nwaiters, 1);
1072 #endif
1073 }
1074 
1075 /*
1076  * Remove ourselves from the waitlist.
1077  *
1078  * This is used if we queued ourselves because we thought we needed to sleep
1079  * but, after further checking, we discovered that we don't actually need to
1080  * do so.
1081  */
1082 static void
1084 {
1085  bool on_waitlist;
1086 
1087 #ifdef LWLOCK_STATS
1088  lwlock_stats *lwstats;
1089 
1090  lwstats = get_lwlock_stats_entry(lock);
1091 
1092  lwstats->dequeue_self_count++;
1093 #endif
1094 
1095  LWLockWaitListLock(lock);
1096 
1097  /*
1098  * Remove ourselves from the waitlist, unless we've already been removed.
1099  * The removal happens with the wait list lock held, so there's no race in
1100  * this check.
1101  */
1102  on_waitlist = MyProc->lwWaiting == LW_WS_WAITING;
1103  if (on_waitlist)
1104  proclist_delete(&lock->waiters, MyProcNumber, lwWaitLink);
1105 
1106  if (proclist_is_empty(&lock->waiters) &&
1107  (pg_atomic_read_u32(&lock->state) & LW_FLAG_HAS_WAITERS) != 0)
1108  {
1110  }
1111 
1112  /* XXX: combine with fetch_and above? */
1113  LWLockWaitListUnlock(lock);
1114 
1115  /* clear waiting state again, nice for debugging */
1116  if (on_waitlist)
1118  else
1119  {
1120  int extraWaits = 0;
1121 
1122  /*
1123  * Somebody else dequeued us and has or will wake us up. Deal with the
1124  * superfluous absorption of a wakeup.
1125  */
1126 
1127  /*
1128  * Reset RELEASE_OK flag if somebody woke us before we removed
1129  * ourselves - they'll have set it to false.
1130  */
1132 
1133  /*
1134  * Now wait for the scheduled wakeup, otherwise our ->lwWaiting would
1135  * get reset at some inconvenient point later. Most of the time this
1136  * will immediately return.
1137  */
1138  for (;;)
1139  {
1142  break;
1143  extraWaits++;
1144  }
1145 
1146  /*
1147  * Fix the process wait semaphore's count for any absorbed wakeups.
1148  */
1149  while (extraWaits-- > 0)
1151  }
1152 
1153 #ifdef LOCK_DEBUG
1154  {
1155  /* not waiting anymore */
1156  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1157 
1158  Assert(nwaiters < MAX_BACKENDS);
1159  }
1160 #endif
1161 }
1162 
1163 /*
1164  * LWLockAcquire - acquire a lightweight lock in the specified mode
1165  *
1166  * If the lock is not available, sleep until it is. Returns true if the lock
1167  * was available immediately, false if we had to sleep.
1168  *
1169  * Side effect: cancel/die interrupts are held off until lock release.
1170  */
1171 bool
1173 {
1174  PGPROC *proc = MyProc;
1175  bool result = true;
1176  int extraWaits = 0;
1177 #ifdef LWLOCK_STATS
1178  lwlock_stats *lwstats;
1179 
1180  lwstats = get_lwlock_stats_entry(lock);
1181 #endif
1182 
1184 
1185  PRINT_LWDEBUG("LWLockAcquire", lock, mode);
1186 
1187 #ifdef LWLOCK_STATS
1188  /* Count lock acquisition attempts */
1189  if (mode == LW_EXCLUSIVE)
1190  lwstats->ex_acquire_count++;
1191  else
1192  lwstats->sh_acquire_count++;
1193 #endif /* LWLOCK_STATS */
1194 
1195  /*
1196  * We can't wait if we haven't got a PGPROC. This should only occur
1197  * during bootstrap or shared memory initialization. Put an Assert here
1198  * to catch unsafe coding practices.
1199  */
1200  Assert(!(proc == NULL && IsUnderPostmaster));
1201 
1202  /* Ensure we will have room to remember the lock */
1204  elog(ERROR, "too many LWLocks taken");
1205 
1206  /*
1207  * Lock out cancel/die interrupts until we exit the code section protected
1208  * by the LWLock. This ensures that interrupts will not interfere with
1209  * manipulations of data structures in shared memory.
1210  */
1211  HOLD_INTERRUPTS();
1212 
1213  /*
1214  * Loop here to try to acquire lock after each time we are signaled by
1215  * LWLockRelease.
1216  *
1217  * NOTE: it might seem better to have LWLockRelease actually grant us the
1218  * lock, rather than retrying and possibly having to go back to sleep. But
1219  * in practice that is no good because it means a process swap for every
1220  * lock acquisition when two or more processes are contending for the same
1221  * lock. Since LWLocks are normally used to protect not-very-long
1222  * sections of computation, a process needs to be able to acquire and
1223  * release the same lock many times during a single CPU time slice, even
1224  * in the presence of contention. The efficiency of being able to do that
1225  * outweighs the inefficiency of sometimes wasting a process dispatch
1226  * cycle because the lock is not free when a released waiter finally gets
1227  * to run. See pgsql-hackers archives for 29-Dec-01.
1228  */
1229  for (;;)
1230  {
1231  bool mustwait;
1232 
1233  /*
1234  * Try to grab the lock the first time, we're not in the waitqueue
1235  * yet/anymore.
1236  */
1237  mustwait = LWLockAttemptLock(lock, mode);
1238 
1239  if (!mustwait)
1240  {
1241  LOG_LWDEBUG("LWLockAcquire", lock, "immediately acquired lock");
1242  break; /* got the lock */
1243  }
1244 
1245  /*
1246  * Ok, at this point we couldn't grab the lock on the first try. We
1247  * cannot simply queue ourselves to the end of the list and wait to be
1248  * woken up because by now the lock could long have been released.
1249  * Instead add us to the queue and try to grab the lock again. If we
1250  * succeed we need to revert the queuing and be happy, otherwise we
1251  * recheck the lock. If we still couldn't grab it, we know that the
1252  * other locker will see our queue entries when releasing since they
1253  * existed before we checked for the lock.
1254  */
1255 
1256  /* add to the queue */
1257  LWLockQueueSelf(lock, mode);
1258 
1259  /* we're now guaranteed to be woken up if necessary */
1260  mustwait = LWLockAttemptLock(lock, mode);
1261 
1262  /* ok, grabbed the lock the second time round, need to undo queueing */
1263  if (!mustwait)
1264  {
1265  LOG_LWDEBUG("LWLockAcquire", lock, "acquired, undoing queue");
1266 
1267  LWLockDequeueSelf(lock);
1268  break;
1269  }
1270 
1271  /*
1272  * Wait until awakened.
1273  *
1274  * It is possible that we get awakened for a reason other than being
1275  * signaled by LWLockRelease. If so, loop back and wait again. Once
1276  * we've gotten the LWLock, re-increment the sema by the number of
1277  * additional signals received.
1278  */
1279  LOG_LWDEBUG("LWLockAcquire", lock, "waiting");
1280 
1281 #ifdef LWLOCK_STATS
1282  lwstats->block_count++;
1283 #endif
1284 
1285  LWLockReportWaitStart(lock);
1286  if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1287  TRACE_POSTGRESQL_LWLOCK_WAIT_START(T_NAME(lock), mode);
1288 
1289  for (;;)
1290  {
1291  PGSemaphoreLock(proc->sem);
1292  if (proc->lwWaiting == LW_WS_NOT_WAITING)
1293  break;
1294  extraWaits++;
1295  }
1296 
1297  /* Retrying, allow LWLockRelease to release waiters again. */
1299 
1300 #ifdef LOCK_DEBUG
1301  {
1302  /* not waiting anymore */
1303  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1304 
1305  Assert(nwaiters < MAX_BACKENDS);
1306  }
1307 #endif
1308 
1309  if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1310  TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(T_NAME(lock), mode);
1312 
1313  LOG_LWDEBUG("LWLockAcquire", lock, "awakened");
1314 
1315  /* Now loop back and try to acquire lock again. */
1316  result = false;
1317  }
1318 
1319  if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_ENABLED())
1320  TRACE_POSTGRESQL_LWLOCK_ACQUIRE(T_NAME(lock), mode);
1321 
1322  /* Add lock to list of locks held by this backend */
1325 
1326  /*
1327  * Fix the process wait semaphore's count for any absorbed wakeups.
1328  */
1329  while (extraWaits-- > 0)
1330  PGSemaphoreUnlock(proc->sem);
1331 
1332  return result;
1333 }
1334 
1335 /*
1336  * LWLockConditionalAcquire - acquire a lightweight lock in the specified mode
1337  *
1338  * If the lock is not available, return false with no side-effects.
1339  *
1340  * If successful, cancel/die interrupts are held off until lock release.
1341  */
1342 bool
1344 {
1345  bool mustwait;
1346 
1348 
1349  PRINT_LWDEBUG("LWLockConditionalAcquire", lock, mode);
1350 
1351  /* Ensure we will have room to remember the lock */
1353  elog(ERROR, "too many LWLocks taken");
1354 
1355  /*
1356  * Lock out cancel/die interrupts until we exit the code section protected
1357  * by the LWLock. This ensures that interrupts will not interfere with
1358  * manipulations of data structures in shared memory.
1359  */
1360  HOLD_INTERRUPTS();
1361 
1362  /* Check for the lock */
1363  mustwait = LWLockAttemptLock(lock, mode);
1364 
1365  if (mustwait)
1366  {
1367  /* Failed to get lock, so release interrupt holdoff */
1369 
1370  LOG_LWDEBUG("LWLockConditionalAcquire", lock, "failed");
1371  if (TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL_ENABLED())
1372  TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL(T_NAME(lock), mode);
1373  }
1374  else
1375  {
1376  /* Add lock to list of locks held by this backend */
1379  if (TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_ENABLED())
1380  TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE(T_NAME(lock), mode);
1381  }
1382  return !mustwait;
1383 }
1384 
1385 /*
1386  * LWLockAcquireOrWait - Acquire lock, or wait until it's free
1387  *
1388  * The semantics of this function are a bit funky. If the lock is currently
1389  * free, it is acquired in the given mode, and the function returns true. If
1390  * the lock isn't immediately free, the function waits until it is released
1391  * and returns false, but does not acquire the lock.
1392  *
1393  * This is currently used for WALWriteLock: when a backend flushes the WAL,
1394  * holding WALWriteLock, it can flush the commit records of many other
1395  * backends as a side-effect. Those other backends need to wait until the
1396  * flush finishes, but don't need to acquire the lock anymore. They can just
1397  * wake up, observe that their records have already been flushed, and return.
1398  */
1399 bool
1401 {
1402  PGPROC *proc = MyProc;
1403  bool mustwait;
1404  int extraWaits = 0;
1405 #ifdef LWLOCK_STATS
1406  lwlock_stats *lwstats;
1407 
1408  lwstats = get_lwlock_stats_entry(lock);
1409 #endif
1410 
1412 
1413  PRINT_LWDEBUG("LWLockAcquireOrWait", lock, mode);
1414 
1415  /* Ensure we will have room to remember the lock */
1417  elog(ERROR, "too many LWLocks taken");
1418 
1419  /*
1420  * Lock out cancel/die interrupts until we exit the code section protected
1421  * by the LWLock. This ensures that interrupts will not interfere with
1422  * manipulations of data structures in shared memory.
1423  */
1424  HOLD_INTERRUPTS();
1425 
1426  /*
1427  * NB: We're using nearly the same twice-in-a-row lock acquisition
1428  * protocol as LWLockAcquire(). Check its comments for details.
1429  */
1430  mustwait = LWLockAttemptLock(lock, mode);
1431 
1432  if (mustwait)
1433  {
1435 
1436  mustwait = LWLockAttemptLock(lock, mode);
1437 
1438  if (mustwait)
1439  {
1440  /*
1441  * Wait until awakened. Like in LWLockAcquire, be prepared for
1442  * bogus wakeups.
1443  */
1444  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "waiting");
1445 
1446 #ifdef LWLOCK_STATS
1447  lwstats->block_count++;
1448 #endif
1449 
1450  LWLockReportWaitStart(lock);
1451  if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1452  TRACE_POSTGRESQL_LWLOCK_WAIT_START(T_NAME(lock), mode);
1453 
1454  for (;;)
1455  {
1456  PGSemaphoreLock(proc->sem);
1457  if (proc->lwWaiting == LW_WS_NOT_WAITING)
1458  break;
1459  extraWaits++;
1460  }
1461 
1462 #ifdef LOCK_DEBUG
1463  {
1464  /* not waiting anymore */
1465  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1466 
1467  Assert(nwaiters < MAX_BACKENDS);
1468  }
1469 #endif
1470  if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1471  TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(T_NAME(lock), mode);
1473 
1474  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "awakened");
1475  }
1476  else
1477  {
1478  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "acquired, undoing queue");
1479 
1480  /*
1481  * Got lock in the second attempt, undo queueing. We need to treat
1482  * this as having successfully acquired the lock, otherwise we'd
1483  * not necessarily wake up people we've prevented from acquiring
1484  * the lock.
1485  */
1486  LWLockDequeueSelf(lock);
1487  }
1488  }
1489 
1490  /*
1491  * Fix the process wait semaphore's count for any absorbed wakeups.
1492  */
1493  while (extraWaits-- > 0)
1494  PGSemaphoreUnlock(proc->sem);
1495 
1496  if (mustwait)
1497  {
1498  /* Failed to get lock, so release interrupt holdoff */
1500  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "failed");
1501  if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL_ENABLED())
1502  TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL(T_NAME(lock), mode);
1503  }
1504  else
1505  {
1506  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "succeeded");
1507  /* Add lock to list of locks held by this backend */
1510  if (TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_ENABLED())
1511  TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT(T_NAME(lock), mode);
1512  }
1513 
1514  return !mustwait;
1515 }
1516 
1517 /*
1518  * Does the lwlock in its current state need to wait for the variable value to
1519  * change?
1520  *
1521  * If we don't need to wait, and it's because the value of the variable has
1522  * changed, store the current value in newval.
1523  *
1524  * *result is set to true if the lock was free, and false otherwise.
1525  */
1526 static bool
1527 LWLockConflictsWithVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval,
1528  uint64 *newval, bool *result)
1529 {
1530  bool mustwait;
1531  uint64 value;
1532 
1533  /*
1534  * Test first to see if it the slot is free right now.
1535  *
1536  * XXX: the unique caller of this routine, WaitXLogInsertionsToFinish()
1537  * via LWLockWaitForVar(), uses an implied barrier with a spinlock before
1538  * this, so we don't need a memory barrier here as far as the current
1539  * usage is concerned. But that might not be safe in general.
1540  */
1541  mustwait = (pg_atomic_read_u32(&lock->state) & LW_VAL_EXCLUSIVE) != 0;
1542 
1543  if (!mustwait)
1544  {
1545  *result = true;
1546  return false;
1547  }
1548 
1549  *result = false;
1550 
1551  /*
1552  * Reading this value atomically is safe even on platforms where uint64
1553  * cannot be read without observing a torn value.
1554  */
1555  value = pg_atomic_read_u64(valptr);
1556 
1557  if (value != oldval)
1558  {
1559  mustwait = false;
1560  *newval = value;
1561  }
1562  else
1563  {
1564  mustwait = true;
1565  }
1566 
1567  return mustwait;
1568 }
1569 
1570 /*
1571  * LWLockWaitForVar - Wait until lock is free, or a variable is updated.
1572  *
1573  * If the lock is held and *valptr equals oldval, waits until the lock is
1574  * either freed, or the lock holder updates *valptr by calling
1575  * LWLockUpdateVar. If the lock is free on exit (immediately or after
1576  * waiting), returns true. If the lock is still held, but *valptr no longer
1577  * matches oldval, returns false and sets *newval to the current value in
1578  * *valptr.
1579  *
1580  * Note: this function ignores shared lock holders; if the lock is held
1581  * in shared mode, returns 'true'.
1582  *
1583  * Be aware that LWLockConflictsWithVar() does not include a memory barrier,
1584  * hence the caller of this function may want to rely on an explicit barrier or
1585  * an implied barrier via spinlock or LWLock to avoid memory ordering issues.
1586  */
1587 bool
1588 LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval,
1589  uint64 *newval)
1590 {
1591  PGPROC *proc = MyProc;
1592  int extraWaits = 0;
1593  bool result = false;
1594 #ifdef LWLOCK_STATS
1595  lwlock_stats *lwstats;
1596 
1597  lwstats = get_lwlock_stats_entry(lock);
1598 #endif
1599 
1600  PRINT_LWDEBUG("LWLockWaitForVar", lock, LW_WAIT_UNTIL_FREE);
1601 
1602  /*
1603  * Lock out cancel/die interrupts while we sleep on the lock. There is no
1604  * cleanup mechanism to remove us from the wait queue if we got
1605  * interrupted.
1606  */
1607  HOLD_INTERRUPTS();
1608 
1609  /*
1610  * Loop here to check the lock's status after each time we are signaled.
1611  */
1612  for (;;)
1613  {
1614  bool mustwait;
1615 
1616  mustwait = LWLockConflictsWithVar(lock, valptr, oldval, newval,
1617  &result);
1618 
1619  if (!mustwait)
1620  break; /* the lock was free or value didn't match */
1621 
1622  /*
1623  * Add myself to wait queue. Note that this is racy, somebody else
1624  * could wakeup before we're finished queuing. NB: We're using nearly
1625  * the same twice-in-a-row lock acquisition protocol as
1626  * LWLockAcquire(). Check its comments for details. The only
1627  * difference is that we also have to check the variable's values when
1628  * checking the state of the lock.
1629  */
1631 
1632  /*
1633  * Set RELEASE_OK flag, to make sure we get woken up as soon as the
1634  * lock is released.
1635  */
1637 
1638  /*
1639  * We're now guaranteed to be woken up if necessary. Recheck the lock
1640  * and variables state.
1641  */
1642  mustwait = LWLockConflictsWithVar(lock, valptr, oldval, newval,
1643  &result);
1644 
1645  /* Ok, no conflict after we queued ourselves. Undo queueing. */
1646  if (!mustwait)
1647  {
1648  LOG_LWDEBUG("LWLockWaitForVar", lock, "free, undoing queue");
1649 
1650  LWLockDequeueSelf(lock);
1651  break;
1652  }
1653 
1654  /*
1655  * Wait until awakened.
1656  *
1657  * It is possible that we get awakened for a reason other than being
1658  * signaled by LWLockRelease. If so, loop back and wait again. Once
1659  * we've gotten the LWLock, re-increment the sema by the number of
1660  * additional signals received.
1661  */
1662  LOG_LWDEBUG("LWLockWaitForVar", lock, "waiting");
1663 
1664 #ifdef LWLOCK_STATS
1665  lwstats->block_count++;
1666 #endif
1667 
1668  LWLockReportWaitStart(lock);
1669  if (TRACE_POSTGRESQL_LWLOCK_WAIT_START_ENABLED())
1670  TRACE_POSTGRESQL_LWLOCK_WAIT_START(T_NAME(lock), LW_EXCLUSIVE);
1671 
1672  for (;;)
1673  {
1674  PGSemaphoreLock(proc->sem);
1675  if (proc->lwWaiting == LW_WS_NOT_WAITING)
1676  break;
1677  extraWaits++;
1678  }
1679 
1680 #ifdef LOCK_DEBUG
1681  {
1682  /* not waiting anymore */
1683  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1684 
1685  Assert(nwaiters < MAX_BACKENDS);
1686  }
1687 #endif
1688 
1689  if (TRACE_POSTGRESQL_LWLOCK_WAIT_DONE_ENABLED())
1690  TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(T_NAME(lock), LW_EXCLUSIVE);
1692 
1693  LOG_LWDEBUG("LWLockWaitForVar", lock, "awakened");
1694 
1695  /* Now loop back and check the status of the lock again. */
1696  }
1697 
1698  /*
1699  * Fix the process wait semaphore's count for any absorbed wakeups.
1700  */
1701  while (extraWaits-- > 0)
1702  PGSemaphoreUnlock(proc->sem);
1703 
1704  /*
1705  * Now okay to allow cancel/die interrupts.
1706  */
1708 
1709  return result;
1710 }
1711 
1712 
1713 /*
1714  * LWLockUpdateVar - Update a variable and wake up waiters atomically
1715  *
1716  * Sets *valptr to 'val', and wakes up all processes waiting for us with
1717  * LWLockWaitForVar(). It first sets the value atomically and then wakes up
1718  * waiting processes so that any process calling LWLockWaitForVar() on the same
1719  * lock is guaranteed to see the new value, and act accordingly.
1720  *
1721  * The caller must be holding the lock in exclusive mode.
1722  */
1723 void
1725 {
1727  proclist_mutable_iter iter;
1728 
1729  PRINT_LWDEBUG("LWLockUpdateVar", lock, LW_EXCLUSIVE);
1730 
1731  /*
1732  * Note that pg_atomic_exchange_u64 is a full barrier, so we're guaranteed
1733  * that the variable is updated before waking up waiters.
1734  */
1735  pg_atomic_exchange_u64(valptr, val);
1736 
1738 
1739  LWLockWaitListLock(lock);
1740 
1742 
1743  /*
1744  * See if there are any LW_WAIT_UNTIL_FREE waiters that need to be woken
1745  * up. They are always in the front of the queue.
1746  */
1747  proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
1748  {
1749  PGPROC *waiter = GetPGProcByNumber(iter.cur);
1750 
1751  if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
1752  break;
1753 
1754  proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
1755  proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
1756 
1757  /* see LWLockWakeup() */
1758  Assert(waiter->lwWaiting == LW_WS_WAITING);
1759  waiter->lwWaiting = LW_WS_PENDING_WAKEUP;
1760  }
1761 
1762  /* We are done updating shared state of the lock itself. */
1763  LWLockWaitListUnlock(lock);
1764 
1765  /*
1766  * Awaken any waiters I removed from the queue.
1767  */
1768  proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1769  {
1770  PGPROC *waiter = GetPGProcByNumber(iter.cur);
1771 
1772  proclist_delete(&wakeup, iter.cur, lwWaitLink);
1773  /* check comment in LWLockWakeup() about this barrier */
1774  pg_write_barrier();
1775  waiter->lwWaiting = LW_WS_NOT_WAITING;
1776  PGSemaphoreUnlock(waiter->sem);
1777  }
1778 }
1779 
1780 
1781 /*
1782  * LWLockRelease - release a previously acquired lock
1783  */
1784 void
1786 {
1787  LWLockMode mode;
1788  uint32 oldstate;
1789  bool check_waiters;
1790  int i;
1791 
1792  /*
1793  * Remove lock from list of locks held. Usually, but not always, it will
1794  * be the latest-acquired lock; so search array backwards.
1795  */
1796  for (i = num_held_lwlocks; --i >= 0;)
1797  if (lock == held_lwlocks[i].lock)
1798  break;
1799 
1800  if (i < 0)
1801  elog(ERROR, "lock %s is not held", T_NAME(lock));
1802 
1803  mode = held_lwlocks[i].mode;
1804 
1805  num_held_lwlocks--;
1806  for (; i < num_held_lwlocks; i++)
1807  held_lwlocks[i] = held_lwlocks[i + 1];
1808 
1809  PRINT_LWDEBUG("LWLockRelease", lock, mode);
1810 
1811  /*
1812  * Release my hold on lock, after that it can immediately be acquired by
1813  * others, even if we still have to wakeup other waiters.
1814  */
1815  if (mode == LW_EXCLUSIVE)
1816  oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_EXCLUSIVE);
1817  else
1818  oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_SHARED);
1819 
1820  /* nobody else can have that kind of lock */
1821  Assert(!(oldstate & LW_VAL_EXCLUSIVE));
1822 
1823  if (TRACE_POSTGRESQL_LWLOCK_RELEASE_ENABLED())
1824  TRACE_POSTGRESQL_LWLOCK_RELEASE(T_NAME(lock));
1825 
1826  /*
1827  * We're still waiting for backends to get scheduled, don't wake them up
1828  * again.
1829  */
1830  if ((oldstate & (LW_FLAG_HAS_WAITERS | LW_FLAG_RELEASE_OK)) ==
1832  (oldstate & LW_LOCK_MASK) == 0)
1833  check_waiters = true;
1834  else
1835  check_waiters = false;
1836 
1837  /*
1838  * As waking up waiters requires the spinlock to be acquired, only do so
1839  * if necessary.
1840  */
1841  if (check_waiters)
1842  {
1843  /* XXX: remove before commit? */
1844  LOG_LWDEBUG("LWLockRelease", lock, "releasing waiters");
1845  LWLockWakeup(lock);
1846  }
1847 
1848  /*
1849  * Now okay to allow cancel/die interrupts.
1850  */
1852 }
1853 
1854 /*
1855  * LWLockReleaseClearVar - release a previously acquired lock, reset variable
1856  */
1857 void
1859 {
1860  /*
1861  * Note that pg_atomic_exchange_u64 is a full barrier, so we're guaranteed
1862  * that the variable is updated before releasing the lock.
1863  */
1864  pg_atomic_exchange_u64(valptr, val);
1865 
1866  LWLockRelease(lock);
1867 }
1868 
1869 
1870 /*
1871  * LWLockReleaseAll - release all currently-held locks
1872  *
1873  * Used to clean up after ereport(ERROR). An important difference between this
1874  * function and retail LWLockRelease calls is that InterruptHoldoffCount is
1875  * unchanged by this operation. This is necessary since InterruptHoldoffCount
1876  * has been set to an appropriate level earlier in error recovery. We could
1877  * decrement it below zero if we allow it to drop for each released lock!
1878  */
1879 void
1881 {
1882  while (num_held_lwlocks > 0)
1883  {
1884  HOLD_INTERRUPTS(); /* match the upcoming RESUME_INTERRUPTS */
1885 
1887  }
1888 }
1889 
1890 
1891 /*
1892  * LWLockHeldByMe - test whether my process holds a lock in any mode
1893  *
1894  * This is meant as debug support only.
1895  */
1896 bool
1898 {
1899  int i;
1900 
1901  for (i = 0; i < num_held_lwlocks; i++)
1902  {
1903  if (held_lwlocks[i].lock == lock)
1904  return true;
1905  }
1906  return false;
1907 }
1908 
1909 /*
1910  * LWLockAnyHeldByMe - test whether my process holds any of an array of locks
1911  *
1912  * This is meant as debug support only.
1913  */
1914 bool
1915 LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
1916 {
1917  char *held_lock_addr;
1918  char *begin;
1919  char *end;
1920  int i;
1921 
1922  begin = (char *) lock;
1923  end = begin + nlocks * stride;
1924  for (i = 0; i < num_held_lwlocks; i++)
1925  {
1926  held_lock_addr = (char *) held_lwlocks[i].lock;
1927  if (held_lock_addr >= begin &&
1928  held_lock_addr < end &&
1929  (held_lock_addr - begin) % stride == 0)
1930  return true;
1931  }
1932  return false;
1933 }
1934 
1935 /*
1936  * LWLockHeldByMeInMode - test whether my process holds a lock in given mode
1937  *
1938  * This is meant as debug support only.
1939  */
1940 bool
1942 {
1943  int i;
1944 
1945  for (i = 0; i < num_held_lwlocks; i++)
1946  {
1947  if (held_lwlocks[i].lock == lock && held_lwlocks[i].mode == mode)
1948  return true;
1949  }
1950  return false;
1951 }
static uint32 pg_atomic_fetch_and_u32(volatile pg_atomic_uint32 *ptr, uint32 and_)
Definition: atomics.h:391
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:344
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:405
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:434
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:376
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:216
#define pg_write_barrier()
Definition: atomics.h:152
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:361
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:234
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:462
static uint64 pg_atomic_exchange_u64(volatile pg_atomic_uint64 *ptr, uint64 newval)
Definition: atomics.h:498
unsigned short uint16
Definition: c.h:492
unsigned int uint32
Definition: c.h:493
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:169
#define Max(x, y)
Definition: c.h:985
#define pg_unreachable()
Definition: c.h:283
#define lengthof(array)
Definition: c.h:775
#define MemSet(start, val, len)
Definition: c.h:1007
size_t Size
Definition: c.h:592
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1395
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errhidestmt(bool hide_stmt)
Definition: elog.c:1413
int errhidecontext(bool hide_ctx)
Definition: elog.c:1432
#define LOG
Definition: elog.h:31
#define FATAL
Definition: elog.h:41
#define PANIC
Definition: elog.h:42
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
int MyProcPid
Definition: globals.c:45
ProcNumber MyProcNumber
Definition: globals.c:87
bool IsUnderPostmaster
Definition: globals.c:117
#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:664
static struct @150 value
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
int j
Definition: isn.c:74
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
#define LW_VAL_EXCLUSIVE
Definition: lwlock.c:101
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition: lwlock.c:1724
StaticAssertDecl(LW_VAL_EXCLUSIVE >(uint32) MAX_BACKENDS, "MAX_BACKENDS too big for lwlock.c")
static void LWLockWakeup(LWLock *lock)
Definition: lwlock.c:924
#define LW_FLAG_LOCKED
Definition: lwlock.c:99
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1897
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:771
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition: lwlock.c:574
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:207
static int LWLockTrancheNamesAllocated
Definition: lwlock.c:182
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition: lwlock.c:1858
const char *const IndividualLWLockNames[]
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1172
void CreateLWLocks(void)
Definition: lwlock.c:451
NamedLWLockTranche * NamedLWLockTrancheArray
Definition: lwlock.c:228
#define LW_VAL_SHARED
Definition: lwlock.c:102
static bool LWLockAttemptLock(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:788
static void LWLockWaitListLock(LWLock *lock)
Definition: lwlock.c:859
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:628
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1941
static void LWLockReportWaitEnd(void)
Definition: lwlock.c:734
struct LWLockHandle LWLockHandle
bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
Definition: lwlock.c:1588
int LWLockNewTrancheId(void)
Definition: lwlock.c:604
slock_t * ShmemLock
Definition: shmem.c:87
static const char * GetLWTrancheName(uint16 trancheId)
Definition: lwlock.c:743
#define LW_LOCK_MASK
Definition: lwlock.c:104
int NamedLWLockTrancheRequests
Definition: lwlock.c:225
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition: lwlock.c:670
#define LW_FLAG_RELEASE_OK
Definition: lwlock.c:98
#define LW_FLAG_HAS_WAITERS
Definition: lwlock.c:97
#define MAX_SIMUL_LWLOCKS
Definition: lwlock.c:197
struct NamedLWLockTrancheRequest NamedLWLockTrancheRequest
static int NumLWLocksForNamedTranches(void)
Definition: lwlock.c:406
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1785
#define T_NAME(lock)
Definition: lwlock.c:235
static int num_held_lwlocks
Definition: lwlock.c:206
void LWLockReleaseAll(void)
Definition: lwlock.c:1880
static void InitializeLWLocks(void)
Definition: lwlock.c:491
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
static int NamedLWLockTrancheRequestsAllocated
Definition: lwlock.c:217
static const char *const BuiltinTrancheNames[]
Definition: lwlock.c:131
static NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition: lwlock.c:216
static void LWLockWaitListUnlock(LWLock *lock)
Definition: lwlock.c:911
static const char ** LWLockTrancheNames
Definition: lwlock.c:181
#define LOG_LWDEBUG(a, b, c)
Definition: lwlock.c:300
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1343
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1400
static void LWLockQueueSelf(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1040
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:299
static void LWLockReportWaitStart(LWLock *lock)
Definition: lwlock.c:725
LWLockPadded * MainLWLockArray
Definition: lwlock.c:189
static void LWLockDequeueSelf(LWLock *lock)
Definition: lwlock.c:1083
Size LWLockShmemSize(void)
Definition: lwlock.c:421
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
Definition: lwlock.c:1915
#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:1527
void InitLWLockAccess(void)
Definition: lwlock.c:558
@ LW_WS_NOT_WAITING
Definition: lwlock.h:29
@ LW_WS_WAITING
Definition: lwlock.h:30
@ LW_WS_PENDING_WAKEUP
Definition: lwlock.h:31
#define LWLOCK_PADDED_SIZE
Definition: lwlock.h:61
#define BUFFER_MAPPING_LWLOCK_OFFSET
Definition: lwlock.h:106
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:99
@ LWTRANCHE_FIRST_USER_DEFINED
Definition: lwlock.h:219
@ LWTRANCHE_SHARED_TIDBITMAP
Definition: lwlock.h:202
@ LWTRANCHE_SERIAL_SLRU
Definition: lwlock.h:216
@ LWTRANCHE_PER_SESSION_DSA
Definition: lwlock.h:198
@ LWTRANCHE_PARALLEL_QUERY_DSA
Definition: lwlock.h:197
@ LWTRANCHE_COMMITTS_BUFFER
Definition: lwlock.h:182
@ LWTRANCHE_PGSTATS_HASH
Definition: lwlock.h:206
@ LWTRANCHE_SUBTRANS_BUFFER
Definition: lwlock.h:183
@ LWTRANCHE_PER_SESSION_RECORD_TYPMOD
Definition: lwlock.h:200
@ LWTRANCHE_LAUNCHER_HASH
Definition: lwlock.h:209
@ LWTRANCHE_DSM_REGISTRY_DSA
Definition: lwlock.h:210
@ LWTRANCHE_XACT_BUFFER
Definition: lwlock.h:181
@ LWTRANCHE_DSM_REGISTRY_HASH
Definition: lwlock.h:211
@ LWTRANCHE_NOTIFY_SLRU
Definition: lwlock.h:215
@ LWTRANCHE_REPLICATION_ORIGIN_STATE
Definition: lwlock.h:190
@ LWTRANCHE_MULTIXACTOFFSET_SLRU
Definition: lwlock.h:214
@ LWTRANCHE_PARALLEL_APPEND
Definition: lwlock.h:203
@ LWTRANCHE_REPLICATION_SLOT_IO
Definition: lwlock.h:191
@ LWTRANCHE_SUBTRANS_SLRU
Definition: lwlock.h:217
@ LWTRANCHE_MULTIXACTMEMBER_SLRU
Definition: lwlock.h:213
@ LWTRANCHE_BUFFER_CONTENT
Definition: lwlock.h:189
@ LWTRANCHE_MULTIXACTMEMBER_BUFFER
Definition: lwlock.h:185
@ LWTRANCHE_NOTIFY_BUFFER
Definition: lwlock.h:186
@ LWTRANCHE_PER_SESSION_RECORD_TYPE
Definition: lwlock.h:199
@ LWTRANCHE_PREDICATE_LOCK_MANAGER
Definition: lwlock.h:195
@ LWTRANCHE_BUFFER_MAPPING
Definition: lwlock.h:193
@ LWTRANCHE_SERIAL_BUFFER
Definition: lwlock.h:187
@ LWTRANCHE_LAUNCHER_DSA
Definition: lwlock.h:208
@ LWTRANCHE_PGSTATS_DSA
Definition: lwlock.h:205
@ LWTRANCHE_PARALLEL_HASH_JOIN
Definition: lwlock.h:196
@ LWTRANCHE_COMMITTS_SLRU
Definition: lwlock.h:212
@ LWTRANCHE_PGSTATS_DATA
Definition: lwlock.h:207
@ LWTRANCHE_PER_XACT_PREDICATE_LIST
Definition: lwlock.h:204
@ LWTRANCHE_XACT_SLRU
Definition: lwlock.h:218
@ LWTRANCHE_MULTIXACTOFFSET_BUFFER
Definition: lwlock.h:184
@ LWTRANCHE_WAL_INSERT
Definition: lwlock.h:188
@ LWTRANCHE_LOCK_MANAGER
Definition: lwlock.h:194
@ LWTRANCHE_SHARED_TUPLESTORE
Definition: lwlock.h:201
@ LWTRANCHE_LOCK_FASTPATH
Definition: lwlock.h:192
#define LOCK_MANAGER_LWLOCK_OFFSET
Definition: lwlock.h:107
#define NUM_BUFFER_PARTITIONS
Definition: lwlock.h:95
#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET
Definition: lwlock.h:109
#define NUM_FIXED_LWLOCKS
Definition: lwlock.h:111
LWLockMode
Definition: lwlock.h:115
@ LW_SHARED
Definition: lwlock.h:117
@ LW_WAIT_UNTIL_FREE
Definition: lwlock.h:118
@ LW_EXCLUSIVE
Definition: lwlock.h:116
#define NUM_PREDICATELOCK_PARTITIONS
Definition: lwlock.h:103
MemoryContext TopMemoryContext
Definition: mcxt.c:137
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1202
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1528
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1168
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:442
void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow)
Definition: mcxt.c:682
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:135
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
bool process_shmem_requests_in_progress
Definition: miscinit.c:1782
#define repalloc0_array(pointer, type, oldcount, count)
Definition: palloc.h:109
void * arg
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:189
static PgChecksumMode mode
Definition: pg_checksums.c:56
#define NAMEDATALEN
#define fprintf
Definition: port.h:242
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:340
void PGSemaphoreLock(PGSemaphore sema)
Definition: posix_sema.c:320
uintptr_t Datum
Definition: postgres.h:64
#define MAX_BACKENDS
Definition: postmaster.h:95
#define GetPGProcByNumber(n)
Definition: proc.h:428
#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
void perform_spin_delay(SpinDelayStatus *status)
Definition: s_lock.c:132
void finish_spin_delay(SpinDelayStatus *status)
Definition: s_lock.c:192
#define init_local_spin_delay(status)
Definition: s_lock.h:843
int slock_t
Definition: s_lock.h:735
void * ShmemAlloc(Size size)
Definition: shmem.c:152
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static pg_noinline void Size size
Definition: slab.c:607
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
PGPROC * MyProc
Definition: proc.c:66
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:220
LWLockMode mode
Definition: lwlock.c:203
LWLock * lock
Definition: lwlock.c:202
Definition: lwlock.h:41
pg_atomic_uint32 state
Definition: lwlock.h:43
uint16 tranche
Definition: lwlock.h:42
proclist_head waiters
Definition: lwlock.h:44
char tranche_name[NAMEDATALEN]
Definition: lwlock.c:212
char * trancheName
Definition: lwlock.h:79
Definition: proc.h:157
uint8 lwWaitMode
Definition: proc.h:220
PGSemaphore sem
Definition: proc.h:162
uint8 lwWaiting
Definition: proc.h:219
Definition: regguts.h:323
LWLock lock
Definition: lwlock.h:69
#define PG_WAIT_LWLOCK
Definition: wait_event.h:18
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:88
static void pgstat_report_wait_end(void)
Definition: wait_event.h:104
const char * name
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]
Definition: walreceiver.c:129