PostgreSQL Source Code git master
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-2025, 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#ifdef FRONTEND
18#error "lwlock.h may not be included from frontend code"
19#endif
20
21#include "port/atomics.h"
22#include "storage/lwlocknames.h"
24
25struct PGPROC;
26
27/* what state of the wait process is a backend in */
28typedef enum LWLockWaitState
29{
30 LW_WS_NOT_WAITING, /* not currently waiting / woken up */
31 LW_WS_WAITING, /* currently waiting */
32 LW_WS_PENDING_WAKEUP, /* removed from waitlist, but not yet
33 * signalled */
35
36/*
37 * Code outside of lwlock.c should not manipulate the contents of this
38 * structure directly, but we have to declare it here to allow LWLocks to be
39 * incorporated into other data structures.
40 */
41typedef struct LWLock
42{
43 uint16 tranche; /* tranche ID */
44 pg_atomic_uint32 state; /* state of exclusive/nonexclusive lockers */
45 proclist_head waiters; /* list of waiting PGPROCs */
46#ifdef LOCK_DEBUG
47 pg_atomic_uint32 nwaiters; /* number of waiters */
48 struct PGPROC *owner; /* last exclusive owner of the lock */
49#endif
51
52/*
53 * In most cases, it's desirable to force each tranche of LWLocks to be aligned
54 * on a cache line boundary and make the array stride a power of 2. This saves
55 * a few cycles in indexing, but more importantly ensures that individual
56 * LWLocks don't cross cache line boundaries. This reduces cache contention
57 * problems, especially on AMD Opterons. In some cases, it's useful to add
58 * even more padding so that each LWLock takes up an entire cache line; this is
59 * useful, for example, in the main LWLock array, where the overall number of
60 * locks is small but some are heavily contended.
61 */
62#define LWLOCK_PADDED_SIZE PG_CACHE_LINE_SIZE
63
65 "Miscalculated LWLock padding");
66
67/* LWLock, padded to a full cache line size */
68typedef union LWLockPadded
69{
73
75
76/* forward declaration of private type for use only by lwlock.c */
78
82extern PGDLLIMPORT int *LWLockCounter;
83
84/*
85 * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
86 * here, but we need them to figure out offsets within MainLWLockArray, and
87 * having this file include lock.h or bufmgr.h would be backwards.
88 */
89
90/* Number of partitions of the shared buffer mapping hashtable */
91#define NUM_BUFFER_PARTITIONS 128
92
93/* Number of partitions the shared lock tables are divided into */
94#define LOG2_NUM_LOCK_PARTITIONS 4
95#define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)
96
97/* Number of partitions the shared predicate lock tables are divided into */
98#define LOG2_NUM_PREDICATELOCK_PARTITIONS 4
99#define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
100
101/* Offsets for various chunks of preallocated lwlocks. */
102#define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS
103#define LOCK_MANAGER_LWLOCK_OFFSET \
104 (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
105#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \
106 (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
107#define NUM_FIXED_LWLOCKS \
108 (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
109
110typedef enum LWLockMode
111{
114 LW_WAIT_UNTIL_FREE, /* A special mode used in PGPROC->lwWaitMode,
115 * when waiting for lock to become free. Not
116 * to be used as LWLockAcquire argument */
118
119
120#ifdef LOCK_DEBUG
121extern PGDLLIMPORT bool Trace_lwlocks;
122#endif
123
124extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);
126extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
127extern void LWLockRelease(LWLock *lock);
128extern void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val);
129extern void LWLockReleaseAll(void);
130extern void LWLockDisown(LWLock *lock);
131extern void LWLockReleaseDisowned(LWLock *lock, LWLockMode mode);
132extern void ForEachLWLockHeldByMe(void (*callback) (LWLock *, LWLockMode, void *),
133 void *context);
134extern bool LWLockHeldByMe(LWLock *lock);
135extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
136extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
137
138extern bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval);
139extern void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val);
140
141extern Size LWLockShmemSize(void);
142extern void CreateLWLocks(void);
143extern void InitLWLockAccess(void);
144
145extern const char *GetLWLockIdentifier(uint32 classId, uint16 eventId);
146
147/*
148 * Extensions (or core code) can obtain an LWLocks by calling
149 * RequestNamedLWLockTranche() during postmaster startup. Subsequently,
150 * call GetNamedLWLockTranche() to obtain a pointer to an array containing
151 * the number of LWLocks requested.
152 */
153extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
155
156/*
157 * There is another, more flexible method of obtaining lwlocks. First, call
158 * LWLockNewTrancheId to obtain a tranche ID; this allocates from a shared
159 * counter. Second, LWLockInitialize should be called just once per lwlock,
160 * passing the tranche ID as an argument.
161 */
162extern int LWLockNewTrancheId(const char *name);
163extern void LWLockInitialize(LWLock *lock, int tranche_id);
164
165/*
166 * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,
167 * we reserve additional tranche IDs for builtin tranches not included in
168 * the set of individual LWLocks. A call to LWLockNewTrancheId will never
169 * return a value less than LWTRANCHE_FIRST_USER_DEFINED. The actual list of
170 * built-in tranches is kept in lwlocklist.h.
171 */
173{
174 /*
175 * LWTRANCHE_INVALID is an unused value that only exists to initialize the
176 * rest of the tranches to appropriate values.
177 */
178 LWTRANCHE_INVALID = NUM_INDIVIDUAL_LWLOCKS - 1,
179
180#define PG_LWLOCK(id, name)
181#define PG_LWLOCKTRANCHE(id, name) LWTRANCHE_##id,
182#include "storage/lwlocklist.h"
183#undef PG_LWLOCK
184#undef PG_LWLOCKTRANCHE
185
188
189/*
190 * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
191 * to LWLocks. New code should instead use LWLock *. However, for the
192 * convenience of third-party code, we include the following typedef.
193 */
195
196#endif /* LWLOCK_H */
#define PGDLLIMPORT
Definition: c.h:1310
uint64_t uint64
Definition: c.h:544
uint16_t uint16
Definition: c.h:542
uint32_t uint32
Definition: c.h:543
size_t Size
Definition: c.h:615
#define newval
long val
Definition: informix.c:689
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition: lwlock.c:1726
union LWLockPadded LWLockPadded
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1977
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition: lwlock.c:1923
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void CreateLWLocks(void)
Definition: lwlock.c:441
LWLockWaitState
Definition: lwlock.h:29
@ LW_WS_NOT_WAITING
Definition: lwlock.h:30
@ LW_WS_WAITING
Definition: lwlock.h:31
@ LW_WS_PENDING_WAKEUP
Definition: lwlock.h:32
void LWLockDisown(LWLock *lock)
Definition: lwlock.c:1883
int LWLockNewTrancheId(const char *name)
Definition: lwlock.c:596
#define LWLOCK_PADDED_SIZE
Definition: lwlock.h:62
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition: lwlock.c:566
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:2021
PGDLLIMPORT char ** LWLockTrancheNames
Definition: lwlock.c:154
PGDLLIMPORT int NamedLWLockTrancheRequests
Definition: lwlock.c:192
bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
Definition: lwlock.c:1590
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition: lwlock.c:649
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
BuiltinTrancheIds
Definition: lwlock.h:173
@ LWTRANCHE_FIRST_USER_DEFINED
Definition: lwlock.h:186
@ LWTRANCHE_INVALID
Definition: lwlock.h:178
PGDLLIMPORT int * LWLockCounter
Definition: lwlock.c:199
void LWLockReleaseAll(void)
Definition: lwlock.c:1945
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:698
void LWLockReleaseDisowned(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1914
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1345
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1402
LWLockMode
Definition: lwlock.h:111
@ LW_SHARED
Definition: lwlock.h:113
@ LW_WAIT_UNTIL_FREE
Definition: lwlock.h:114
@ LW_EXCLUSIVE
Definition: lwlock.h:112
PGDLLIMPORT LWLockPadded * MainLWLockArray
Definition: lwlock.c:161
StaticAssertDecl(sizeof(LWLock)<=LWLOCK_PADDED_SIZE, "Miscalculated LWLock padding")
LWLock * LWLockId
Definition: lwlock.h:194
PGDLLIMPORT NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition: lwlock.c:193
void ForEachLWLockHeldByMe(void(*callback)(LWLock *, LWLockMode, void *), void *context)
Definition: lwlock.c:1962
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition: lwlock.c:773
struct LWLock LWLock
Size LWLockShmemSize(void)
Definition: lwlock.c:397
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
Definition: lwlock.c:1995
void InitLWLockAccess(void)
Definition: lwlock.c:550
static PgChecksumMode mode
Definition: pg_checksums.c:56
Definition: lwlock.h:42
pg_atomic_uint32 state
Definition: lwlock.h:44
uint16 tranche
Definition: lwlock.h:43
proclist_head waiters
Definition: lwlock.h:45
char tranche_name[NAMEDATALEN]
Definition: lwlock.c:183
Definition: proc.h:179
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46
LWLock lock
Definition: lwlock.h:70
char pad[LWLOCK_PADDED_SIZE]
Definition: lwlock.h:71
const char * name