lock.h 8.1 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * lock.h
4
 *	  POSTGRES low-level lock mechanism
5 6
 *
 *
7
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
J
Jan Wieck 已提交
10
 * $Id: lock.h,v 1.60 2002/06/11 13:40:52 wieck Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#ifndef LOCK_H_
#define LOCK_H_

17
#include "storage/itemptr.h"
18
#include "storage/lwlock.h"
B
Bruce Momjian 已提交
19
#include "storage/shmem.h"
20 21


22 23 24
/* originally in procq.h */
typedef struct PROC_QUEUE
{
J
Jan Wieck 已提交
25
	SHM_QUEUE	links;			/* head of list of PGPROC objects */
26
	int			size;			/* number of entries in list */
27 28
} PROC_QUEUE;

J
Jan Wieck 已提交
29 30
/* struct PGPROC is declared in storage/proc.h, but must forward-reference it */
typedef struct PGPROC PGPROC;
31 32


33 34
extern int	max_locks_per_xact;

35
#ifdef LOCK_DEBUG
B
Bruce Momjian 已提交
36
extern int	Trace_lock_oidmin;
37 38
extern bool Trace_locks;
extern bool Trace_userlocks;
B
Bruce Momjian 已提交
39
extern int	Trace_lock_table;
40
extern bool Debug_deadlocks;
41
#endif   /* LOCK_DEBUG */
42 43


44 45
typedef int LOCKMASK;

46 47
typedef int LOCKMODE;
typedef int LOCKMETHOD;
48

49
/* MAX_LOCKMODES cannot be larger than the # of bits in LOCKMASK */
50
#define MAX_LOCKMODES		10
51

52
/* MAX_LOCK_METHODS is the number of distinct lock control tables allowed */
53
#define MAX_LOCK_METHODS	3
54

M
 
Marc G. Fournier 已提交
55 56 57 58 59
#define INVALID_TABLEID		0

#define INVALID_LOCKMETHOD	INVALID_TABLEID
#define DEFAULT_LOCKMETHOD	1
#define USER_LOCKMETHOD		2
60

61 62 63
/*
 * There is normally only one lock method, the default one.
 * If user locks are enabled, an additional lock method is present.
B
Bruce Momjian 已提交
64 65
 *
 * LOCKMETHODCTL and LOCKMETHODTABLE are split because the first lives
66
 * in shared memory.  (There isn't any really good reason for the split.)
B
Bruce Momjian 已提交
67
 * LOCKMETHODTABLE exists in private memory.  Both are created by the
68
 * postmaster and should be the same in all backends.
B
Bruce Momjian 已提交
69
 */
70

71 72
/*
 * This is the control structure for a lock table.	It
B
Bruce Momjian 已提交
73
 * lives in shared memory.	This information is the same
74
 * for all backends.
75
 *
76 77
 * lockmethod -- the handle used by the lock table's clients to
 *		refer to the type of lock table being used.
78
 *
79
 * numLockModes -- number of lock types (READ,WRITE,etc) that
80
 *		are defined on this lock table
81 82
 *
 * conflictTab -- this is an array of bitmasks showing lock
83 84
 *		type conflicts. conflictTab[i] is a mask with the j-th bit
 *		turned on if lock types i and j conflict.
85
 *
86
 * prio -- each lockmode has a priority, so, for example, waiting
87
 *		writers can be given priority over readers (to avoid
88
 *		starvation).  XXX this field is not actually used at present!
89
 *
90
 * masterLock -- synchronizes access to the table
91
 */
92
typedef struct LOCKMETHODCTL
93
{
94
	LOCKMETHOD	lockmethod;
95 96 97
	int			numLockModes;
	int			conflictTab[MAX_LOCKMODES];
	int			prio[MAX_LOCKMODES];
98
	LWLockId	masterLock;
99
} LOCKMETHODCTL;
100 101

/*
102
 * Eack backend has a non-shared lock table header.
103 104 105 106
 *
 * lockHash -- hash table holding per-locked-object lock information
 * holderHash -- hash table holding per-lock-holder lock information
 * ctl - shared control structure described above.
107
 */
108
typedef struct LOCKMETHODTABLE
109
{
110
	HTAB	   *lockHash;
111
	HTAB	   *holderHash;
112
	LOCKMETHODCTL *ctl;
113
} LOCKMETHODTABLE;
114

M
 
Marc G. Fournier 已提交
115

116 117
/*
 * LOCKTAG is the key information needed to look up a LOCK item in the
B
Bruce Momjian 已提交
118
 * lock hashtable.	A LOCKTAG value uniquely identifies a lockable object.
119 120
 */
typedef struct LOCKTAG
121
{
122 123 124 125 126 127 128
	Oid			relId;
	Oid			dbId;
	union
	{
		BlockNumber blkno;
		TransactionId xid;
	}			objId;
129

130 131 132 133 134 135
	/*
	 * offnum should be part of objId.tupleId above, but would increase
	 * sizeof(LOCKTAG) and so moved here; currently used by userlocks
	 * only.
	 */
	OffsetNumber offnum;
136

137 138
	uint16		lockmethod;		/* needed by userlocks */
} LOCKTAG;
139 140 141


/*
142
 * Per-locked-object lock information:
143 144
 *
 * tag -- uniquely identifies the object being locked
145 146
 * grantMask -- bitmask for all lock types currently granted on this object.
 * waitMask -- bitmask for all lock types currently awaited on this object.
147
 * lockHolders -- list of HOLDER objects for this lock.
148 149 150 151 152 153
 * 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.
154
 */
