Skip to content

Commit 981d045

Browse files
committed
Complete TODO item:
* Merge LockMethodCtl and LockMethodTable into one shared structure (Bruce)
1 parent 85d2a62 commit 981d045

File tree

4 files changed

+46
-77
lines changed

4 files changed

+46
-77
lines changed

src/backend/storage/lmgr/deadlock.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/deadlock.c,v 1.10 2002/06/20 20:29:35 momjian Exp $
15+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/deadlock.c,v 1.11 2002/07/18 23:06:19 momjian Exp $
1616
*
1717
* Interface:
1818
*
@@ -170,10 +170,6 @@ InitDeadLockChecking(void)
170170
* only look at regular locks.
171171
*
172172
* We must have already locked the master lock before being called.
173-
* NOTE: although the lockctl structure appears to allow each lock
174-
* table to have a different LWLock, all locks that can block had
175-
* better use the same LWLock, else this code will not be adequately
176-
* interlocked!
177173
*/
178174
bool
179175
DeadLockCheck(PGPROC *proc)
@@ -384,7 +380,6 @@ FindLockCycleRecurse(PGPROC *checkProc,
384380
HOLDER *holder;
385381
SHM_QUEUE *lockHolders;
386382
LOCKMETHODTABLE *lockMethodTable;
387-
LOCKMETHODCTL *lockctl;
388383
PROC_QUEUE *waitQueue;
389384
int queue_size;
390385
int conflictMask;
@@ -423,9 +418,8 @@ FindLockCycleRecurse(PGPROC *checkProc,
423418
if (lock == NULL)
424419
return false;
425420
lockMethodTable = GetLocksMethodTable(lock);
426-
lockctl = lockMethodTable->ctl;
427-
numLockModes = lockctl->numLockModes;
428-
conflictMask = lockctl->conflictTab[checkProc->waitLockMode];
421+
numLockModes = lockMethodTable->numLockModes;
422+
conflictMask = lockMethodTable->conflictTab[checkProc->waitLockMode];
429423

430424
/*
431425
* Scan for procs that already hold conflicting locks. These are

src/backend/storage/lmgr/lock.c

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.108 2002/06/20 20:29:35 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.109 2002/07/18 23:06:19 momjian Exp $
1212
*
1313
* NOTES
1414
* Outside modules can create a lock table and acquire/release
@@ -213,12 +213,12 @@ LockMethodInit(LOCKMETHODTABLE *lockMethodTable,
213213
{
214214
int i;
215215

216-
lockMethodTable->ctl->numLockModes = numModes;
216+
lockMethodTable->numLockModes = numModes;
217217
numModes++;
218218
for (i = 0; i < numModes; i++, prioP++, conflictsP++)
219219
{
220-
lockMethodTable->ctl->conflictTab[i] = *conflictsP;
221-
lockMethodTable->ctl->prio[i] = *prioP;
220+
lockMethodTable->conflictTab[i] = *conflictsP;
221+
lockMethodTable->prio[i] = *prioP;
222222
}
223223
}
224224

@@ -263,24 +263,16 @@ LockMethodTableInit(char *tabName,
263263

264264
/* each lock table has a non-shared, permanent header */
265265
lockMethodTable = (LOCKMETHODTABLE *)
266-
MemoryContextAlloc(TopMemoryContext, sizeof(LOCKMETHODTABLE));
266+
ShmemInitStruct(shmemName, sizeof(LOCKMETHODTABLE), &found);
267+
268+
if (!lockMethodTable)
269+
elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
267270

268271
/*
269272
* Lock the LWLock for the table (probably not necessary here)
270273
*/
271274
LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);
272275

273-
/*
274-
* allocate a control structure from shared memory or attach to it if
275-
* it already exists.
276-
*/
277-
sprintf(shmemName, "%s (ctl)", tabName);
278-
lockMethodTable->ctl = (LOCKMETHODCTL *)
279-
ShmemInitStruct(shmemName, sizeof(LOCKMETHODCTL), &found);
280-
281-
if (!lockMethodTable->ctl)
282-
elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
283-
284276
/*
285277
* no zero-th table
286278
*/
@@ -291,9 +283,9 @@ LockMethodTableInit(char *tabName,
291283
*/
292284
if (!found)
293285
{
294-
MemSet(lockMethodTable->ctl, 0, sizeof(LOCKMETHODCTL));
295-
lockMethodTable->ctl->masterLock = LockMgrLock;
296-
lockMethodTable->ctl->lockmethod = NumLockMethods;
286+
MemSet(lockMethodTable, 0, sizeof(LOCKMETHODTABLE));
287+
lockMethodTable->masterLock = LockMgrLock;
288+
lockMethodTable->lockmethod = NumLockMethods;
297289
}
298290

299291
/*
@@ -342,14 +334,14 @@ LockMethodTableInit(char *tabName,
342334
if (!lockMethodTable->holderHash)
343335
elog(FATAL, "LockMethodTableInit: couldn't initialize %s", tabName);
344336

345-
/* init ctl data structures */
337+
/* init data structures */
346338
LockMethodInit(lockMethodTable, conflictsP, prioP, numModes);
347339

348340
LWLockRelease(LockMgrLock);
349341

350342
pfree(shmemName);
351343

352-
return lockMethodTable->ctl->lockmethod;
344+
return lockMethodTable->lockmethod;
353345
}
354346

355347
/*
@@ -476,7 +468,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
476468
return FALSE;
477469
}
478470

479-
masterLock = lockMethodTable->ctl->masterLock;
471+
masterLock = lockMethodTable->masterLock;
480472

481473
LWLockAcquire(masterLock, LW_EXCLUSIVE);
482474

@@ -576,7 +568,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
576568
* XXX Doing numeric comparison on the lockmodes is a hack; it'd be
577569
* better to use a table. For now, though, this works.
578570
*/
579-
for (i = lockMethodTable->ctl->numLockModes; i > 0; i--)
571+
for (i = lockMethodTable->numLockModes; i > 0; i--)
580572
{
581573
if (holder->holding[i] > 0)
582574
{
@@ -631,7 +623,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
631623
* join wait queue. Otherwise, check for conflict with already-held
632624
* locks. (That's last because most complex check.)
633625
*/
634-
if (lockMethodTable->ctl->conflictTab[lockmode] & lock->waitMask)
626+
if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
635627
status = STATUS_FOUND;
636628
else
637629
status = LockCheckConflicts(lockMethodTable, lockmode,
@@ -683,7 +675,7 @@ LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
683675
int tmpMask;
684676

685677
for (i = 1, tmpMask = 2;
686-
i <= lockMethodTable->ctl->numLockModes;
678+
i <= lockMethodTable->numLockModes;
687679
i++, tmpMask <<= 1)
688680
{
689681
if (myHolding[i] > 0)
@@ -749,8 +741,7 @@ LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
749741
PGPROC *proc,
750742
int *myHolding) /* myHolding[] array or NULL */
751743
{
752-
LOCKMETHODCTL *lockctl = lockMethodTable->ctl;
753-
int numLockModes = lockctl->numLockModes;
744+
int numLockModes = lockMethodTable->numLockModes;
754745
int bitmask;
755746
int i,
756747
tmpMask;
@@ -765,7 +756,7 @@ LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
765756
* each type of lock that conflicts with request. Bitwise compare
766757
* tells if there is a conflict.
767758
*/
768-
if (!(lockctl->conflictTab[lockmode] & lock->grantMask))
759+
if (!(lockMethodTable->conflictTab[lockmode] & lock->grantMask))
769760
{
770761
HOLDER_PRINT("LockCheckConflicts: no conflict", holder);
771762
return STATUS_OK;
@@ -798,7 +789,7 @@ LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
798789
* locks held by other processes. If one of these conflicts with the
799790
* kind of lock that I want, there is a conflict and I have to sleep.
800791
*/
801-
if (!(lockctl->conflictTab[lockmode] & bitmask))
792+
if (!(lockMethodTable->conflictTab[lockmode] & bitmask))
802793
{
803794
/* no conflict. OK to get the lock */
804795
HOLDER_PRINT("LockCheckConflicts: resolved", holder);
@@ -918,7 +909,7 @@ WaitOnLock(LOCKMETHOD lockmethod, LOCKMODE lockmode,
918909
* needed, will happen in xact cleanup (see above for motivation).
919910
*/
920911
LOCK_PRINT("WaitOnLock: aborting on lock", lock, lockmode);
921-
LWLockRelease(lockMethodTable->ctl->masterLock);
912+
LWLockRelease(lockMethodTable->masterLock);
922913
elog(ERROR, "deadlock detected");
923914
/* not reached */
924915
}
@@ -1014,7 +1005,7 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
10141005
return FALSE;
10151006
}
10161007

1017-
masterLock = lockMethodTable->ctl->masterLock;
1008+
masterLock = lockMethodTable->masterLock;
10181009
LWLockAcquire(masterLock, LW_EXCLUSIVE);
10191010

10201011
/*
@@ -1109,7 +1100,7 @@ LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
11091100
* granted locks might belong to some waiter, who could now be
11101101
* awakened because he doesn't conflict with his own locks.
11111102
*/
1112-
if (lockMethodTable->ctl->conflictTab[lockmode] & lock->waitMask)
1103+
if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
11131104
wakeupNeeded = true;
11141105

11151106
if (lock->nRequested == 0)
@@ -1208,8 +1199,8 @@ LockReleaseAll(LOCKMETHOD lockmethod, PGPROC *proc,
12081199
return FALSE;
12091200
}
12101201

1211-
numLockModes = lockMethodTable->ctl->numLockModes;
1212-
masterLock = lockMethodTable->ctl->masterLock;
1202+
numLockModes = lockMethodTable->numLockModes;
1203+
masterLock = lockMethodTable->masterLock;
12131204

12141205
LWLockAcquire(masterLock, LW_EXCLUSIVE);
12151206

@@ -1264,7 +1255,7 @@ LockReleaseAll(LOCKMETHOD lockmethod, PGPROC *proc,
12641255
* Read comments in LockRelease
12651256
*/
12661257
if (!wakeupNeeded &&
1267-
lockMethodTable->ctl->conflictTab[i] & lock->waitMask)
1258+
lockMethodTable->conflictTab[i] & lock->waitMask)
12681259
wakeupNeeded = true;
12691260
}
12701261
}
@@ -1355,8 +1346,8 @@ LockShmemSize(int maxBackends)
13551346

13561347
size += MAXALIGN(sizeof(PROC_HDR)); /* ProcGlobal */
13571348
size += maxBackends * MAXALIGN(sizeof(PGPROC)); /* each MyProc */
1358-
size += MAX_LOCK_METHODS * MAXALIGN(sizeof(LOCKMETHODCTL)); /* each
1359-
* lockMethodTable->ctl */
1349+
size += MAX_LOCK_METHODS * MAXALIGN(sizeof(LOCKMETHODTABLE)); /* each
1350+
* lockMethodTable */
13601351

13611352
/* lockHash table */
13621353
size += hash_estimate_size(max_table_size, sizeof(LOCK));

src/backend/storage/lmgr/proc.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.122 2002/07/13 01:02:14 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.123 2002/07/18 23:06:20 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -503,8 +503,7 @@ ProcSleep(LOCKMETHODTABLE *lockMethodTable,
503503
LOCK *lock,
504504
HOLDER *holder)
505505
{
506-
LOCKMETHODCTL *lockctl = lockMethodTable->ctl;
507-
LWLockId masterLock = lockctl->masterLock;
506+
LWLockId masterLock = lockMethodTable->masterLock;
508507
PROC_QUEUE *waitQueue = &(lock->waitProcs);
509508
int myHeldLocks = MyProc->heldLocks;
510509
bool early_deadlock = false;
@@ -537,10 +536,10 @@ ProcSleep(LOCKMETHODTABLE *lockMethodTable,
537536
for (i = 0; i < waitQueue->size; i++)
538537
{
539538
/* Must he wait for me? */
540-
if (lockctl->conflictTab[proc->waitLockMode] & myHeldLocks)
539+
if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
541540
{
542541
/* Must I wait for him ? */
543-
if (lockctl->conflictTab[lockmode] & proc->heldLocks)
542+
if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
544543
{
545544
/*
546545
* Yes, so we have a deadlock. Easiest way to clean
@@ -553,7 +552,7 @@ ProcSleep(LOCKMETHODTABLE *lockMethodTable,
553552
break;
554553
}
555554
/* I must go before this waiter. Check special case. */
556-
if ((lockctl->conflictTab[lockmode] & aheadRequests) == 0 &&
555+
if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
557556
LockCheckConflicts(lockMethodTable,
558557
lockmode,
559558
lock,
@@ -725,7 +724,6 @@ ProcWakeup(PGPROC *proc, int errType)
725724
void
726725
ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock)
727726
{
728-
LOCKMETHODCTL *lockctl = lockMethodTable->ctl;
729727
PROC_QUEUE *waitQueue = &(lock->waitProcs);
730728
int queue_size = waitQueue->size;
731729
PGPROC *proc;
@@ -746,7 +744,7 @@ ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock)
746744
* Waken if (a) doesn't conflict with requests of earlier waiters,
747745
* and (b) doesn't conflict with already-held locks.
748746
*/
749-
if ((lockctl->conflictTab[lockmode] & aheadRequests) == 0 &&
747+
if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
750748
LockCheckConflicts(lockMethodTable,
751749
lockmode,
752750
lock,

src/include/storage/lock.h

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: lock.h,v 1.61 2002/06/20 20:29:52 momjian Exp $
10+
* $Id: lock.h,v 1.62 2002/07/18 23:06:20 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -62,17 +62,14 @@ typedef int LOCKMETHOD;
6262
* There is normally only one lock method, the default one.
6363
* If user locks are enabled, an additional lock method is present.
6464
*
65-
* LOCKMETHODCTL and LOCKMETHODTABLE are split because the first lives
66-
* in shared memory. (There isn't any really good reason for the split.)
67-
* LOCKMETHODTABLE exists in private memory. Both are created by the
68-
* postmaster and should be the same in all backends.
69-
*/
70-
71-
/*
7265
* This is the control structure for a lock table. It
7366
* lives in shared memory. This information is the same
7467
* for all backends.
7568
*
69+
* lockHash -- hash table holding per-locked-object lock information
70+
*
71+
* holderHash -- hash table holding per-lock-holder lock information
72+
*
7673
* lockmethod -- the handle used by the lock table's clients to
7774
* refer to the type of lock table being used.
7875
*
@@ -88,28 +85,17 @@ typedef int LOCKMETHOD;
8885
* starvation). XXX this field is not actually used at present!
8986
*
9087
* masterLock -- synchronizes access to the table
88+
*
9189
*/
92-
typedef struct LOCKMETHODCTL
90+
typedef struct LOCKMETHODTABLE
9391
{
92+
HTAB *lockHash;
93+
HTAB *holderHash;
9494
LOCKMETHOD lockmethod;
9595
int numLockModes;
9696
int conflictTab[MAX_LOCKMODES];
9797
int prio[MAX_LOCKMODES];
9898
LWLockId masterLock;
99-
} LOCKMETHODCTL;
100-
101-
/*
102-
* Eack backend has a non-shared lock table header.
103-
*
104-
* lockHash -- hash table holding per-locked-object lock information
105-
* holderHash -- hash table holding per-lock-holder lock information
106-
* ctl - shared control structure described above.
107-
*/
108-
typedef struct LOCKMETHODTABLE
109-
{
110-
HTAB *lockHash;
111-
HTAB *holderHash;
112-
LOCKMETHODCTL *ctl;
11399
} LOCKMETHODTABLE;
114100

115101

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy