cec: stick some interfaces before the pthread stuff, so we can add other implementati...
[deb_libcec.git] / src / lib / platform / threads.cpp
index 7c5b6ce4ed716de8cda00689de685482fca57eda..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)
 {
-  m_mutex->Lock();
+  if (m_mutex)
+    m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
 }
 
 CLockObject::~CLockObject(void)
@@ -74,91 +51,53 @@ CLockObject::~CLockObject(void)
 
 void CLockObject::Leave(void)
 {
-  m_mutex->Unlock();
+  if (m_mutex && m_bLocked)
+  {
+    m_bLocked = false;
+    m_mutex->Unlock();
+  }
 }
 
 void CLockObject::Lock(void)
 {
-  m_mutex->Lock();
+  if (m_mutex)
+    m_bLocked = m_mutex->Lock();
 }
 
-CCondition::CCondition(void)
+void ICondition::Sleep(uint32_t iTimeout)
 {
-  pthread_cond_init(&m_cond, NULL);
-}
-
-CCondition::~CCondition(void)
-{
-  pthread_cond_broadcast(&m_cond);
-  pthread_cond_destroy(&m_cond);
-}
-
-void CCondition::Signal(void)
-{
-  pthread_cond_broadcast(&m_cond);
-}
-
-bool CCondition::Wait(CMutex *mutex, int64_t iTimeout)
-{
-  bool bReturn(false);
-  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(int iTimeout)
-{
-  sched_yield();
   CCondition w;
   CMutex m;
   CLockObject lock(&m);
-  w.Wait(&m, 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;
-  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);
-
-  if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
-  {
-    m_bRunning = true;
-    pthread_detach(m_thread);
-    bReturn = true;
-  }
+  m_bStop = true;
+  m_threadCondition->Broadcast();
+  bWaitForExit = true;
 
-  return bReturn;
+  return false;
 }
 
-void *CThread::ThreadHandler(CThread *thread)
+bool IThread::Sleep(uint32_t iTimeout)
 {
-  if (thread)
-    return thread->Process();
-  return NULL;
-}
-
-void CThread::StopThread(bool bWaitForExit /* = true */)
-{
-  m_bStop = true;
-  if (bWaitForExit)
-    pthread_join(m_thread, NULL);
+  CLockObject lock(m_threadMutex);
+  return m_bStop ? false : m_threadCondition->Wait(m_threadMutex, iTimeout);
 }