]> granicus.if.org Git - postgresql/blob - src/include/storage/lwlock.h
Convert the PGPROC->lwWaitLink list into a dlist instead of open coding it.
[postgresql] / src / include / storage / lwlock.h
1 /*-------------------------------------------------------------------------
2  *
3  * lwlock.h
4  *        Lightweight lock manager
5  *
6  *
7  * Portions Copyright (c) 1996-2014, 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
20 struct PGPROC;
21
22 /*
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.
34  */
35 typedef struct LWLockTranche
36 {
37         const char *name;
38         void       *array_base;
39         Size            array_stride;
40 } LWLockTranche;
41
42 /*
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.
46  */
47 typedef struct LWLock
48 {
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 */
55 } LWLock;
56
57 /*
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
67  * aligned.)
68  *
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.
74  */
75 #define LWLOCK_PADDED_SIZE      (sizeof(LWLock) <= 32 ? 32 : 64)
76
77 typedef union LWLockPadded
78 {
79         LWLock          lock;
80         char            pad[LWLOCK_PADDED_SIZE];
81 } LWLockPadded;
82 extern PGDLLIMPORT LWLockPadded *MainLWLockArray;
83
84 /*
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.
90  */
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)
131
132 #define NUM_INDIVIDUAL_LWLOCKS          40
133
134 /*
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.
138  */
139
140 /* Number of partitions of the shared buffer mapping hashtable */
141 #define NUM_BUFFER_PARTITIONS  128
142
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)
146
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)
150
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)
159
160 typedef enum LWLockMode
161 {
162         LW_EXCLUSIVE,
163         LW_SHARED,
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 */
167 } LWLockMode;
168
169
170 #ifdef LOCK_DEBUG
171 extern bool Trace_lwlocks;
172 #endif
173
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);
180
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);
184
185 extern Size LWLockShmemSize(void);
186 extern void CreateLWLocks(void);
187 extern void InitLWLockAccess(void);
188
189 /*
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.
194  */
195 extern void RequestAddinLWLocks(int n);
196 extern LWLock *LWLockAssign(void);
197
198 /*
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.
205  *
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.
210  */
211 extern int      LWLockNewTrancheId(void);
212 extern void LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche);
213 extern void LWLockInitialize(LWLock *lock, int tranche_id);
214
215 /*
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.
219  */
220 typedef LWLock *LWLockId;
221
222 #endif   /* LWLOCK_H */