Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lock_guard<mutex> in itkPlatformMultiThreaderWindows, ParallelSparseFieldLevelSetImageFilter, and PDEDeformable Registration #4180

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Modules/Core/Common/src/itkPlatformMultiThreaderWindows.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ PlatformMultiThreader::TerminateThread(ThreadIdType WorkUnitID)
return;
}

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

WaitForSingleObject(m_SpawnedThreadProcessID[WorkUnitID], INFINITE);
CloseHandle(m_SpawnedThreadProcessID[WorkUnitID]);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -2483,11 +2483,10 @@ void
ParallelSparseFieldLevelSetImageFilter<TInputImage, TOutputImage>::SignalNeighbor(unsigned int SemaphoreArrayNumber,
ThreadIdType ThreadId)
{
ThreadData & td = m_Data[ThreadId];
td.m_Lock[SemaphoreArrayNumber].lock();
ThreadData & td = m_Data[ThreadId];
const std::lock_guard<std::mutex> lockGuard(td.m_Lock[SemaphoreArrayNumber]);
++td.m_Semaphore[SemaphoreArrayNumber];
td.m_Condition[SemaphoreArrayNumber].notify_one();
td.m_Lock[SemaphoreArrayNumber].unlock();
}

template <typename TInputImage, typename TOutputImage>
Expand Down