Improved locking code

This commit is contained in:
Andrew Kane
2024-01-07 22:34:41 -08:00
parent 9e680884bd
commit 520e274dde

View File

@@ -390,6 +390,26 @@ InsertTupleInMemory(Relation index, Datum *values, ItemPointer heaptid, HnswBuil
return true; return true;
} }
/*
* Acquire a lock if needed
*/
static inline void
HnswLockAcquire(HnswShared * hnswshared)
{
if (hnswshared)
SpinLockAcquire(&hnswshared->mutex);
}
/*
* Release a lock if needed
*/
static inline void
HnswLockRelease(HnswShared * hnswshared)
{
if (hnswshared)
SpinLockRelease(&hnswshared->mutex);
}
/* /*
* Callback for table_index_build_scan * Callback for table_index_build_scan
*/ */
@@ -414,8 +434,7 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
/* Flush pages if needed */ /* Flush pages if needed */
if (!graph->flushed && graph->memoryUsed >= graph->memoryTotal) if (!graph->flushed && graph->memoryUsed >= graph->memoryTotal)
{ {
if (hnswshared) HnswLockAcquire(hnswshared);
SpinLockAcquire(&hnswshared->mutex);
if (!hnswshared) if (!hnswshared)
ereport(NOTICE, ereport(NOTICE,
@@ -426,8 +445,7 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
if (!graph->flushed) if (!graph->flushed)
FlushPages(buildstate); FlushPages(buildstate);
if (hnswshared) HnswLockRelease(hnswshared);
SpinLockRelease(&hnswshared->mutex);
} }
oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx); oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
@@ -441,13 +459,9 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values,
/* Update progress */ /* Update progress */
if (inserted) if (inserted)
{ {
if (hnswshared) HnswLockAcquire(hnswshared);
SpinLockAcquire(&hnswshared->mutex);
UpdateProgress(PROGRESS_CREATEIDX_TUPLES_DONE, ++graph->indtuples); UpdateProgress(PROGRESS_CREATEIDX_TUPLES_DONE, ++graph->indtuples);
HnswLockRelease(hnswshared);
if (hnswshared)
SpinLockRelease(&hnswshared->mutex);
} }
/* Reset memory context */ /* Reset memory context */