1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Trying to do a first pass to make this thread safe… not sure…

This commit is contained in:
John Harrison 2012-01-17 15:52:57 -06:00
parent fe61b346da
commit 976307626d
4 changed files with 15 additions and 6 deletions

View File

@ -42,6 +42,8 @@
#include <vector>
#include <limits>
#include <boost/thread/mutex.hpp>
namespace serial {
/*!
@ -442,6 +444,8 @@ private:
// Pimpl idiom, d_pointer
class SerialImpl;
SerialImpl *pimpl_;
boost::mutex mut;
};
} // namespace serial

View File

@ -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

View File

@ -5,6 +5,8 @@
#include <cstring>
#include <algorithm>
#include <boost/thread/mutex.hpp>
#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_);
}

View File

@ -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;