PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
lwlock.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * lwlock.h
4  * Lightweight lock manager
5  *
6  *
7  * Portions Copyright (c) 1996-2015, 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 
17 #include "lib/ilist.h"
18 #include "storage/s_lock.h"
19 #include "port/atomics.h"
20 
21 struct PGPROC;
22 
23 /*
24  * It's occasionally necessary to identify a particular LWLock "by name"; e.g.
25  * because we wish to report the lock to dtrace. We could store a name or
26  * other identifying information in the lock itself, but since it's common
27  * to have many nearly-identical locks (e.g. one per buffer) this would end
28  * up wasting significant amounts of memory. Instead, each lwlock stores a
29  * tranche ID which tells us which array it's part of. Based on that, we can
30  * figure out where the lwlock lies within the array using the data structure
31  * shown below; the lock is then identified based on the tranche name and
32  * computed array index. We need the array stride because the array might not
33  * be an array of lwlocks, but rather some larger data structure that includes
34  * one or more lwlocks per element.
35  */
36 typedef struct LWLockTranche
37 {
38  const char *name;
39  void *array_base;
42 
43 /*
44  * Code outside of lwlock.c should not manipulate the contents of this
45  * structure directly, but we have to declare it here to allow LWLocks to be
46  * incorporated into other data structures.
47  */
48 typedef struct LWLock
49 {
50  slock_t mutex; /* Protects LWLock and queue of PGPROCs */
51  uint16 tranche; /* tranche ID */
52 
53  pg_atomic_uint32 state; /* state of exlusive/nonexclusive lockers */
54 #ifdef LOCK_DEBUG
55  pg_atomic_uint32 nwaiters; /* number of waiters */
56 #endif
57  dlist_head waiters; /* list of waiting PGPROCs */
58 #ifdef LOCK_DEBUG
59  struct PGPROC *owner; /* last exlusive owner of the lock */
60 #endif
61 } LWLock;
62 
63 /*
64  * Prior to PostgreSQL 9.4, every lightweight lock in the system was stored
65  * in a single array. For convenience and for compatibility with past
66  * releases, we still have a main array, but it's now also permissible to
67  * store LWLocks elsewhere in the main shared memory segment or in a dynamic
68  * shared memory segment. In the main array, we force the array stride to
69  * be a power of 2, which saves a few cycles in indexing, but more importantly
70  * also ensures that individual LWLocks don't cross cache line boundaries.
71  * This reduces cache contention problems, especially on AMD Opterons.
72  * (Of course, we have to also ensure that the array start address is suitably
73  * aligned.)
74  *
75  * On a 32-bit platforms a LWLock will these days fit into 16 bytes, but since
76  * that didn't use to be the case and cramming more lwlocks into a cacheline
77  * might be detrimental performancewise we still use 32 byte alignment
78  * there. So, both on 32 and 64 bit platforms, it should fit into 32 bytes
79  * unless slock_t is really big. We allow for that just in case.
80  */
81 #define LWLOCK_PADDED_SIZE (sizeof(LWLock) <= 32 ? 32 : 64)
82 
83 typedef union LWLockPadded
84 {
87 } LWLockPadded;
89 
90 /*
91  * Some commonly-used locks have predefined positions within MainLWLockArray;
92  * defining macros here makes it much easier to keep track of these. If you
93  * add a lock, add it to the end to avoid renumbering the existing locks;
94  * if you remove a lock, consider leaving a gap in the numbering sequence for
95  * the benefit of DTrace and other external debugging scripts.
96  */
97 /* 0 is available; was formerly BufFreelistLock */
98 #define ShmemIndexLock (&MainLWLockArray[1].lock)
99 #define OidGenLock (&MainLWLockArray[2].lock)
100 #define XidGenLock (&MainLWLockArray[3].lock)
101 #define ProcArrayLock (&MainLWLockArray[4].lock)
102 #define SInvalReadLock (&MainLWLockArray[5].lock)
103 #define SInvalWriteLock (&MainLWLockArray[6].lock)
104 #define WALBufMappingLock (&MainLWLockArray[7].lock)
105 #define WALWriteLock (&MainLWLockArray[8].lock)
106 #define ControlFileLock (&MainLWLockArray[9].lock)
107 #define CheckpointLock (&MainLWLockArray[10].lock)
108 #define CLogControlLock (&MainLWLockArray[11].lock)
109 #define SubtransControlLock (&MainLWLockArray[12].lock)
110 #define MultiXactGenLock (&MainLWLockArray[13].lock)
111 #define MultiXactOffsetControlLock (&MainLWLockArray[14].lock)
112 #define MultiXactMemberControlLock (&MainLWLockArray[15].lock)
113 #define RelCacheInitLock (&MainLWLockArray[16].lock)
114 #define CheckpointerCommLock (&MainLWLockArray[17].lock)
115 #define TwoPhaseStateLock (&MainLWLockArray[18].lock)
116 #define TablespaceCreateLock (&MainLWLockArray[19].lock)
117 #define BtreeVacuumLock (&MainLWLockArray[20].lock)
118 #define AddinShmemInitLock (&MainLWLockArray[21].lock)
119 #define AutovacuumLock (&MainLWLockArray[22].lock)
120 #define AutovacuumScheduleLock (&MainLWLockArray[23].lock)
121 #define SyncScanLock (&MainLWLockArray[24].lock)
122 #define RelationMappingLock (&MainLWLockArray[25].lock)
123 #define AsyncCtlLock (&MainLWLockArray[26].lock)
124 #define AsyncQueueLock (&MainLWLockArray[27].lock)
125 #define SerializableXactHashLock (&MainLWLockArray[28].lock)
126 #define SerializableFinishedListLock (&MainLWLockArray[29].lock)
127 #define SerializablePredicateLockListLock (&MainLWLockArray[30].lock)
128 #define OldSerXidLock (&MainLWLockArray[31].lock)
129 #define SyncRepLock (&MainLWLockArray[32].lock)
130 #define BackgroundWorkerLock (&MainLWLockArray[33].lock)
131 #define DynamicSharedMemoryControlLock (&MainLWLockArray[34].lock)
132 #define AutoFileLock (&MainLWLockArray[35].lock)
133 #define ReplicationSlotAllocationLock (&MainLWLockArray[36].lock)
134 #define ReplicationSlotControlLock (&MainLWLockArray[37].lock)
135 #define CommitTsControlLock (&MainLWLockArray[38].lock)
136 #define CommitTsLock (&MainLWLockArray[39].lock)
137 #define ReplicationOriginLock (&MainLWLockArray[40].lock)
138 
139 #define NUM_INDIVIDUAL_LWLOCKS 41
140 
141 /*
142  * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
143  * here, but we need them to figure out offsets within MainLWLockArray, and
144  * having this file include lock.h or bufmgr.h would be backwards.
145  */
146 
147 /* Number of partitions of the shared buffer mapping hashtable */
148 #define NUM_BUFFER_PARTITIONS 128
149 
150 /* Number of partitions the shared lock tables are divided into */
151 #define LOG2_NUM_LOCK_PARTITIONS 4
152 #define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)
153 
154 /* Number of partitions the shared predicate lock tables are divided into */
155 #define LOG2_NUM_PREDICATELOCK_PARTITIONS 4
156 #define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
157 
158 /* Offsets for various chunks of preallocated lwlocks. */
159 #define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS
160 #define LOCK_MANAGER_LWLOCK_OFFSET \
161  (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
162 #define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \
163  (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
164 #define NUM_FIXED_LWLOCKS \
165  (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
166 
167 typedef enum LWLockMode
168 {
171  LW_WAIT_UNTIL_FREE /* A special mode used in PGPROC->lwlockMode,
172  * when waiting for lock to become free. Not
173  * to be used as LWLockAcquire argument */
174 } LWLockMode;
175 
176 
177 #ifdef LOCK_DEBUG
178 extern bool Trace_lwlocks;
179 #endif
180 
181 extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);
182 extern bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode);
183 extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
184 extern void LWLockRelease(LWLock *lock);
185 extern void LWLockReleaseAll(void);
186 extern bool LWLockHeldByMe(LWLock *lock);
187 
188 extern bool LWLockAcquireWithVar(LWLock *lock, uint64 *valptr, uint64 val);
189 extern bool LWLockWaitForVar(LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval);
190 extern void LWLockUpdateVar(LWLock *lock, uint64 *valptr, uint64 value);
191 
192 extern Size LWLockShmemSize(void);
193 extern void CreateLWLocks(void);
194 extern void InitLWLockAccess(void);
195 
196 /*
197  * The traditional method for obtaining an lwlock for use by an extension is
198  * to call RequestAddinLWLocks() during postmaster startup; this will reserve
199  * space for the indicated number of locks in MainLWLockArray. Subsequently,
200  * a lock can be allocated using LWLockAssign.
201  */
202 extern void RequestAddinLWLocks(int n);
203 extern LWLock *LWLockAssign(void);
204 
205 /*
206  * There is another, more flexible method of obtaining lwlocks. First, call
207  * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from
208  * a shared counter. Next, each individual process using the tranche should
209  * call LWLockRegisterTranche() to associate that tranche ID with appropriate
210  * metadata. Finally, LWLockInitialize should be called just once per lwlock,
211  * passing the tranche ID as an argument.
212  *
213  * It may seem strange that each process using the tranche must register it
214  * separately, but dynamic shared memory segments aren't guaranteed to be
215  * mapped at the same address in all coordinating backends, so storing the
216  * registration in the main shared memory segment wouldn't work for that case.
217  */
218 extern int LWLockNewTrancheId(void);
219 extern void LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche);
220 extern void LWLockInitialize(LWLock *lock, int tranche_id);
221 
222 /*
223  * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
224  * to LWLocks. New code should instead use LWLock *. However, for the
225  * convenience of third-party code, we include the following typedef.
226  */
227 typedef LWLock *LWLockId;
228 
229 #endif /* LWLOCK_H */
int slock_t
Definition: s_lock.h:898
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1620
Definition: lwlock.h:48
LWLock * LWLockId
Definition: lwlock.h:227
void LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche)
Definition: lwlock.c:539
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1150
Size LWLockShmemSize(void)
Definition: lwlock.c:402
LWLockMode
Definition: lwlock.h:167
void LWLockUpdateVar(LWLock *lock, uint64 *valptr, uint64 value)
Definition: lwlock.c:1457
struct LWLockTranche LWLockTranche
bool LWLockAcquireWithVar(LWLock *lock, uint64 *valptr, uint64 val)
Definition: lwlock.c:914
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:563
void InitLWLockAccess(void)
Definition: lwlock.c:482
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1095
pg_atomic_uint32 state
Definition: lwlock.h:53
void RequestAddinLWLocks(int n)
Definition: lwlock.c:390
int LWLockNewTrancheId(void)
Definition: lwlock.c:519
void LWLockReleaseAll(void)
Definition: lwlock.c:1602
#define PGDLLIMPORT
Definition: c.h:1052
const char * name
Definition: lwlock.h:38
Size array_stride
Definition: lwlock.h:40
unsigned short uint16
Definition: c.h:240
static struct @72 value
void CreateLWLocks(void)
Definition: lwlock.c:422
uint16 tranche
Definition: lwlock.h:51
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:903
LWLock lock
Definition: lwlock.h:85
void * array_base
Definition: lwlock.h:39
size_t Size
Definition: c.h:333
dlist_head waiters
Definition: lwlock.h:57
#define newval
PGDLLIMPORT LWLockPadded * MainLWLockArray
Definition: lwlock.c:129
bool LWLockWaitForVar(LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval)
Definition: lwlock.c:1281
#define LWLOCK_PADDED_SIZE
Definition: lwlock.h:81
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1521
Definition: proc.h:77
char pad[LWLOCK_PADDED_SIZE]
Definition: lwlock.h:86
long val
Definition: informix.c:689
slock_t mutex
Definition: lwlock.h:50
struct LWLock LWLock
LWLock * LWLockAssign(void)
Definition: lwlock.c:498
union LWLockPadded LWLockPadded