2
0

lwlock.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*-------------------------------------------------------------------------
  2. *
  3. * lwlock.h
  4. * Lightweight lock manager
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/storage/lwlock.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef LWLOCK_H
  15. #define LWLOCK_H
  16. #ifdef FRONTEND
  17. #error "lwlock.h may not be included from frontend code"
  18. #endif
  19. #include "port/atomics.h"
  20. #include "storage/proclist_types.h"
  21. struct PGPROC;
  22. /*
  23. * Code outside of lwlock.c should not manipulate the contents of this
  24. * structure directly, but we have to declare it here to allow LWLocks to be
  25. * incorporated into other data structures.
  26. */
  27. typedef struct LWLock
  28. {
  29. uint16 tranche; /* tranche ID */
  30. pg_atomic_uint32 state; /* state of exclusive/nonexclusive lockers */
  31. proclist_head waiters; /* list of waiting PGPROCs */
  32. #ifdef LOCK_DEBUG
  33. pg_atomic_uint32 nwaiters; /* number of waiters */
  34. struct PGPROC *owner; /* last exclusive owner of the lock */
  35. #endif
  36. } LWLock;
  37. /*
  38. * In most cases, it's desirable to force each tranche of LWLocks to be aligned
  39. * on a cache line boundary and make the array stride a power of 2. This saves
  40. * a few cycles in indexing, but more importantly ensures that individual
  41. * LWLocks don't cross cache line boundaries. This reduces cache contention
  42. * problems, especially on AMD Opterons. In some cases, it's useful to add
  43. * even more padding so that each LWLock takes up an entire cache line; this is
  44. * useful, for example, in the main LWLock array, where the overall number of
  45. * locks is small but some are heavily contended.
  46. */
  47. #define LWLOCK_PADDED_SIZE PG_CACHE_LINE_SIZE
  48. /* LWLock, padded to a full cache line size */
  49. typedef union LWLockPadded
  50. {
  51. LWLock lock;
  52. char pad[LWLOCK_PADDED_SIZE];
  53. } LWLockPadded;
  54. extern PGDLLIMPORT LWLockPadded *MainLWLockArray;
  55. /* struct for storing named tranche information */
  56. typedef struct NamedLWLockTranche
  57. {
  58. int trancheId;
  59. char *trancheName;
  60. } NamedLWLockTranche;
  61. extern PGDLLIMPORT NamedLWLockTranche *NamedLWLockTrancheArray;
  62. extern PGDLLIMPORT int NamedLWLockTrancheRequests;
  63. /* Names for fixed lwlocks */
  64. #include "storage/lwlocknames.h"
  65. /*
  66. * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
  67. * here, but we need them to figure out offsets within MainLWLockArray, and
  68. * having this file include lock.h or bufmgr.h would be backwards.
  69. */
  70. /* Number of partitions of the shared buffer mapping hashtable */
  71. #define NUM_BUFFER_PARTITIONS 128
  72. /* Number of partitions the shared lock tables are divided into */
  73. #define LOG2_NUM_LOCK_PARTITIONS 4
  74. #define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)
  75. /* Number of partitions the shared predicate lock tables are divided into */
  76. #define LOG2_NUM_PREDICATELOCK_PARTITIONS 4
  77. #define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
  78. /* Offsets for various chunks of preallocated lwlocks. */
  79. #define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS
  80. #define LOCK_MANAGER_LWLOCK_OFFSET \
  81. (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
  82. #define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \
  83. (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
  84. #define NUM_FIXED_LWLOCKS \
  85. (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
  86. typedef enum LWLockMode
  87. {
  88. LW_EXCLUSIVE,
  89. LW_SHARED,
  90. LW_WAIT_UNTIL_FREE /* A special mode used in PGPROC->lwWaitMode,
  91. * when waiting for lock to become free. Not
  92. * to be used as LWLockAcquire argument */
  93. } LWLockMode;
  94. #ifdef LOCK_DEBUG
  95. extern PGDLLIMPORT bool Trace_lwlocks;
  96. #endif
  97. extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);
  98. extern bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode);
  99. extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
  100. extern void LWLockRelease(LWLock *lock);
  101. extern void LWLockReleaseClearVar(LWLock *lock, uint64 *valptr, uint64 val);
  102. extern void LWLockReleaseAll(void);
  103. extern bool LWLockHeldByMe(LWLock *lock);
  104. extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
  105. extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
  106. extern bool LWLockWaitForVar(LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval);
  107. extern void LWLockUpdateVar(LWLock *lock, uint64 *valptr, uint64 value);
  108. extern Size LWLockShmemSize(void);
  109. extern void CreateLWLocks(void);
  110. extern void InitLWLockAccess(void);
  111. extern const char *GetLWLockIdentifier(uint32 classId, uint16 eventId);
  112. /*
  113. * Extensions (or core code) can obtain an LWLocks by calling
  114. * RequestNamedLWLockTranche() during postmaster startup. Subsequently,
  115. * call GetNamedLWLockTranche() to obtain a pointer to an array containing
  116. * the number of LWLocks requested.
  117. */
  118. extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
  119. extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);
  120. /*
  121. * There is another, more flexible method of obtaining lwlocks. First, call
  122. * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from
  123. * a shared counter. Next, each individual process using the tranche should
  124. * call LWLockRegisterTranche() to associate that tranche ID with a name.
  125. * Finally, LWLockInitialize should be called just once per lwlock, passing
  126. * the tranche ID as an argument.
  127. *
  128. * It may seem strange that each process using the tranche must register it
  129. * separately, but dynamic shared memory segments aren't guaranteed to be
  130. * mapped at the same address in all coordinating backends, so storing the
  131. * registration in the main shared memory segment wouldn't work for that case.
  132. */
  133. extern int LWLockNewTrancheId(void);
  134. extern void LWLockRegisterTranche(int tranche_id, const char *tranche_name);
  135. extern void LWLockInitialize(LWLock *lock, int tranche_id);
  136. /*
  137. * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,
  138. * we reserve additional tranche IDs for builtin tranches not included in
  139. * the set of individual LWLocks. A call to LWLockNewTrancheId will never
  140. * return a value less than LWTRANCHE_FIRST_USER_DEFINED.
  141. */
  142. typedef enum BuiltinTrancheIds
  143. {
  144. LWTRANCHE_XACT_BUFFER = NUM_INDIVIDUAL_LWLOCKS,
  145. LWTRANCHE_COMMITTS_BUFFER,
  146. LWTRANCHE_SUBTRANS_BUFFER,
  147. LWTRANCHE_MULTIXACTOFFSET_BUFFER,
  148. LWTRANCHE_MULTIXACTMEMBER_BUFFER,
  149. LWTRANCHE_NOTIFY_BUFFER,
  150. LWTRANCHE_SERIAL_BUFFER,
  151. LWTRANCHE_WAL_INSERT,
  152. LWTRANCHE_BUFFER_CONTENT,
  153. LWTRANCHE_REPLICATION_ORIGIN_STATE,
  154. LWTRANCHE_REPLICATION_SLOT_IO,
  155. LWTRANCHE_LOCK_FASTPATH,
  156. LWTRANCHE_BUFFER_MAPPING,
  157. LWTRANCHE_LOCK_MANAGER,
  158. LWTRANCHE_PREDICATE_LOCK_MANAGER,
  159. LWTRANCHE_PARALLEL_HASH_JOIN,
  160. LWTRANCHE_PARALLEL_QUERY_DSA,
  161. LWTRANCHE_PER_SESSION_DSA,
  162. LWTRANCHE_PER_SESSION_RECORD_TYPE,
  163. LWTRANCHE_PER_SESSION_RECORD_TYPMOD,
  164. LWTRANCHE_SHARED_TUPLESTORE,
  165. LWTRANCHE_SHARED_TIDBITMAP,
  166. LWTRANCHE_PARALLEL_APPEND,
  167. LWTRANCHE_PER_XACT_PREDICATE_LIST,
  168. LWTRANCHE_PGSTATS_DSA,
  169. LWTRANCHE_PGSTATS_HASH,
  170. LWTRANCHE_PGSTATS_DATA,
  171. LWTRANCHE_FIRST_USER_DEFINED
  172. } BuiltinTrancheIds;
  173. /*
  174. * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
  175. * to LWLocks. New code should instead use LWLock *. However, for the
  176. * convenience of third-party code, we include the following typedef.
  177. */
  178. typedef LWLock *LWLockId;
  179. #endif /* LWLOCK_H */