Commit e7ca8674 authored by Bruce Momjian's avatar Bruce Momjian

Try to reduce confusion about what is a lock method identifier, a lock

method control structure, or a table of control structures.

. Use type LOCKMASK where an int is not a counter.

. Get rid of INVALID_TABLEID, use INVALID_LOCKMETHOD instead.

. Use INVALID_LOCKMETHOD instead of (LOCKMETHOD) NULL, because
  LOCKMETHOD is not a pointer.

. Define and use macro LockMethodIsValid.

. Rename LOCKMETHOD to LOCKMETHODID.

. Remove global variable LongTermTableId in lmgr.c, because it is
  never used.

. Make LockTableId static in lmgr.c, because it is used nowhere else.
  Why not remove it and use DEFAULT_LOCKMETHOD?

. Rename the lock method control structure from LOCKMETHODTABLE to
  LockMethodData.  Introduce a pointer type named LockMethod.

. Remove elog(FATAL) after InitLockTable() call in
  CreateSharedMemoryAndSemaphores(), because if something goes wrong,
  there is elog(FATAL) in LockMethodTableInit(), and if this doesn't
  help, an elog(ERROR) in InitLockTable() is promoted to FATAL.

. Make InitLockTable() void, because its only caller does not use its
  return value any more.

. Rename variables in lock.c to avoid statements like
        LockMethodTable[NumLockMethods] = lockMethodTable;
        lockMethodTable = LockMethodTable[lockmethod];

. Change LOCKMETHODID type to uint16 to fit into struct LOCKTAG.

. Remove static variables BITS_OFF and BITS_ON from lock.c, because
  I agree to this doubt:
 * XXX is a fetch from a static array really faster than a shift?

. Define and use macros LOCKBIT_ON/OFF.


