agora inbox for [email protected]help / color / mirror / Atom feed
[PATCH v1] Minimal fix for WAIT FOR ... MODE 'standby_flush' 194+ messages / 2 participants [nested] [flat]
* [PATCH v1] Minimal fix for WAIT FOR ... MODE 'standby_flush' @ 2026-04-07 13:11 Andres Freund <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Andres Freund @ 2026-04-07 13:11 UTC (permalink / raw) The investigation into the negative test performance impact of 7e8aeb9e483 lead to discovering that there are a few issues with WAIT FOR. This commit is just a minimal fix to prevent hangs in standby_flush mode, due to WAIT FOR ... 'standby_flush' seeing a 0 LSN if a newly started walreceiver does not receive any writes, because the stanby is already caught up. There are several other issues and this is isn't necessarily the best fix. But this way we get the hangs out of the way. Reported-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/zqbppucpmkeqecfy4s5kscnru4tbk6khp3ozqz6ad2zijz354k@w4bdf4z3wqoz --- src/backend/replication/walreceiver.c | 2 -- src/backend/replication/walreceiverfuncs.c | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index a437273cf9a..09fde92bfd7 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -242,8 +242,6 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) SpinLockRelease(&walrcv->mutex); - pg_atomic_write_u64(&WalRcv->writtenUpto, 0); - /* Arrange to clean up at walreceiver exit */ on_shmem_exit(WalRcvDie, PointerGetDatum(&startpointTLI)); diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c index 4e03e721872..bd5d47be964 100644 --- a/src/backend/replication/walreceiverfuncs.c +++ b/src/backend/replication/walreceiverfuncs.c @@ -321,6 +321,7 @@ RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo, walrcv->flushedUpto = recptr; walrcv->receivedTLI = tli; walrcv->latestChunkStart = recptr; + pg_atomic_write_u64(&walrcv->writtenUpto, recptr); } walrcv->receiveStart = recptr; walrcv->receiveStartTLI = tli; -- 2.53.0.1.gb2826b52eb --ags3ltqh73mz7p2n-- ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
* [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic @ 2026-07-09 19:53 Nathan Bossart <[email protected]> 0 siblings, 0 replies; 194+ messages in thread From: Nathan Bossart @ 2026-07-09 19:53 UTC (permalink / raw) --- src/backend/storage/file/sharedfileset.c | 27 +++++++++--------------- src/include/storage/sharedfileset.h | 5 ++--- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/storage/file/sharedfileset.c b/src/backend/storage/file/sharedfileset.c index d76bd72dc63..4f12f92beae 100644 --- a/src/backend/storage/file/sharedfileset.c +++ b/src/backend/storage/file/sharedfileset.c @@ -38,8 +38,7 @@ void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) { /* Initialize the shared fileset specific members. */ - SpinLockInit(&fileset->mutex); - fileset->refcnt = 1; + pg_atomic_init_u32(&fileset->refcnt, 1); /* Initialize the fileset. */ FileSetInit(&fileset->fs); @@ -55,19 +54,15 @@ SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg) { - bool success; + uint32 refcnt; - SpinLockAcquire(&fileset->mutex); - if (fileset->refcnt == 0) - success = false; - else - { - ++fileset->refcnt; - success = true; - } - SpinLockRelease(&fileset->mutex); + refcnt = pg_atomic_read_u32(&fileset->refcnt); + while (refcnt != 0 && + !pg_atomic_compare_exchange_u32(&fileset->refcnt, &refcnt, + refcnt + 1)) + ; - if (!success) + if (refcnt == 0) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("could not attach to a SharedFileSet that is already destroyed"))); @@ -98,11 +93,9 @@ SharedFileSetOnDetach(dsm_segment *segment, Datum datum) bool unlink_all = false; SharedFileSet *fileset = (SharedFileSet *) DatumGetPointer(datum); - SpinLockAcquire(&fileset->mutex); - Assert(fileset->refcnt > 0); - if (--fileset->refcnt == 0) + Assert(pg_atomic_read_u32(&fileset->refcnt) > 0); + if (pg_atomic_sub_fetch_u32(&fileset->refcnt, 1) == 0) unlink_all = true; - SpinLockRelease(&fileset->mutex); /* * If we are the last to detach, we delete the directory in all diff --git a/src/include/storage/sharedfileset.h b/src/include/storage/sharedfileset.h index 904396e7173..d89626ae64b 100644 --- a/src/include/storage/sharedfileset.h +++ b/src/include/storage/sharedfileset.h @@ -15,10 +15,10 @@ #ifndef SHAREDFILESET_H #define SHAREDFILESET_H +#include "port/atomics.h" #include "storage/dsm.h" #include "storage/fd.h" #include "storage/fileset.h" -#include "storage/spin.h" /* * A set of temporary files that can be shared by multiple backends. @@ -26,8 +26,7 @@ typedef struct SharedFileSet { FileSet fs; - slock_t mutex; /* mutex protecting the reference count */ - int refcnt; /* number of attached backends */ + pg_atomic_uint32 refcnt; /* number of attached backends */ } SharedFileSet; extern void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg); -- 2.50.1 (Apple Git-155) --Ot5x3ffkWEuxilWO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0007-convert-ParallelBlockTableScanDescData-phs_-start.patch ^ permalink raw reply [nested|flat] 194+ messages in thread
end of thread, other threads:[~2026-07-09 19:53 UTC | newest] Thread overview: 194+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-04-07 13:11 [PATCH v1] Minimal fix for WAIT FOR ... MODE 'standby_flush' Andres Freund <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]> 2026-07-09 19:53 [PATCH v1 6/8] convert SharedFileSet->refcnt to an atomic Nathan Bossart <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox