From 6f14b51266d3af3f215171a4a5fca5be011c023e Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sat, 14 Jan 2012 00:49:42 +0100 Subject: [PATCH] cec: stick some interfaces before the pthread stuff, so we can add other implementations too --- src/lib/AdapterCommunication.cpp | 9 +- src/lib/AdapterCommunication.h | 3 +- src/lib/CECProcessor.h | 2 +- src/lib/Makefile.am | 26 +-- src/lib/devices/CECBusDevice.h | 2 +- src/lib/implementations/CECCommandHandler.cpp | 12 +- src/lib/implementations/CECCommandHandler.h | 4 +- src/lib/platform/os-dependent.h | 4 +- src/lib/platform/{linux => posix}/os_posix.h | 1 + src/lib/platform/posix/pthreads.cpp | 168 ++++++++++++++++++ src/lib/platform/posix/pthreads.h | 82 +++++++++ .../platform/{linux => posix}/serialport.cpp | 0 src/lib/platform/threads.cpp | 154 ++-------------- src/lib/platform/threads.h | 81 ++++----- src/lib/platform/timeutils.h | 2 + src/lib/platform/windows/os_windows.h | 3 + src/lib/util/buffer.h | 8 +- src/testclient/Makefile.am | 3 +- 18 files changed, 333 insertions(+), 231 deletions(-) rename src/lib/platform/{linux => posix}/os_posix.h (97%) create mode 100644 src/lib/platform/posix/pthreads.cpp create mode 100644 src/lib/platform/posix/pthreads.h rename src/lib/platform/{linux => posix}/serialport.cpp (100%) diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 8d351f0..9697759 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -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(); diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index 1b81526..d076a2f 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -32,10 +32,9 @@ */ #include -#include "platform/threads.h" #include "util/buffer.h" -#include "util/StdString.h" #include +#include "util/StdString.h" namespace CEC { diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 8c007b8..5064b9c 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -34,7 +34,7 @@ #include #include #include "AdapterCommunication.h" -#include "platform/threads.h" +#include "platform/os-dependent.h" #include "util/buffer.h" #include "util/StdString.h" diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 64ed009..adcbafa 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -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 diff --git a/src/lib/devices/CECBusDevice.h b/src/lib/devices/CECBusDevice.h index 2bef3a7..5d835c7 100644 --- a/src/lib/devices/CECBusDevice.h +++ b/src/lib/devices/CECBusDevice.h @@ -33,7 +33,7 @@ #include #include -#include "../platform/threads.h" +#include "../platform/os-dependent.h" #include "../util/StdString.h" namespace CEC diff --git a/src/lib/implementations/CECCommandHandler.cpp b/src/lib/implementations/CECCommandHandler.cpp index bed4ff9..5df30f4 100644 --- a/src/lib/implementations/CECCommandHandler.cpp +++ b/src/lib/implementations/CECCommandHandler.cpp @@ -426,7 +426,7 @@ bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command) m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str()); vector 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 &devices) const +size_t CCECCommandHandler::GetMyDevices(vector &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); diff --git a/src/lib/implementations/CECCommandHandler.h b/src/lib/implementations/CECCommandHandler.h index 71badde..f3d3d0c 100644 --- a/src/lib/implementations/CECCommandHandler.h +++ b/src/lib/implementations/CECCommandHandler.h @@ -34,7 +34,7 @@ #include #include #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 &devices) const; + virtual size_t GetMyDevices(std::vector &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; diff --git a/src/lib/platform/os-dependent.h b/src/lib/platform/os-dependent.h index 3ea7cb4..90e42ab 100644 --- a/src/lib/platform/os-dependent.h +++ b/src/lib/platform/os-dependent.h @@ -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" diff --git a/src/lib/platform/linux/os_posix.h b/src/lib/platform/posix/os_posix.h similarity index 97% rename from src/lib/platform/linux/os_posix.h rename to src/lib/platform/posix/os_posix.h index 9cc4d09..65d2f27 100644 --- a/src/lib/platform/linux/os_posix.h +++ b/src/lib/platform/posix/os_posix.h @@ -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 index 0000000..e5b4cf1 --- /dev/null +++ b/src/lib/platform/posix/pthreads.cpp @@ -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 + * 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(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 index 0000000..e09945a --- /dev/null +++ b/src/lib/platform/posix/pthreads.h @@ -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 + * 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; + }; +}; diff --git a/src/lib/platform/linux/serialport.cpp b/src/lib/platform/posix/serialport.cpp similarity index 100% rename from src/lib/platform/linux/serialport.cpp rename to src/lib/platform/posix/serialport.cpp diff --git a/src/lib/platform/threads.cpp b/src/lib/platform/threads.cpp index d168e90..c9f6c16 100644 --- a/src/lib/platform/threads.cpp +++ b/src/lib/platform/threads.cpp @@ -32,48 +32,11 @@ #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); } diff --git a/src/lib/platform/threads.h b/src/lib/platform/threads.h index a258081..2c3c2c5 100644 --- a/src/lib/platform/threads.h +++ b/src/lib/platform/threads.h @@ -31,48 +31,60 @@ * http://www.pulse-eight.net/ */ -#include "os-dependent.h" #include 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; - }; }; diff --git a/src/lib/platform/timeutils.h b/src/lib/platform/timeutils.h index d793005..f05f7e7 100644 --- a/src/lib/platform/timeutils.h +++ b/src/lib/platform/timeutils.h @@ -41,6 +41,8 @@ #include #endif +#include "os-dependent.h" + namespace CEC { inline int64_t GetTimeMs() diff --git a/src/lib/platform/windows/os_windows.h b/src/lib/platform/windows/os_windows.h index ae17d63..b35a891 100644 --- a/src/lib/platform/windows/os_windows.h +++ b/src/lib/platform/windows/os_windows.h @@ -24,7 +24,10 @@ #include #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 diff --git a/src/lib/util/buffer.h b/src/lib/util/buffer.h index ded4001..e0afddf 100644 --- a/src/lib/util/buffer.h +++ b/src/lib/util/buffer.h @@ -31,7 +31,7 @@ * http://www.pulse-eight.net/ */ -#include "../platform/threads.h" +#include "../platform/os-dependent.h" #include 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; }; diff --git a/src/testclient/Makefile.am b/src/testclient/Makefile.am index 34af79b..14b36f8 100644 --- a/src/testclient/Makefile.am +++ b/src/testclient/Makefile.am @@ -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 -- 2.34.1