Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8e5ac74

Browse files
committed
Some refinement for the "fast path" lock patch.
1. In GetLockStatusData, avoid initializing instance before we've ensured that the array is large enough. Otherwise, if repalloc moves the block around, we're hosed. 2. Add the word "Relation" to the name of some identifiers, to avoid assuming that the fast-path mechanism will only ever apply to relations (though these particular parts certainly will). Some of the macros could possibly use similar treatment, but the names are getting awfully long already. 3. Add a missing word to comment in AtPrepare_Locks().
1 parent cdd6123 commit 8e5ac74

File tree

1 file changed

+42
-37
lines changed
  • src/backend/storage/lmgr

1 file changed

+42
-37
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ static int FastPathLocalUseCount = 0;
155155
#define FastPathStrongMode(mode) ((mode) > ShareUpdateExclusiveLock)
156156
#define FastPathRelevantMode(mode) ((mode) != ShareUpdateExclusiveLock)
157157

158-
static bool FastPathGrantLock(Oid relid, LOCKMODE lockmode);
159-
static bool FastPathUnGrantLock(Oid relid, LOCKMODE lockmode);
160-
static bool FastPathTransferLocks(LockMethod lockMethodTable,
158+
static bool FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode);
159+
static bool FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode);
160+
static bool FastPathTransferRelationLocks(LockMethod lockMethodTable,
161161
const LOCKTAG *locktag, uint32 hashcode);
162-
static PROCLOCK *FastPathGetLockEntry(LOCALLOCK *locallock);
162+
static PROCLOCK *FastPathGetRelationLockEntry(LOCALLOCK *locallock);
163163

164164
/*
165165
* To make the fast-path lock mechanism work, we must have some way of
@@ -186,9 +186,9 @@ typedef struct
186186
{
187187
slock_t mutex;
188188
uint32 count[FAST_PATH_STRONG_LOCK_HASH_PARTITIONS];
189-
} FastPathStrongLockData;
189+
} FastPathStrongRelationLockData;
190190

191-
FastPathStrongLockData *FastPathStrongLocks;
191+
FastPathStrongRelationLockData *FastPathStrongRelationLocks;
192192

193193
#ifndef LOCK_DEBUG
194194
static bool Dummy_trace = false;
@@ -415,10 +415,11 @@ InitLocks(void)
415415
/*
416416
* Allocate fast-path structures.
417417
*/
418-
FastPathStrongLocks = ShmemInitStruct("Fast Path Strong Lock Data",
419-
sizeof(FastPathStrongLockData), &found);
418+
FastPathStrongRelationLocks =
419+
ShmemInitStruct("Fast Path Strong Relation Lock Data",
420+
sizeof(FastPathStrongRelationLockData), &found);
420421
if (!found)
421-
SpinLockInit(&FastPathStrongLocks->mutex);
422+
SpinLockInit(&FastPathStrongRelationLocks->mutex);
422423

