From: Lars Op den Kamp Date: Tue, 4 Oct 2011 18:48:02 +0000 (+0200) Subject: cec: mutex in CSerialPort X-Git-Tag: upstream/2.2.0~1^2~294 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b1f5095277970600e999f0ba268b1ea11e778614;p=deb_libcec.git cec: mutex in CSerialPort --- diff --git a/include/CECExportsCpp.h b/include/CECExportsCpp.h index 54dbcbf..f052592 100644 --- a/include/CECExportsCpp.h +++ b/include/CECExportsCpp.h @@ -183,6 +183,7 @@ inline CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_addres */ inline void UnloadLibCec(CEC::ICECAdapter *device) { + device->Close(); delete device; }; #endif diff --git a/src/lib/platform/linux/serialport.cpp b/src/lib/platform/linux/serialport.cpp index e6bfafa..1877b0e 100644 --- a/src/lib/platform/linux/serialport.cpp +++ b/src/lib/platform/linux/serialport.cpp @@ -38,7 +38,8 @@ CSerialPort::~CSerialPort() int CSerialPort::Write(uint8_t* data, int len) { fd_set port; - + + CLockObject lock(&m_mutex); if (m_fd == -1) { m_error = "port closed"; @@ -87,6 +88,7 @@ int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /*= -1*/) int64_t now, target; int bytesread = 0; + CLockObject lock(&m_mutex); if (m_fd == -1) { m_error = "port closed"; @@ -157,7 +159,8 @@ bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stop { m_name = name; m_error = strerror(errno); - + CLockObject lock(&m_mutex); + if (databits < 5 || databits > 8) { m_error = "Databits has to be between 5 and 8"; @@ -244,6 +247,7 @@ bool CSerialPort::Open(string name, int baudrate, int databits/* = 8*/, int stop void CSerialPort::Close() { + CLockObject lock(&m_mutex); if (m_fd != -1) { close(m_fd); @@ -286,7 +290,8 @@ bool CSerialPort::SetBaudRate(int baudrate) return true; } -bool CSerialPort::IsOpen() const +bool CSerialPort::IsOpen() { + CLockObject lock(&m_mutex); return m_fd != -1; } diff --git a/src/lib/platform/serialport.h b/src/lib/platform/serialport.h index c7e0e6c..2cdc108 100644 --- a/src/lib/platform/serialport.h +++ b/src/lib/platform/serialport.h @@ -23,11 +23,11 @@ #include #include #include +#include "../platform/threads.h" #ifndef __WINDOWS__ #include #else -#include "../util/threads.h" #include "../util/buffer.h" #endif @@ -44,8 +44,7 @@ namespace CEC virtual ~CSerialPort(); bool Open(std::string name, int baudrate, int databits = 8, int stopbits = 1, int parity = PAR_NONE); - bool SetBaudRate(int baudrate); - bool IsOpen() const; + bool IsOpen(); void Close(); int Write(std::vector data) @@ -59,8 +58,11 @@ namespace CEC std::string GetName() { return m_name; } private: + bool SetBaudRate(int baudrate); + std::string m_error; std::string m_name; + CMutex m_mutex; #ifdef __WINDOWS__ bool SetTimeouts(bool bBlocking);