Skip to content

Commit

Permalink
STYLE: Rename private and internal data members from "Lock" to "Mutex"
Browse files Browse the repository at this point in the history
Replaced the word "Lock" with "Mutex", in the identifier of private and internal
member variables that are declared as `std::mutex`:

    MersenneTwisterRandomVariateGenerator::m_InstanceLock
    FFTWGlobalConfiguration::m_Lock
    FFTWGlobalConfigurationGlobals::m_CreationLock
    FEMFactoryBase::m_CreationLock
    m_MetricCalculationLock (from RegistrationFunction classes)
    MultiThreaderBaseGlobals::globalDefaultInitializerLock
    PlatformMultiThreader::m_SpawnedThreadActiveFlagLock
    ioDefaultSplitterLock
    createImageIOLock

Follow-up to commit d0a8534 "ENH: using
standard library's mutex primitives", Dženan Zukić, October 31, 2018.
  • Loading branch information
N-Dekker authored and dzenanz committed Sep 19, 2023
1 parent 8149ba9 commit 0150e46
Show file tree
Hide file tree
Showing 24 changed files with 46 additions and 46 deletions.
Expand Up @@ -324,7 +324,7 @@ class ITKCommon_EXPORT MersenneTwisterRandomVariateGenerator : public RandomVari
CreateInstance();

// Local lock to enable concurrent access to singleton
std::mutex m_InstanceLock{};
std::mutex m_InstanceMutex{};

// Static/Global Variable need to be thread-safely accessed

Expand All @@ -337,7 +337,7 @@ class ITKCommon_EXPORT MersenneTwisterRandomVariateGenerator : public RandomVari
inline void
MersenneTwisterRandomVariateGenerator::Initialize(const IntegerType seed)
{
const std::lock_guard<std::mutex> mutexHolder(m_InstanceLock);
const std::lock_guard<std::mutex> mutexHolder(m_InstanceMutex);
this->m_Seed = seed;
// Initialize generator state with seed
// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkPlatformMultiThreader.h
Expand Up @@ -162,7 +162,7 @@ class ITKCommon_EXPORT PlatformMultiThreader : public MultiThreaderBase
/** Storage of MutexFunctions and ints used to control spawned
* threads and the spawned thread ids. */
int m_SpawnedThreadActiveFlag[ITK_MAX_THREADS]{};
std::shared_ptr<std::mutex> m_SpawnedThreadActiveFlagLock[ITK_MAX_THREADS]{};
std::shared_ptr<std::mutex> m_SpawnedThreadActiveFlagMutex[ITK_MAX_THREADS]{};
ThreadProcessIdType m_SpawnedThreadProcessID[ITK_MAX_THREADS]{};
WorkUnitInfo m_SpawnedThreadInfoArray[ITK_MAX_THREADS]{};

Expand Down
14 changes: 7 additions & 7 deletions Modules/Core/Common/src/itkMultiThreaderBase.cxx
Expand Up @@ -67,7 +67,7 @@ struct MultiThreaderBaseGlobals
// API is ever used by the developer, the developers choice is
// respected over the environmental variable.
bool GlobalDefaultThreaderTypeIsInitialized{ false };
std::mutex globalDefaultInitializerLock;
std::mutex globalDefaultInitializerMutex;

// Global value to control which threader to be used by default. First it is initialized with the default preprocessor
// definition from CMake configuration value, for compile time control of default. This initial value can be
Expand Down Expand Up @@ -114,7 +114,7 @@ MultiThreaderBase::GetGlobalDefaultUseThreadPool()
void
MultiThreaderBase::SetGlobalDefaultThreaderPrivate(ThreaderEnum threaderType)
{
// m_PimplGlobals->globalDefaultInitializerLock must be already held here!
// m_PimplGlobals->globalDefaultInitializerMutex must be already held here!

m_PimplGlobals->m_GlobalDefaultThreader = threaderType;
m_PimplGlobals->GlobalDefaultThreaderTypeIsInitialized = true;
Expand All @@ -126,15 +126,15 @@ MultiThreaderBase::SetGlobalDefaultThreader(ThreaderEnum threaderType)
itkInitGlobalsMacro(PimplGlobals);

// Acquire mutex then call private method to do the real work.
const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerLock);
const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerMutex);

MultiThreaderBase::SetGlobalDefaultThreaderPrivate(threaderType);
}

MultiThreaderBase::ThreaderEnum
MultiThreaderBase::GetGlobalDefaultThreaderPrivate()
{
// m_PimplGlobals->globalDefaultInitializerLock must be already held here!
// m_PimplGlobals->globalDefaultInitializerMutex must be already held here!

if (!m_PimplGlobals->GlobalDefaultThreaderTypeIsInitialized)
{
Expand Down Expand Up @@ -182,7 +182,7 @@ MultiThreaderBase::GetGlobalDefaultThreader()
itkInitGlobalsMacro(PimplGlobals);

// Acquire mutex then call private method to do the real work.
const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerLock);
const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerMutex);

