updated copyright messages for 2013
[deb_libcec.git] / src / lib / platform / threads / threads.h
index 7fea6026a228ede1ecd43023d2d6d5433ac12562..dc304f056e9b7472fbaac95874dd17ea4ee9f042 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-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.
@@ -40,11 +40,16 @@ namespace PLATFORM
   public:
     CThread(void) :
         m_bStop(false),
-        m_bRunning(false) {}
+        m_bRunning(false),
+        m_bStopped(false),
+        m_thread(INVALID_THREAD_VALUE) {}
 
     virtual ~CThread(void)
     {
-      StopThread();
+      StopThread(0);
+      void *retVal = NULL;
+      if (m_thread != INVALID_THREAD_VALUE)
+        if (ThreadsWait(m_thread, &retVal)){}; // silence unused warning
     }
 
     static void *ThreadHandler(CThread *thread)
@@ -53,52 +58,60 @@ namespace PLATFORM
 
       if (thread)
       {
-        CLockObject lock(thread->m_threadMutex);
-        thread->m_bRunning = true;
-        lock.Unlock();
-        thread->m_threadCondition.Broadcast();
+        {
+          CLockObject lock(thread->m_threadMutex);
+          thread->m_bRunning = true;
+          thread->m_bStopped = false;
+          thread->m_threadCondition.Broadcast();
+        }
 
         retVal = thread->Process();
 
-        lock.Lock();
-        thread->m_bRunning = false;
-        lock.Unlock();
-        thread->m_threadCondition.Broadcast();
+        {
+          CLockObject lock(thread->m_threadMutex);
+          thread->m_bRunning = false;
+          thread->m_bStopped = true;
+          thread->m_threadCondition.Broadcast();
+        }
       }
 
       return retVal;
     }
 
-    inline bool IsRunning(void)
+    virtual bool IsRunning(void)
     {
       CLockObject lock(m_threadMutex);
       return m_bRunning;
     }
 
-    inline bool IsStopped(void)
+    virtual bool IsStopped(void)
     {
       CLockObject lock(m_threadMutex);
       return m_bStop;
     }
 
-    inline bool CreateThread(bool bWait = true)
+    virtual bool CreateThread(bool bWait = true)
     {
-        bool bReturn(false);
-        CLockObject lock(m_threadMutex);
-        if (!IsRunning())
+      bool bReturn(false);
+      CLockObject lock(m_threadMutex);
+      if (!IsRunning())
+      {
+        m_bStop = false;
+        if (ThreadsCreate(m_thread, CThread::ThreadHandler, ((void*)static_cast<CThread *>(this))))
         {
-          m_bStop = false;
-          if (ThreadsCreate(m_thread, CThread::ThreadHandler, ((void*)static_cast<CThread *>(this))))
-          {
-            if (bWait)
-              m_threadCondition.Wait(m_threadMutex);
-            bReturn = true;
-          }
+          if (bWait)
+            m_threadCondition.Wait(m_threadMutex, m_bRunning);
+          bReturn = true;
         }
+      }
       return bReturn;
     }
 
-    inline bool StopThread(bool bWaitForExit = true)
+    /*!
+     * @brief Stop the thread
+     * @param iWaitMs negative = don't wait, 0 = infinite, or the amount of ms to wait
+     */
+    virtual bool StopThread(int iWaitMs = 5000)
     {
       bool bReturn(true);
       bool bRunning(false);
@@ -106,33 +119,38 @@ namespace PLATFORM
         CLockObject lock(m_threadMutex);
         bRunning = IsRunning();
         m_bStop = true;
-        m_threadCondition.Broadcast();
       }
 
-      if (bRunning && bWaitForExit)
+      if (bRunning && iWaitMs >= 0)
       {
-        void *retVal = NULL;
-        bReturn = ThreadsWait(m_thread, &retVal);
+        CLockObject lock(m_threadMutex);
+        bReturn = m_threadCondition.Wait(m_threadMutex, m_bStopped, iWaitMs);
       }
-      return true;
+      else
+      {
+        bReturn = true;
+      }
+
+      return bReturn;
     }
 
-    inline bool Sleep(uint32_t iTimeout)
+    virtual bool Sleep(uint32_t iTimeout)
     {
       CLockObject lock(m_threadMutex);
-      return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, iTimeout);
+      return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, m_bStopped, iTimeout);
     }
 
     virtual void *Process(void) = 0;
 
   protected:
     void SetRunning(bool bSetTo);
+    CMutex           m_threadMutex;
 
   private:
-    bool       m_bStop;
-    bool       m_bRunning;
-    CCondition m_threadCondition;
-    CMutex     m_threadMutex;
-    thread_t   m_thread;
+    bool             m_bStop;
+    bool             m_bRunning;
+    bool             m_bStopped;
+    CCondition<bool> m_threadCondition;
+    thread_t         m_thread;
   };
 };