diff options
author | Robert Haas <rhaas@postgresql.org> | 2022-02-08 15:52:40 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2022-02-08 15:53:19 -0500 |
commit | aa64f23b02924724eafbd9eadbf26d85df30a12b (patch) | |
tree | 54f72bfd7e36c2e879dc87149eb94db220d1cae3 /src/backend/storage/ipc/procsignal.c | |
parent | 2da896182ce11240774af6c4d769777f90a09536 (diff) | |
download | postgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.tar.gz postgresql-aa64f23b02924724eafbd9eadbf26d85df30a12b.zip |
Remove MaxBackends variable in favor of GetMaxBackends() function.
Previously, it was really easy to write code that accessed MaxBackends
before we'd actually initialized it, especially when coding up an
extension. To make this less error-prune, introduce a new function
GetMaxBackends() which should be used to obtain the correct value.
This will ERROR if called too early. Demote the global variable to
a file-level static, so that nobody can peak at it directly.
Nathan Bossart. Idea by Andres Freund. Review by Greg Sabino Mullane,
by Michael Paquier (who had doubts about the approach), and by me.
Discussion: http://postgr.es/m/20210802224204.bckcikl45uezv5e4@alap3.anarazel.de
Diffstat (limited to 'src/backend/storage/ipc/procsignal.c')
-rw-r--r-- | src/backend/storage/ipc/procsignal.c | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c index f1c8ff8f9e4..d158bb7a19f 100644 --- a/src/backend/storage/ipc/procsignal.c +++ b/src/backend/storage/ipc/procsignal.c @@ -80,13 +80,6 @@ typedef struct ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER]; } ProcSignalHeader; -/* - * We reserve a slot for each possible BackendId, plus one for each - * possible auxiliary process type. (This scheme assumes there is not - * more than one of any auxiliary process type at a time.) - */ -#define NumProcSignalSlots (MaxBackends + NUM_AUXPROCTYPES) - /* Check whether the relevant type bit is set in the flags. */ #define BARRIER_SHOULD_CHECK(flags, type) \ (((flags) & (((uint32) 1) << (uint32) (type))) != 0) @@ -102,6 +95,20 @@ static bool CheckProcSignal(ProcSignalReason reason); static void CleanupProcSignalState(int status, Datum arg); static void ResetProcSignalBarrierBits(uint32 flags); static bool ProcessBarrierPlaceholder(void); +static inline int GetNumProcSignalSlots(void); + +/* + * GetNumProcSignalSlots + * + * We reserve a slot for each possible BackendId, plus one for each possible + * auxiliary process type. (This scheme assume there is not more than one of + * any auxiliary process type at a time.) + */ +static inline int +GetNumProcSignalSlots(void) +{ + return GetMaxBackends() + NUM_AUXPROCTYPES; +} /* * ProcSignalShmemSize @@ -112,7 +119,7 @@ ProcSignalShmemSize(void) { Size size; - size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot)); + size = mul_size(GetNumProcSignalSlots(), sizeof(ProcSignalSlot)); size = add_size(size, offsetof(ProcSignalHeader, psh_slot)); return size; } @@ -126,6 +133,7 @@ ProcSignalShmemInit(void) { Size size = ProcSignalShmemSize(); bool found; + int numProcSignalSlots = GetNumProcSignalSlots(); ProcSignal = (ProcSignalHeader *) ShmemInitStruct("ProcSignal", size, &found); @@ -137,7 +145,7 @@ ProcSignalShmemInit(void) pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0); - for (i = 0; i < NumProcSignalSlots; ++i) + for (i = 0; i < numProcSignalSlots; ++i) { ProcSignalSlot *slot = &ProcSignal->psh_slot[i]; @@ -163,7 +171,7 @@ ProcSignalInit(int pss_idx) ProcSignalSlot *slot; uint64 barrier_generation; - Assert(pss_idx >= 1 && pss_idx <= NumProcSignalSlots); + Assert(pss_idx >= 1 && pss_idx <= GetNumProcSignalSlots()); slot = &ProcSignal->psh_slot[pss_idx - 1]; @@ -292,7 +300,7 @@ SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId) */ int i; - for (i = NumProcSignalSlots - 1; i >= 0; i--) + for (i = GetNumProcSignalSlots() - 1; i >= 0; i--) { slot = &ProcSignal->psh_slot[i]; @@ -333,6 +341,7 @@ EmitProcSignalBarrier(ProcSignalBarrierType type) { uint32 flagbit = 1 << (uint32) type; uint64 generation; + int numProcSignalSlots = GetNumProcSignalSlots(); /* * Set all the flags. @@ -342,7 +351,7 @@ EmitProcSignalBarrier(ProcSignalBarrierType type) * anything that we do afterwards. (This is also true of the later call to * pg_atomic_add_fetch_u64.) */ - for (int i = 0; i < NumProcSignalSlots; i++) + for (int i = 0; i < numProcSignalSlots; i++) { volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i]; @@ -368,7 +377,7 @@ EmitProcSignalBarrier(ProcSignalBarrierType type) * backends that need to update state - but they won't actually need to * change any state. */ - for (int i = NumProcSignalSlots - 1; i >= 0; i--) + for (int i = numProcSignalSlots - 1; i >= 0; i--) { volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i]; pid_t pid = slot->pss_pid; @@ -393,7 +402,7 @@ WaitForProcSignalBarrier(uint64 generation) { Assert(generation <= pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration)); - for (int i = NumProcSignalSlots - 1; i >= 0; i--) + for (int i = GetNumProcSignalSlots() - 1; i >= 0; i--) { ProcSignalSlot *slot = &ProcSignal->psh_slot[i]; uint64 oldval; |