PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 */
73
75
76/*
77 * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS
78 * here, but we need them to figure out offsets within MainLWLockArray, and
79 * having this file include lock.h or bufmgr.h would be backwards.
80 */
81
82/* Number of partitions of the shared buffer mapping hashtable */
83#define NUM_BUFFER_PARTITIONS 128
84
85/* Number of partitions the shared lock tables are divided into */
86#define LOG2_NUM_LOCK_PARTITIONS 4
87#define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)
88
89/* Number of partitions the shared predicate lock tables are divided into */
90#define LOG2_NUM_PREDICATELOCK_PARTITIONS 4
91#define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
92
93/* Offsets for various chunks of preallocated lwlocks. */
94#define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS
95#define LOCK_MANAGER_LWLOCK_OFFSET \
96 (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
97#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \
98 (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
99#define NUM_FIXED_LWLOCKS \
100 (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
101
102typedef enum LWLockMode
103{
106 LW_WAIT_UNTIL_FREE, /* A special mode used in PGPROC->lwWaitMode,
107 * when waiting for lock to become free. Not
108 * to be used as LWLockAcquire argument */
110
111
112#ifdef LOCK_DEBUG
113extern PGDLLIMPORT bool Trace_lwlocks;
114#endif
115
116extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);
118extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
119extern void LWLockRelease(LWLock *lock);
121extern void LWLockReleaseAll(void);
122extern bool LWLockHeldByMe(LWLock *lock);
123extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
124extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
125
128
129extern void InitLWLockAccess(void);
130
131extern const char *GetLWLockIdentifier(uint32 classId, uint16 eventId);
132
133/*
134 * Extensions (or core code) can obtain an LWLocks by calling
135 * RequestNamedLWLockTranche() during postmaster startup. Subsequently,
136 * call GetNamedLWLockTranche() to obtain a pointer to an array containing
137 * the number of LWLocks requested.
138 */
139extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
140extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);
141
142/*
143 * There is another, more flexible method of obtaining lwlocks. First, call
144 * LWLockNewTrancheId to obtain a tranche ID; this allocates from a shared
145 * counter. Second, LWLockInitialize should be called just once per lwlock,
146 * passing the tranche ID as an argument.
147 */
148extern int LWLockNewTrancheId(const char *name);
149extern void LWLockInitialize(LWLock *lock, int tranche_id);
150
151/*
152 * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,
153 * we reserve additional tranche IDs for builtin tranches not included in
154 * the set of individual LWLocks. A call to LWLockNewTrancheId will never
155 * return a value less than LWTRANCHE_FIRST_USER_DEFINED. The actual list of
156 * built-in tranches is kept in lwlocklist.h.
157 */
159{
160 /*
161 * LWTRANCHE_INVALID is an unused value that only exists to initialize the
162 * rest of the tranches to appropriate values.
163 */
165
166#define PG_LWLOCK(id, name)
167#define PG_LWLOCKTRANCHE(id, name) LWTRANCHE_##id,
168#include "storage/lwlocklist.h"
169#undef PG_LWLOCK
170#undef PG_LWLOCKTRANCHE
171
174
175/*
176 * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
177 * to LWLocks. New code should instead use LWLock *. However, for the
178 * convenience of third-party code, we include the following typedef.
179 */
181
182#endif /* LWLOCK_H */
#define PGDLLIMPORT
Definition c.h:1421
uint64_t uint64
Definition c.h:625
uint16_t uint16
Definition c.h:623
uint32_t uint32
Definition c.h:624
#define StaticAssertDecl(condition, errmessage)
Definition c.h:1008
#define newval
long val
Definition informix.c:689
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition lwlock.c:1702
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1885
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition lwlock.c:1840
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
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
int LWLockNewTrancheId(const char *name)
Definition lwlock.c:562
#define LWLOCK_PADDED_SIZE
Definition lwlock.h:62
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition lwlock.c:522
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1929
bool LWLockWaitForVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 oldval, uint64 *newval)
Definition lwlock.c:1566
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition lwlock.c:620
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
BuiltinTrancheIds
Definition lwlock.h:159
@ LWTRANCHE_FIRST_USER_DEFINED
Definition lwlock.h:172
@ LWTRANCHE_INVALID
Definition lwlock.h:164
void LWLockReleaseAll(void)
Definition lwlock.c:1866
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:670
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1321
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1378
LWLockMode
Definition lwlock.h:103
@ LW_SHARED
Definition lwlock.h:105
@ LW_WAIT_UNTIL_FREE
Definition lwlock.h:106
@ LW_EXCLUSIVE
Definition lwlock.h:104
PGDLLIMPORT LWLockPadded * MainLWLockArray
Definition lwlock.c:150
LWLock * LWLockId
Definition lwlock.h:180
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition lwlock.c:747
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
Definition lwlock.c:1903
void InitLWLockAccess(void)
Definition lwlock.c:506
static PgChecksumMode mode
static int fb(int x)
pg_atomic_uint32 state
Definition lwlock.h:44
uint16 tranche
Definition lwlock.h:43
proclist_head waiters
Definition lwlock.h:45
Definition proc.h:179
LWLock lock
Definition lwlock.h:70
char pad[LWLOCK_PADDED_SIZE]
Definition lwlock.h:71
const char * name