From 976307626d640ba5c79bbf0c888ed39560b66aaf Mon Sep 17 00:00:00 2001 From: John Harrison Date: Tue, 17 Jan 2012 15:52:57 -0600 Subject: [PATCH] =?UTF-8?q?Trying=20to=20do=20a=20first=20pass=20to=20make?= =?UTF-8?q?=20this=20thread=20safe=E2=80=A6=20not=20sure=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/serial/serial.h | 4 ++++ serial.cmake | 5 ----- src/serial.cc | 10 ++++++++++ tests/serial_tests.cc | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index 473907e..fa21c72 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -42,6 +42,8 @@ #include #include +#include + namespace serial { /*! @@ -442,6 +444,8 @@ private: // Pimpl idiom, d_pointer class SerialImpl; SerialImpl *pimpl_; + + boost::mutex mut; }; } // namespace serial diff --git a/serial.cmake b/serial.cmake index f6d81a6..8fa0f68 100644 --- a/serial.cmake +++ b/serial.cmake @@ -74,11 +74,6 @@ IF( WIN32 ) target_link_libraries(serial wsock32) ENDIF( ) -# Check for OS X and if so disable kqueue support in asio -IF(CMAKE_SYSTEM_NAME MATCHES Darwin) - add_definitions(-DBOOST_ASIO_DISABLE_KQUEUE) -ENDIF(CMAKE_SYSTEM_NAME MATCHES Darwin) - ## Build Examples # If asked to diff --git a/src/serial.cc b/src/serial.cc index beed5ba..a024538 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -5,6 +5,8 @@ #include #include +#include + #include "serial/serial.h" #ifdef _WIN32 @@ -29,11 +31,14 @@ using serial::parity_t; using serial::stopbits_t; using serial::flowcontrol_t; +using boost::mutex; + Serial::Serial (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, flowcontrol_t flowcontrol, const size_t buffer_size) : buffer_size_(buffer_size) { + mutex::scoped_lock scoped_lock(mut); pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol); read_cache_ = new char[buffer_size_]; @@ -74,6 +79,7 @@ Serial::available () string Serial::read (size_t size) { + mutex::scoped_lock scoped_lock (mut); size_t cache_size = strlen (read_cache_); if (cache_size >= size) { @@ -184,12 +190,14 @@ Serial::readlines(string eol) size_t Serial::write (const string &data) { + mutex::scoped_lock scoped_lock(mut); return pimpl_->write (data); } void Serial::setPort (const string &port) { + mutex::scoped_lock scoped_lock(mut); bool was_open = pimpl_->isOpen(); if (was_open) close(); pimpl_->setPort (port); @@ -275,6 +283,7 @@ Serial::getFlowcontrol () const void Serial::flush () { + mutex::scoped_lock scoped_lock (mut); pimpl_->flush (); memset (read_cache_, 0, buffer_size_); } @@ -286,6 +295,7 @@ void Serial::flushInput () void Serial::flushOutput () { + mutex::scoped_lock scoped_lock (mut); pimpl_->flushOutput (); memset (read_cache_, 0, buffer_size_); } diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc index d52e1e6..42983a1 100644 --- a/tests/serial_tests.cc +++ b/tests/serial_tests.cc @@ -17,7 +17,7 @@ int main(int argc, char **argv) { while (1) { // size_t available = s.available(); // cout << "avialable: " << available << endl; - string line = s.read(); + string line = s.readline(); if (line.empty()) cout << "Nothing\n"; cout << count << ": " << line << line.length() << endl;