cec: stick some interfaces before the pthread stuff, so we can add other implementati...
authorLars Op den Kamp <lars@opdenkamp.eu>
Fri, 13 Jan 2012 23:49:42 +0000 (00:49 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Fri, 13 Jan 2012 23:49:42 +0000 (00:49 +0100)
18 files changed:
src/lib/AdapterCommunication.cpp
src/lib/AdapterCommunication.h
src/lib/CECProcessor.h
src/lib/Makefile.am
src/lib/devices/CECBusDevice.h
src/lib/implementations/CECCommandHandler.cpp
src/lib/implementations/CECCommandHandler.h
src/lib/platform/os-dependent.h
src/lib/platform/posix/os_posix.h [moved from src/lib/platform/linux/os_posix.h with 97% similarity]
src/lib/platform/posix/pthreads.cpp [new file with mode: 0644]
src/lib/platform/posix/pthreads.h [new file with mode: 0644]
src/lib/platform/posix/serialport.cpp [moved from src/lib/platform/linux/serialport.cpp with 100% similarity]
src/lib/platform/threads.cpp
src/lib/platform/threads.h
src/lib/platform/timeutils.h
src/lib/platform/windows/os_windows.h
src/lib/util/buffer.h
src/testclient/Makefile.am

index 8d351f058a49164409bd11380211934aa1d650a7..9697759e4e081bfa65da88a235148e456b229725 100644 (file)
@@ -33,9 +33,8 @@
 #include "AdapterCommunication.h"
 
 #include "CECProcessor.h"
+#include "platform/os-dependent.h"
 #include "platform/serialport.h"
-#include "util/StdString.h"
-#include "platform/timeutils.h"
 
 using namespace std;
 using namespace CEC;
@@ -197,9 +196,9 @@ CStdString CCECAdapterMessage::ToString(void) const
     case MSGCODE_HIGH_ERROR:
     case MSGCODE_LOW_ERROR:
       {
-        int iLine      = (size() >= 3) ? (at(1) << 8) | at(2) : 0;
+        uint32_t iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0;
         uint32_t iTime = (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
-        strMsg.AppendFormat(" line:%i", iLine);
+        strMsg.AppendFormat(" line:%u", iLine);
         strMsg.AppendFormat(" time:%u", iTime);
       }
       break;
@@ -373,7 +372,7 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
 {
   CLockObject lock(&m_mutex);
-  for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
+  for (uint8_t iPtr = 0; iPtr < iLen; iPtr++)
     m_inBuffer.Push(data[iPtr]);
 
   m_rcvCondition.Signal();
index 1b815269f8ebb4cf65f2c96a43b64e1aa80133d1..d076a2facec6ce2a3f4f42c0d59096908c0fdf05 100644 (file)
  */
 
 #include <cectypes.h>
-#include "platform/threads.h"
 #include "util/buffer.h"
-#include "util/StdString.h"
 #include <string>
+#include "util/StdString.h"
 
 namespace CEC
 {
index 8c007b8aeacbbf54fc416d19f34d20a15eb9f135..5064b9c561271cf47f50c897b2fabbbfb75efde4 100644 (file)
@@ -34,7 +34,7 @@
 #include <string>
 #include <cectypes.h>
 #include "AdapterCommunication.h"
-#include "platform/threads.h"
+#include "platform/os-dependent.h"
 #include "util/buffer.h"
 #include "util/StdString.h"
 
index 64ed009176c0298f6128da9ad26716c1e4f873e7..adcbafab0d7d52ba3397a4fc9514955264ac67b3 100644 (file)
@@ -9,43 +9,23 @@ pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libcec.pc
 
 libcec_la_SOURCES = AdapterCommunication.cpp \
-                    AdapterCommunication.h \
                     AdapterDetection.cpp \
-                    AdapterDetection.h \
                     CECProcessor.cpp \
-                    CECProcessor.h \
                     LibCEC.cpp \
-                    LibCEC.h \
                     LibCECC.cpp \
-                    util/StdString.h \
-                    devices/CECAudioSystem.h \
                                        devices/CECAudioSystem.cpp \
                     devices/CECBusDevice.cpp \
-                    devices/CECBusDevice.h \
-                    devices/CECPlaybackDevice.h \
                     devices/CECPlaybackDevice.cpp \
-                    devices/CECRecordingDevice.h \
                                        devices/CECRecordingDevice.cpp \
-                    devices/CECTuner.h \
                     devices/CECTuner.cpp \
-                    devices/CECTV.h \
                     devices/CECTV.cpp \
                     implementations/ANCommandHandler.cpp \
-                    implementations/ANCommandHandler.h \
                     implementations/CECCommandHandler.cpp \
-                    implementations/CECCommandHandler.h \
                     implementations/SLCommandHandler.cpp \
-                    implementations/SLCommandHandler.h \
                     implementations/VLCommandHandler.cpp \
-                    implementations/VLCommandHandler.h \
-                    platform/timeutils.h \
-                    platform/baudrate.h \
-                    platform/os-dependent.h \
-                    platform/linux/os_posix.h \
-                    platform/linux/serialport.cpp \
-                    platform/serialport.h \
-                    platform/threads.cpp \
-                    platform/threads.h
+                    platform/posix/serialport.cpp \
+                    platform/posix/pthreads.cpp \
+                    platform/threads.cpp
 
 libcec_la_LDFLAGS = @LIBS@ -version-info @VERSION@
 libcec_la_CPPFLAGS = -I@abs_top_srcdir@/include
index 2bef3a7657d5f61afb81c55f76d02e1950edfc9d..5d835c7b47eaf52115b58442a716a13ca0711166 100644 (file)
@@ -33,7 +33,7 @@
 
 #include <cectypes.h>
 #include <set>
-#include "../platform/threads.h"
+#include "../platform/os-dependent.h"
 #include "../util/StdString.h"
 
 namespace CEC
index bed4ff9fbb33eac6d48be69a8b46673016fd1b25..5df30f44a272a172ba5e7fb65dfefe46696a96b7 100644 (file)
@@ -426,7 +426,7 @@ bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
     m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
 
     vector<CCECBusDevice *> devices;
-    for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
+    for (int iDevicePtr = GetMyDevices(devices) - 1; iDevicePtr >=0; iDevicePtr--)
       devices[iDevicePtr]->TransmitActiveSource();
 
     return true;
@@ -651,9 +651,9 @@ void CCECCommandHandler::UnhandledCommand(const cec_command &command)
   m_busDevice->AddLog(CEC_LOG_DEBUG, strLog);
 }
 
-unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
+size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
 {
-  unsigned int iReturn(0);
+  size_t iReturn(0);
 
   cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
   for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
@@ -842,7 +842,7 @@ bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, c
 {
   cec_command command;
   cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
-  for (unsigned int iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
+  for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
     command.parameters.PushBack(strDeviceName.at(iPtr));
 
   return Transmit(command, false);
@@ -854,10 +854,10 @@ bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator,
   cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
   command.parameters.PushBack((uint8_t)duration);
 
-  unsigned int iLen = strlen(strMessage);
+  size_t iLen = strlen(strMessage);
   if (iLen > 13) iLen = 13;
 
-  for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
+  for (size_t iPtr = 0; iPtr < iLen; iPtr++)
     command.parameters.PushBack(strMessage[iPtr]);
 
   return Transmit(command, false);
index 71baddeffcf251d4cdc00f663be3110a537a162d..f3d3d0c5fa4cb2bb06609bc1c823d7b01a2abf3d 100644 (file)
@@ -34,7 +34,7 @@
 #include <cectypes.h>
 #include <vector>
 #include "../util/StdString.h"
-#include "../platform/threads.h"
+#include "../platform/os-dependent.h"
 
 namespace CEC
 {
@@ -125,7 +125,7 @@ namespace CEC
     virtual bool HandleVendorCommand(const cec_command &command);
     virtual void UnhandledCommand(const cec_command &command);
 
-    virtual unsigned int GetMyDevices(std::vector<CCECBusDevice *> &devices) const;
+    virtual size_t GetMyDevices(std::vector<CCECBusDevice *> &devices) const;
     virtual CCECBusDevice *GetDevice(cec_logical_address iLogicalAddress) const;
     virtual CCECBusDevice *GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const;
     virtual CCECBusDevice *GetDeviceByType(cec_device_type type) const;
index 3ea7cb4483e4174ea4ba55fa65fe5e9af8ebc09e..90e42abb82de83ecfb4aa5fe6a7cf90da4776a42 100644 (file)
@@ -27,7 +27,7 @@
 #if defined(__WINDOWS__)
 #include "windows/os_windows.h"
 #else
-#include "linux/os_posix.h"
+#include "posix/os_posix.h"
 #endif
 
 #if !defined(TRUE)
@@ -37,3 +37,5 @@
 #if !defined(FALSE)
 #define FALSE 0
 #endif
+
+#include "timeutils.h"
similarity index 97%
rename from src/lib/platform/linux/os_posix.h
rename to src/lib/platform/posix/os_posix.h
index 9cc4d095424f4f3c7792c251f4e5f4d9b7277904..65d2f27eda0425f6b57ccad0ad661457c9d6b2bb 100644 (file)
@@ -31,3 +31,4 @@
 
 #define LIBTYPE
 #define DECLSPEC
+#include "pthreads.h"
diff --git a/src/lib/platform/posix/pthreads.cpp b/src/lib/platform/posix/pthreads.cpp
new file mode 100644 (file)
index 0000000..e5b4cf1
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * This file is part of the libCEC(R) library.
+ *
+ * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is an original work, containing original code.
+ *
+ * libCEC(R) is a trademark of Pulse-Eight Limited.
+ *
+ * This program is dual-licensed; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ * Alternatively, you can license this library under a commercial license,
+ * please contact Pulse-Eight Licensing for more information.
+ *
+ * For more information contact:
+ * Pulse-Eight Licensing       <license@pulse-eight.com>
+ *     http://www.pulse-eight.com/
+ *     http://www.pulse-eight.net/
+ */
+
+#include "../threads.h"
+#include "../timeutils.h"
+
+using namespace CEC;
+
+CMutex::CMutex(bool bRecursive /* = true */) :
+    IMutex(bRecursive)
+{
+  pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : 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);
+}
+
+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;
+}
+
+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);
+}
+
+void CCondition::Signal(void)
+{
+  pthread_cond_signal(&m_cond);
+}
+
+bool CCondition::Wait(IMutex *mutex, uint32_t iTimeout /* = 0 */)
+{
+  bool bReturn(false);
+  sched_yield();
+  CMutex *pmutex = static_cast<CMutex *>(mutex);
+  if (pmutex)
+  {
+    if (iTimeout > 0)
+    {
+      struct timespec abstime;
+      struct timeval now;
+      gettimeofday(&now, NULL);
+      iTimeout       += now.tv_usec / 1000;
+      abstime.tv_sec  = now.tv_sec + (time_t)(iTimeout / 1000);
+      abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
+      bReturn         = (pthread_cond_timedwait(&m_cond, &pmutex->m_mutex, &abstime) == 0);
+    }
+    else
+    {
+      bReturn         = (pthread_cond_wait(&m_cond, &pmutex->m_mutex) == 0);
+    }
+  }
+  return bReturn;
+}
+
+bool CThread::CreateThread(bool bWait /* = 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)
+  {
+    if (bWait)
+      m_threadCondition->Wait(m_threadMutex);
+    bReturn = true;
+  }
+  return bReturn;
+}
+
+bool CThread::StopThread(bool bWaitForExit /* = true */)
+{
+  bool bReturn = IThread::StopThread(bWaitForExit);
+
+  void *retVal;
+  if (bWaitForExit && m_bRunning)
+    bReturn = (pthread_join(m_thread, &retVal) == 0);
+
+  return bReturn;
+}
+
+void *CThread::ThreadHandler(CThread *thread)
+{
+  void *retVal = NULL;
+
+  if (thread)
+  {
+    CLockObject lock(thread->m_threadMutex);
+    thread->m_bRunning = true;
+    lock.Leave();
+    thread->m_threadCondition->Broadcast();
+
+    retVal = thread->Process();
+
+    lock.Lock();
+    thread->m_bRunning = false;
+    lock.Leave();
+    thread->m_threadCondition->Broadcast();
+  }
+
+  return retVal;
+}
diff --git a/src/lib/platform/posix/pthreads.h b/src/lib/platform/posix/pthreads.h
new file mode 100644 (file)
index 0000000..e09945a
--- /dev/null
@@ -0,0 +1,82 @@
+#pragma once
+/*
+ * This file is part of the libCEC(R) library.
+ *
+ * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is an original work, containing original code.
+ *
+ * libCEC(R) is a trademark of Pulse-Eight Limited.
+ *
+ * This program is dual-licensed; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ * Alternatively, you can license this library under a commercial license,
+ * please contact Pulse-Eight Licensing for more information.
+ *
+ * For more information contact:
+ * Pulse-Eight Licensing       <license@pulse-eight.com>
+ *     http://www.pulse-eight.com/
+ *     http://www.pulse-eight.net/
+ */
+
+#include "../threads.h"
+
+namespace CEC
+{
+  class CMutex : public IMutex
+  {
+  public:
+    CMutex(bool bRecursive = true);
+    virtual ~CMutex(void);
+
+    virtual bool TryLock(void);
+    virtual bool Lock(void);
+    virtual void Unlock(void);
+
+    pthread_mutex_t m_mutex;
+
+  private:
+    static pthread_mutexattr_t *GetMutexAttribute();
+  };
+
+  class CCondition : public ICondition
+  {
+  public:
+    CCondition(void);
+    virtual ~CCondition(void);
+
+    virtual void Broadcast(void);
+    virtual void Signal(void);
+    virtual bool Wait(IMutex *mutex, uint32_t iTimeout = 0);
+
+  private:
+    pthread_cond_t  m_cond;
+  };
+
+  class CThread : public IThread
+  {
+  public:
+    CThread(void);
+    virtual ~CThread(void);
+
+    virtual bool CreateThread(bool bWait = true);
+    virtual bool StopThread(bool bWaitForExit = true);
+
+    static void *ThreadHandler(CThread *thread);
+
+  private:
+    pthread_t  m_thread;
+  };
+};
index d168e906a671ddc8e0caab582c9ae30a58bd2853..c9f6c16214f652fb0179e669014cd44885da34f6 100644 (file)
 
 #include "threads.h"
 #include "timeutils.h"
+#include "os-dependent.h"
 
 using namespace CEC;
 
-CMutex::CMutex(bool bRecursive /* = true */)
-{
-  pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : 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);
-}
-
-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 */) :
+CLockObject::CLockObject(IMutex *mutex, bool bTryLock /* = false */) :
   m_mutex(mutex)
 {
   if (m_mutex)
@@ -101,53 +64,7 @@ void CLockObject::Lock(void)
     m_bLocked = 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);
-}
-
-void CCondition::Signal(void)
-{
-  pthread_cond_signal(&m_cond);
-}
-
-bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout /* = 0 */)
-{
-  bool bReturn(false);
-  sched_yield();
-  if (mutex)
-  {
-    if (iTimeout > 0)
-    {
-      struct timespec abstime;
-      struct timeval now;
-      gettimeofday(&now, NULL);
-      iTimeout       += now.tv_usec / 1000;
-      abstime.tv_sec  = now.tv_sec + (time_t)(iTimeout / 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
-    {
-      bReturn         = (pthread_cond_wait(&m_cond, &mutex->m_mutex) == 0);
-    }
-  }
-
-  return bReturn;
-}
-
-void CCondition::Sleep(uint32_t iTimeout)
+void ICondition::Sleep(uint32_t iTimeout)
 {
   CCondition w;
   CMutex m;
@@ -155,71 +72,32 @@ void CCondition::Sleep(uint32_t iTimeout)
   w.Wait(&m, iTimeout);
 }
 
-CThread::CThread(void) :
+IThread::IThread(void) :
     m_bStop(false),
     m_bRunning(false)
 {
+  m_threadCondition = new CCondition();
+  m_threadMutex     = new CMutex();
 }
 
-CThread::~CThread(void)
+IThread::~IThread(void)
 {
   StopThread();
+  delete m_threadCondition;
+  delete m_threadMutex;
 }
 
-bool CThread::CreateThread(bool bWait /* = 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)
-  {
-    if (bWait)
-      m_threadCondition.Wait(&m_threadMutex);
-    bReturn = true;
-  }
-
-  return bReturn;
-}
-
-void *CThread::ThreadHandler(CThread *thread)
-{
-  void *retVal = NULL;
-
-  if (thread)
-  {
-    CLockObject lock(&thread->m_threadMutex);
-    thread->m_bRunning = true;
-    lock.Leave();
-    thread->m_threadCondition.Broadcast();
-
-    retVal = thread->Process();
-
-    lock.Lock();
-    thread->m_bRunning = false;
-    lock.Leave();
-    thread->m_threadCondition.Broadcast();
-  }
-
-  return retVal;
-}
-
-bool CThread::StopThread(bool bWaitForExit /* = true */)
+bool IThread::StopThread(bool bWaitForExit /* = true */)
 {
-  bool bReturn(true);
   m_bStop = true;
+  m_threadCondition->Broadcast();
+  bWaitForExit = true;
 
-  m_threadCondition.Broadcast();
-
-  void *retVal;
-  if (bWaitForExit && m_bRunning)
-    bReturn = (pthread_join(m_thread, &retVal) == 0);
-
-  return bReturn;
+  return false;
 }
 
-bool CThread::Sleep(uint32_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);
 }
index a258081ccef8bbdf6a5bc8f33826fbaf26ae6c84..2c3c2c540de03a369a3332a4e7e0e8a30309fc6d 100644 (file)
  *     http://www.pulse-eight.net/
  */
 
-#include "os-dependent.h"
 #include <stdint.h>
 
 namespace CEC
 {
-  class CMutex;
+  class IMutex
+  {
+  public:
+    IMutex(bool bRecursive = true) { m_bRecursive = bRecursive ; };
+    virtual ~IMutex(void) {};
+
+    virtual bool TryLock(void) = 0;
+    virtual bool Lock(void) = 0;
+    virtual void Unlock(void) = 0;
+
+  protected:
+    bool m_bRecursive;
+  };
 
-  class CCondition
+  class ICondition
   {
   public:
-    CCondition(void);
-    virtual ~CCondition(void);
+    virtual void Broadcast(void) = 0;
+    virtual void Signal(void) = 0;
+    virtual bool Wait(IMutex *mutex, uint32_t iTimeout = 0) = 0;
 
-    void Broadcast(void);
-    void Signal(void);
-    bool Wait(CMutex *mutex, uint32_t iTimeout = 0);
     static void Sleep(uint32_t iTimeout);
-
-  private:
-    pthread_cond_t  m_cond;
   };
 
-  class CMutex
+  class IThread
   {
   public:
-    CMutex(bool bRecursive = true);
-    virtual ~CMutex(void);
+    IThread(void);
+    virtual ~IThread(void);
 
-    bool TryLock(void);
-    bool Lock(void);
-    void Unlock(void);
+    virtual bool IsRunning(void) const { return m_bRunning; };
+    virtual bool CreateThread(bool bWait = true) = 0;
+    virtual bool IsStopped(void) const { return m_bStop; };
 
-    pthread_mutex_t m_mutex;
+    virtual bool StopThread(bool bWaitForExit = true);
+    virtual bool Sleep(uint32_t iTimeout);
 
-  private:
-    static pthread_mutexattr_t *GetMutexAttribute();
+    virtual void *Process(void) = 0;
+
+  protected:
+    bool        m_bStop;
+    bool        m_bRunning;
+    ICondition *m_threadCondition;
+    IMutex     *m_threadMutex;
   };
 
   class CLockObject
   {
   public:
-    CLockObject(CMutex *mutex, bool bTryLock = false);
+    CLockObject(IMutex *mutex, bool bTryLock = false);
     ~CLockObject(void);
 
     bool IsLocked(void) const { return m_bLocked; }
@@ -80,32 +92,7 @@ namespace CEC
     void Lock(void);
 
   private:
-    CMutex *m_mutex;
+    IMutex *m_mutex;
     bool    m_bLocked;
   };
-
-  class CThread
-  {
-  public:
-    CThread(void);
-    virtual ~CThread(void);
-
-    virtual bool IsRunning(void) const { return m_bRunning; }
-    virtual bool CreateThread(bool bWait = true);
-    virtual bool StopThread(bool bWaitForExit = true);
-    virtual bool IsStopped(void) const { return m_bStop; };
-    virtual bool Sleep(uint32_t iTimeout);
-
-    static void *ThreadHandler(CThread *thread);
-    virtual void *Process(void) = 0;
-
-  protected:
-    CCondition m_threadCondition;
-
-  private:
-    pthread_t  m_thread;
-    CMutex     m_threadMutex;
-    bool       m_bStop;
-    bool       m_bRunning;
-  };
 };
index d793005b80fbcd210df373d58d874fcc09af5b74..f05f7e7139b598b98c820c7bf8c6091e46461847 100644 (file)
@@ -41,6 +41,8 @@
 #include <sys/time.h>
 #endif
 
+#include "os-dependent.h"
+
 namespace CEC
 {
   inline int64_t GetTimeMs()
index ae17d63b6537e93d09bbce38fd9f4659ceef7ae5..b35a89101508d6cf88c8b3f930d5005036e1b39c 100644 (file)
 #include <winsock2.h>
 #pragma warning(default:4005)
 
+#if defined(_WIN32)
 #include "../pthread_win32/pthread.h"
+#include "../pthreads.h"
+#endif
 
 #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
 # define __USE_FILE_OFFSET64   1
index ded4001dac43586d92fac960938de629195f6b65..e0afddfa16d1daaf52af97f6fee5fbd5c7e4b0e9 100644 (file)
@@ -31,7 +31,7 @@
  *     http://www.pulse-eight.net/
  */
 
-#include "../platform/threads.h"
+#include "../platform/os-dependent.h"
 #include <queue>
 
 namespace CEC
@@ -40,7 +40,7 @@ namespace CEC
     struct CecBuffer
     {
     public:
-      CecBuffer(unsigned int iMaxSize = 100)
+      CecBuffer(size_t iMaxSize = 100)
       {
         m_maxSize = iMaxSize;
       }
@@ -56,7 +56,7 @@ namespace CEC
           m_buffer.pop();
       }
 
-      int Size(void) const { return m_buffer.size(); }
+      size_t Size(void) const { return m_buffer.size(); }
 
       bool Push(_BType entry)
       {
@@ -82,7 +82,7 @@ namespace CEC
       }
 
     private:
-      unsigned int       m_maxSize;
+      size_t             m_maxSize;
       std::queue<_BType> m_buffer;
       CMutex             m_mutex;
     };
index 34af79bcd62478162de6603aae250fb770b7f232..14b36f818821ddbe2d679ae9872428d9d84fb5c0 100644 (file)
@@ -1,6 +1,7 @@
 bin_PROGRAMS = cec-client
 cec_client_SOURCES = main.cpp \
-                     ../lib/platform/threads.cpp
+                     ../lib/platform/threads.cpp \
+                     ../lib/platform/posix/pthreads.cpp
 
 cec_client_CPPFLAGS = -I@abs_top_srcdir@/include
 cec_client_LDFLAGS = @LIBS_DL@
\ No newline at end of file