Skip to content

Commit

Permalink
WIP: Use call_once for dynamic loading ObjectFactoryBase::Initialize()
Browse files Browse the repository at this point in the history
Removed `ObjectFactoryBasePrivate::m_Initialized`. Moved `RegisterInternal()`
call from `ObjectFactoryBase::Initialize()` to the default-constructor of
`ObjectFactoryBasePrivate`.
  • Loading branch information
N-Dekker committed Sep 22, 2023
1 parent b8c3ac2 commit 8e44857
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
3 changes: 1 addition & 2 deletions Modules/Core/Common/include/itkObjectFactoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
28 changes: 10 additions & 18 deletions Modules/Core/Common/src/itkObjectFactoryBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "itksys/SystemTools.hxx"
#include <cstring>
#include <algorithm>
#include <atomic>
#include <mutex>

namespace
{
Expand Down Expand Up @@ -119,12 +119,14 @@ class ObjectFactoryBase::ObjectFactoryBasePrivate : public LightObject
}
}

ObjectFactoryBasePrivate() = default;
ObjectFactoryBasePrivate() { ObjectFactoryBase::RegisterInternal(); }

FactoryListType m_RegisteredFactories{};
FactoryListType m_InternalFactories{};
std::atomic<bool> 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
Expand Down Expand Up @@ -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
}
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -706,7 +699,6 @@ ObjectFactoryBase::UnRegisterAllFactories()
}
#endif
m_PimplGlobals->m_RegisteredFactories.clear();
m_PimplGlobals->m_Initialized = false;
}

/**
Expand Down

0 comments on commit 8e44857

Please sign in to comment.