Convert archiver's force_dir_scan variable to an atomic variable.

Commit bd5132db55 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
This commit is contained in:
Nathan Bossart 2024-02-29 10:17:55 -06:00
parent bd5132db55
commit 3179701505

View File

@ -45,7 +45,6 @@
#include "storage/proc.h" #include "storage/proc.h"
#include "storage/procsignal.h" #include "storage/procsignal.h"
#include "storage/shmem.h" #include "storage/shmem.h"
#include "storage/spin.h"
#include "utils/guc.h" #include "utils/guc.h"
#include "utils/memutils.h" #include "utils/memutils.h"
#include "utils/ps_status.h" #include "utils/ps_status.h"
@ -83,11 +82,9 @@ typedef struct PgArchData
int pgprocno; /* pgprocno of archiver process */ int pgprocno; /* pgprocno of archiver process */
/* /*
* Forces a directory scan in pgarch_readyXlog(). Protected by arch_lck. * Forces a directory scan in pgarch_readyXlog().
*/ */
bool force_dir_scan; pg_atomic_uint32 force_dir_scan;
slock_t arch_lck;
} PgArchData; } PgArchData;
char *XLogArchiveLibrary = ""; char *XLogArchiveLibrary = "";
@ -174,7 +171,7 @@ PgArchShmemInit(void)
/* First time through, so initialize */ /* First time through, so initialize */
MemSet(PgArch, 0, PgArchShmemSize()); MemSet(PgArch, 0, PgArchShmemSize());
PgArch->pgprocno = INVALID_PGPROCNO; PgArch->pgprocno = INVALID_PGPROCNO;
SpinLockInit(&PgArch->arch_lck); pg_atomic_init_u32(&PgArch->force_dir_scan, 0);
} }
} }
@ -545,18 +542,12 @@ pgarch_readyXlog(char *xlog)
char XLogArchiveStatusDir[MAXPGPATH]; char XLogArchiveStatusDir[MAXPGPATH];
DIR *rldir; DIR *rldir;
struct dirent *rlde; struct dirent *rlde;
bool force_dir_scan;
/* /*
* If a directory scan was requested, clear the stored file names and * If a directory scan was requested, clear the stored file names and
* proceed. * proceed.
*/ */
SpinLockAcquire(&PgArch->arch_lck); if (pg_atomic_exchange_u32(&PgArch->force_dir_scan, 0) == 1)
force_dir_scan = PgArch->force_dir_scan;
PgArch->force_dir_scan = false;
SpinLockRelease(&PgArch->arch_lck);
if (force_dir_scan)
arch_files->arch_files_size = 0; arch_files->arch_files_size = 0;
/* /*
@ -707,9 +698,7 @@ ready_file_comparator(Datum a, Datum b, void *arg)
void void
PgArchForceDirScan(void) PgArchForceDirScan(void)
{ {
SpinLockAcquire(&PgArch->arch_lck); pg_atomic_write_membarrier_u32(&PgArch->force_dir_scan, 1);
PgArch->force_dir_scan = true;
SpinLockRelease(&PgArch->arch_lck);
} }
/* /*