From a048449dbdfd9e9c55d6412cfcc05ece9cdc6d6f Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Fri, 23 Feb 2018 19:38:45 +0100
Subject: [PATCH] ENH: protect AbortGenerateData with mutex for ImageFileReader

---
 .../IO/ImageIO/include/otbImageFileWriter.h   |  9 +++++++
 .../IO/ImageIO/include/otbImageFileWriter.txx | 25 +++++++++++++++++--
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/Modules/IO/ImageIO/include/otbImageFileWriter.h b/Modules/IO/ImageIO/include/otbImageFileWriter.h
index b7bc6499b0..93be343529 100644
--- a/Modules/IO/ImageIO/include/otbImageFileWriter.h
+++ b/Modules/IO/ImageIO/include/otbImageFileWriter.h
@@ -25,6 +25,7 @@
 #include "itkProcessObject.h"
 #include "otbStreamingManager.h"
 #include "otbExtendedFilenameToWriterOptions.h"
+#include "itkFastMutexLock.h"
 
 namespace otb
 {
@@ -199,6 +200,11 @@ public:
   itkGetObjectMacro(ImageIO, otb::ImageIOBase);
   itkGetConstObjectMacro(ImageIO, otb::ImageIOBase);
 
+  // the interface of the superclass getter function is not thread safe
+  bool GetAbortGenerateDataMutex() const;
+
+  void SetAbortGenerateData(bool val) override;
+
 protected:
   ImageFileWriter();
   ~ImageFileWriter() ITK_OVERRIDE;
@@ -270,6 +276,9 @@ private:
    *  This variable can be the number of components in m_ImageIO or the
    *  number of components in the m_BandList (if used) */
   unsigned int m_IOComponents;
+
+  /** Lock to ensure thread-safety (added for the AbortGenerateData flag) */
+  itk::SimpleFastMutexLock m_Lock;
 };
 
 } // end namespace otb
diff --git a/Modules/IO/ImageIO/include/otbImageFileWriter.txx b/Modules/IO/ImageIO/include/otbImageFileWriter.txx
index b1472291f1..454fdea478 100644
--- a/Modules/IO/ImageIO/include/otbImageFileWriter.txx
+++ b/Modules/IO/ImageIO/include/otbImageFileWriter.txx
@@ -616,7 +616,7 @@ ImageFileWriter<TInputImage>
     }
 
   for (m_CurrentDivision = 0;
-       m_CurrentDivision < m_NumberOfDivisions && !this->GetAbortGenerateData();
+       m_CurrentDivision < m_NumberOfDivisions && !this->GetAbortGenerateDataMutex();
        m_CurrentDivision++, m_DivisionProgress = 0, this->UpdateFilterProgress())
     {
     streamRegion = m_StreamingManager->GetSplit(m_CurrentDivision);
@@ -645,7 +645,7 @@ ImageFileWriter<TInputImage>
    * If we ended due to aborting, push the progress up to 1.0 (since
    * it probably didn't end there)
    */
-  if (!this->GetAbortGenerateData())
+  if (!this->GetAbortGenerateDataMutex())
     {
     this->UpdateProgress(1.0);
     }
@@ -843,6 +843,27 @@ ImageFileWriter<TInputImage>
 return this->m_FilenameHelper->GetSimpleFileName();
 }
 
+template <class TInputImage>
+bool
+ImageFileWriter<TInputImage>
+::GetAbortGenerateDataMutex() const
+{
+  m_Lock.Lock();
+  bool ret = Superclass::GetAbortGenerateData();
+  m_Lock.Unlock();
+  return ret;
+}
+
+template <class TInputImage>
+void
+ImageFileWriter<TInputImage>
+::SetAbortGenerateData(bool val)
+{
+  m_Lock.Lock();
+  Superclass::SetAbortGenerateData(val);
+  m_Lock.Unlock();
+}
+
 } // end namespace otb
 
 #endif
-- 
GitLab