加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgreSQL 中的 Lock

发布时间:2020-12-13 17:56:35 所属栏目:百科 来源:网络整理
导读: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 proc

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;

LOCALLOCK 是在私有进程中的结构,它有指针指向共享内存中实际的锁

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 */
int64 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;

共享内存中实际的锁

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;

LOCALLOCK 和 LOCK 为了方便查找,全部放在了 hash table 中

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

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;

未获得锁的等待是通过每个进程的临界区实现的,当没获得锁时,就进入本进程的临界区,并把本进程放入LOCK的waitProcs队列中,当别的进程解锁后,检查waitProcs,把里面存储的等候的进程出临界区

锁的种类

#define NoLock 0

#define AccessShareLock 1 /* SELECT */#define RowShareLock 2 /* SELECT FOR UPDATE/FOR SHARE */#define RowExclusiveLock 3 /* INSERT,UPDATE,DELETE */#define ShareUpdateExclusiveLock 4 /* VACUUM (non-FULL),ANALYZE,CREATE * INDEX CONCURRENTLY */#define ShareLock 5 /* CREATE INDEX (WITHOUT CONCURRENTLY) */#define ShareRowExclusiveLock 6 /* like EXCLUSIVE MODE,but allows ROW * SHARE */#define ExclusiveLock 7 /* blocks ROW SHARE/SELECT...FOR * UPDATE */#define AccessExclusiveLock 8 /* ALTER TABLE,DROP TABLE,VACUUM * FULL,and unqualified LOCK TABLE */

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读