From: tbaudier Date: Fri, 15 Feb 2019 15:43:37 +0000 (+0100) Subject: Change itkSimpleFastMutexLock to std::mutex X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=1c6241402b25efea0052f0464be9a494e761b19e;p=clitk.git Change itkSimpleFastMutexLock to std::mutex This is mandatory with ITK5 and c++11 The filters created an itk::Image but this procedure is not possible with itk::Image (cannot copy a mutex) So I created a mutex alone and lock/unlock it when it is necessary --- diff --git a/itk/clitkForwardWarpImageFilter.h b/itk/clitkForwardWarpImageFilter.h index 7efac8f..ec46f0c 100644 --- a/itk/clitkForwardWarpImageFilter.h +++ b/itk/clitkForwardWarpImageFilter.h @@ -25,7 +25,11 @@ #include "itkImageRegionIterator.h" #include "itkImageRegionIteratorWithIndex.h" #include "itkNumericTraits.h" +#if ITK_VERSION_MAJOR <= 4 #include "itkSimpleFastMutexLock.h" +#else +#include +#endif namespace clitk { @@ -56,7 +60,9 @@ namespace clitk //Some other typedefs typedef double CoordRepType; typedef itk::Image WeightsImageType; +#if ITK_VERSION_MAJOR <= 4 typedef itk::Image MutexImageType; +#endif /** Point type */ typedef itk::Point PointType; diff --git a/itk/clitkForwardWarpImageFilter.txx b/itk/clitkForwardWarpImageFilter.txx index 88804f5..4c38b5a 100644 --- a/itk/clitkForwardWarpImageFilter.txx +++ b/itk/clitkForwardWarpImageFilter.txx @@ -53,7 +53,9 @@ public: //Typedefs typedef typename OutputImageType::PixelType OutputPixelType; typedef itk::Image WeightsImageType; - typedef itk::Image MutexImageType; +#if ITK_VERSION_MAJOR <= 4 + typedef itk::Image MutexImageType; +#endif //=================================================================================== //Set methods void SetWeights(const typename WeightsImageType::Pointer input) { @@ -64,11 +66,18 @@ public: m_DeformationField=input; this->Modified(); } +#if ITK_VERSION_MAJOR <= 4 void SetMutexImage(const typename MutexImageType::Pointer input) { m_MutexImage=input; this->Modified(); m_ThreadSafe=true; } +#else + void SetMutexImage() { + this->Modified(); + m_ThreadSafe=true; + } +#endif //Get methods typename WeightsImageType::Pointer GetWeights() { @@ -89,7 +98,11 @@ protected: //member data typename itk::Image< double, ImageDimension>::Pointer m_Weights; typename DeformationFieldType::Pointer m_DeformationField; +#if ITK_VERSION_MAJOR <= 4 typename MutexImageType::Pointer m_MutexImage; +#else + std::mutex m_Mutex; +#endif bool m_ThreadSafe; }; @@ -224,14 +237,22 @@ void HelperClass1::Thread } else { //Entering critilal section: shared memory +#if ITK_VERSION_MAJOR <= 4 m_MutexImage->GetPixel(neighIndex).Lock(); +#else + m_Mutex.lock(); +#endif //Set the pixel and weight at neighIndex outputPtr->SetPixel(neighIndex, outputPtr->GetPixel(neighIndex) + overlap * static_cast(inputIt.Get())); m_Weights->SetPixel(neighIndex, m_Weights->GetPixel(neighIndex) + overlap); //Unlock +#if ITK_VERSION_MAJOR <= 4 m_MutexImage->GetPixel(neighIndex).Unlock(); +#else + m_Mutex.unlock(); +#endif } //Add to total overlap @@ -459,11 +480,15 @@ void ForwardWarpImageFilterSetRegions(region); mutex->Allocate(); mutex->SetSpacing(inputPtr->GetSpacing()); helper1->SetMutexImage(mutex); +#else + helper1->SetMutexImage(); +#endif if (m_Verbose) std::cout <<"Forwarp warping using a thread-safe algorithm" < WeightsImageType; +#if ITK_VERSION_MAJOR <= 4 typedef itk::Image MutexImageType; +#endif /** Point type */ typedef itk::Point PointType; diff --git a/itk/clitkInvertVFFilter.txx b/itk/clitkInvertVFFilter.txx index ec3c90f..6c91dbb 100644 --- a/itk/clitkInvertVFFilter.txx +++ b/itk/clitkInvertVFFilter.txx @@ -47,7 +47,9 @@ public: //Typedefs typedef typename OutputImageType::PixelType PixelType; typedef itk::Image WeightsImageType; - typedef itk::Image MutexImageType; +#if ITK_VERSION_MAJOR <= 4 + typedef itk::Image MutexImageType; +#endif //=================================================================================== //Set methods @@ -55,11 +57,18 @@ public: m_Weights = input; this->Modified(); } +#if ITK_VERSION_MAJOR <= 4 void SetMutexImage(const typename MutexImageType::Pointer input) { m_MutexImage=input; this->Modified(); m_ThreadSafe=true; } +#else + void SetMutexImage() { + this->Modified(); + m_ThreadSafe=true; + } +#endif //Get methods typename WeightsImageType::Pointer GetWeights() { @@ -79,7 +88,11 @@ protected: //member data typename WeightsImageType::Pointer m_Weights; +#if ITK_VERSION_MAJOR <= 4 typename MutexImageType::Pointer m_MutexImage; +#else + std::mutex m_Mutex; +#endif bool m_ThreadSafe; }; @@ -215,14 +228,22 @@ void HelperClass1::ThreadedGenerateData(const O else { //Entering critilal section: shared memory +#if ITK_VERSION_MAJOR <= 4 m_MutexImage->GetPixel(neighIndex).Lock(); +#else + m_Mutex.lock(); +#endif //Set the pixel and weight at neighIndex outputPtr->SetPixel(neighIndex, outputPtr->GetPixel(neighIndex) - (displacement*overlap)); m_Weights->SetPixel(neighIndex, m_Weights->GetPixel(neighIndex) + overlap); //Unlock +#if ITK_VERSION_MAJOR <= 4 m_MutexImage->GetPixel(neighIndex).Unlock(); +#else + m_Mutex.unlock(); +#endif } //Add to total overlap @@ -451,11 +472,15 @@ template void InvertVFFilterSetRegions(region); mutex->Allocate(); mutex->SetSpacing(inputPtr->GetSpacing()); helper1->SetMutexImage(mutex); +#else + helper1->SetMutexImage(); +#endif if (m_Verbose) std::cout <<"Inverting using a thread-safe algorithm" < +#endif #include "itkImageMaskSpatialObject.h" namespace clitk