From 1dce23761237d85c2cfafd0757f1bef87b0087c4 Mon Sep 17 00:00:00 2001 From: svost Date: Thu, 6 Jan 2022 17:12:18 +0300 Subject: [PATCH] No more boost::mutex in code --- src/allocators.h | 18 ++++++++------ src/serialize.h | 17 ++++--------- src/sync.h | 67 +++++++++++++++++++++++++---------------------------- src/util.cpp | 8 +++--- 4 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/allocators.h b/src/allocators.h index bb72117..fc95452 100644 --- a/src/allocators.h +++ b/src/allocators.h @@ -5,11 +5,13 @@ #ifndef BITCOIN_ALLOCATORS_H #define BITCOIN_ALLOCATORS_H -#include +#include // for OPENSSL_cleanse() + +#include #include -#include +#include +#include #include -#include // 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 -#include // for PAGESIZE +#include // for PAGESIZE #include // 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(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(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 Histogram; diff --git a/src/serialize.h b/src/serialize.h index bc8ce01..23e8be3 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -5,28 +5,21 @@ #ifndef BITCOIN_SERIALIZE_H #define BITCOIN_SERIALIZE_H -#include +#include "allocators.h" +#include "version.h" + #include -#include #include #include #include -#include #include -#include #include - -//#ifndef Q_MOC_RUN -//#include -//#endif +#include +#include #if defined __USE_MINGW_ANSI_STDIO #undef __USE_MINGW_ANSI_STDIO // This constant forces MinGW to conduct stupid behavior #endif -#include - -#include "allocators.h" -#include "version.h" class CScript; diff --git a/src/sync.h b/src/sync.h index e80efbe..e532c6b 100644 --- a/src/sync.h +++ b/src/sync.h @@ -5,19 +5,14 @@ #ifndef BITCOIN_SYNC_H #define BITCOIN_SYNC_H -#include -#include -#include -#include - - - +#include +#include /** 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 */ +/** Wrapper around std::unique_lock */ template class CMutexLock { private: - boost::unique_lock lock; + std::unique_lock 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 &GetLock() + std::unique_lock &GetLock() { return lock; } @@ -123,23 +118,26 @@ typedef CMutexLock 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 lock(mutex); - while (value < 1) { - condition.wait(lock); - } + //std::unique_lock lock(mutex); + //while (value < 1) { + // condition.wait(lock); + //} + //value--; + std::unique_lock lock(mutex); + condition.wait(lock, [&]() { return value >= 1; }); value--; } bool try_wait() { - boost::unique_lock lock(mutex); + std::unique_lock lock(mutex); if (value < 1) return false; value--; @@ -148,7 +146,7 @@ public: void post() { { - boost::unique_lock lock(mutex); + std::unique_lock 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 - diff --git a/src/util.cpp b/src/util.cpp index afb0324..465b5dd 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -24,7 +24,7 @@ #include /* for _commit */ #include "shlobj.h" #elif defined(__linux__) -# include +#include #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) { -- 1.7.1