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:
parent
fe61b346da
commit
976307626d
@ -42,6 +42,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
#include <boost/thread/mutex.hpp>
|
||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -442,6 +444,8 @@ private:
|
|||||||
// Pimpl idiom, d_pointer
|
// Pimpl idiom, d_pointer
|
||||||
class SerialImpl;
|
class SerialImpl;
|
||||||
SerialImpl *pimpl_;
|
SerialImpl *pimpl_;
|
||||||
|
|
||||||
|
boost::mutex mut;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace serial
|
} // namespace serial
|
||||||
|
|||||||
@ -74,11 +74,6 @@ IF( WIN32 )
|
|||||||
target_link_libraries(serial wsock32)
|
target_link_libraries(serial wsock32)
|
||||||
ENDIF( )
|
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
|
## Build Examples
|
||||||
|
|
||||||
# If asked to
|
# If asked to
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <boost/thread/mutex.hpp>
|
||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -29,11 +31,14 @@ using serial::parity_t;
|
|||||||
using serial::stopbits_t;
|
using serial::stopbits_t;
|
||||||
using serial::flowcontrol_t;
|
using serial::flowcontrol_t;
|
||||||
|
|
||||||
|
using boost::mutex;
|
||||||
|
|
||||||
Serial::Serial (const string &port, unsigned long baudrate, long timeout,
|
Serial::Serial (const string &port, unsigned long baudrate, long timeout,
|
||||||
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
|
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
|
||||||
flowcontrol_t flowcontrol, const size_t buffer_size)
|
flowcontrol_t flowcontrol, const size_t buffer_size)
|
||||||
: buffer_size_(buffer_size)
|
: buffer_size_(buffer_size)
|
||||||
{
|
{
|
||||||
|
mutex::scoped_lock scoped_lock(mut);
|
||||||
pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity,
|
pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity,
|
||||||
stopbits, flowcontrol);
|
stopbits, flowcontrol);
|
||||||
read_cache_ = new char[buffer_size_];
|
read_cache_ = new char[buffer_size_];
|
||||||
@ -74,6 +79,7 @@ Serial::available ()
|
|||||||
string
|
string
|
||||||
Serial::read (size_t size)
|
Serial::read (size_t size)
|
||||||
{
|
{
|
||||||
|
mutex::scoped_lock scoped_lock (mut);
|
||||||
size_t cache_size = strlen (read_cache_);
|
size_t cache_size = strlen (read_cache_);
|
||||||
if (cache_size >= size)
|
if (cache_size >= size)
|
||||||
{
|
{
|
||||||
@ -184,12 +190,14 @@ Serial::readlines(string eol)
|
|||||||
size_t
|
size_t
|
||||||
Serial::write (const string &data)
|
Serial::write (const string &data)
|
||||||
{
|
{
|
||||||
|
mutex::scoped_lock scoped_lock(mut);
|
||||||
return pimpl_->write (data);
|
return pimpl_->write (data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::setPort (const string &port)
|
Serial::setPort (const string &port)
|
||||||
{
|
{
|
||||||
|
mutex::scoped_lock scoped_lock(mut);
|
||||||
bool was_open = pimpl_->isOpen();
|
bool was_open = pimpl_->isOpen();
|
||||||
if (was_open) close();
|
if (was_open) close();
|
||||||
pimpl_->setPort (port);
|
pimpl_->setPort (port);
|
||||||
@ -275,6 +283,7 @@ Serial::getFlowcontrol () const
|
|||||||
|
|
||||||
void Serial::flush ()
|
void Serial::flush ()
|
||||||
{
|
{
|
||||||
|
mutex::scoped_lock scoped_lock (mut);
|
||||||
pimpl_->flush ();
|
pimpl_->flush ();
|
||||||
memset (read_cache_, 0, buffer_size_);
|
memset (read_cache_, 0, buffer_size_);
|
||||||
}
|
}
|
||||||
@ -286,6 +295,7 @@ void Serial::flushInput ()
|
|||||||
|
|
||||||
void Serial::flushOutput ()
|
void Serial::flushOutput ()
|
||||||
{
|
{
|
||||||
|
mutex::scoped_lock scoped_lock (mut);
|
||||||
pimpl_->flushOutput ();
|
pimpl_->flushOutput ();
|
||||||
memset (read_cache_, 0, buffer_size_);
|
memset (read_cache_, 0, buffer_size_);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ int main(int argc, char **argv) {
|
|||||||
while (1) {
|
while (1) {
|
||||||
// size_t available = s.available();
|
// size_t available = s.available();
|
||||||
// cout << "avialable: " << available << endl;
|
// cout << "avialable: " << available << endl;
|
||||||
string line = s.read();
|
string line = s.readline();
|
||||||
if (line.empty())
|
if (line.empty())
|
||||||
cout << "Nothing\n";
|
cout << "Nothing\n";
|
||||||
cout << count << ": " << line << line.length() << endl;
|
cout << count << ": " << line << line.length() << endl;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user