1 /*-------------------------------------------------------------------------
4 * Lightweight lock manager
7 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/storage/lwlock.h
12 *-------------------------------------------------------------------------
17 #include "lib/ilist.h"
18 #include "storage/s_lock.h"
23 * It's occasionally necessary to identify a particular LWLock "by name"; e.g.
24 * because we wish to report the lock to dtrace. We could store a name or
25 * other identifying information in the lock itself, but since it's common
26 * to have many nearly-identical locks (e.g. one per buffer) this would end
27 * up wasting significant amounts of memory. Instead, each lwlock stores a
28 * tranche ID which tells us which array it's part of. Based on that, we can
29 * figure out where the lwlock lies within the array using the data structure
30 * shown below; the lock is then identified based on the tranche name and
31 * computed array index. We need the array stride because the array might not
32 * be an array of lwlocks, but rather some larger data structure that includes
33 * one or more lwlocks per element.
35 typedef struct LWLockTranche
43 * Code outside of lwlock.c should not manipulate the contents of this
44 * structure directly, but we have to declare it here to allow LWLocks to be
45 * incorporated into other data structures.
49 slock_t mutex; /* Protects LWLock and queue of PGPROCs */
50 bool releaseOK; /* T if ok to release waiters */
51 char exclusive; /* # of exclusive holders (0 or 1) */
52 int shared; /* # of shared holders (0..MaxBackends) */
53 int tranche; /* tranche ID */
54 dlist_head waiters; /* list of waiting PGPROCs */
58 * Prior to PostgreSQL 9.4, every lightweight lock in the system was stored
59 * in a single array. For convenience and for compatibility with past
60 * releases, we still have a main array, but it's now also permissible to
61 * store LWLocks elsewhere in the main shared memory segment or in a dynamic
62 * shared memory segment. In the main array, we force the array stride to
63 * be a power of 2, which saves a few cycles in indexing, but more importantly
64 * also ensures that individual LWLocks don't cross cache line boundaries.
65 * This reduces cache contention problems, especially on AMD Opterons.
66 * (Of course, we have to also ensure that the array start address is suitably
69 * Even on a 32-bit platform, an lwlock will be more than 16 bytes, because
70 * it contains 2 integers and 2 pointers, plus other stuff. It should fit
71 * into 32 bytes, though, unless slock_t is really big. On a 64-bit platform,
72 * it should fit into 32 bytes unless slock_t is larger than 4 bytes. We
73 * allow for that just in case.
75 #define LWLOCK_PADDED_SIZE (sizeof(LWLock) <= 32 ? 32 : 64)
77 typedef union LWLockPadded
80 char pad[LWLOCK_PADDED_SIZE];
82 extern PGDLLIMPORT LWLockPadded *MainLWLockArray;
85 * Some commonly-used locks have predefined positions within MainLWLockArray;
86 * defining macros here makes it much easier to keep track of these. If you
87 * add a lock, add it to the end to avoid renumbering the existing locks;
88 * if you remove a lock, consider leaving a gap in the numbering sequence for
89 * the benefit of DTrace and other external debugging scripts.
91 /* 0 is available; was formerly BufFreelistLock */
92 #define ShmemIndexLock (&MainLWLockArray[1].lock)
93 #define OidGenLock (&MainLWLockArray[2].lock)
94 #define XidGenLock (&MainLWLockArray[3].lock)
95 #define ProcArrayLock (&MainLWLockArray[4].lock)
96 #define SInvalReadLock (&MainLWLockArray[5].lock)
97 #define SInvalWriteLock (&MainLWLockArray[6].lock)
98 #define WALBufMappingLock (&MainLWLockArray[7].lock)
99 #define WALWriteLock (&MainLWLockArray[8].lock)
100 #define ControlFileLock (&MainLWLockArray[9].lock)
101 #define CheckpointLock (&MainLWLockArray[10].lock)
102 #define CLogControlLock (&MainLWLockArray[11].lock)
103 #define SubtransControlLock (&MainLWLockArray[12].lock)
104 #define MultiXactGenLock (&MainLWLockArray[13].lock)
105 #define MultiXactOffsetControlLock (&MainLWLockArray[14].lock)
106 #define MultiXactMemberControlLock (&MainLWLockArray[15].lock)
107 #define RelCacheInitLock (&MainLWLockArray[16].lock)
108 #define CheckpointerCommLock (&MainLWLockArray[17].lock)
109 #define TwoPhaseStateLock (&MainLWLockArray[18].lock)
110 #define TablespaceCreateLock (&MainLWLockArray[19].lock)
111 #define BtreeVacuumLock (&MainLWLockArray[20].lock)
112 #define AddinShmemInitLock (&MainLWLockArray[21].lock)
113 #define AutovacuumLock (&MainLWLockArray[22].lock)
114 #define AutovacuumScheduleLock (&MainLWLockArray[23].lock)
115 #define SyncScanLock (&MainLWLockArray[24].lock)
116 #define RelationMappingLock (&MainLWLockArray[25].lock)
117 #define AsyncCtlLock (&MainLWLockArray[26].lock)
118 #define AsyncQueueLock (&MainLWLockArray[27].lock)
119 #define SerializableXactHashLock (&MainLWLockArray[28].lock)
120 #define SerializableFinishedListLock (&MainLWLockArray[29].lock)
121 #define SerializablePredicateLockListLock (&MainLWLockArray[30].lock)
122 #define OldSerXidLock (&MainLWLockArray[31].lock)
123 #define SyncRepLock (&MainLWLockArray[32].lock)
124 #define BackgroundWorkerLock (&MainLWLockArray[33].lock)
125 #define DynamicSharedMemoryControlLock (&MainLWLockArray[34].lock)
126 #define AutoFileLock (&MainLWLockArray[35].lock)
127 #define ReplicationSlotAllocationLock (&MainLWLockArray[36].lock)
128 #define ReplicationSlotControlLock (&MainLWLockArray[37].lock)
129 #define CommitTsControlLock (&MainLWLockArray[38].lock)
130 #define CommitTsLock (&MainLWLockArray[39].lock)
132 #define NUM_INDIVIDUAL_LWLOCKS 40
135 * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
136 * here, but we need them to figure out offsets within MainLWLockArray, and
137 * having this file include lock.h or bufmgr.h would be backwards.
140 /* Number of partitions of the shared buffer mapping hashtable */
141 #define NUM_BUFFER_PARTITIONS 128
143 /* Number of partitions the shared lock tables are divided into */
144 #define LOG2_NUM_LOCK_PARTITIONS 4
145 #define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)
147 /* Number of partitions the shared predicate lock tables are divided into */
148 #define LOG2_NUM_PREDICATELOCK_PARTITIONS 4
149 #define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
151 /* Offsets for various chunks of preallocated lwlocks. */
152 #define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS
153 #define LOCK_MANAGER_LWLOCK_OFFSET \
154 (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
155 #define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \
156 (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
157 #define NUM_FIXED_LWLOCKS \
158 (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
160 typedef enum LWLockMode
164 LW_WAIT_UNTIL_FREE /* A special mode used in PGPROC->lwlockMode,
165 * when waiting for lock to become free. Not
166 * to be used as LWLockAcquire argument */
171 extern bool Trace_lwlocks;
174 extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);
175 extern bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode);
176 extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
177 extern void LWLockRelease(LWLock *lock);
178 extern void LWLockReleaseAll(void);
179 extern bool LWLockHeldByMe(LWLock *lock);
181 extern bool LWLockAcquireWithVar(LWLock *lock, uint64 *valptr, uint64 val);
182 extern bool LWLockWaitForVar(LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval);
183 extern void LWLockUpdateVar(LWLock *lock, uint64 *valptr, uint64 value);
185 extern Size LWLockShmemSize(void);
186 extern void CreateLWLocks(void);
187 extern void InitLWLockAccess(void);
190 * The traditional method for obtaining an lwlock for use by an extension is
191 * to call RequestAddinLWLocks() during postmaster startup; this will reserve
192 * space for the indicated number of locks in MainLWLockArray. Subsequently,
193 * a lock can be allocated using LWLockAssign.
195 extern void RequestAddinLWLocks(int n);
196 extern LWLock *LWLockAssign(void);
199 * There is another, more flexible method of obtaining lwlocks. First, call
200 * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from
201 * a shared counter. Next, each individual process using the tranche should
202 * call LWLockRegisterTranche() to associate that tranche ID with appropriate
203 * metadata. Finally, LWLockInitialize should be called just once per lwlock,
204 * passing the tranche ID as an argument.
206 * It may seem strange that each process using the tranche must register it
207 * separately, but dynamic shared memory segments aren't guaranteed to be
208 * mapped at the same address in all coordinating backends, so storing the
209 * registration in the main shared memory segment wouldn't work for that case.
211 extern int LWLockNewTrancheId(void);
212 extern void LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche);
213 extern void LWLockInitialize(LWLock *lock, int tranche_id);
216 * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
217 * to LWLocks. New code should instead use LWLock *. However, for the
218 * convenience of third-party code, we include the following typedef.
220 typedef LWLock *LWLockId;
222 #endif /* LWLOCK_H */