Revert "platform: added predicate in CCondition"
[deb_libcec.git] / src / lib / platform / threads / mutex.h
index 4a2ad795f86e153cc59cb3c607e443198eecc735..e8481012773e428561c0e1f7c7652117926aa421 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
  *     http://www.pulse-eight.net/
  */
 
+#include "../os.h"
+
+#if defined(__WINDOWS__)
+#include "../windows/os-threads.h"
+#else
+#include "../posix/os-threads.h"
+#endif
+
 namespace PLATFORM
 {
   class PreventCopy
@@ -81,8 +89,17 @@ namespace PLATFORM
 
     inline void Unlock(void)
     {
-      --m_iLockCount;
-      MutexUnlock(m_mutex);
+      if (Lock())
+      {
+        if (m_iLockCount >= 2)
+        {
+          --m_iLockCount;
+          MutexUnlock(m_mutex);
+        }
+
+        --m_iLockCount;
+        MutexUnlock(m_mutex);
+      }
     }
 
     inline bool Clear(void)
@@ -99,8 +116,8 @@ namespace PLATFORM
     }
 
   private:
-    mutex_t      m_mutex;
-    unsigned int m_iLockCount;
+    mutex_t               m_mutex;
+    volatile unsigned int m_iLockCount;
   };
 
   class CLockObject : public PreventCopy
@@ -146,33 +163,86 @@ namespace PLATFORM
     bool    m_bClearOnExit;
   };
 
-  class CCondition : public PreventCopy
+  class CTryLockObject : public PreventCopy
   {
   public:
-    inline CCondition(void)
+    inline CTryLockObject(CMutex &mutex, bool bClearOnExit = false) :
+      m_mutex(mutex),
+      m_bClearOnExit(bClearOnExit),
+      m_bIsLocked(m_mutex.TryLock())
+    {
+    }
+
+    inline ~CTryLockObject(void)
+    {
+      if (m_bClearOnExit)
+        Clear();
+      else if (m_bIsLocked)
+        Unlock();
+    }
+
+    inline bool TryLock(void)
+    {
+      bool bReturn = m_mutex.TryLock();
+      m_bIsLocked |= bReturn;
+      return bReturn;
+    }
+
+    inline void Unlock(void)
     {
-      ConditionCreate(m_condition);
+      if (m_bIsLocked)
+      {
+        m_bIsLocked = false;
+        m_mutex.Unlock();
+      }
+    }
+
+    inline bool Clear(void)
+    {
+      m_bIsLocked = false;
+      return m_mutex.Clear();
+    }
+
+    inline bool Lock(void)
+    {
+      bool bReturn = m_mutex.Lock();
+      m_bIsLocked |= bReturn;
+      return bReturn;
+    }
+
+    inline bool IsLocked(void) const
+    {
+      return m_bIsLocked;
     }
 
+  private:
+    CMutex &      m_mutex;
+    bool          m_bClearOnExit;
+    volatile bool m_bIsLocked;
+  };
+
+  class CCondition : public PreventCopy
+  {
+  public:
+    inline CCondition(void) {}
     inline ~CCondition(void)
     {
-      Broadcast();
-      ConditionDelete(m_condition);
+      m_condition.Broadcast();
     }
 
     inline void Broadcast(void)
     {
-      ConditionBroadcast(m_condition);
+      m_condition.Broadcast();
     }
 
     inline void Signal(void)
     {
-      ConditionSignal(m_condition);
+      m_condition.Signal();
     }
 
     inline bool Wait(CMutex &mutex, uint32_t iTimeout = 0)
     {
-      return ConditionWait(m_condition, mutex.m_mutex, iTimeout);
+      return m_condition.Wait(mutex.m_mutex, iTimeout);
     }
 
     static void Sleep(uint32_t iTimeout)
@@ -183,6 +253,7 @@ namespace PLATFORM
       w.Wait(m, iTimeout);
     }
 
-    condition_t m_condition;
+  private:
+    CConditionImpl m_condition;
   };
 }