From 5ccafb6cf29db6e3fcd9976be34cbf289cd4677d Mon Sep 17 00:00:00 2001
From: Dimitry Kh <dimitry@ethereum.org>
Date: Mon, 6 Sep 2021 18:12:59 +0200
Subject: [PATCH] macOS Mutex

---
 retesteth/Options.cpp             |  4 ++++
 retesteth/dataObject/SPointer.cpp | 29 ++++++++++++++++++++++-------
 retesteth/dataObject/SPointer.h   |  5 +++++
 3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/retesteth/Options.cpp b/retesteth/Options.cpp
index a4d51cd45..8770bff75 100644
--- a/retesteth/Options.cpp
+++ b/retesteth/Options.cpp
@@ -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;
@@ -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)
diff --git a/retesteth/dataObject/SPointer.cpp b/retesteth/dataObject/SPointer.cpp
index e0c69a062..5132a5b25 100644
--- a/retesteth/dataObject/SPointer.cpp
+++ b/retesteth/dataObject/SPointer.cpp
@@ -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;
 }
 
diff --git a/retesteth/dataObject/SPointer.h b/retesteth/dataObject/SPointer.h
index d9129aee4..8505d7a52 100644
--- a/retesteth/dataObject/SPointer.h
+++ b/retesteth/dataObject/SPointer.h
@@ -1,6 +1,7 @@
 #pragma once
 #include <exception>
 #include <string>
+#include <mutex>
 
 namespace dataobject
 {
@@ -25,6 +26,7 @@ struct SPointerException : virtual std::exception
 };
 
 void throwException(std::string const& _ex);
+void disableThreadsafe();
 
 template <class T>
 class GCP_SPointer;
@@ -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) {}
@@ -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)