No more boost::mutex in code
authorsvost <ya.nowa@yandex.ru>
Thu, 6 Jan 2022 14:12:18 +0000 (17:12 +0300)
committersvost <ya.nowa@yandex.ru>
Thu, 6 Jan 2022 14:12:18 +0000 (17:12 +0300)
src/allocators.h
src/serialize.h
src/sync.h
src/util.cpp

index bb72117..fc95452 100644 (file)
@@ -5,11 +5,13 @@
 #ifndef BITCOIN_ALLOCATORS_H
 #define BITCOIN_ALLOCATORS_H
 
-#include <string.h>
+#include <openssl/crypto.h> // for OPENSSL_cleanse()
+
+#include <cstring>
 #include <string>
-#include <boost/thread/mutex.hpp>
+#include <mutex>
+#include <cassert>
 #include <map>
-#include <openssl/crypto.h> // for OPENSSL_cleanse()
 
 #ifdef WIN32
 #ifdef _WIN32_WINNT
@@ -27,7 +29,7 @@
 // the pagefile except in rare circumstances where memory is extremely low.
 #else
 #include <sys/mman.h>
-#include <limits.h> // for PAGESIZE
+#include <climits> // for PAGESIZE
 #include <unistd.h> // for sysconf
 #endif
 
@@ -56,7 +58,7 @@ public:
     // For all pages in affected range, increase lock count
     void LockRange(void *p, size_t size)
     {
-        boost::mutex::scoped_lock lock(mutex);
+        std::scoped_lock lock(mutex);
         if(!size) return;
         const size_t base_addr = reinterpret_cast<size_t>(p);
         const size_t start_page = base_addr & page_mask;
@@ -79,7 +81,7 @@ public:
     // For all pages in affected range, decrease lock count
     void UnlockRange(void *p, size_t size)
     {
-        boost::mutex::scoped_lock lock(mutex);
+        std::scoped_lock lock(mutex);
         if(!size) return;
         const size_t base_addr = reinterpret_cast<size_t>(p);
         const size_t start_page = base_addr & page_mask;
@@ -102,13 +104,13 @@ public:
     // Get number of locked pages for diagnostics
     int GetLockedPageCount()
     {
-        boost::mutex::scoped_lock lock(mutex);
+        std::scoped_lock lock(mutex);
         return histogram.size();
     }
 
 private:
     Locker locker;
-    boost::mutex mutex;
+    std::mutex mutex;
     size_t page_size, page_mask;
     // map of page base address to lock count
     typedef std::map<size_t,int> Histogram;
index bc8ce01..23e8be3 100644 (file)
@@ -5,28 +5,21 @@
 #ifndef BITCOIN_SERIALIZE_H
 #define BITCOIN_SERIALIZE_H
 
-#include <string>
+#include "allocators.h"
+#include "version.h"
+
 #include <vector>
-#include <map>
 #include <set>
 #include <tuple>
 #include <type_traits>
-#include <cassert>
 #include <limits>
-#include <cstring>
 #include <cstdio>
-
-//#ifndef Q_MOC_RUN
-//#include <boost/type_traits/is_fundamental.hpp>
-//#endif
+#include <ios>
+#include <cinttypes>
 
 #if defined __USE_MINGW_ANSI_STDIO
 #undef __USE_MINGW_ANSI_STDIO // This constant forces MinGW to conduct stupid behavior
 #endif
-#include <inttypes.h>
-
-#include "allocators.h"
-#include "version.h"
 
 
 class CScript;
index e80efbe..e532c6b 100644 (file)
@@ -5,19 +5,14 @@
 #ifndef BITCOIN_SYNC_H
 #define BITCOIN_SYNC_H
 
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/recursive_mutex.hpp>
-#include <boost/thread/locks.hpp>
-#include <boost/thread/condition_variable.hpp>
-
-
-
+#include <mutex>
+#include <condition_variable>
 
 /** Wrapped boost mutex: supports recursive locking, but no waiting  */
-typedef boost::recursive_mutex CCriticalSection;
+typedef std::recursive_mutex CCriticalSection;
 
 /** Wrapped boost mutex: supports waiting but not recursive locking */
-typedef boost::mutex CWaitableCriticalSection;
+typedef std::mutex CWaitableCriticalSection;
 
 #ifdef DEBUG_LOCKORDER
 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
@@ -31,12 +26,12 @@ void static inline LeaveCritical() {}
 void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
 #endif
 
-/** Wrapper around boost::unique_lock<Mutex> */
+/** Wrapper around std::unique_lock<Mutex> */
 template<typename Mutex>
 class CMutexLock
 {
 private:
-    boost::unique_lock<Mutex> lock;
+    std::unique_lock<Mutex> lock;
 public:
 
     void Enter(const char* pszName, const char* pszFile, int nLine)
@@ -77,7 +72,7 @@ public:
         return lock.owns_lock();
     }
 
-    CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
+    CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, std::defer_lock)
     {
         if (fTry)
             TryEnter(pszName, pszFile, nLine);
@@ -91,12 +86,12 @@ public:
             LeaveCritical();
     }
 
-    operator bool()
+    operator bool() const
     {
         return lock.owns_lock();
     }
 
-    boost::unique_lock<Mutex> &GetLock()
+    std::unique_lock<Mutex> &GetLock()
     {
         return lock;
     }
@@ -123,23 +118,26 @@ typedef CMutexLock<CCriticalSection> CCriticalBlock;
 class CSemaphore
 {
 private:
-    boost::condition_variable condition;
-    boost::mutex mutex;
+    std::condition_variable condition;
+    std::mutex mutex;
     int value;
 
 public:
-    CSemaphore(int init) : value(init) {}
+    explicit CSemaphore(int init) : value(init) {}
 
     void wait() {
-        boost::unique_lock<boost::mutex> lock(mutex);
-        while (value < 1) {
-            condition.wait(lock);
-        }
+        //std::unique_lock<std::mutex> lock(mutex);
+        //while (value < 1) {
+        //    condition.wait(lock);
+        //}
+        //value--;
+        std::unique_lock<std::mutex> lock(mutex);
+        condition.wait(lock, [&]() { return value >= 1; });
         value--;
     }
 
     bool try_wait() {
-        boost::unique_lock<boost::mutex> lock(mutex);
+        std::unique_lock<std::mutex> lock(mutex);
         if (value < 1)
             return false;
         value--;
@@ -148,7 +146,7 @@ public:
 
     void post() {
         {
-            boost::unique_lock<boost::mutex> lock(mutex);
+            std::unique_lock<std::mutex> lock(mutex);
             value++;
         }
         condition.notify_one();
@@ -164,17 +162,17 @@ private:
 
 public:
     void Acquire() {
-        if (fHaveGrant)
-            return;
-        sem->wait();
-        fHaveGrant = true;
+        if (!fHaveGrant) {
+            sem->wait();
+            fHaveGrant = true;
+        }
     }
 
     void Release() {
-        if (!fHaveGrant)
-            return;
-        sem->post();
-        fHaveGrant = false;
+        if (fHaveGrant) {
+            sem->post();
+            fHaveGrant = false;
+        }
     }
 
     bool TryAcquire() {
@@ -187,11 +185,11 @@ public:
         grant.Release();
         grant.sem = sem;
         grant.fHaveGrant = fHaveGrant;
-        sem = NULL;
+        sem = nullptr;
         fHaveGrant = false;
     }
 
-    CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
+    CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
 
     CSemaphoreGrant(CSemaphore &sema, bool fTry = false) : sem(&sema), fHaveGrant(false) {
         if (fTry)
@@ -204,9 +202,8 @@ public:
         Release();
     }
 
-    operator bool() {
+    operator bool() const {
         return fHaveGrant;
     }
 };
 #endif
-
index afb0324..465b5dd 100644 (file)
@@ -24,7 +24,7 @@
 #include <io.h> /* for _commit */
 #include "shlobj.h"
 #elif defined(__linux__)
-# include <sys/prctl.h>
+#include <sys/prctl.h>
 #endif
 
 #if !defined(WIN32) && !defined(ANDROID)
@@ -209,9 +209,9 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
             // Since the order of destruction of static/global objects is undefined,
             // allocate mutexDebugLog on the heap the first time this routine
             // is called to avoid crashes during shutdown.
-            static boost::mutex* mutexDebugLog = NULL;
-            if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
-            boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
+            static std::mutex* mutexDebugLog = nullptr;
+            if (mutexDebugLog == nullptr) mutexDebugLog = new std::mutex();
+            std::scoped_lock scoped_lock(*mutexDebugLog);
 
             // reopen the log file, if requested
             if (fReopenDebugLog) {