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