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