kernel: avoid potential duplicate object in shared library/binary

Fixes warning and potential bug whereby init_flag may exist in both
libbitcoinkernel as well as a downstream user, as opposed to being shared as
intended.

src/support/lockedpool.h:224:31:
warning: 'init_flag' may be duplicated when built into a shared library: it is mutable, has hidden visibility, and external linkage [-Wunique-object-duplication]
This commit is contained in:
Cory Fields 2025-02-05 18:27:23 +00:00
parent 5ca103aac6
commit 763c9ddfab
2 changed files with 8 additions and 6 deletions

View file

@ -400,3 +400,10 @@ void LockedPoolManager::CreateInstance()
static LockedPoolManager instance(std::move(allocator)); static LockedPoolManager instance(std::move(allocator));
LockedPoolManager::_instance = &instance; LockedPoolManager::_instance = &instance;
} }
LockedPoolManager& LockedPoolManager::Instance()
{
static std::once_flag init_flag;
std::call_once(init_flag, LockedPoolManager::CreateInstance);
return *LockedPoolManager::_instance;
}

View file

@ -219,12 +219,7 @@ class LockedPoolManager : public LockedPool
{ {
public: public:
/** Return the current instance, or create it once */ /** Return the current instance, or create it once */
static LockedPoolManager& Instance() static LockedPoolManager& Instance();
{
static std::once_flag init_flag;
std::call_once(init_flag, LockedPoolManager::CreateInstance);
return *LockedPoolManager::_instance;
}
private: private:
explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator); explicit LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator);