cec: stick some interfaces before the pthread stuff, so we can add other implementati...
[deb_libcec.git] / src / lib / platform / threads.cpp
index 08a4c3b8884c39e9ce775212e8d5665a78175316..c9f6c16214f652fb0179e669014cd44885da34f6 100644 (file)
 
 #include "threads.h"
 #include "timeutils.h"
+#include "os-dependent.h"
 
 using namespace CEC;
 
-CMutex::CMutex(void)
-{
-  pthread_mutex_init(&m_mutex, NULL);
-}
-
-CMutex::~CMutex(void)
-{
-  pthread_mutex_destroy(&m_mutex);
-}
-
-bool CMutex::TryLock(void)
-{
-  return (pthread_mutex_trylock(&m_mutex) == 0);
-}
-
-bool CMutex::Lock(void)
-{
-  return (pthread_mutex_lock(&m_mutex) == 0);
-}
-
-void CMutex::Unlock(void)
-{
-  pthread_mutex_unlock(&m_mutex);
-}
-
-CLockObject::CLockObject(CMutex *mutex) :
+CLockObject::CLockObject(IMutex *mutex, bool bTryLock /* = false */) :
   m_mutex(mutex)
 {
   if (m_mutex)
-    m_mutex->Lock();
+    m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
 }
 
 CLockObject::~CLockObject(void)
@@ -75,58 +51,20 @@ CLockObject::~CLockObject(void)
 
 void CLockObject::Leave(void)
 {
-  if (m_mutex)
+  if (m_mutex && m_bLocked)
+  {
+    m_bLocked = false;
     m_mutex->Unlock();
+  }
 }
 
 void CLockObject::Lock(void)
 {
   if (m_mutex)
-    m_mutex->Lock();
-}
-
-CCondition::CCondition(void)
-{
-  pthread_cond_init(&m_cond, NULL);
-}
-
-CCondition::~CCondition(void)
-{
-  pthread_cond_broadcast(&m_cond);
-  pthread_cond_destroy(&m_cond);
-}
-
-void CCondition::Broadcast(void)
-{
-  pthread_cond_broadcast(&m_cond);
+    m_bLocked = m_mutex->Lock();
 }
 
-void CCondition::Signal(void)
-{
-  pthread_cond_signal(&m_cond);
-}
-
-bool CCondition::Wait(CMutex *mutex, int64_t iTimeout)
-{
-  bool bReturn(false);
-  sched_yield();
-  if (mutex)
-  {
-    struct timespec abstime;
-    struct timeval now;
-    if (gettimeofday(&now, NULL) == 0)
-    {
-      iTimeout       += now.tv_usec / 1000;
-      abstime.tv_sec  = now.tv_sec + (time_t)(iTimeout / 1000);
-      abstime.tv_nsec = (long)((iTimeout % (unsigned long)1000) * (unsigned long)1000000);
-      bReturn         = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
-    }
-  }
-
-  return bReturn;
-}
-
-void CCondition::Sleep(int64_t iTimeout)
+void ICondition::Sleep(uint32_t iTimeout)
 {
   CCondition w;
   CMutex m;
@@ -134,60 +72,32 @@ void CCondition::Sleep(int64_t iTimeout)
   w.Wait(&m, iTimeout);
 }
 
-CThread::CThread(void) :
-    m_bRunning(false),
-    m_bStop(false)
+IThread::IThread(void) :
+    m_bStop(false),
+    m_bRunning(false)
 {
+  m_threadCondition = new CCondition();
+  m_threadMutex     = new CMutex();
 }
 
-CThread::~CThread(void)
+IThread::~IThread(void)
 {
-  m_bStop = true;
-  m_threadCondition.Broadcast();
-  pthread_join(m_thread, NULL);
+  StopThread();
+  delete m_threadCondition;
+  delete m_threadMutex;
 }
 
-bool CThread::CreateThread(void)
+bool IThread::StopThread(bool bWaitForExit /* = true */)
 {
-  bool bReturn(false);
-
-  CLockObject lock(&m_threadMutex);
-  m_bStop = false;
-  if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
-  {
-    m_bRunning = true;
-    bReturn = true;
-  }
-
-  return bReturn;
-}
-
-void *CThread::ThreadHandler(CThread *thread)
-{
-  void *retVal = NULL;
-
-  if (thread)
-    retVal = thread->Process();
-  thread->m_bRunning = false;
-
-  return retVal;
-}
-
-bool CThread::StopThread(bool bWaitForExit /* = true */)
-{
-  bool bReturn(false);
   m_bStop = true;
+  m_threadCondition->Broadcast();
+  bWaitForExit = true;
 
-  m_threadCondition.Broadcast();
-  void *retVal;
-  if (bWaitForExit)
-    bReturn = (pthread_join(m_thread, &retVal) == 0);
-
-  return bReturn;
+  return false;
 }
 
-bool CThread::Sleep(uint64_t iTimeout)
+bool IThread::Sleep(uint32_t iTimeout)
 {
-  CLockObject lock(&m_threadMutex);
-  return m_bStop ? false :m_threadCondition.Wait(&m_threadMutex, iTimeout);
+  CLockObject lock(m_threadMutex);
+  return m_bStop ? false : m_threadCondition->Wait(m_threadMutex, iTimeout);
 }