溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL 源碼解讀(221)- Locks(PROCLOCK Struct)

發(fā)布時(shí)間:2020-08-08 15:08:03 來(lái)源:ITPUB博客 閱讀:242 作者:husthxd 欄目:關(guān)系型數(shù)據(jù)庫(kù)

本節(jié)是PostgreSQL Locks中介紹PROCLOCK結(jié)構(gòu)體部分,翻譯自README文件.

一、PROCLOCK Struct


/*
 * 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 */
    PGPROC     *groupLeader;    /* proc's lock group leader, or proc itself */
    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))
---------------------------------------------------------------------------
The lock manager's PROCLOCK objects contain:
tag -
    The key fields that are used for hashing entries in the shared memory
    PROCLOCK hash table.  This is declared as a separate struct to ensure that
    we always zero out the correct number of bytes.  It is critical that any
    alignment-padding bytes the compiler might insert in the struct be zeroed
    out, else the hash computation will be random.  (Currently, we are careful
    to define struct PROCLOCKTAG so that there are no padding bytes.)
    tag.myLock
        Pointer to the shared LOCK object this PROCLOCK is for.
    tag.myProc
        Pointer to the PGPROC of backend process that owns this PROCLOCK.
    Note: it's OK to use pointers here because a PROCLOCK never outlives
    either its lock or its proc.  The tag is therefore unique for as long
    as it needs to be, even though the same tag values might mean something
    else at other times.
tag -
    與LOCK結(jié)構(gòu)體中的tag字段類(lèi)似,用于標(biāo)識(shí)共享內(nèi)存中的PROCLOCK哈希表中對(duì)應(yīng)的條目.
    tag.mylock
        指向PROCLOCK所表示的共享LOCK對(duì)象的指針
    tag.myProc
        指向持有該P(yáng)ROCLOCK的PGPROC后臺(tái)進(jìn)程
holdMask -
    A bitmask for the lock modes successfully acquired by this PROCLOCK.
    This should be a subset of the LOCK object's grantMask, and also a
    subset of the PGPROC object's heldLocks mask (if the PGPROC is
    currently waiting for another lock mode on this lock).
holdMask -
    該P(yáng)ROCLOCK請(qǐng)求獲得的lock模式位掩碼.
    應(yīng)為L(zhǎng)OCK's grantMask掩碼的子集,同時(shí)也是PGPROC's heldlocks掩碼的子集.
releaseMask -
    A bitmask for the lock modes due to be released during LockReleaseAll.
    This must be a subset of the holdMask.  Note that it is modified without
    taking the partition LWLock, and therefore it is unsafe for any
    backend except the one owning the PROCLOCK to examine/change it.
releaseMask -
    在LockReleaseAll執(zhí)行過(guò)程中即將被釋放的lock模式位掩碼.
    應(yīng)為holdMask位掩碼的子集.注意更新該值時(shí)不需要持有分區(qū)LWLock,因此除了自身的PROCLOCK外
    其他后臺(tái)進(jìn)程是不安全的.
lockLink -
    List link for shared memory queue of all the PROCLOCK objects for the
    same LOCK.
lockLink -
    共享內(nèi)存中同一個(gè)LOCK的PROCLOCK對(duì)象隊(duì)列鏈表鏈接.
procLink -
    List link for shared memory queue of all the PROCLOCK objects for the
    same backend.
procLink -
    共享內(nèi)存中同一個(gè)后臺(tái)進(jìn)程所有PROCLOCK對(duì)象的的鏈表鏈接.

二、參考資料

README

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI