1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* lock.h--
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
1998-01-24 23:50:57 +01:00
|
|
|
* $Id: lock.h,v 1.9 1998/01/24 22:50:11 momjian Exp $
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef LOCK_H_
|
|
|
|
#define LOCK_H_
|
|
|
|
|
1996-11-05 07:11:08 +01:00
|
|
|
#include <storage/shmem.h>
|
|
|
|
#include <storage/itemptr.h>
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
extern SPINLOCK LockMgrLock;
|
1997-09-08 04:41:22 +02:00
|
|
|
typedef int MASK;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#define INIT_TABLE_SIZE 100
|
|
|
|
#define MAX_TABLE_SIZE 1000
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------
|
|
|
|
* The following defines are used to estimate how much shared
|
1997-09-07 07:04:48 +02:00
|
|
|
* memory the lock manager is going to require.
|
|
|
|
*
|
1996-08-28 03:59:28 +02:00
|
|
|
* NBACKENDS - The number of concurrently running backends
|
|
|
|
* NLOCKS_PER_XACT - The number of unique locks acquired in a transaction
|
|
|
|
* NLOCKENTS - The maximum number of lock entries in the lock table.
|
|
|
|
* ----------------------
|
|
|
|
*/
|
|
|
|
#define NBACKENDS 50
|
|
|
|
#define NLOCKS_PER_XACT 40
|
|
|
|
#define NLOCKENTS NLOCKS_PER_XACT*NBACKENDS
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
typedef int LOCK_TYPE;
|
|
|
|
typedef int LOCKT;
|
|
|
|
typedef int LockTableId;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* MAX_LOCKTYPES cannot be larger than the bits in MASK */
|
|
|
|
#define MAX_LOCKTYPES 6
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MAX_TABLES corresponds to the number of spin locks allocated in
|
|
|
|
* CreateSpinLocks() or the number of shared memory locations allocated
|
|
|
|
* for lock table spin locks in the case of machines with TAS instructions.
|
|
|
|
*/
|
|
|
|
#define MAX_TABLES 2
|
|
|
|
|
|
|
|
#define INVALID_TABLEID 0
|
|
|
|
|
|
|
|
/*typedef struct LOCK LOCK; */
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct ltag
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Oid relId;
|
|
|
|
Oid dbId;
|
1997-09-07 07:04:48 +02:00
|
|
|
ItemPointerData tupleId;
|
1997-09-08 23:56:23 +02:00
|
|
|
} LOCKTAG;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
#define TAGSIZE (sizeof(LOCKTAG))
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* This is the control structure for a lock table. It
|
1996-08-28 03:59:28 +02:00
|
|
|
* lives in shared memory:
|
|
|
|
*
|
|
|
|
* tableID -- the handle used by the lock table's clients to
|
1997-09-07 07:04:48 +02:00
|
|
|
* refer to the table.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* nLockTypes -- number of lock types (READ,WRITE,etc) that
|
1997-09-07 07:04:48 +02:00
|
|
|
* are defined on this lock table
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* conflictTab -- this is an array of bitmasks showing lock
|
1997-09-07 07:04:48 +02:00
|
|
|
* type conflicts. conflictTab[i] is a mask with the j-th bit
|
|
|
|
* turned on if lock types i and j conflict.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* prio -- each locktype has a priority, so, for example, waiting
|
1997-09-07 07:04:48 +02:00
|
|
|
* writers can be given priority over readers (to avoid
|
|
|
|
* starvation).
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* masterlock -- synchronizes access to the table
|
|
|
|
*
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct lockctl
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
LockTableId tableId;
|
|
|
|
int nLockTypes;
|
|
|
|
int conflictTab[MAX_LOCKTYPES];
|
|
|
|
int prio[MAX_LOCKTYPES];
|
|
|
|
SPINLOCK masterLock;
|
1997-09-08 23:56:23 +02:00
|
|
|
} LOCKCTL;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lockHash -- hash table on lock Ids,
|
|
|
|
* xidHash -- hash on xid and lockId in case
|
1997-09-07 07:04:48 +02:00
|
|
|
* multiple processes are holding the lock
|
1996-08-28 03:59:28 +02:00
|
|
|
* ctl - control structure described above.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct ltable
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HTAB *lockHash;
|
|
|
|
HTAB *xidHash;
|
|
|
|
LOCKCTL *ctl;
|
1997-09-08 23:56:23 +02:00
|
|
|
} LOCKTAB;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* -----------------------
|
|
|
|
* A transaction never conflicts with its own locks. Hence, if
|
|
|
|
* multiple transactions hold non-conflicting locks on the same
|
|
|
|
* data, private per-transaction information must be stored in the
|
|
|
|
* XID table. The tag is XID + shared memory lock address so that
|
|
|
|
* all locks can use the same XID table. The private information
|
|
|
|
* we store is the number of locks of each type (holders) and the
|
|
|
|
* total number of locks (nHolding) held by the transaction.
|
|
|
|
*
|
|
|
|
* NOTE: --
|
|
|
|
* There were some problems with the fact that currently TransactionIdData
|
|
|
|
* is a 5 byte entity and compilers long word aligning of structure fields.
|
|
|
|
* If the 3 byte padding is put in front of the actual xid data then the
|
|
|
|
* hash function (which uses XID_TAGSIZE when deciding how many bytes of a
|
|
|
|
* struct to look at for the key) might only see the last two bytes of the xid.
|
|
|
|
*
|
|
|
|
* Clearly this is not good since its likely that these bytes will be the
|
|
|
|
* same for many transactions and hence they will share the same entry in
|
|
|
|
* hash table causing the entry to be corrupted. For this long-winded
|
|
|
|
* reason I have put the tag in a struct of its own to ensure that the
|
|
|
|
* XID_TAGSIZE is computed correctly. It used to be sizeof (SHMEM_OFFSET) +
|
|
|
|
* sizeof(TransactionIdData) which != sizeof(XIDTAG).
|
|
|
|
*
|
|
|
|
* Finally since the hash function will now look at all 12 bytes of the tag
|
|
|
|
* the padding bytes MUST be zero'd before use in hash_search() as they
|
|
|
|
* will have random values otherwise. Jeff 22 July 1991.
|
|
|
|
* -----------------------
|
|
|
|
*/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct XIDTAG
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
SHMEM_OFFSET lock;
|
|
|
|
int pid;
|
|
|
|
TransactionId xid;
|
1997-09-08 23:56:23 +02:00
|
|
|
} XIDTAG;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct XIDLookupEnt
|
|
|
|
{
|
|
|
|
/* tag */
|
1997-09-08 04:41:22 +02:00
|
|
|
XIDTAG tag;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* data */
|
1997-09-08 04:41:22 +02:00
|
|
|
int holders[MAX_LOCKTYPES];
|
|
|
|
int nHolding;
|
|
|
|
SHM_QUEUE queue;
|
1997-09-08 23:56:23 +02:00
|
|
|
} XIDLookupEnt;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
#define XID_TAGSIZE (sizeof(XIDTAG))
|
|
|
|
|
|
|
|
/* originally in procq.h */
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct procQueue
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
SHM_QUEUE links;
|
|
|
|
int size;
|
1997-09-08 23:56:23 +02:00
|
|
|
} PROC_QUEUE;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lock information:
|
|
|
|
*
|
|
|
|
* tag -- uniquely identifies the object being locked
|
|
|
|
* mask -- union of the conflict masks of all lock types
|
1997-09-07 07:04:48 +02:00
|
|
|
* currently held on this object.
|
1996-08-28 03:59:28 +02:00
|
|
|
* waitProcs -- queue of processes waiting for this lock
|
|
|
|
* holders -- count of each lock type currently held on the
|
1997-09-07 07:04:48 +02:00
|
|
|
* lock.
|
1996-08-28 03:59:28 +02:00
|
|
|
* nHolding -- total locks of all types.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct Lock
|
|
|
|
{
|
|
|
|
/* hash key */
|
1997-09-08 04:41:22 +02:00
|
|
|
LOCKTAG tag;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* data */
|
1997-09-08 04:41:22 +02:00
|
|
|
int mask;
|
|
|
|
PROC_QUEUE waitProcs;
|
|
|
|
int holders[MAX_LOCKTYPES];
|
|
|
|
int nHolding;
|
|
|
|
int activeHolders[MAX_LOCKTYPES];
|
|
|
|
int nActive;
|
1997-09-08 23:56:23 +02:00
|
|
|
} LOCK;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
#define LockGetLock_nHolders(l) l->nHolders
|
|
|
|
|
|
|
|
#define LockDecrWaitHolders(lock, lockt) \
|
|
|
|
lock->nHolding--; \
|
|
|
|
lock->holders[lockt]--
|
|
|
|
|
|
|
|
#define LockLockTable() SpinAcquire(LockMgrLock);
|
|
|
|
#define UnlockLockTable() SpinRelease(LockMgrLock);
|
|
|
|
|
|
|
|
extern SPINLOCK LockMgrLock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* function prototypes
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void InitLocks(void);
|
|
|
|
extern void LockDisable(int status);
|
1998-01-24 23:50:57 +01:00
|
|
|
extern LockTableId LockTabInit(char *tabName, MASK *conflictsP, int *prioP,
|
1997-09-07 07:04:48 +02:00
|
|
|
int ntypes);
|
1997-09-08 23:56:23 +02:00
|
|
|
extern bool LockAcquire(LockTableId tableId, LOCKTAG *lockName, LOCKT lockt);
|
1998-01-24 23:50:57 +01:00
|
|
|
extern int LockResolveConflicts(LOCKTAB *ltable, LOCK *lock, LOCKT lockt,
|
1997-09-07 07:04:48 +02:00
|
|
|
TransactionId xid);
|
1997-09-08 23:56:23 +02:00
|
|
|
extern bool LockRelease(LockTableId tableId, LOCKTAG *lockName, LOCKT lockt);
|
|
|
|
extern void GrantLock(LOCK *lock, LOCKT lockt);
|
|
|
|
extern bool LockReleaseAll(LockTableId tableId, SHM_QUEUE *lockQueue);
|
1997-09-08 04:41:22 +02:00
|
|
|
extern int LockShmemSize(void);
|
|
|
|
extern bool LockingDisabled(void);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-02-12 06:25:13 +01:00
|
|
|
#ifdef DEADLOCK_DEBUG
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void DumpLocks(void);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-02-12 06:25:13 +01:00
|
|
|
#endif
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif /* LOCK_H */
|