pgvector.git / summary / log / commit / refs
commit 520e274dde5b150b948174002fadff44a183a20f
Author: Andrew Kane <andrew@ankane.org>
Date: Mon Jan 08 06:34:41 2024 +0000
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 656f9eb7..4727ed37 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 */
[parent: 9e680884bd02]