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