mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
Merge pull request #2004 from alexanderkjeldaas/simplify-cmutexlock
Simplify CMutexLock
This commit is contained in:
commit
3ed1ccb089
42
src/sync.h
42
src/sync.h
@ -37,46 +37,31 @@ class CMutexLock
|
||||
{
|
||||
private:
|
||||
boost::unique_lock<Mutex> lock;
|
||||
public:
|
||||
|
||||
void Enter(const char* pszName, const char* pszFile, int nLine)
|
||||
{
|
||||
if (!lock.owns_lock())
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
if (!lock.try_lock())
|
||||
{
|
||||
PrintLockContention(pszName, pszFile, nLine);
|
||||
#endif
|
||||
lock.lock();
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void Leave()
|
||||
{
|
||||
if (lock.owns_lock())
|
||||
if (!lock.try_lock())
|
||||
{
|
||||
lock.unlock();
|
||||
LeaveCritical();
|
||||
PrintLockContention(pszName, pszFile, nLine);
|
||||
#endif
|
||||
lock.lock();
|
||||
#ifdef DEBUG_LOCKCONTENTION
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool TryEnter(const char* pszName, const char* pszFile, int nLine)
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
|
||||
lock.try_lock();
|
||||
if (!lock.owns_lock())
|
||||
{
|
||||
EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
|
||||
lock.try_lock();
|
||||
if (!lock.owns_lock())
|
||||
LeaveCritical();
|
||||
}
|
||||
LeaveCritical();
|
||||
return lock.owns_lock();
|
||||
}
|
||||
|
||||
public:
|
||||
CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
|
||||
{
|
||||
if (fTry)
|
||||
@ -95,11 +80,6 @@ public:
|
||||
{
|
||||
return lock.owns_lock();
|
||||
}
|
||||
|
||||
boost::unique_lock<Mutex> &GetLock()
|
||||
{
|
||||
return lock;
|
||||
}
|
||||
};
|
||||
|
||||
typedef CMutexLock<CCriticalSection> CCriticalBlock;
|
||||
|
Loading…
Reference in New Issue
Block a user