11 #include <boost/thread/condition_variable.hpp> 12 #include <boost/thread/mutex.hpp> 13 #include <boost/thread/recursive_mutex.hpp> 53 template <
typename PARENT>
69 return PARENT::try_lock();
73 #ifdef DEBUG_LOCKORDER 74 void EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false);
76 std::string LocksHeld();
77 void AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs);
78 void DeleteLock(
void* cs);
80 void static inline EnterCritical(
const char* pszName,
const char* pszFile,
int nLine,
void* cs,
bool fTry =
false) {}
81 void static inline LeaveCritical() {}
82 void static inline AssertLockHeldInternal(
const char* pszName,
const char* pszFile,
int nLine,
void* cs) {}
83 void static inline DeleteLock(
void* cs) {}
85 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs) 95 DeleteLock((
void*)
this);
105 #ifdef DEBUG_LOCKCONTENTION 106 void PrintLockContention(
const char* pszName,
const char* pszFile,
int nLine);
110 template <
typename Mutex>
114 boost::unique_lock<Mutex>
lock;
116 void Enter(
const char* pszName,
const char* pszFile,
int nLine)
118 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()));
119 #ifdef DEBUG_LOCKCONTENTION 120 if (!lock.try_lock()) {
121 PrintLockContention(pszName, pszFile, nLine);
124 #ifdef DEBUG_LOCKCONTENTION 129 bool TryEnter(
const char* pszName,
const char* pszFile,
int nLine)
131 EnterCritical(pszName, pszFile, nLine, (
void*)(lock.mutex()),
true);
133 if (!lock.owns_lock())
135 return lock.owns_lock();
142 TryEnter(pszName, pszFile, nLine);
144 Enter(pszName, pszFile, nLine);
149 if (!pmutexIn)
return;
151 lock = boost::unique_lock<Mutex>(*pmutexIn, boost::defer_lock);
153 TryEnter(pszName, pszFile, nLine);
155 Enter(pszName, pszFile, nLine);
160 if (lock.owns_lock())
166 return lock.owns_lock();
172 #define PASTE(x, y) x ## y 173 #define PASTE2(x, y) PASTE(x, y) 175 #define LOCK(cs) CCriticalBlock PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__) 176 #define LOCK2(cs1, cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__), criticalblock2(cs2, #cs2, __FILE__, __LINE__) 177 #define TRY_LOCK(cs, name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true) 179 #define ENTER_CRITICAL_SECTION(cs) \ 181 EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \ 185 #define LEAVE_CRITICAL_SECTION(cs) \ 203 boost::unique_lock<boost::mutex> lock(mutex);
205 condition.wait(lock);
212 boost::unique_lock<boost::mutex> lock(mutex);
222 boost::unique_lock<boost::mutex> lock(mutex);
225 condition.notify_one();
289 #endif // FABCOIN_SYNC_H void MoveTo(CSemaphoreGrant &grant)
void unlock() UNLOCK_FUNCTION()
#define EXCLUSIVE_LOCK_FUNCTION(...)
boost::condition_variable CConditionVariable
Just a typedef for boost::condition_variable, can be wrapped later if desired.
CMutexLock(Mutex &mutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(mutexIn)
CMutexLock(Mutex *pmutexIn, const char *pszName, const char *pszFile, int nLine, bool fTry=false) EXCLUSIVE_LOCK_FUNCTION(pmutexIn)
~CMutexLock() UNLOCK_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<Mutex>
Template mixin that adds -Wthread-safety locking annotations to a subset of the mutex API...
boost::condition_variable condition
bool TryEnter(const char *pszName, const char *pszFile, int nLine)
#define EXCLUSIVE_TRYLOCK_FUNCTION(...)
boost::unique_lock< Mutex > lock
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...