codekingpro/portable-devtools
114k
1/*-------------------------------------------------------------------------2 *3 * lwlock.h4 * Lightweight lock manager5 *6 *7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group8 * Portions Copyright (c) 1994, Regents of the University of California9 *10 * src/include/storage/lwlock.h11 *12 *-------------------------------------------------------------------------13 */14#ifndef LWLOCK_H15#define LWLOCK_H16 17#ifdef FRONTEND18#error "lwlock.h may not be included from frontend code"19#endif20 21#include "port/atomics.h"22#include "storage/proclist_types.h"23 24struct PGPROC;25 26/* what state of the wait process is a backend in */27typedef enum LWLockWaitState28{29 LW_WS_NOT_WAITING, /* not currently waiting / woken up */30 LW_WS_WAITING, /* currently waiting */31 LW_WS_PENDING_WAKEUP, /* removed from waitlist, but not yet32 * signalled */33} LWLockWaitState;34 35/*36 * Code outside of lwlock.c should not manipulate the contents of this37 * structure directly, but we have to declare it here to allow LWLocks to be38 * incorporated into other data structures.39 */40typedef struct LWLock41{42 uint16 tranche; /* tranche ID */43 pg_atomic_uint32 state; /* state of exclusive/nonexclusive lockers */44 proclist_head waiters; /* list of waiting PGPROCs */45#ifdef LOCK_DEBUG46 pg_atomic_uint32 nwaiters; /* number of waiters */47 struct PGPROC *owner; /* last exclusive owner of the lock */48#endif49} LWLock;50 51/*52 * In most cases, it's desirable to force each tranche of LWLocks to be aligned53 * on a cache line boundary and make the array stride a power of 2. This saves54 * a few cycles in indexing, but more importantly ensures that individual55 * LWLocks don't cross cache line boundaries. This reduces cache contention56 * problems, especially on AMD Opterons. In some cases, it's useful to add57 * even more padding so that each LWLock takes up an entire cache line; this is58 * useful, for example, in the main LWLock array, where the overall number of59 * locks is small but some are heavily contended.60 */61#define LWLOCK_PADDED_SIZE PG_CACHE_LINE_SIZE62 63StaticAssertDecl(sizeof(LWLock) <= LWLOCK_PADDED_SIZE,64 "Miscalculated LWLock padding");65 66/* LWLock, padded to a full cache line size */67typedef union LWLockPadded68{69 LWLock lock;70 char pad[LWLOCK_PADDED_SIZE];71} LWLockPadded;72 73extern PGDLLIMPORT LWLockPadded *MainLWLockArray;74 75/* struct for storing named tranche information */76typedef struct NamedLWLockTranche77{78 int trancheId;79 char *trancheName;80} NamedLWLockTranche;81 82extern PGDLLIMPORT NamedLWLockTranche *NamedLWLockTrancheArray;83extern PGDLLIMPORT int NamedLWLockTrancheRequests;84 85/* Names for fixed lwlocks */86#include "storage/lwlocknames.h"87 88/*89 * It's a bit odd to declare NUM_BUFFER_PARTITIONS and NUM_LOCK_PARTITIONS90 * here, but we need them to figure out offsets within MainLWLockArray, and91 * having this file include lock.h or bufmgr.h would be backwards.92 */93 94/* Number of partitions of the shared buffer mapping hashtable */95#define NUM_BUFFER_PARTITIONS 12896 97/* Number of partitions the shared lock tables are divided into */98#define LOG2_NUM_LOCK_PARTITIONS 499#define NUM_LOCK_PARTITIONS (1 << LOG2_NUM_LOCK_PARTITIONS)100 101/* Number of partitions the shared predicate lock tables are divided into */102#define LOG2_NUM_PREDICATELOCK_PARTITIONS 4103#define NUM_PREDICATELOCK_PARTITIONS (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)104 105/* Offsets for various chunks of preallocated lwlocks. */106#define BUFFER_MAPPING_LWLOCK_OFFSET NUM_INDIVIDUAL_LWLOCKS107#define LOCK_MANAGER_LWLOCK_OFFSET \108 (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)109#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET \110 (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)111#define NUM_FIXED_LWLOCKS \112 (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)113 114typedef enum LWLockMode115{116 LW_EXCLUSIVE,117 LW_SHARED,118 LW_WAIT_UNTIL_FREE /* A special mode used in PGPROC->lwWaitMode,119 * when waiting for lock to become free. Not120 * to be used as LWLockAcquire argument */121} LWLockMode;122 123 124#ifdef LOCK_DEBUG125extern PGDLLIMPORT bool Trace_lwlocks;126#endif127 128extern bool LWLockAcquire(LWLock *lock, LWLockMode mode);129extern bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode);130extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);131extern void LWLockRelease(LWLock *lock);132extern void LWLockReleaseClearVar(LWLock *lock, uint64 *valptr, uint64 val);133extern void LWLockReleaseAll(void);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, uint64 *valptr, uint64 oldval, uint64 *newval);139extern void LWLockUpdateVar(LWLock *lock, 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 calling149 * RequestNamedLWLockTranche() during postmaster startup. Subsequently,150 * call GetNamedLWLockTranche() to obtain a pointer to an array containing151 * the number of LWLocks requested.152 */153extern void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks);154extern LWLockPadded *GetNamedLWLockTranche(const char *tranche_name);155 156/*157 * There is another, more flexible method of obtaining lwlocks. First, call158 * LWLockNewTrancheId just once to obtain a tranche ID; this allocates from159 * a shared counter. Next, each individual process using the tranche should160 * call LWLockRegisterTranche() to associate that tranche ID with a name.161 * Finally, LWLockInitialize should be called just once per lwlock, passing162 * the tranche ID as an argument.163 *164 * It may seem strange that each process using the tranche must register it165 * separately, but dynamic shared memory segments aren't guaranteed to be166 * mapped at the same address in all coordinating backends, so storing the167 * registration in the main shared memory segment wouldn't work for that case.168 */169extern int LWLockNewTrancheId(void);170extern void LWLockRegisterTranche(int tranche_id, const char *tranche_name);171extern void LWLockInitialize(LWLock *lock, int tranche_id);172 173/*174 * Every tranche ID less than NUM_INDIVIDUAL_LWLOCKS is reserved; also,175 * we reserve additional tranche IDs for builtin tranches not included in176 * the set of individual LWLocks. A call to LWLockNewTrancheId will never177 * return a value less than LWTRANCHE_FIRST_USER_DEFINED.178 */179typedef enum BuiltinTrancheIds180{181 LWTRANCHE_XACT_BUFFER = NUM_INDIVIDUAL_LWLOCKS,182 LWTRANCHE_COMMITTS_BUFFER,183 LWTRANCHE_SUBTRANS_BUFFER,184 LWTRANCHE_MULTIXACTOFFSET_BUFFER,185 LWTRANCHE_MULTIXACTMEMBER_BUFFER,186 LWTRANCHE_NOTIFY_BUFFER,187 LWTRANCHE_SERIAL_BUFFER,188 LWTRANCHE_WAL_INSERT,189 LWTRANCHE_BUFFER_CONTENT,190 LWTRANCHE_REPLICATION_ORIGIN_STATE,191 LWTRANCHE_REPLICATION_SLOT_IO,192 LWTRANCHE_LOCK_FASTPATH,193 LWTRANCHE_BUFFER_MAPPING,194 LWTRANCHE_LOCK_MANAGER,195 LWTRANCHE_PREDICATE_LOCK_MANAGER,196 LWTRANCHE_PARALLEL_HASH_JOIN,197 LWTRANCHE_PARALLEL_QUERY_DSA,198 LWTRANCHE_PER_SESSION_DSA,199 LWTRANCHE_PER_SESSION_RECORD_TYPE,200 LWTRANCHE_PER_SESSION_RECORD_TYPMOD,201 LWTRANCHE_SHARED_TUPLESTORE,202 LWTRANCHE_SHARED_TIDBITMAP,203 LWTRANCHE_PARALLEL_APPEND,204 LWTRANCHE_PER_XACT_PREDICATE_LIST,205 LWTRANCHE_PGSTATS_DSA,206 LWTRANCHE_PGSTATS_HASH,207 LWTRANCHE_PGSTATS_DATA,208 LWTRANCHE_LAUNCHER_DSA,209 LWTRANCHE_LAUNCHER_HASH,210 LWTRANCHE_FIRST_USER_DEFINED211} BuiltinTrancheIds;212 213/*214 * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer215 * to LWLocks. New code should instead use LWLock *. However, for the216 * convenience of third-party code, we include the following typedef.217 */218typedef LWLock *LWLockId;219 220#endif /* LWLOCK_H */221 