X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Fsync.h;h=e532c6bf091a28b949bc724c6f6088c2c731b101;hp=e80efbe00161e7863e8ff19cf5c0128e95b3b609;hb=1dce23761237d85c2cfafd0757f1bef87b0087c4;hpb=82597c35e97568d9d53983211cdf29621cefdb51 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 -