return MultiThreaderBase::GetGlobalDefaultThreaderPrivate();
}
Expand Down Expand Up @@ -233,7 +233,7 @@ MultiThreaderBase::SetGlobalDefaultNumberOfThreads(ThreadIdType val)
{
itkInitGlobalsMacro(PimplGlobals);

const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerLock);
const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerMutex);

m_PimplGlobals->m_GlobalDefaultNumberOfThreads =
std::clamp<ThreadIdType>(val, 1, m_PimplGlobals->m_GlobalMaximumNumberOfThreads);
Expand Down Expand Up @@ -263,7 +263,7 @@ MultiThreaderBase::GetGlobalDefaultNumberOfThreads()
{
itkInitGlobalsMacro(PimplGlobals);

const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerLock);
const std::lock_guard<std::mutex> lock(m_PimplGlobals->globalDefaultInitializerMutex);

if (m_PimplGlobals->m_GlobalDefaultNumberOfThreads == 0) // need to initialize
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/src/itkPlatformMultiThreader.cxx
Expand Up @@ -57,7 +57,7 @@ PlatformMultiThreader::PlatformMultiThreader()
#endif

m_SpawnedThreadActiveFlag[i] = 0;
m_SpawnedThreadActiveFlagLock[i] = nullptr;
m_SpawnedThreadActiveFlagMutex[i] = nullptr;
m_SpawnedThreadInfoArray[i].WorkUnitID = i;
}
}
Expand Down
12 changes: 6 additions & 6 deletions Modules/Core/Common/src/itkPlatformMultiThreaderPosix.cxx
Expand Up @@ -109,11 +109,11 @@ PlatformMultiThreader::SpawnThread(ThreadFunctionType f, void * UserData)

for (; id < ITK_MAX_THREADS; ++id)
{
if (!m_SpawnedThreadActiveFlagLock[id])
if (!m_SpawnedThreadActiveFlagMutex[id])
{
m_SpawnedThreadActiveFlagLock[id] = std::make_shared<std::mutex>();
m_SpawnedThreadActiveFlagMutex[id] = std::make_shared<std::mutex>();
}
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[id]);
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagMutex[id]);

if (m_SpawnedThreadActiveFlag[id] == 0)
{
Expand All @@ -131,7 +131,7 @@ PlatformMultiThreader::SpawnThread(ThreadFunctionType f, void * UserData)
m_SpawnedThreadInfoArray[id].UserData = UserData;
m_SpawnedThreadInfoArray[id].NumberOfWorkUnits = 1;
m_SpawnedThreadInfoArray[id].ActiveFlag = &m_SpawnedThreadActiveFlag[id];
m_SpawnedThreadInfoArray[id].ActiveFlagLock = m_SpawnedThreadActiveFlagLock[id];
m_SpawnedThreadInfoArray[id].ActiveFlagLock = m_SpawnedThreadActiveFlagMutex[id];

pthread_attr_t attr;

Expand Down Expand Up @@ -161,13 +161,13 @@ PlatformMultiThreader::TerminateThread(ThreadIdType WorkUnitID)
}

{
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[WorkUnitID]);
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagMutex[WorkUnitID]);
m_SpawnedThreadActiveFlag[WorkUnitID] = 0;
}

pthread_join(m_SpawnedThreadProcessID[WorkUnitID], nullptr);

m_SpawnedThreadActiveFlagLock[WorkUnitID] = nullptr;
m_SpawnedThreadActiveFlagMutex[WorkUnitID] = nullptr;
}
#endif

Expand Down
12 changes: 6 additions & 6 deletions Modules/Core/Common/src/itkPlatformMultiThreaderWindows.cxx
Expand Up @@ -104,11 +104,11 @@ PlatformMultiThreader::SpawnThread(ThreadFunctionType f, void * UserData)

for (; id < ITK_MAX_THREADS; ++id)
{
if (!m_SpawnedThreadActiveFlagLock[id])
if (!m_SpawnedThreadActiveFlagMutex[id])
{
m_SpawnedThreadActiveFlagLock[id] = std::make_shared<std::mutex>();
m_SpawnedThreadActiveFlagMutex[id] = std::make_shared<std::mutex>();
}
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[id]);
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagMutex[id]);

