<commit_before>// Copyright <NAME>, SwampBits LLC 2014
// BSD License

#include <errno.h>

#include "PthreadsMutex.h"
#include "BasicException.h"
#include "Logger.h"

static const std::string EMPTY_STRING = "";

using namespace chaudiere;

//******************************************************************************

PthreadsMutex::PthreadsMutex() :
   PthreadsMutex(EMPTY_STRING)
{
}

//******************************************************************************

PthreadsMutex::PthreadsMutex(const std::string& mutexName) :
   m_mutexName(mutexName),
   m_haveValidMutex(false),
   m_isLocked(false)
{
   Logger::logInstanceCreate("PthreadsMutex");
   char buffer[128];
   
   pthread_mutexattr_t attr;
   int rc = ::pthread_mutexattr_init(&attr);
   if (0 == rc) {
      rc = ::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

      if (0 == rc) {
         rc = ::pthread_mutex_init(&m_mutex, &attr);
         
         ::pthread_mutexattr_destroy(&attr);
      
         if (0 == rc) {
            m_haveValidMutex = true;
         } else {
            snprintf(buffer, 128, "unable to create pthreads mutex, rc=%d", rc);
            Logger::error(buffer);
            throw BasicException(buffer);
         }
      } else {
         snprintf(buffer, 128, "unable to set pthreads mutex type, rc=%d", rc);
         Logger::error(buffer);
         throw BasicException(buffer);
      }
   } else {
      snprintf(buffer, 128, "unable to initialize mutex attributes, rc=%d", rc);
      Logger::error(buffer);
      throw BasicException(buffer);
   }
}

//******************************************************************************

PthreadsMutex::~PthreadsMutex() noexcept
{
   Logger::logInstanceDestroy("PthreadsMutex");

   if (m_haveValidMutex) {
      unlock();
      ::pthread_mutex_destroy(&m_mutex);
   }
}

//******************************************************************************

bool PthreadsMutex::unlock() noexcept
{
   if (m_haveValidMutex) {
      const int rc = ::pthread_mutex_unlock(&m_mutex);
      if (0 == rc) {
         m_isLocked = false;
      } else {
         Logger::error("unable to unlock PthreadsMutex");
      }
      
      return !m_isLocked;
   } else {
      if (!m_haveValidMutex) {
         Logger::error("can't unlock PthreadsMutex, don't have valid mutex");
      }
      
      if (!m_isLocked) {
         Logger::error("can't unlock PthreadsMutex, not locked");
      }
      
      return false;
   }
}

//******************************************************************************

bool PthreadsMutex::lock() noexcept
{
   if (m_haveValidMutex) {
      
      const int rc = ::pthread_mutex_lock(&m_mutex);
      if (0 == rc) {<commit_msg>added checks for locked before unlocking<commit_after>// Copyright <NAME>, SwampBits LLC 2014
// BSD License

#include <errno.h>

#include "PthreadsMutex.h"
#include "BasicException.h"
#include "Logger.h"

static const std::string EMPTY_STRING = "";

using namespace chaudiere;

//******************************************************************************

PthreadsMutex::PthreadsMutex() :
   PthreadsMutex(EMPTY_STRING)
{
}

//******************************************************************************

PthreadsMutex::PthreadsMutex(const std::string& mutexName) :
   m_mutexName(mutexName),
   m_haveValidMutex(false),
   m_isLocked(false)
{
   Logger::logInstanceCreate("PthreadsMutex");
   char buffer[128];
   
   pthread_mutexattr_t attr;
   int rc = ::pthread_mutexattr_init(&attr);
   if (0 == rc) {
      rc = ::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

      if (0 == rc) {
         rc = ::pthread_mutex_init(&m_mutex, &attr);
         
         ::pthread_mutexattr_destroy(&attr);
      
         if (0 == rc) {
            m_haveValidMutex = true;
         } else {
            snprintf(buffer, 128, "unable to create pthreads mutex, rc=%d", rc);
            Logger::error(buffer);
            throw BasicException(buffer);
         }
      } else {
         snprintf(buffer, 128, "unable to set pthreads mutex type, rc=%d", rc);
         Logger::error(buffer);
         throw BasicException(buffer);
      }
   } else {
      snprintf(buffer, 128, "unable to initialize mutex attributes, rc=%d", rc);
      Logger::error(buffer);
      throw BasicException(buffer);
   }
}

//******************************************************************************

PthreadsMutex::~PthreadsMutex() noexcept
{
   Logger::logInstanceDestroy("PthreadsMutex");

   if (m_haveValidMutex) {
      if (m_isLocked) {
         unlock();
      }
      ::pthread_mutex_destroy(&m_mutex);
   }
}

//******************************************************************************

bool PthreadsMutex::unlock() noexcept
{
   if (m_haveValidMutex && m_isLocked) {
      const int rc = ::pthread_mutex_unlock(&m_mutex);
      if (0 == rc) {
         m_isLocked = false;
      } else {
         Logger::error("unable to unlock PthreadsMutex");
      }
      
      return !m_isLocked;
   } else {
      if (!m_haveValidMutex) {
         Logger::error("can't unlock PthreadsMutex, don't have valid mutex");
      }
      
      if (!m_isLocked) {
         Logger::error("can't unlock PthreadsMutex, not locked");
      }
      
      return false;
   }
}

//******************************************************************************

bool PthreadsMutex::lock() noexcept
{
   if (m_haveValidMutex) {
      
      const int rc = ::pthread_mutex_lock(&m_mutex);
      if (0 == rc) {
{'fraction_non_alphanumeric': 0.25480943738656986, 'fraction_numerical': 0.007622504537205082, 'mean_word_length': 3.262180974477958, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5410041', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1612, 'n_words': 320}