7 #ifndef ANONCOIN_SYNC_H
8 #define ANONCOIN_SYNC_H
12 #include <boost/thread/condition_variable.hpp>
13 #include <boost/thread/locks.hpp>
14 #include <boost/thread/mutex.hpp>
15 #include <boost/thread/recursive_mutex.hpp>
61 template <
typename PARENT>
77 return PARENT::try_lock();
88 #ifdef DEBUG_LOCKORDER
89 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
91 std::string LocksHeld();
92 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void *cs);
94 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
95 void static inline LeaveCritical() {}
96 void static inline AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void *cs) {}
98 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
100 #ifdef DEBUG_LOCKCONTENTION
101 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
105 template<
typename Mutex>
109 boost::unique_lock<Mutex>
lock;
111 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
113 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()));
114 #ifdef DEBUG_LOCKCONTENTION
115 if (!lock.try_lock())
117 PrintLockContention(pszName, pszFile, nLine);
120 #ifdef DEBUG_LOCKCONTENTION
125 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
127 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()),
true);
129 if (!lock.owns_lock())
131 return lock.owns_lock();
135 CMutexLock(Mutex& mutexIn,
const char* pszName,
const char* pszFile,
int nLine,
bool fTry =
false) : lock(mutexIn,
boost::defer_lock)
140 Enter(pszName, pszFile, nLine);
145 if (lock.owns_lock())
151 return lock.owns_lock();
157 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
158 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
159 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
161 #define ENTER_CRITICAL_SECTION(cs) \
163 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
167 #define LEAVE_CRITICAL_SECTION(cs) \
184 boost::unique_lock<boost::mutex> lock(mutex);
186 condition.wait(lock);
192 boost::unique_lock<boost::mutex> lock(mutex);
201 boost::unique_lock<boost::mutex> lock(mutex);
204 condition.notify_one();
void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
RAII-style semaphore lock.
CMutexLock< CCriticalSection > CCriticalBlock
void lock() EXCLUSIVE_LOCK_FUNCTION()
#define UNLOCK_FUNCTION(...)
CSemaphoreGrant(CSemaphore &sema, bool fTry=false)
void Enter(const char *pszName, const char *pszFile, int nLine)
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
AnnotatedMixin< boost::mutex > CWaitableCriticalSection
Wrapped boost mutex: supports waiting but not recursive locking.
Wrapper around boost::unique_lock
boost::condition_variable condition
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
AnnotatedMixin< boost::recursive_mutex > CCriticalSection
Wrapped boost mutex: supports recursive locking, but no waiting.
CMutexLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false)
boost::unique_lock< Mutex > lock