cec: add a bRecursive parameter to the constructor of CMutex
[deb_libcec.git] / src / lib / platform / threads.cpp
index d8ac5a7b0428df70a810eed5357c117ee100f0cf..d168e906a671ddc8e0caab582c9ae30a58bd2853 100644 (file)
@@ -35,9 +35,9 @@
 
 using namespace CEC;
 
-CMutex::CMutex(void)
+CMutex::CMutex(bool bRecursive /* = true */)
 {
-  pthread_mutex_init(&m_mutex, NULL);
+  pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : NULL);
 }
 
 CMutex::~CMutex(void)
@@ -60,6 +60,19 @@ void CMutex::Unlock(void)
   pthread_mutex_unlock(&m_mutex);
 }
 
+static pthread_mutexattr_t g_mutexAttr;
+pthread_mutexattr_t *CMutex::GetMutexAttribute()
+{
+  static bool bAttributeInitialised = false;
+  if (!bAttributeInitialised)
+  {
+    pthread_mutexattr_init(&g_mutexAttr);
+    pthread_mutexattr_settype(&g_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
+    bAttributeInitialised = true;
+  }
+  return &g_mutexAttr;
+}
+
 CLockObject::CLockObject(CMutex *mutex, bool bTryLock /* = false */) :
   m_mutex(mutex)
 {
@@ -122,7 +135,7 @@ bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout /* = 0 */)
       gettimeofday(&now, NULL);
       iTimeout       += now.tv_usec / 1000;
       abstime.tv_sec  = now.tv_sec + (time_t)(iTimeout / 1000);
-      abstime.tv_nsec = (time_t) ( (iTimeout % (uint32_t)1000) * (uint32_t)1000);
+      abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
       bReturn         = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
     }
     else