155
typedef struct LOCK
156 157
{
	/* hash key */
158
	LOCKTAG		tag;			/* unique identifier of lockable object */
159 160

	/* data */
161 162
	int			grantMask;		/* bitmask for lock types already granted */
	int			waitMask;		/* bitmask for lock types awaited */
163
	SHM_QUEUE	lockHolders;	/* list of HOLDER objects assoc. with lock */
J
Jan Wieck 已提交
164
	PROC_QUEUE	waitProcs;		/* list of PGPROC objects waiting on lock */
B
Bruce Momjian 已提交
165 166
	int			requested[MAX_LOCKMODES];		/* counts of requested
												 * locks */
167
	int			nRequested;		/* total of requested[] array */
B
Bruce Momjian 已提交
168
	int			granted[MAX_LOCKMODES]; /* counts of granted locks */
169
	int			nGranted;		/* total of granted[] array */
170
} LOCK;
171

172
#define LOCK_LOCKMETHOD(lock) ((lock).tag.lockmethod)
173

174 175 176 177 178 179

/*
 * We may have several different transactions holding or awaiting locks
 * on the same lockable object.  We need to store some per-holder information
 * for each such holder (or would-be holder).
 *
180 181
 * HOLDERTAG is the key information needed to look up a HOLDER item in the
 * holder hashtable.  A HOLDERTAG value uniquely identifies a lock holder.
182 183
 *
 * There are two possible kinds of holder tags: a transaction (identified
J
Jan Wieck 已提交
184 185
 * both by the PGPROC of the backend running it, and the xact's own ID) and
 * a session (identified by backend PGPROC, with xid = InvalidTransactionId).
186 187 188 189
 *
 * Currently, session holders are used for user locks and for cross-xact
 * locks obtained for VACUUM.  We assume that a session lock never conflicts
 * with per-transaction locks obtained by the same backend.
190 191
 *
 * The holding[] array counts the granted locks (of each type) represented
B
Bruce Momjian 已提交
192
 * by this holder.	Note that there will be a holder object, possibly with
193 194 195
 * zero holding[], for any lock that the process is currently waiting on.
 * Otherwise, holder objects whose counts have gone to zero are recycled
 * as soon as convenient.
196
 *
197
 * Each HOLDER object is linked into lists for both the associated LOCK object
J
Jan Wieck 已提交
198
 * and the owning PGPROC object.	Note that the HOLDER is entered into these
199
 * lists as soon as it is created, even if no lock has yet been granted.
J
Jan Wieck 已提交
200
 * A PGPROC that is waiting for a lock to be granted will also be linked into
201
 * the lock's waitProcs queue.
202
 */
203
typedef struct HOLDERTAG
204 205
{
	SHMEM_OFFSET lock;			/* link to per-lockable-object information */
J
Jan Wieck 已提交
206
	SHMEM_OFFSET proc;			/* link to PGPROC of owning backend */
207
	TransactionId xid;			/* xact ID, or InvalidTransactionId */
208
} HOLDERTAG;
209

210
typedef struct HOLDER
211 212
{
	/* tag */
213
	HOLDERTAG	tag;			/* unique identifier of holder object */
214 215

	/* data */
B
Bruce Momjian 已提交
216
	int			holding[MAX_LOCKMODES]; /* count of locks currently held */
217
	int			nHolding;		/* total of holding[] array */
218 219
	SHM_QUEUE	lockLink;		/* list link for lock's list of holders */
	SHM_QUEUE	procLink;		/* list link for process's list of holders */
220
} HOLDER;
221

222
#define HOLDER_LOCKMETHOD(holder) \
223 224 225
		(((LOCK *) MAKE_PTR((holder).tag.lock))->tag.lockmethod)


226 227 228
/*
 * function prototypes
 */
229
extern void InitLocks(void);
230
extern LOCKMETHODTABLE *GetLocksMethodTable(LOCK *lock);
B
Bruce Momjian 已提交
231
extern LOCKMETHOD LockMethodTableInit(char *tabName, LOCKMASK *conflictsP,
232
					int *prioP, int numModes, int maxBackends);
M
 
Marc G. Fournier 已提交
233 234
extern LOCKMETHOD LockMethodTableRename(LOCKMETHOD lockmethod);
extern bool LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
235
			TransactionId xid, LOCKMODE lockmode, bool dontWait);
M
 
Marc G. Fournier 已提交
236
extern bool LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
B
Bruce Momjian 已提交
237
			TransactionId xid, LOCKMODE lockmode);
J
Jan Wieck 已提交
238
extern bool LockReleaseAll(LOCKMETHOD lockmethod, PGPROC *proc,
B
Bruce Momjian 已提交
239
			   bool allxids, TransactionId xid);
240
extern int LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
B
Bruce Momjian 已提交
241
				   LOCKMODE lockmode,
J
Jan Wieck 已提交
242
				   LOCK *lock, HOLDER *holder, PGPROC *proc,
B
Bruce Momjian 已提交
243
				   int *myHolding);
244
extern void GrantLock(LOCK *lock, HOLDER *holder, LOCKMODE lockmode);
J
Jan Wieck 已提交
245
extern void RemoveFromWaitQueue(PGPROC *proc);
246
extern int	LockShmemSize(int maxBackends);
J
Jan Wieck 已提交
247
extern bool DeadLockCheck(PGPROC *proc);
248
extern void InitDeadLockChecking(void);
249

250
#ifdef LOCK_DEBUG
251
extern void DumpLocks(void);
M
 
Marc G. Fournier 已提交
252
extern void DumpAllLocks(void);
253
#endif
254

255
#endif   /* LOCK_H */