From 520e274dde5b150b948174002fadff44a183a20f Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 7 Jan 2024 22:34:41 -0800 Subject: [PATCH] Improved locking code --- src/hnswbuild.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/hnswbuild.c b/src/hnswbuild.c index 656f9eb..4727ed3 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -390,6 +390,26 @@ InsertTupleInMemory(Relation index, Datum *values, ItemPointer heaptid, HnswBuil 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 */ @@ -414,8 +434,7 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values, /* Flush pages if needed */ if (!graph->flushed && graph->memoryUsed >= graph->memoryTotal) { - if (hnswshared) - SpinLockAcquire(&hnswshared->mutex); + HnswLockAcquire(hnswshared); if (!hnswshared) ereport(NOTICE, @@ -426,8 +445,7 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values, if (!graph->flushed) FlushPages(buildstate); - if (hnswshared) - SpinLockRelease(&hnswshared->mutex); + HnswLockRelease(hnswshared); } oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx); @@ -441,13 +459,9 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values, /* Update progress */ if (inserted) { - if (hnswshared) - SpinLockAcquire(&hnswshared->mutex); - + HnswLockAcquire(hnswshared); UpdateProgress(PROGRESS_CREATEIDX_TUPLES_DONE, ++graph->indtuples); - - if (hnswshared) - SpinLockRelease(&hnswshared->mutex); + HnswLockRelease(hnswshared); } /* Reset memory context */