From 660ff174f299a6dfb28f7725dfbb08f140a5474b Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 13 May 2012 17:55:23 +0200 Subject: Use boost::thread locking instead of interprocess --- src/sync.h | 77 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 35 insertions(+), 42 deletions(-) (limited to 'src/sync.h') diff --git a/src/sync.h b/src/sync.h index 44d753ac15..1604338fb6 100644 --- a/src/sync.h +++ b/src/sync.h @@ -5,19 +5,19 @@ #ifndef BITCOIN_SYNC_H #define BITCOIN_SYNC_H -#include -#include -#include -#include +#include +#include +#include +#include /** Wrapped boost mutex: supports recursive locking, but no waiting */ -typedef boost::interprocess::interprocess_recursive_mutex CCriticalSection; +typedef boost::recursive_mutex CCriticalSection; /** Wrapped boost mutex: supports waiting but not recursive locking */ -typedef boost::interprocess::interprocess_mutex CWaitableCriticalSection; +typedef boost::mutex CWaitableCriticalSection; #ifdef DEBUG_LOCKORDER void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false); @@ -32,12 +32,12 @@ template class CMutexLock { private: - boost::interprocess::scoped_lock lock; + boost::unique_lock lock; public: void Enter(const char* pszName, const char* pszFile, int nLine) { - if (!lock.owns()) + if (!lock.owns_lock()) { EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex())); #ifdef DEBUG_LOCKCONTENTION @@ -55,7 +55,7 @@ public: void Leave() { - if (lock.owns()) + if (lock.owns_lock()) { lock.unlock(); LeaveCritical(); @@ -64,17 +64,17 @@ public: bool TryEnter(const char* pszName, const char* pszFile, int nLine) { - if (!lock.owns()) + if (!lock.owns_lock()) { EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true); lock.try_lock(); - if (!lock.owns()) + if (!lock.owns_lock()) LeaveCritical(); } - return lock.owns(); + return lock.owns_lock(); } - CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::interprocess::defer_lock) + CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock) { if (fTry) TryEnter(pszName, pszFile, nLine); @@ -84,16 +84,16 @@ public: ~CMutexLock() { - if (lock.owns()) + if (lock.owns_lock()) LeaveCritical(); } operator bool() { - return lock.owns(); + return lock.owns_lock(); } - boost::interprocess::scoped_lock &GetLock() + boost::unique_lock &GetLock() { return lock; } @@ -117,47 +117,40 @@ typedef CMutexLock CCriticalBlock; LeaveCritical(); \ } -#ifdef MAC_OSX -// boost::interprocess::interprocess_semaphore seems to spinlock on OSX; prefer polling instead class CSemaphore { private: - CCriticalSection cs; - int val; + boost::condition_variable condition; + boost::mutex mutex; + int value; public: - CSemaphore(int init) : val(init) {} + CSemaphore(int init) : value(init) {} void wait() { - do { - { - LOCK(cs); - if (val>0) { - val--; - return; - } - } - Sleep(100); - } while(1); + boost::unique_lock lock(mutex); + while (value < 1) { + condition.wait(lock); + } + value--; } bool try_wait() { - LOCK(cs); - if (val>0) { - val--; - return true; - } - return false; + boost::unique_lock lock(mutex); + if (value < 1) + return false; + value--; + return true; } void post() { - LOCK(cs); - val++; + { + boost::unique_lock lock(mutex); + value++; + } + condition.notify_one(); } }; -#else -typedef boost::interprocess::interprocess_semaphore CSemaphore; -#endif /** RAII-style semaphore lock */ class CSemaphoreGrant -- cgit v1.2.3