if (m_SpawnedThreadActiveFlag[id] == 0)
{
Expand All @@ -126,7 +126,7 @@ PlatformMultiThreader::SpawnThread(ThreadFunctionType f, void * UserData)
m_SpawnedThreadInfoArray[id].UserData = UserData;
m_SpawnedThreadInfoArray[id].NumberOfWorkUnits = 1;
m_SpawnedThreadInfoArray[id].ActiveFlag = &m_SpawnedThreadActiveFlag[id];
m_SpawnedThreadInfoArray[id].ActiveFlagLock = m_SpawnedThreadActiveFlagLock[id];
m_SpawnedThreadInfoArray[id].ActiveFlagLock = m_SpawnedThreadActiveFlagMutex[id];

// Using _beginthreadex on a PC
//
Expand All @@ -148,13 +148,13 @@ PlatformMultiThreader::TerminateThread(ThreadIdType WorkUnitID)
}

{
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagLock[WorkUnitID]);
const std::lock_guard<std::mutex> lockGuard(*m_SpawnedThreadActiveFlagMutex[WorkUnitID]);
m_SpawnedThreadActiveFlag[WorkUnitID] = 0;
}

WaitForSingleObject(m_SpawnedThreadProcessID[WorkUnitID], INFINITE);
CloseHandle(m_SpawnedThreadProcessID[WorkUnitID]);
m_SpawnedThreadActiveFlagLock[WorkUnitID] = nullptr;
m_SpawnedThreadActiveFlagMutex[WorkUnitID] = nullptr;
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Modules/Filtering/FFT/include/itkFFTWGlobalConfiguration.h
Expand Up @@ -367,7 +367,7 @@ class ITKFFT_EXPORT FFTWGlobalConfiguration : public Object

static FFTWGlobalConfigurationGlobals * m_PimplGlobals;

std::mutex m_Lock;
std::mutex m_Mutex;
bool m_NewWisdomAvailable{ false };
int m_PlanRigor{ 0 };
bool m_WriteWisdomCache{ false };
Expand Down
6 changes: 3 additions & 3 deletions Modules/Filtering/FFT/src/itkFFTWGlobalConfiguration.cxx
Expand Up @@ -48,7 +48,7 @@ struct FFTWGlobalConfigurationGlobals
: m_Instance(nullptr){};

FFTWGlobalConfiguration::Pointer m_Instance;
std::mutex m_CreationLock;
std::mutex m_CreationMutex;
};

