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