Skip to content

Commit 3179701

Browse files
Convert archiver's force_dir_scan variable to an atomic variable.
Commit bd5132d introduced new atomic read/write functions with full barrier semantics, which are intended to simplify converting non-performance-critical code to use atomic variables. This commit demonstrates one such conversion. Reviewed-by: Yong Li Discussion: https://postgr.es/m/20231110205128.GB1315705%40nathanxps13
1 parent bd5132d commit 3179701

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

src/backend/postmaster/pgarch.c

+5-16
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include "storage/proc.h"
4646
#include "storage/procsignal.h"
4747
#include "storage/shmem.h"
48-
#include "storage/spin.h"
4948
#include "utils/guc.h"
5049
#include "utils/memutils.h"
5150
#include "utils/ps_status.h"
@@ -83,11 +82,9 @@ typedef struct PgArchData
8382
int pgprocno; /* pgprocno of archiver process */
8483

8584
/*
86-
* Forces a directory scan in pgarch_readyXlog(). Protected by arch_lck.
85+
* Forces a directory scan in pgarch_readyXlog().
8786
*/
88-
bool force_dir_scan;
89-
90-
slock_t arch_lck;
87+
pg_atomic_uint32 force_dir_scan;
9188
} PgArchData;
9289

9390
char *XLogArchiveLibrary = "";
@@ -174,7 +171,7 @@ PgArchShmemInit(void)
174171
/* First time through, so initialize */
175172
MemSet(PgArch, 0, PgArchShmemSize());
176173
PgArch->pgprocno = INVALID_PGPROCNO;
177-
SpinLockInit(&PgArch->arch_lck);
174+
pg_atomic_init_u32(&PgArch->force_dir_scan, 0);
178175
}
179176
}
180177

@@ -545,18 +542,12 @@ pgarch_readyXlog(char *xlog)
545542
char XLogArchiveStatusDir[MAXPGPATH];
546543
DIR *rldir;
547544
struct dirent *rlde;
548-
bool force_dir_scan;
549545

550546
/*
551547
* If a directory scan was requested, clear the stored file names and
552548
* proceed.
553549
*/
554-
SpinLockAcquire(&PgArch->arch_lck);
555-
force_dir_scan = PgArch->force_dir_scan;
556-
PgArch->force_dir_scan = false;
557-
SpinLockRelease(&PgArch->arch_lck);
558-
559-
if (force_dir_scan)
550+
if (pg_atomic_exchange_u32(&PgArch->force_dir_scan, 0) == 1)
560551
arch_files->arch_files_size = 0;
561552

562553
/*
@@ -707,9 +698,7 @@ ready_file_comparator(Datum a, Datum b, void *arg)
707698
void
708699
PgArchForceDirScan(void)
709700
{
710-
SpinLockAcquire(&PgArch->arch_lck);
711-
PgArch->force_dir_scan = true;
712-
SpinLockRelease(&PgArch->arch_lck);
701+
pg_atomic_write_membarrier_u32(&PgArch->force_dir_scan, 1);
713702
}
714703

715704
/*

0 commit comments

Comments
 (0)