423424
/*
424425
* Allocate non-shared hash table for LOCALLOCK structs. This stores lock
@@ -720,10 +721,11 @@ LockAcquireExtended(const LOCKTAG *locktag,
720721
* yet to begin to transfer fast-path locks.
721722
*/
722723
LWLockAcquire(MyProc->backendLock, LW_EXCLUSIVE);
723-
if (FastPathStrongLocks->count[fasthashcode] != 0)
724+
if (FastPathStrongRelationLocks->count[fasthashcode] != 0)
724725
acquired = false;
725726
else
726-
acquired = FastPathGrantLock(locktag->locktag_field2, lockmode);
727+
acquired = FastPathGrantRelationLock(locktag->locktag_field2,
728+
lockmode);
727729
LWLockRelease(MyProc->backendLock);
728730
if (acquired)
729731
{
@@ -742,11 +744,12 @@ LockAcquireExtended(const LOCKTAG *locktag,
742744
* instruction here, on architectures where that is supported.
743745
*/
744746
Assert(locallock->holdsStrongLockCount == FALSE);
745-
SpinLockAcquire(&FastPathStrongLocks->mutex);
746-
FastPathStrongLocks->count[fasthashcode]++;
747+
SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
748+
FastPathStrongRelationLocks->count[fasthashcode]++;
747749
locallock->holdsStrongLockCount = TRUE;
748-
SpinLockRelease(&FastPathStrongLocks->mutex);
749-
if (!FastPathTransferLocks(lockMethodTable, locktag, hashcode))
750+
SpinLockRelease(&FastPathStrongRelationLocks->mutex);
751+
if (!FastPathTransferRelationLocks(lockMethodTable, locktag,
752+
hashcode))
750753
{
751754
if (reportMemoryError)
752755
ereport(ERROR,
@@ -1099,11 +1102,11 @@ RemoveLocalLock(LOCALLOCK *locallock)
10991102
uint32 fasthashcode;
11001103
fasthashcode = FastPathStrongLockHashPartition(locallock->hashcode);
11011104

1102-
SpinLockAcquire(&FastPathStrongLocks->mutex);
1103-
Assert(FastPathStrongLocks->count[fasthashcode] > 0);
1104-
FastPathStrongLocks->count[fasthashcode]--;
1105+
SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
1106+
Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
1107+
FastPathStrongRelationLocks->count[fasthashcode]--;
11051108
locallock->holdsStrongLockCount = FALSE;
1106-
SpinLockRelease(&FastPathStrongLocks->mutex);
1109+
SpinLockRelease(&FastPathStrongRelationLocks->mutex);
11071110
}
11081111
if (!hash_search(LockMethodLocalHash,
11091112
(void *) &(locallock->tag),
@@ -1642,7 +1645,8 @@ LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
16421645
* it here. Another backend may have moved it to the main table.
16431646
*/
16441647
LWLockAcquire(MyProc->backendLock, LW_EXCLUSIVE);
1645-
released = FastPathUnGrantLock(locktag->locktag_field2, lockmode);
1648+
released = FastPathUnGrantRelationLock(locktag->locktag_field2,
1649+
lockmode);
16461650
LWLockRelease(MyProc->backendLock);
16471651
if (released)
16481652
{
@@ -1825,7 +1829,7 @@ LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
18251829

18261830
/* Attempt fast-path release. */
18271831
relid = locallock->tag.lock.locktag_field2;
1828-
if (FastPathUnGrantLock(relid, lockmode))
1832+
if (FastPathUnGrantRelationLock(relid, lockmode))
18291833
{
18301834
RemoveLocalLock(locallock);
18311835
continue;
@@ -2117,11 +2121,11 @@ LockReassignCurrentOwner(void)
21172121
}
21182122

21192123
/*
2120-
* FastPathGrantLock
2124+
* FastPathGrantRelationLock
21212125
* Grant lock using per-backend fast-path array, if there is space.
21222126
*/
21232127
static bool
2124-
FastPathGrantLock(Oid relid, LOCKMODE lockmode)
2128+
FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
21252129
{
21262130
uint32 f;
21272131
uint32 unused_slot = FP_LOCK_SLOTS_PER_BACKEND;
@@ -2153,12 +2157,12 @@ FastPathGrantLock(Oid relid, LOCKMODE lockmode)
21532157
}
21542158

21552159
/*
2156-
* FastPathUnGrantLock
2160+
* FastPathUnGrantRelationLock
21572161
* Release fast-path lock, if present. Update backend-private local
21582162
* use count, while we're at it.
21592163
*/
21602164
static bool
2161-
FastPathUnGrantLock(Oid relid, LOCKMODE lockmode)
2165+
FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode)
21622166
{
21632167
uint32 f;
21642168
bool result = false;
@@ -2180,12 +2184,12 @@ FastPathUnGrantLock(Oid relid, LOCKMODE lockmode)
21802184
}
21812185

21822186
/*
2183-
* FastPathTransferLocks
2187+
* FastPathTransferRelationLocks
21842188
* Transfer locks matching the given lock tag from per-backend fast-path
21852189
* arrays to the shared hash table.
21862190
*/
21872191
static bool
2188-
FastPathTransferLocks(LockMethod lockMethodTable, const LOCKTAG *locktag,
2192+
FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag,
21892193
uint32 hashcode)
21902194
{
21912195
LWLockId partitionLock = LockHashPartitionLock(hashcode);
@@ -2267,7 +2271,7 @@ FastPathTransferLocks(LockMethod lockMethodTable, const LOCKTAG *locktag,
22672271
* transferring it to the primary lock table if necessary.
22682272
*/
22692273
static PROCLOCK *
2270-
FastPathGetLockEntry(LOCALLOCK *locallock)
2274+
FastPathGetRelationLockEntry(LOCALLOCK *locallock)
22712275
{
22722276
LockMethod lockMethodTable = LockMethods[DEFAULT_LOCKMETHOD];
22732277
LOCKTAG *locktag = &locallock->tag.lock;
@@ -2650,9 +2654,9 @@ LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc,
26502654
&& FastPathTag(&lock->tag) && FastPathStrongMode(lockmode))
26512655
{
26522656
uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode);
2653-
SpinLockAcquire(&FastPathStrongLocks->mutex);
2654-
FastPathStrongLocks->count[fasthashcode]--;
2655-
SpinLockRelease(&FastPathStrongLocks->mutex);
2657+
SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
2658+
FastPathStrongRelationLocks->count[fasthashcode]--;
2659+
SpinLockRelease(&FastPathStrongRelationLocks->mutex);
26562660
}
26572661
}
26582662

@@ -2715,11 +2719,11 @@ AtPrepare_Locks(void)
27152719
/*
27162720
* If the local lock was taken via the fast-path, we need to move it
27172721
* to the primary lock table, or just get a pointer to the existing
2718-
* primary lock table if by chance it's already been transferred.
2722+
* primary lock table entry if by chance it's already been transferred.
27192723
*/
27202724
if (locallock->proclock == NULL)
27212725
{
2722-
locallock->proclock = FastPathGetLockEntry(locallock);
2726+
locallock->proclock = FastPathGetRelationLockEntry(locallock);
27232727
locallock->lock = locallock->proclock->tag.myLock;
27242728
}
27252729

@@ -3010,7 +3014,7 @@ GetLockStatusData(void)
30103014

30113015
for (f = 0; f < FP_LOCK_SLOTS_PER_BACKEND; ++f)
30123016
{
3013-
LockInstanceData *instance = &data->locks[el];
3017+
LockInstanceData *instance;
30143018
uint32 lockbits = FAST_PATH_GET_BITS(proc, f);
30153019

30163020
/* Skip unallocated slots. */
@@ -3024,6 +3028,7 @@ GetLockStatusData(void)
30243028
repalloc(data->locks, sizeof(LockInstanceData) * els);
30253029
}
30263030

3031+
instance = &data->locks[el];
30273032
SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId,
30283033
proc->fpRelId[f]);
30293034
instance->holdMask = lockbits << FAST_PATH_LOCKNUMBER_OFFSET;
@@ -3455,9 +3460,9 @@ lock_twophase_recover(TransactionId xid, uint16 info,
34553460
if (FastPathTag(&lock->tag) && FastPathStrongMode(lockmode))
34563461
{
34573462
uint32 fasthashcode = FastPathStrongLockHashPartition(hashcode);
3458-
SpinLockAcquire(&FastPathStrongLocks->mutex);
3459-
FastPathStrongLocks->count[fasthashcode]++;
3460-
SpinLockRelease(&FastPathStrongLocks->mutex);
3463+
SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
3464+
FastPathStrongRelationLocks->count[fasthashcode]++;
3465+
SpinLockRelease(&FastPathStrongRelationLocks->mutex);
34613466
}
34623467

34633468
LWLockRelease(partitionLock);

0 commit comments

Comments
 (0)