From 46e58b136efba34ccad50f522877c87b5ed890c1 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 2 Jun 2020 07:04:10 -0400 Subject: [PATCH] Merge #19111: Limit scope of all global std::once_flag fa9c67559186f5416c1c0b26c0a1d5e72c234ccb Limit scope of all global std::once_flag (MarcoFalke) Pull request description: `once_flag` is a helper (as the name might suggest) to execute a callable only once. Thus, the scope of the flag does never need to extend beyond where the callable is called. Typically this is function scope. Move all the flags to function scope to * simplify code review * avoid mistakes where similarly named flags are accidentally exchanged * avoid polluting the global scope ACKs for top commit: hebasto: ACK fa9c67559186f5416c1c0b26c0a1d5e72c234ccb, tested on Linux Mint 19.3 (x86_64). promag: Code review ACK fa9c67559186f5416c1c0b26c0a1d5e72c234ccb. Tree-SHA512: 095a0c11d93d0ddcb82b3c71676090ecc7e3de3d5e7a2a63ab2583093be279242acac43523bbae2060b4dcfa8f92b54256a0e91fbbae78fa92d2d49e9db62e57 --- src/rpc/server.cpp | 3 +-- src/support/lockedpool.cpp | 1 - src/support/lockedpool.h | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 33f9f512c0db8d..5a25982f6212a2 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -25,8 +25,6 @@ static Mutex g_rpc_warmup_mutex; static std::atomic g_rpc_running{false}; -static std::once_flag g_rpc_interrupt_flag; -static std::once_flag g_rpc_stop_flag; static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true; static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started"; /* Timer-creating functions */ @@ -329,6 +327,7 @@ void StartRPC() void InterruptRPC() { + static std::once_flag g_rpc_interrupt_flag; // This function could be called twice if the GUI has been started with -server=1. std::call_once(g_rpc_interrupt_flag, []() { LogPrint(BCLog::RPC, "Interrupting RPC\n"); diff --git a/src/support/lockedpool.cpp b/src/support/lockedpool.cpp index bd4f64234be518..a862bf822d8bf8 100644 --- a/src/support/lockedpool.cpp +++ b/src/support/lockedpool.cpp @@ -29,7 +29,6 @@ #endif LockedPoolManager* LockedPoolManager::_instance = nullptr; -std::once_flag LockedPoolManager::init_flag; /*******************************************************************************/ // Utilities diff --git a/src/support/lockedpool.h b/src/support/lockedpool.h index de668f0773fc06..b9e2e99d1aba27 100644 --- a/src/support/lockedpool.h +++ b/src/support/lockedpool.h @@ -221,7 +221,8 @@ class LockedPoolManager : public LockedPool /** Return the current instance, or create it once */ static LockedPoolManager& Instance() { - std::call_once(LockedPoolManager::init_flag, LockedPoolManager::CreateInstance); + static std::once_flag init_flag; + std::call_once(init_flag, LockedPoolManager::CreateInstance); return *LockedPoolManager::_instance; } @@ -234,7 +235,6 @@ class LockedPoolManager : public LockedPool static bool LockingFailed(); static LockedPoolManager* _instance; - static std::once_flag init_flag; }; #endif // BITCOIN_SUPPORT_LOCKEDPOOL_H