lock.h
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· C头文件 代码 · 共 459 行 · 第 1/2 页
H
459 行
(locktag).locktag_lockmethodid = USER_LOCKMETHOD)/* * Per-locked-object lock information: * * tag -- uniquely identifies the object being locked * grantMask -- bitmask for all lock types currently granted on this object. * waitMask -- bitmask for all lock types currently awaited on this object. * procLocks -- list of PROCLOCK objects for this lock. * waitProcs -- queue of processes waiting for this lock. * requested -- count of each lock type currently requested on the lock * (includes requests already granted!!). * nRequested -- total requested locks of all types. * granted -- count of each lock type currently granted on the lock. * nGranted -- total granted locks of all types. * * Note: these counts count 1 for each backend. Internally to a backend, * there may be multiple grabs on a particular lock, but this is not reflected * into shared memory. */typedef struct LOCK{ /* hash key */ LOCKTAG tag; /* unique identifier of lockable object */ /* data */ LOCKMASK grantMask; /* bitmask for lock types already granted */ LOCKMASK waitMask; /* bitmask for lock types awaited */ SHM_QUEUE procLocks; /* list of PROCLOCK objects assoc. with lock */ PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */ int requested[MAX_LOCKMODES]; /* counts of requested locks */ int nRequested; /* total of requested[] array */ int granted[MAX_LOCKMODES]; /* counts of granted locks */ int nGranted; /* total of granted[] array */} LOCK;#define LOCK_LOCKMETHOD(lock) ((LOCKMETHODID) (lock).tag.locktag_lockmethodid)/* * We may have several different backends holding or awaiting locks * on the same lockable object. We need to store some per-holder/waiter * information for each such holder (or would-be holder). This is kept in * a PROCLOCK struct. * * PROCLOCKTAG is the key information needed to look up a PROCLOCK item in the * proclock hashtable. A PROCLOCKTAG value uniquely identifies the combination * of a lockable object and a holder/waiter for that object. (We can use * pointers here because the PROCLOCKTAG need only be unique for the lifespan * of the PROCLOCK, and it will never outlive the lock or the proc.) * * Internally to a backend, it is possible for the same lock to be held * for different purposes: the backend tracks transaction locks separately * from session locks. However, this is not reflected in the shared-memory * state: we only track which backend(s) hold the lock. This is OK since a * backend can never block itself. * * The holdMask field shows the already-granted locks represented by this * proclock. Note that there will be a proclock object, possibly with * zero holdMask, for any lock that the process is currently waiting on. * Otherwise, proclock objects whose holdMasks are zero are recycled * as soon as convenient. * * releaseMask is workspace for LockReleaseAll(): it shows the locks due * to be released during the current call. This must only be examined or * set by the backend owning the PROCLOCK. * * Each PROCLOCK object is linked into lists for both the associated LOCK * object and the owning PGPROC object. Note that the PROCLOCK is entered * into these lists as soon as it is created, even if no lock has yet been * granted. A PGPROC that is waiting for a lock to be granted will also be * linked into the lock's waitProcs queue. */typedef struct PROCLOCKTAG{ /* NB: we assume this struct contains no padding! */ LOCK *myLock; /* link to per-lockable-object information */ PGPROC *myProc; /* link to PGPROC of owning backend */} PROCLOCKTAG;typedef struct PROCLOCK{ /* tag */ PROCLOCKTAG tag; /* unique identifier of proclock object */ /* data */ LOCKMASK holdMask; /* bitmask for lock types currently held */ LOCKMASK releaseMask; /* bitmask for lock types to be released */ SHM_QUEUE lockLink; /* list link in LOCK's list of proclocks */ SHM_QUEUE procLink; /* list link in PGPROC's list of proclocks */} PROCLOCK;#define PROCLOCK_LOCKMETHOD(proclock) \ LOCK_LOCKMETHOD(*((proclock).tag.myLock))/* * Each backend also maintains a local hash table with information about each * lock it is currently interested in. In particular the local table counts * the number of times that lock has been acquired. This allows multiple * requests for the same lock to be executed without additional accesses to * shared memory. We also track the number of lock acquisitions per * ResourceOwner, so that we can release just those locks belonging to a * particular ResourceOwner. */typedef struct LOCALLOCKTAG{ LOCKTAG lock; /* identifies the lockable object */ LOCKMODE mode; /* lock mode for this table entry */} LOCALLOCKTAG;typedef struct LOCALLOCKOWNER{ /* * Note: if owner is NULL then the lock is held on behalf of the session; * otherwise it is held on behalf of my current transaction. * * Must use a forward struct reference to avoid circularity. */ struct ResourceOwnerData *owner; int nLocks; /* # of times held by this owner */} LOCALLOCKOWNER;typedef struct LOCALLOCK{ /* tag */ LOCALLOCKTAG tag; /* unique identifier of locallock entry */ /* data */ LOCK *lock; /* associated LOCK object in shared mem */ PROCLOCK *proclock; /* associated PROCLOCK object in shmem */ uint32 hashcode; /* copy of LOCKTAG's hash value */ int nLocks; /* total number of times lock is held */ int numLockOwners; /* # of relevant ResourceOwners */ int maxLockOwners; /* allocated size of array */ LOCALLOCKOWNER *lockOwners; /* dynamically resizable array */} LOCALLOCK;#define LOCALLOCK_LOCKMETHOD(llock) ((llock).tag.lock.locktag_lockmethodid)/* * This struct holds information passed from lmgr internals to the lock * listing user-level functions (in lockfuncs.c). For each PROCLOCK in * the system, copies of the PROCLOCK object and associated PGPROC and * LOCK objects are stored. Note there will often be multiple copies * of the same PGPROC or LOCK --- to detect whether two are the same, * compare the PROCLOCK tag fields. */typedef struct LockData{ int nelements; /* The length of each of the arrays */ PROCLOCK *proclocks; PGPROC *procs; LOCK *locks;} LockData;/* Result codes for LockAcquire() */typedef enum{ LOCKACQUIRE_NOT_AVAIL, /* lock not available, and dontWait=true */ LOCKACQUIRE_OK, /* lock successfully acquired */ LOCKACQUIRE_ALREADY_HELD /* incremented count for lock already held */} LockAcquireResult;/* * The lockmgr's shared hash tables are partitioned to reduce contention. * To determine which partition a given locktag belongs to, compute the tag's * hash code with LockTagHashCode(), then apply one of these macros. * NB: NUM_LOCK_PARTITIONS must be a power of 2! */#define LockHashPartition(hashcode) \ ((hashcode) % NUM_LOCK_PARTITIONS)#define LockHashPartitionLock(hashcode) \ ((LWLockId) (FirstLockMgrLock + LockHashPartition(hashcode)))/* * function prototypes */extern void InitLocks(void);extern LockMethod GetLocksMethodTable(const LOCK *lock);extern uint32 LockTagHashCode(const LOCKTAG *locktag);extern LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait);extern bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock);extern void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks);extern void LockReleaseCurrentOwner(void);extern void LockReassignCurrentOwner(void);extern List *GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode);extern void AtPrepare_Locks(void);extern void PostPrepare_Locks(TransactionId xid);extern int LockCheckConflicts(LockMethod lockMethodTable, LOCKMODE lockmode, LOCK *lock, PROCLOCK *proclock, PGPROC *proc);extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);extern void GrantAwaitedLock(void);extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode);extern Size LockShmemSize(void);extern LockData *GetLockStatusData(void);extern const char *GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode);extern void lock_twophase_recover(TransactionId xid, uint16 info, void *recdata, uint32 len);extern void lock_twophase_postcommit(TransactionId xid, uint16 info, void *recdata, uint32 len);extern void lock_twophase_postabort(TransactionId xid, uint16 info, void *recdata, uint32 len);extern bool DeadLockCheck(PGPROC *proc);extern void DeadLockReport(void);extern void RememberSimpleDeadLock(PGPROC *proc1, LOCKMODE lockmode, LOCK *lock, PGPROC *proc2);extern void InitDeadLockChecking(void);#ifdef LOCK_DEBUGextern void DumpLocks(PGPROC *proc);extern void DumpAllLocks(void);#endif#endif /* LOCK_H */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?