changed the year in copyright notices to 2011-2012
[deb_libcec.git] / src / lib / platform / threads / mutex.h
index 4a2ad795f86e153cc59cb3c607e443198eecc735..a203fbf110a61ebc7b070c573877b720d4e5917c 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