From: Lars Op den Kamp Date: Wed, 28 Mar 2012 18:46:09 +0000 (+0200) Subject: cec: give priority to messages from the TV. removed the global lock in CCECProcessor... X-Git-Tag: upstream/2.2.0~1^2~31^2~43 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a4b9f56178fcd5e18255f12eba86bf544528816a;p=deb_libcec.git cec: give priority to messages from the TV. removed the global lock in CCECProcessor when sending. this is no longer needed. bugzid: 238 --- diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 378b221..96b5c73 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -456,23 +456,27 @@ void CCECProcessor::ReplaceHandlers(void) bool CCECProcessor::OnCommandReceived(const cec_command &command) { - ParseCommand(command); - return true; + return m_inBuffer.Push(command); } void *CCECProcessor::Process(void) { CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started"); + cec_command command; + command.Clear(); + while (!IsStopped() && m_communication->IsOpen()) { + if (m_inBuffer.Pop(command, 500)) + ParseCommand(command); + if (IsInitialised()) { ReplaceHandlers(); m_controller->CheckKeypressTimeout(); } - Sleep(5); } return NULL; diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 41f3fde..ce32fea 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -43,6 +43,58 @@ namespace CEC class IAdapterCommunication; class CCECBusDevice; + // a buffer that priotises the input from the TV. + // if we need more than this, we'll have to change it into a priority_queue + class CCECInputBuffer + { + public: + CCECInputBuffer(void) : m_bHasData(false) {} + virtual ~CCECInputBuffer(void) + { + m_condition.Broadcast(); + } + + bool Push(const cec_command &command) + { + bool bReturn(false); + PLATFORM::CLockObject lock(m_mutex); + if (command.initiator == CECDEVICE_TV) + bReturn = m_tvInBuffer.Push(command); + else + bReturn = m_inBuffer.Push(command); + + m_bHasData |= bReturn; + if (bReturn) + m_condition.Signal(); + + return bReturn; + } + + bool Pop(cec_command &command, uint16_t iTimeout = 10000) + { + bool bReturn(false); + PLATFORM::CLockObject lock(m_mutex); + if (m_tvInBuffer.IsEmpty() && m_inBuffer.IsEmpty() && + !m_condition.Wait(m_mutex, m_bHasData, iTimeout)) + return bReturn; + + if (m_tvInBuffer.Pop(command)) + bReturn = true; + else if (m_inBuffer.Pop(command)) + bReturn = true; + + m_bHasData = !m_tvInBuffer.IsEmpty() || !m_inBuffer.IsEmpty(); + return bReturn; + } + + private: + PLATFORM::CMutex m_mutex; + PLATFORM::CCondition m_condition; + volatile bool m_bHasData; + PLATFORM::SyncedBuffer m_tvInBuffer; + PLATFORM::SyncedBuffer m_inBuffer; + }; + class CCECProcessor : public PLATFORM::CThread, public IAdapterCommunicationCallback { public: @@ -145,7 +197,6 @@ namespace CEC virtual bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = 10000); CCECBusDevice * m_busDevices[16]; - PLATFORM::CMutex m_transmitMutex; private: bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true); @@ -175,6 +226,7 @@ namespace CEC uint8_t m_iStandardLineTimeout; uint8_t m_iRetryLineTimeout; uint64_t m_iLastTransmission; + CCECInputBuffer m_inBuffer; libcec_configuration m_configuration; }; diff --git a/src/lib/implementations/CECCommandHandler.cpp b/src/lib/implementations/CECCommandHandler.cpp index 6e1aaac..0668ee7 100644 --- a/src/lib/implementations/CECCommandHandler.cpp +++ b/src/lib/implementations/CECCommandHandler.cpp @@ -974,7 +974,6 @@ bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* { uint8_t iTries(0), iMaxTries(command.opcode == CEC_OPCODE_NONE ? 1 : m_iTransmitRetries + 1); - CLockObject writeLock(m_processor->m_transmitMutex); while (!bReturn && ++iTries <= iMaxTries) { if ((bReturn = m_processor->Transmit(command)) == true)