WisdomFilenameGeneratorBase::WisdomFilenameGeneratorBase() = default;
Expand Down Expand Up @@ -148,7 +148,7 @@ FFTWGlobalConfiguration::GetInstance()
itkInitGlobalsMacro(PimplGlobals);
if (!m_PimplGlobals->m_Instance)
{
const std::lock_guard<std::mutex> lockGuard(m_PimplGlobals->m_CreationLock);
const std::lock_guard<std::mutex> lockGuard(m_PimplGlobals->m_CreationMutex);
// Need to make sure that during gaining access
// to the lock that some other thread did not
// initialize the singleton.
Expand Down Expand Up @@ -734,7 +734,7 @@ FFTWGlobalConfiguration::ExportWisdomFileDouble(const std::string &
std::mutex &
FFTWGlobalConfiguration::GetLockMutex()
{
return GetInstance()->m_Lock;
return GetInstance()->m_Mutex;
}

void
Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/ImageBase/src/itkImageIOBase.cxx
Expand Up @@ -937,7 +937,7 @@ ImageIOBase::ReadBufferAsASCII(std::istream & is, void * buffer, IOComponentEnum

namespace
{
std::mutex ioDefaultSplitterLock;
std::mutex ioDefaultSplitterMutex;
ImageRegionSplitterBase::Pointer ioDefaultSplitter;

} // namespace
Expand All @@ -949,7 +949,7 @@ ImageIOBase::GetImageRegionSplitter() const
{
// thread safe lazy initialization, prevent race condition on
// setting, with an atomic set if null.
const std::lock_guard<std::mutex> lock(ioDefaultSplitterLock);
const std::lock_guard<std::mutex> lock(ioDefaultSplitterMutex);
if (ioDefaultSplitter.IsNull())
{
ioDefaultSplitter = ImageRegionSplitterSlowDimension::New().GetPointer();
Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/ImageBase/src/itkImageIOFactory.cxx
Expand Up @@ -26,15 +26,15 @@ namespace itk

namespace
{
std::mutex createImageIOLock;
std::mutex createImageIOMutex;
}

ImageIOBase::Pointer
ImageIOFactory::CreateImageIO(const char * path, IOFileModeEnum mode)
{
std::list<ImageIOBase::Pointer> possibleImageIO;

const std::lock_guard<std::mutex> mutexHolder(createImageIOLock);
const std::lock_guard<std::mutex> mutexHolder(createImageIOMutex);

for (auto & allobject : ObjectFactoryBase::CreateAllInstance("itkImageIOBase"))
{
Expand Down
4 changes: 2 additions & 2 deletions Modules/Numerics/FEM/include/itkFEMFactoryBase.h
Expand Up @@ -75,7 +75,7 @@ class ITKFEM_EXPORT FEMFactoryBase : public ObjectFactoryBase
{
if (m_Factory == nullptr)
{
const std::lock_guard<std::mutex> lockGuard(m_CreationLock);
const std::lock_guard<std::mutex> lockGuard(m_CreationMutex);
// Need to make sure that during gaining access
// to the lock that some other thread did not
// initialize the singleton.
Expand Down Expand Up @@ -110,7 +110,7 @@ class ITKFEM_EXPORT FEMFactoryBase : public ObjectFactoryBase
~FEMFactoryBase() override;

private:
static std::mutex m_CreationLock;
static std::mutex m_CreationMutex;
static FEMFactoryBase * m_Factory;
};
} // end namespace itk
Expand Down
2 changes: 1 addition & 1 deletion Modules/Numerics/FEM/src/itkFEMFactoryBase.cxx
Expand Up @@ -42,7 +42,7 @@
namespace itk
{
FEMFactoryBase * FEMFactoryBase::m_Factory = nullptr;
std::mutex FEMFactoryBase::m_CreationLock;
std::mutex FEMFactoryBase::m_CreationMutex;

FEMFactoryBase::FEMFactoryBase() = default;

Expand Down
Expand Up @@ -292,7 +292,7 @@ class ITK_TEMPLATE_EXPORT GPUDemonsRegistrationFunction
mutable GPUReduction<float>::Pointer m_GPUSquaredDifference{};

/** Mutex lock to protect modification to metric. */
mutable std::mutex m_MetricCalculationLock{};
mutable std::mutex m_MetricCalculationMutex{};
};
} // end namespace itk

Expand Down
Expand Up @@ -376,7 +376,7 @@ GPUDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::Re
{
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationMutex);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand Down
Expand Up @@ -258,7 +258,7 @@ class ITK_TEMPLATE_EXPORT DemonsRegistrationFunction
mutable double m_SumOfSquaredChange{};

/** Mutex lock to protect modification to metric. */
mutable std::mutex m_MetricCalculationLock{};
mutable std::mutex m_MetricCalculationMutex{};
};
} // end namespace itk

Expand Down
Expand Up @@ -237,7 +237,7 @@ DemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::Relea
{
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationMutex);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand Down
Expand Up @@ -323,7 +323,7 @@ class ITK_TEMPLATE_EXPORT ESMDemonsRegistrationFunction
mutable double m_SumOfSquaredChange{};

/** Mutex lock to protect modification to metric. */
mutable std::mutex m_MetricCalculationLock{};
mutable std::mutex m_MetricCalculationMutex{};
};
} // end namespace itk

Expand Down
Expand Up @@ -409,7 +409,7 @@ ESMDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::Re
{
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationMutex);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand Down
Expand Up @@ -242,7 +242,7 @@ class ITK_TEMPLATE_EXPORT FastSymmetricForcesDemonsRegistrationFunction
mutable double m_SumOfSquaredChange{};

/** Mutex lock to protect modification to metric. */
mutable std::mutex m_MetricCalculationLock{};
mutable std::mutex m_MetricCalculationMutex{};
};
} // end namespace itk

Expand Down
Expand Up @@ -262,7 +262,7 @@ FastSymmetricForcesDemonsRegistrationFunction<TFixedImage, TMovingImage, TDispla
{
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationMutex);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand Down
Expand Up @@ -282,7 +282,7 @@ class ITK_TEMPLATE_EXPORT LevelSetMotionRegistrationFunction
mutable double m_SumOfSquaredChange{};

/** Mutex lock to protect modification to metric. */
mutable std::mutex m_MetricCalculationLock{};
mutable std::mutex m_MetricCalculationMutex{};

bool m_UseImageSpacing{};
};
Expand Down
Expand Up @@ -377,7 +377,7 @@ LevelSetMotionRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField
{
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationMutex);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand Down
Expand Up @@ -243,7 +243,7 @@ class ITK_TEMPLATE_EXPORT SymmetricForcesDemonsRegistrationFunction
mutable double m_SumOfSquaredChange{};

/** Mutex lock to protect modification to metric. */
mutable std::mutex m_MetricCalculationLock{};
mutable std::mutex m_MetricCalculationMutex{};
};
} // end namespace itk

Expand Down
Expand Up @@ -288,7 +288,7 @@ SymmetricForcesDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplaceme
{
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationMutex);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand Down

0 comments on commit 0150e46

Please sign in to comment.