Skip to content

Commit

Permalink
macOS Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Sep 6, 2021
1 parent db66e36 commit 5ccafb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions retesteth/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <testStructures/Common.h>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <retesteth/dataObject/SPointer.h>

using namespace std;
using namespace test;
Expand Down Expand Up @@ -475,6 +476,9 @@ Options::Options(int argc, const char** argv)
BOOST_THROW_EXCEPTION(
InvalidOption("--seed <uint> could be used only with --createRandomTest \n"));
}

if (threadCount == 1)
dataobject::GCP_SPointer<int>::DISABLETHREADSAFE();
}

Options const& Options::get(int argc, const char** argv)
Expand Down
29 changes: 22 additions & 7 deletions retesteth/dataObject/SPointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@
#include <string>
#include <mutex>


namespace dataobject
{

bool G_IS_THREADSAFE = true;
std::mutex GCP_SPointerBase::g_spRefAccessMutex;

void disableThreadsafe()
{
G_IS_THREADSAFE = false;
}

// To debug exceptions as breakpoints does not work from header
void throwException(std::string const& _ex)
{
throw SPointerException(_ex);
}

std::mutex g_spMutexAdd;
std::mutex g_spMutexDel;
void GCP_SPointerBase::AddRef()
{
// very heavy. use thread unsafe pointer preferably
std::lock_guard<std::mutex> lock(g_spMutexAdd);
_nRef++;
if (G_IS_THREADSAFE)
{
std::lock_guard<std::mutex> lock(g_spRefAccessMutex);
_nRef++;
}
else
_nRef++;
}
int GCP_SPointerBase::DelRef()
{
std::lock_guard<std::mutex> lock(g_spMutexDel);
_nRef--;
if (G_IS_THREADSAFE)
{
std::lock_guard<std::mutex> lock(g_spRefAccessMutex);
_nRef--;
}
else
_nRef--;
return _nRef;
}

Expand Down
5 changes: 5 additions & 0 deletions retesteth/dataObject/SPointer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <exception>
#include <string>
#include <mutex>

namespace dataobject
{
Expand All @@ -25,6 +26,7 @@ struct SPointerException : virtual std::exception
};

void throwException(std::string const& _ex);
void disableThreadsafe();

template <class T>
class GCP_SPointer;
Expand All @@ -35,6 +37,8 @@ class GCP_SPointerBase
bool _isEmpty;
void AddRef();
int DelRef();
static std::mutex g_spRefAccessMutex;


public:
GCP_SPointerBase() : _nRef(0), _isEmpty(false) {}
Expand Down Expand Up @@ -66,6 +70,7 @@ class GCP_SPointer
}

public:
static void DISABLETHREADSAFE() { disableThreadsafe(); };
explicit GCP_SPointer() : _pointee(nullptr) {}
GCP_SPointer(int) : _pointee(nullptr) {}
explicit GCP_SPointer(T* pointee)
Expand Down

0 comments on commit 5ccafb6

Please sign in to comment.