Menu

#13 Issue while using in a process with multiple threads

None
closed
nobody
None
5
2016-05-12
2012-02-04
No

At the outset I would like to state that I am relatively new to libserial, C++ programming and ROS, so please excuse any obvious mistakes.
I am using libserial to communicate with peripherals, in applications developed using the ROS framework (http://www.ros.org/wiki). I was experiencing read timeouts (even for large values of timeouts), random wrong data being read from the port, std::runtime_error 's in read and write operations to ports and segmentation faults in random sections of code when using libserial as a part of ROS Nodes (applications). The same programs worked fine when I did not use any ROS components in them. Similarly programs using ROS framework without libserial worked fine.
The only difference I could find was that, programs using ROS components caused a process to spawn multiple threads while without ROS the process executed a single thread.
After some debugging I found that replacing the following code in <SerialPort::SerialPortImpl::Open>
/*
* Direct all SIGIO and SIGURG signals for the port to the current
* process.
*/
if ( fcntl( mFileDescriptor,
F_SETOWN,
getpid() ) < 0 )
{
throw SerialPort::OpenFailed( strerror(errno) ) ;
}

with

/*
* Direct all SIGIO and SIGURG signals for the port to the current
* thread.
*/
struct f_owner_ex this_thread;
this_thread.type = F_OWNER_TID;
this_thread.pid = syscall(SYS_gettid);
if ( fcntl( mFileDescriptor,
F_SETOWN_EX,
&this_thread ) < 0 )
{
throw SerialPort::OpenFailed( strerror(errno) ) ;
}

has solved my problems. I have tested the applications thoroughly. Before applications would crash after running for anywhere from a couple of minutes to some hours. Now they run continuously without any of the above stated errors for as long as required.

Discussion

  • Crayzee Wulf

    Crayzee Wulf - 2016-05-12
    • status: open --> closed
    • Group: -->
     

Log in to post a comment.