From 8e448578a9e7406fa975d926736d6eeaf8815c82 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 21 Sep 2023 22:49:56 +0200 Subject: [PATCH] WIP: Use call_once for dynamic loading ObjectFactoryBase::Initialize() Removed `ObjectFactoryBasePrivate::m_Initialized`. Moved `RegisterInternal()` call from `ObjectFactoryBase::Initialize()` to the default-constructor of `ObjectFactoryBasePrivate`. --- .../Common/include/itkObjectFactoryBase.h | 3 +- .../Core/Common/src/itkObjectFactoryBase.cxx | 28 +++++++------------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/Modules/Core/Common/include/itkObjectFactoryBase.h b/Modules/Core/Common/include/itkObjectFactoryBase.h index ddfa46b52a2..c228f82d028 100644 --- a/Modules/Core/Common/include/itkObjectFactoryBase.h +++ b/Modules/Core/Common/include/itkObjectFactoryBase.h @@ -226,8 +226,7 @@ class ITKCommon_EXPORT ObjectFactoryBase : public Object (void)staticFactoryRegistration; } - /** Initialize the static members of ObjectFactoryBase. - * RegisterInternal() and InitializeFactoryList() are called here. */ + /** Initialize the static members of ObjectFactoryBase. */ static void Initialize(); diff --git a/Modules/Core/Common/src/itkObjectFactoryBase.cxx b/Modules/Core/Common/src/itkObjectFactoryBase.cxx index 136491b9f21..be139f3963b 100644 --- a/Modules/Core/Common/src/itkObjectFactoryBase.cxx +++ b/Modules/Core/Common/src/itkObjectFactoryBase.cxx @@ -37,7 +37,7 @@ #include "itksys/SystemTools.hxx" #include #include -#include +#include namespace { @@ -119,12 +119,14 @@ class ObjectFactoryBase::ObjectFactoryBasePrivate : public LightObject } } - ObjectFactoryBasePrivate() = default; + ObjectFactoryBasePrivate() { ObjectFactoryBase::RegisterInternal(); } - FactoryListType m_RegisteredFactories{}; - FactoryListType m_InternalFactories{}; - std::atomic m_Initialized{ false }; - bool m_StrictVersionChecking{ false }; + FactoryListType m_RegisteredFactories{}; + FactoryListType m_InternalFactories{}; +#if defined(ITK_DYNAMIC_LOADING) && !defined(ITK_WRAPPING) + std::once_flag m_DynamicLoadingOnceFlag{}; +#endif + bool m_StrictVersionChecking{ false }; }; auto @@ -228,14 +230,9 @@ ObjectFactoryBase::Initialize() { itkInitGlobalsMacro(PimplGlobals); - // Atomically set m_Initialized to true. If it was false before, enter the if. - if (!m_PimplGlobals->m_Initialized.exchange(true)) - { - ObjectFactoryBase::RegisterInternal(); #if defined(ITK_DYNAMIC_LOADING) && !defined(ITK_WRAPPING) - ObjectFactoryBase::LoadDynamicFactories(); + std::call_once(m_PimplGlobals->m_DynamicLoadingOnceFlag, [] { ObjectFactoryBase::LoadDynamicFactories(); }); #endif - } } /** @@ -512,11 +509,7 @@ ObjectFactoryBase::RegisterFactoryInternal(ObjectFactoryBase * factory) // initialization. m_PimplGlobals->m_InternalFactories.push_back(factory); factory->Register(); - // if the internal factories have already been register add this one too - if (m_PimplGlobals->m_Initialized) - { - m_PimplGlobals->m_RegisteredFactories.push_back(factory); - } + m_PimplGlobals->m_RegisteredFactories.push_back(factory); } /** @@ -706,7 +699,6 @@ ObjectFactoryBase::UnRegisterAllFactories() } #endif m_PimplGlobals->m_RegisteredFactories.clear(); - m_PimplGlobals->m_Initialized = false; } /**