updated copyright messages for 2013
[deb_libcec.git] / src / lib / platform / threads / mutex.h
index 4a0c331a1fa6d64cbffa29e8466e25c29aca211a..2bd908e533ee00d2298c6c1083afdd6bcd44f3b5 100644 (file)
@@ -2,7 +2,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2013 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"
+#include "lib/platform/os.h"
 
 #if defined(__WINDOWS__)
-#include "../windows/os-threads.h"
+#include "lib/platform/windows/os-threads.h"
 #else
-#include "../posix/os-threads.h"
+#include "lib/platform/posix/os-threads.h"
 #endif
 
-#include "../util/timeutils.h"
+#include "lib/platform/util/timeutils.h"
 
 namespace PLATFORM
 {
@@ -51,7 +51,7 @@ namespace PLATFORM
 
   private:
     inline PreventCopy(const PreventCopy &c) { *this = c; }
-    inline PreventCopy &operator=(const PreventCopy &c){ *this = c; return *this; }
+    inline PreventCopy &operator=(const PreventCopy & UNUSED(c)){ return *this; }
   };
 
   template <typename _Predicate>
@@ -86,9 +86,12 @@ namespace PLATFORM
 
     inline bool Lock(void)
     {
-      MutexLock(m_mutex);
-      ++m_iLockCount;
-      return true;
+      if (MutexLock(m_mutex))
+      {
+        ++m_iLockCount;
+        return true;
+      }
+      return false;
     }
 
     inline void Unlock(void)
@@ -257,18 +260,19 @@ namespace PLATFORM
         if (iTimeout == 0)
           return Wait(mutex, predicate);
 
-        bool bReturn(true);
-        if (!predicate)
+        if (predicate)
+          return true;
+
+        bool bReturn(false);
+        bool bBreak(false);
+        CTimeout timeout(iTimeout);
+        uint32_t iMsLeft(0);
+
+        while (!bReturn && !bBreak)
         {
-          CTimeout timeout(iTimeout);
-          uint32_t iMsLeft(0);
-          bReturn = false;
-          while (!bReturn)
-          {
-            iMsLeft = timeout.TimeLeft();
-            if ((bReturn = iMsLeft == 0 || predicate) == false)
-              m_condition.Wait(mutex.m_mutex, iMsLeft);
-          }
+          iMsLeft = timeout.TimeLeft();
+          if ((bReturn = predicate) == false && (bBreak = iMsLeft == 0) == false)
+            m_condition.Wait(mutex.m_mutex, iMsLeft);
         }
         return bReturn;
       }
@@ -280,10 +284,11 @@ namespace PLATFORM
   class CEvent
   {
   public:
-    CEvent(void) :
+    CEvent(bool bAutoReset = true) :
       m_bSignaled(false),
       m_bBroadcast(false),
-      m_iWaitingThreads(0) {}
+      m_iWaitingThreads(0),
+      m_bAutoReset(bAutoReset) {}
     virtual ~CEvent(void) {}
 
     void Broadcast(void)
@@ -336,15 +341,16 @@ namespace PLATFORM
     {
       CLockObject lock(m_mutex);
       bool bReturn(m_bSignaled);
-      if (bReturn && (--m_iWaitingThreads == 0 || !m_bBroadcast))
+      if (bReturn && (--m_iWaitingThreads == 0 || !m_bBroadcast) && m_bAutoReset)
         m_bSignaled = false;
       return bReturn;
     }
 
-    volatile bool              m_bSignaled;
-    CCondition<volatile bool&> m_condition;
-    CMutex                     m_mutex;
-    volatile bool              m_bBroadcast;
-    unsigned int               m_iWaitingThreads;
+    volatile bool             m_bSignaled;
+    CCondition<volatile bool> m_condition;
+    CMutex                    m_mutex;
+    volatile bool             m_bBroadcast;
+    unsigned int              m_iWaitingThreads;
+    bool                      m_bAutoReset;
   };
 }