Skip to content

Commit

Permalink
STYLE: Use lock_guard<mutex> in PDEDeformable Registration classes
Browse files Browse the repository at this point in the history
Following C++ Core Guidelines, April 13, 2023, "Use RAII, never plain `lock()`/`unlock()`"
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp20-use-raii-never-plain-lockunlock

Also replaced raw `GlobalDataStruct` pointers by `std::unique_ptr` and removed
manual `delete globalData` statements from `ReleaseGlobalDataPointer` member
functions.
  • Loading branch information
N-Dekker authored and dzenanz committed Sep 1, 2023
1 parent 5e1f0cf commit 873a724
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ template <typename TFixedImage, typename TMovingImage, typename TDisplacementFie
void
GPUDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::ReleaseGlobalDataPointer(void * gd) const
{
auto * globalData = (GlobalDataStruct *)gd;
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

m_MetricCalculationLock.lock();
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand All @@ -385,9 +385,6 @@ GPUDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::Re
m_Metric = m_SumOfSquaredDifference / static_cast<double>(m_NumberOfPixelsProcessed);
m_RMSChange = std::sqrt(m_SumOfSquaredChange / static_cast<double>(m_NumberOfPixelsProcessed));
}
m_MetricCalculationLock.unlock();

delete globalData;
}

} // end namespace itk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ template <typename TFixedImage, typename TMovingImage, typename TDisplacementFie
void
DemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::ReleaseGlobalDataPointer(void * gd) const
{
auto * globalData = (GlobalDataStruct *)gd;
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

m_MetricCalculationLock.lock();
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand All @@ -246,9 +246,6 @@ DemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::Relea
m_Metric = m_SumOfSquaredDifference / static_cast<double>(m_NumberOfPixelsProcessed);
m_RMSChange = std::sqrt(m_SumOfSquaredChange / static_cast<double>(m_NumberOfPixelsProcessed));
}
m_MetricCalculationLock.unlock();

delete globalData;
}
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ template <typename TFixedImage, typename TMovingImage, typename TDisplacementFie
void
ESMDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::ReleaseGlobalDataPointer(void * gd) const
{
auto * globalData = (GlobalDataStruct *)gd;
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

m_MetricCalculationLock.lock();
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand All @@ -418,9 +418,6 @@ ESMDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::Re
m_Metric = m_SumOfSquaredDifference / static_cast<double>(m_NumberOfPixelsProcessed);
m_RMSChange = std::sqrt(m_SumOfSquaredChange / static_cast<double>(m_NumberOfPixelsProcessed));
}
m_MetricCalculationLock.unlock();

delete globalData;
}
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ void
FastSymmetricForcesDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::ReleaseGlobalDataPointer(
void * gd) const
{
auto * globalData = (GlobalDataStruct *)gd;
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

m_MetricCalculationLock.lock();
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand All @@ -271,9 +271,6 @@ FastSymmetricForcesDemonsRegistrationFunction<TFixedImage, TMovingImage, TDispla
m_Metric = m_SumOfSquaredDifference / static_cast<double>(m_NumberOfPixelsProcessed);
m_RMSChange = std::sqrt(m_SumOfSquaredChange / static_cast<double>(m_NumberOfPixelsProcessed));
}
m_MetricCalculationLock.unlock();

delete globalData;
}
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ void
LevelSetMotionRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::ReleaseGlobalDataPointer(
void * gd) const
{
auto * globalData = (GlobalDataStruct *)gd;
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

m_MetricCalculationLock.lock();
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand All @@ -386,9 +386,6 @@ LevelSetMotionRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField
m_Metric = m_SumOfSquaredDifference / static_cast<double>(m_NumberOfPixelsProcessed);
m_RMSChange = std::sqrt(m_SumOfSquaredChange / static_cast<double>(m_NumberOfPixelsProcessed));
}
m_MetricCalculationLock.unlock();

delete globalData;
}
} // end namespace itk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ void
SymmetricForcesDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplacementField>::ReleaseGlobalDataPointer(
void * gd) const
{
auto * globalData = (GlobalDataStruct *)gd;
const std::unique_ptr<const GlobalDataStruct> globalData(static_cast<GlobalDataStruct *>(gd));

m_MetricCalculationLock.lock();
const std::lock_guard<std::mutex> lockGuard(m_MetricCalculationLock);
m_SumOfSquaredDifference += globalData->m_SumOfSquaredDifference;
m_NumberOfPixelsProcessed += globalData->m_NumberOfPixelsProcessed;
m_SumOfSquaredChange += globalData->m_SumOfSquaredChange;
Expand All @@ -297,9 +297,6 @@ SymmetricForcesDemonsRegistrationFunction<TFixedImage, TMovingImage, TDisplaceme
m_Metric = m_SumOfSquaredDifference / static_cast<double>(m_NumberOfPixelsProcessed);
m_RMSChange = std::sqrt(m_SumOfSquaredChange / static_cast<double>(m_NumberOfPixelsProcessed));
}
m_MetricCalculationLock.unlock();

delete globalData;
}
} // end namespace itk
#endif

0 comments on commit 873a724

Please sign in to comment.