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/* 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);
129extern void LWLockReleaseAll(void);
130extern bool LWLockHeldByMe(LWLock *lock);
131extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
132extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
133
136
137extern Size LWLockShmemSize(void);
138extern void CreateLWLocks(void);
139extern void InitLWLockAccess(void);
140
141extern const char *GetLWLockIdentifier(uint32 classId, uint16 eventId);
142
143/*
144 * Extensions (or core code) can obtain an LWLocks by calling
145 * RequestNamedLWLockTranche() during postmaster startup. Subsequently,
146 * call GetNamedLWLockTranche() to obtain a pointer to an array containing
147 * the number of LWLocks requested.
148 */
149extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);
151
152/*
153 * There is another, more flexible method of obtaining lwlocks. First, call
154 * LWLockNewTrancheId to obtain a tranche ID; this allocates from a shared
155 * counter. Second, LWLockInitialize should be called just once per lwlock,
156 * passing the tranche ID as an argument.
157 */
158extern int LWLockNewTrancheId(const char *name);
159extern void LWLockInitialize(LWLock *lock, int tranche_id);
160
161/*
162 * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,
163 * we reserve additional tranche IDs for builtin tranches not included in
164 * the set of individual LWLocks. A call to LWLockNewTrancheId will never
165 * return a value less than LWTRANCHE_FIRST_USER_DEFINED. The actual list of
166 * built-in tranches is kept in lwlocklist.h.
167 */
169{
170 /*
171 * LWTRANCHE_INVALID is an unused value that only exists to initialize the
172 * rest of the tranches to appropriate values.
173 */
175
176#define PG_LWLOCK(id, name)
177#define PG_LWLOCKTRANCHE(id, name) LWTRANCHE_##id,
178#include "storage/lwlocklist.h"
179#undef PG_LWLOCK
180#undef PG_LWLOCKTRANCHE
181
184
185/*
186 * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
187 * to LWLocks. New code should instead use LWLock *. However, for the
188 * convenience of third-party code, we include the following typedef.
189 */
191
192#endif /* LWLOCK_H */
#define PGDLLIMPORT
Definition c.h:1328
uint64_t uint64
Definition c.h:547
uint16_t uint16
Definition c.h:545
uint32_t uint32
Definition c.h:546
#define StaticAssertDecl(condition, errmessage)
Definition c.h:942
size_t Size
Definition c.h:619
#define newval
long val
Definition informix.c:689
void LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition lwlock.c:1728
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1911
void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
Definition lwlock.c:1866
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
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
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:1955
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:1592
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition lwlock.c:649
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
BuiltinTrancheIds
Definition lwlock.h:169
@ LWTRANCHE_FIRST_USER_DEFINED
Definition lwlock.h:182
@ LWTRANCHE_INVALID
Definition lwlock.h:174
PGDLLIMPORT int * LWLockCounter
Definition lwlock.c:199
void LWLockReleaseAll(void)
Definition lwlock.c:1892
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:698
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1347
bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1404
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
LWLock * LWLockId
Definition lwlock.h:190
PGDLLIMPORT NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition lwlock.c:193
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition lwlock.c:773
Size LWLockShmemSize(void)
Definition lwlock.c:397
bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride)
Definition lwlock.c:1929
void InitLWLockAccess(void)
Definition lwlock.c:550
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
char tranche_name[NAMEDATALEN]
Definition lwlock.c:183
Definition proc.h:179
LWLock lock
Definition lwlock.h:70
char pad[LWLOCK_PADDED_SIZE]
Definition lwlock.h:71
const char * name