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