Skip to content

Commit 32f717c

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#19111: Limit scope of all global std::once_flag
fa9c675 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 fa9c675, tested on Linux Mint 19.3 (x86_64). promag: Code review ACK fa9c675. Tree-SHA512: 095a0c11d93d0ddcb82b3c71676090ecc7e3de3d5e7a2a63ab2583093be279242acac43523bbae2060b4dcfa8f92b54256a0e91fbbae78fa92d2d49e9db62e57
1 parent 7a6667f commit 32f717c

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

src/rpc/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
static Mutex g_rpc_warmup_mutex;
2727
static std::atomic<bool> g_rpc_running{false};
28-
static std::once_flag g_rpc_interrupt_flag;
29-
static std::once_flag g_rpc_stop_flag;
3028
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
3129
static std::string rpcWarmupStatus GUARDED_BY(g_rpc_warmup_mutex) = "RPC server started";
3230
/* Timer-creating functions */
@@ -329,6 +327,7 @@ void StartRPC()
329327

330328
void InterruptRPC()
331329
{
330+
static std::once_flag g_rpc_interrupt_flag;
332331
// This function could be called twice if the GUI has been started with -server=1.
333332
std::call_once(g_rpc_interrupt_flag, []() {
334333
LogPrint(BCLog::RPC, "Interrupting RPC\n");
@@ -339,6 +338,7 @@ void InterruptRPC()
339338

340339
void StopRPC()
341340
{
341+
static std::once_flag g_rpc_stop_flag;
342342
// This function could be called twice if the GUI has been started with -server=1.
343343
assert(!g_rpc_running);
344344
std::call_once(g_rpc_stop_flag, []() {

src/support/lockedpool.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#endif
3030

3131
LockedPoolManager* LockedPoolManager::_instance = nullptr;
32-
std::once_flag LockedPoolManager::init_flag;
3332

3433
/*******************************************************************************/
3534
// Utilities

src/support/lockedpool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ class LockedPoolManager : public LockedPool
221221
/** Return the current instance, or create it once */
222222
static LockedPoolManager& Instance()
223223
{
224-
std::call_once(LockedPoolManager::init_flag, LockedPoolManager::CreateInstance);
224+
static std::once_flag init_flag;
225+
std::call_once(init_flag, LockedPoolManager::CreateInstance);
225226
return *LockedPoolManager::_instance;
226227
}
227228

@@ -234,7 +235,6 @@ class LockedPoolManager : public LockedPool
234235
static bool LockingFailed();
235236

236237
static LockedPoolManager* _instance;
237-
static std::once_flag init_flag;
238238
};
239239

240240
#endif // BITCOIN_SUPPORT_LOCKEDPOOL_H

0 commit comments

Comments
 (0)