Manfred Koizar
parent e2ac58c7
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.58 2003/11/29 19:51:56 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.59 2003/12/01 21:59:25 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -111,8 +111,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate,
* Set up lock manager
*/
InitLocks();
if (InitLockTable(maxBackends) == INVALID_TABLEID)
elog(FATAL, "could not create the lock table");
InitLockTable(maxBackends);
/*
* Set up process table
......
......@@ -12,7 +12,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.26 2003/11/29 19:51:56 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.27 2003/12/01 21:59:25 momjian Exp $
*
* Interface:
*
......@@ -428,7 +428,7 @@ FindLockCycleRecurse(PGPROC *checkProc,
LOCK *lock;
PROCLOCK *proclock;
SHM_QUEUE *lockHolders;
LOCKMETHODTABLE *lockMethodTable;
LockMethod lockMethodTable;
PROC_QUEUE *waitQueue;
int queue_size;
int conflictMask;
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lmgr.c,v 1.61 2003/11/29 19:51:56 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lmgr.c,v 1.62 2003/12/01 21:59:25 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -65,26 +65,24 @@ static LOCKMASK LockConflicts[] = {
};
LOCKMETHOD LockTableId = (LOCKMETHOD) NULL;
LOCKMETHOD LongTermTableId = (LOCKMETHOD) NULL;
static LOCKMETHODID LockTableId = INVALID_LOCKMETHOD;
/*
* Create the lock table described by LockConflicts
*/
LOCKMETHOD
void
InitLockTable(int maxBackends)
{
int lockmethod;
LOCKMETHODID LongTermTableId;
/* number of lock modes is lengthof()-1 because of dummy zero */
lockmethod = LockMethodTableInit("LockTable",
LockConflicts,
lengthof(LockConflicts) - 1,
maxBackends);
LockTableId = lockmethod;
if (!(LockTableId))
LockTableId = LockMethodTableInit("LockTable",
LockConflicts,
lengthof(LockConflicts) - 1,
maxBackends);
if (!LockMethodIsValid(LockTableId))
elog(ERROR, "could not initialize lock table");
Assert(LockTableId == DEFAULT_LOCKMETHOD);
#ifdef USER_LOCKS
......@@ -92,11 +90,10 @@ InitLockTable(int maxBackends)
* Allocate another tableId for long-term locks
*/
LongTermTableId = LockMethodTableRename(LockTableId);
if (!(LongTermTableId))
if (!LockMethodIsValid(LongTermTableId))
elog(ERROR, "could not rename long-term lock table");
Assert(LongTermTableId == USER_LOCKMETHOD);
#endif
return LockTableId;
}
/*
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.138 2003/11/29 19:51:57 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.139 2003/12/01 21:59:25 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -543,14 +543,14 @@ ProcQueueInit(PROC_QUEUE *queue)
* semaphore is normally zero, so when we try to acquire it, we sleep.
*/
int
ProcSleep(LOCKMETHODTABLE *lockMethodTable,
ProcSleep(LockMethod lockMethodTable,
LOCKMODE lockmode,
LOCK *lock,
PROCLOCK *proclock)
{
LWLockId masterLock = lockMethodTable->masterLock;
PROC_QUEUE *waitQueue = &(lock->waitProcs);
int myHeldLocks = MyProc->heldLocks;
LOCKMASK myHeldLocks = MyProc->heldLocks;
bool early_deadlock = false;
PGPROC *proc;
int i;
......@@ -575,7 +575,7 @@ ProcSleep(LOCKMETHODTABLE *lockMethodTable,
*/
if (myHeldLocks != 0)
{
int aheadRequests = 0;
LOCKMASK aheadRequests = 0;
proc = (PGPROC *) MAKE_PTR(waitQueue->links.next);
for (i = 0; i < waitQueue->size; i++)
......@@ -615,7 +615,7 @@ ProcSleep(LOCKMETHODTABLE *lockMethodTable,
break;
}
/* Nope, so advance to next waiter */
aheadRequests |= (1 << proc->waitLockMode);
aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
proc = (PGPROC *) MAKE_PTR(proc->links.next);
}
......@@ -637,7 +637,7 @@ ProcSleep(LOCKMETHODTABLE *lockMethodTable,
SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
waitQueue->size++;
lock->waitMask |= (1 << lockmode);
lock->waitMask |= LOCKBIT_ON(lockmode);
/* Set up wait information in PGPROC object, too */
MyProc->waitLock = lock;
......@@ -769,12 +769,12 @@ ProcWakeup(PGPROC *proc, int errType)
* for lock, waken any that are no longer blocked.
*/
void
ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock)
ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
{
PROC_QUEUE *waitQueue = &(lock->waitProcs);
int queue_size = waitQueue->size;
PGPROC *proc;
int aheadRequests = 0;
LOCKMASK aheadRequests = 0;
Assert(queue_size >= 0);
......@@ -815,7 +815,7 @@ ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock)
* Cannot wake this guy. Remember his request for later
* checks.
*/
aheadRequests |= (1 << lockmode);
aheadRequests |= LOCKBIT_ON(lockmode);
proc = (PGPROC *) MAKE_PTR(proc->links.next);
}
}
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/storage/lmgr.h,v 1.41 2003/11/29 22:41:13 pgsql Exp $
* $PostgreSQL: pgsql/src/include/storage/lmgr.h,v 1.42 2003/12/01 21:59:25 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -40,10 +40,7 @@
* so increase that if you want to add more modes.
*/
extern LOCKMETHOD LockTableId;
extern LOCKMETHOD InitLockTable(int maxBackends);
extern void InitLockTable(int maxBackends);
extern void RelationInitLockInfo(Relation relation);
/* Lock a relation */
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/storage/lock.h,v 1.74 2003/11/29 22:41:13 pgsql Exp $
* $PostgreSQL: pgsql/src/include/storage/lock.h,v 1.75 2003/12/01 21:59:25 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -42,22 +42,23 @@ extern bool Debug_deadlocks;
typedef int LOCKMASK;
typedef int LOCKMODE;
typedef int LOCKMETHOD;
/* MAX_LOCKMODES cannot be larger than the # of bits in LOCKMASK */
#define MAX_LOCKMODES 10
#define LOCKBIT_ON(lockmode) (1 << (lockmode))
#define LOCKBIT_OFF(lockmode) (~(1 << (lockmode)))
typedef uint16 LOCKMETHODID;
/* MAX_LOCK_METHODS is the number of distinct lock control tables allowed */
#define MAX_LOCK_METHODS 3
#define INVALID_TABLEID 0
#define INVALID_LOCKMETHOD INVALID_TABLEID
#define INVALID_LOCKMETHOD 0
#define DEFAULT_LOCKMETHOD 1
#define USER_LOCKMETHOD 2
#define LockMethodIsValid(lockmethodid) ((lockmethodid) != INVALID_LOCKMETHOD)
/*
* There is normally only one lock method, the default one.
* If user locks are enabled, an additional lock method is present.
......@@ -83,15 +84,16 @@ typedef int LOCKMETHOD;
* masterLock -- synchronizes access to the table
*
*/
typedef struct LOCKMETHODTABLE
typedef struct LockMethodData
{
HTAB *lockHash;
HTAB *proclockHash;
LOCKMETHOD lockmethod;
int numLockModes;
int conflictTab[MAX_LOCKMODES];
LWLockId masterLock;
} LOCKMETHODTABLE;
HTAB *lockHash;
HTAB *proclockHash;
LOCKMETHODID lockmethodid;
int numLockModes;
LOCKMASK conflictTab[MAX_LOCKMODES];
LWLockId masterLock;
} LockMethodData;
typedef LockMethodData *LockMethod;
/*
......@@ -115,7 +117,7 @@ typedef struct LOCKTAG
*/
OffsetNumber offnum;
uint16 lockmethod; /* needed by userlocks */
LOCKMETHODID lockmethodid; /* needed by userlocks */
} LOCKTAG;
......@@ -139,8 +141,8 @@ typedef struct LOCK
LOCKTAG tag; /* unique identifier of lockable object */
/* data */
int grantMask; /* bitmask for lock types already granted */
int waitMask; /* bitmask for lock types awaited */
LOCKMASK grantMask; /* bitmask for lock types already granted */
LOCKMASK waitMask; /* bitmask for lock types awaited */
SHM_QUEUE lockHolders; /* list of PROCLOCK objects assoc. with
* lock */
PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */
......@@ -151,7 +153,7 @@ typedef struct LOCK
int nGranted; /* total of granted[] array */
} LOCK;
#define LOCK_LOCKMETHOD(lock) ((lock).tag.lockmethod)
#define LOCK_LOCKMETHOD(lock) ((lock).tag.lockmethodid)
/*
......@@ -204,7 +206,7 @@ typedef struct PROCLOCK
} PROCLOCK;
#define PROCLOCK_LOCKMETHOD(proclock) \
(((LOCK *) MAKE_PTR((proclock).tag.lock))->tag.lockmethod)
(((LOCK *) MAKE_PTR((proclock).tag.lock))->tag.lockmethodid)
/*
* This struct holds information passed from lmgr internals to the lock
......@@ -227,17 +229,17 @@ typedef struct
* function prototypes
*/
extern void InitLocks(void);
extern LOCKMETHODTABLE *GetLocksMethodTable(LOCK *lock);
extern LOCKMETHOD LockMethodTableInit(char *tabName, LOCKMASK *conflictsP,
extern LockMethod GetLocksMethodTable(LOCK *lock);
extern LOCKMETHODID LockMethodTableInit(char *tabName, LOCKMASK *conflictsP,
int numModes, int maxBackends);
extern LOCKMETHOD LockMethodTableRename(LOCKMETHOD lockmethod);
extern bool LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
extern LOCKMETHODID LockMethodTableRename(LOCKMETHODID lockmethodid);
extern bool LockAcquire(LOCKMETHODID lockmethodid, LOCKTAG *locktag,
TransactionId xid, LOCKMODE lockmode, bool dontWait);
extern bool LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
extern bool LockRelease(LOCKMETHODID lockmethodid, LOCKTAG *locktag,
TransactionId xid, LOCKMODE lockmode);
extern bool LockReleaseAll(LOCKMETHOD lockmethod, PGPROC *proc,
extern bool LockReleaseAll(LOCKMETHODID lockmethodid, PGPROC *proc,
bool allxids, TransactionId xid);
extern int LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
extern int LockCheckConflicts(LockMethod lockMethodTable,
LOCKMODE lockmode,
LOCK *lock, PROCLOCK *proclock, PGPROC *proc,
int *myHolding);
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.66 2003/11/29 22:41:13 pgsql Exp $
* $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.67 2003/12/01 21:59:25 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -106,10 +106,10 @@ extern void InitDummyProcess(int proctype);
extern void ProcReleaseLocks(bool isCommit);
extern void ProcQueueInit(PROC_QUEUE *queue);
extern int ProcSleep(LOCKMETHODTABLE *lockMethodTable, LOCKMODE lockmode,
extern int ProcSleep(LockMethod lockMethodTable, LOCKMODE lockmode,
LOCK *lock, PROCLOCK *proclock);
extern PGPROC *ProcWakeup(PGPROC *proc, int errType);
extern void ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock);
extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
extern bool LockWaitCancel(void);
extern void ProcWaitForSignal(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment