]> Git Repo - VerusCoin.git/commitdiff
Simplify CMutexLock
authorAlexander Kjeldaas <[email protected]>
Sun, 11 Nov 2012 06:11:13 +0000 (03:11 -0300)
committerAlexander Kjeldaas <[email protected]>
Wed, 14 Nov 2012 03:00:23 +0000 (00:00 -0300)
o Remove unused Leave and GetLock functions
o Make Enter and TryEnter private.
o Simplify Enter and TryEnter.
  boost::unique_lock doesn't really know whether the
  mutex it wraps is locked or not when the defer_lock
  option is used.
  The boost::recursive_mutex does not expose this
  information, so unique_lock only infers this
  knowledge.  When taking the lock is defered, it
  (randomly) assumes that the lock is not taken.
  boost::unique_lock has the following definition:

        unique_lock(Mutex& m_,defer_lock_t):
            m(&m_),is_locked(false)
        {}

        bool owns_lock() const
        {
            return is_locked;
        }
  Thus it is a mistake to check owns_lock() in Enter
  and TryEnter - they will always return false.

src/sync.h

index e80efbe00161e7863e8ff19cf5c0128e95b3b609..9dfc6697c6b5771c58dae086c53660df4ea878af 100644 (file)
@@ -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);
+        if (!lock.try_lock())
+        {
+            PrintLockContention(pszName, pszFile, nLine);
 #endif
-            lock.lock();
+        lock.lock();
 #ifdef DEBUG_LOCKCONTENTION
-            }
-#endif
-        }
-    }
-
-    void Leave()
-    {
-        if (lock.owns_lock())
-        {
-            lock.unlock();
-            LeaveCritical();
         }
+#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;
This page took 0.028032 seconds and 4 git commands to generate.