<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "mitkImageWriteAccessor.h"

mitk::ImageWriteAccessor::ImageWriteAccessor(ImagePointer image, const mitk::ImageDataItem *iDI, int OptionFlags)
  : ImageAccessorBase(image.GetPointer(), iDI, OptionFlags), m_Image(image)

{
  OrganizeWriteAccess();
}

mitk::ImageWriteAccessor::~ImageWriteAccessor()
{
  // In case of non-coherent memory, copied area needs to be written back
  // TODO

  m_Image->m_ReadWriteLock.Lock();

  // delete self from list of ImageReadAccessors in Image
  auto it = std::find(m_Image->m_Writers.begin(), m_Image->m_Writers.end(), this);
  m_Image->m_Writers.erase(it);

  // delete lock, if there are no waiting ImageAccessors
  if (m_WaitLock->m_WaiterCount <= 0)
  {
    m_WaitLock->m_Mutex.Unlock();
    delete m_WaitLock;
  }
  else
  {
    m_WaitLock->m_Mutex.Unlock();
  }

  m_Image->m_ReadWriteLock.Unlock();
}

const mitk::Image *mitk::ImageWriteAccessor::GetImage() const
{
  return m_Image.GetPointer();
}

void mitk::ImageWriteAccessor::OrganizeWriteAccess()
{
  m_Image->m_ReadWriteLock.Lock();

  bool readOverlap = false;
  bool writeOverlap = false;

  ImageAccessorWaitLock *overlapLock = nullptr;

  // Check, if there is any Read-Access going on
  if (m_Image->m_Readers.size() > 0)
  {
    // Check for every ReadAccessor, if the Region of this ImageAccessors overlaps
    // make sure this iterator is not used, when m_ReadWriteLock is Unlocked!
    auto it = m_Image->m_Readers.begin();

    for (; it != m_Image->m_Readers.end(); ++it)
    {
      ImageAccessorBase *r = *it;

      if ((r->m_Options & IgnoreLock) == 0 && Overlap(r))
      {
        // An Overlap was detected.

        PreventRecursiveMutexLock(r);

        readOverlap = true;
        overlapLock = r->m_WaitLock;
        break;
      } // if
    }   // for
  }     // if

  // Check, if there is any Write-Access going on
  if (m_Image->m_Writers.size() > 0)
  {
    // Check for every WriteAccessor, if the Region of this ImageAccessors overlaps
    // make sure this iterator is not used, when m_ReadWriteLock is Unlocked!
    auto it = m_Image->m_Writers.begin();

    for (; it != m_Image->m_Writers.end(); ++it)
    {
      ImageAccessorBase *w = *it;

      if (Overlap(w))
      {
        // An Overlap was detected.

        PreventRecursiveMutexLock(w);

        // save overlapping Waitlock
        writeOverlap = true;
        overlapLock = w->m_WaitLock;
        break;

      } // if
    }   // for
  }     // if

  if (readOverlap || writeOverlap)<commit_msg>Consider IgnoreLock option for write accessors<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "mitkImageWriteAccessor.h"

mitk::ImageWriteAccessor::ImageWriteAccessor(ImagePointer image, const mitk::ImageDataItem *iDI, int OptionFlags)
  : ImageAccessorBase(image.GetPointer(), iDI, OptionFlags), m_Image(image)

{
  OrganizeWriteAccess();
}

mitk::ImageWriteAccessor::~ImageWriteAccessor()
{
  // In case of non-coherent memory, copied area needs to be written back
  // TODO

  m_Image->m_ReadWriteLock.Lock();

  // delete self from list of ImageReadAccessors in Image
  auto it = std::find(m_Image->m_Writers.begin(), m_Image->m_Writers.end(), this);
  m_Image->m_Writers.erase(it);

  // delete lock, if there are no waiting ImageAccessors
  if (m_WaitLock->m_WaiterCount <= 0)
  {
    m_WaitLock->m_Mutex.Unlock();
    delete m_WaitLock;
  }
  else
  {
    m_WaitLock->m_Mutex.Unlock();
  }

  m_Image->m_ReadWriteLock.Unlock();
}

const mitk::Image *mitk::ImageWriteAccessor::GetImage() const
{
  return m_Image.GetPointer();
}

void mitk::ImageWriteAccessor::OrganizeWriteAccess()
{
  m_Image->m_ReadWriteLock.Lock();

  bool readOverlap = false;
  bool writeOverlap = false;

  ImageAccessorWaitLock *overlapLock = nullptr;

  // Check, if there is any Read-Access going on
  if (m_Image->m_Readers.size() > 0)
  {
    // Check for every ReadAccessor, if the Region of this ImageAccessors overlaps
    // make sure this iterator is not used, when m_ReadWriteLock is Unlocked!
    auto it = m_Image->m_Readers.begin();

    for (; it != m_Image->m_Readers.end(); ++it)
    {
      ImageAccessorBase *r = *it;

      if ((r->m_Options & IgnoreLock) == 0 && Overlap(r))
      {
        // An Overlap was detected.

        PreventRecursiveMutexLock(r);

        readOverlap = true;
        overlapLock = r->m_WaitLock;
        break;
      } // if
    }   // for
  }     // if

  // Check, if there is any Write-Access going on
  if (m_Image->m_Writers.size() > 0)
  {
    // Check for every WriteAccessor, if the Region of this ImageAccessors overlaps
    // make sure this iterator is not used, when m_ReadWriteLock is Unlocked!
    auto it = m_Image->m_Writers.begin();

    for (; it != m_Image->m_Writers.end(); ++it)
    {
      ImageAccessorBase *w = *it;

      if ((w->m_Options & IgnoreLock) == 0 && Overlap(w))
      {
        // An Overlap was detected.

        PreventRecursiveMutexLock(w);

        // save overlapping Waitlock
        writeOverlap = true;
        overlapLock = w->m_WaitLock;
        break;

      } // if
    }   // for
  }     // if

  if (readOverlap || writeOverlap)
{'fraction_non_alphanumeric': 0.1526479750778816, 'fraction_numerical': 0.0014756517461878996, 'mean_word_length': 3.298801973220578, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4438787', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1860, 'n_words': 509}