Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes undefined behaviour which occurs when mutex is locked in one #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/sick_safetyscanners/cola2/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <sick_safetyscanners/data_processing/ReadWriteHelper.hpp>

#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>

namespace sick {
namespace cola2 {
Expand Down Expand Up @@ -197,7 +198,9 @@ class Command
private:
std::shared_ptr<sick::data_processing::ParseTCPPacket> m_tcp_parser_ptr;

boost::condition_variable m_execute_finished_cond_var;
boost::mutex m_execution_mutex;
bool m_is_executing;

bool m_was_successful;

Expand Down
13 changes: 10 additions & 3 deletions src/cola2/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace cola2 {

Command::Command(Cola2Session& session, const uint16_t& command_type, const uint16_t& command_mode)
: m_session(session)
, m_is_executing(false)
, m_command_mode(command_mode)
, m_command_type(command_type)
{
Expand All @@ -52,7 +53,8 @@ Command::Command(Cola2Session& session, const uint16_t& command_type, const uint

void Command::lockExecutionMutex()
{
m_execution_mutex.lock();
boost::mutex::scoped_lock lock(m_execution_mutex);
m_is_executing = true;
}

std::vector<uint8_t> Command::constructTelegram(const std::vector<uint8_t>& telegram) const
Expand All @@ -65,12 +67,17 @@ void Command::processReplyBase(const std::vector<uint8_t>& packet)
{
m_tcp_parser_ptr->parseTCPSequence(packet, *this);
m_was_successful = processReply();
m_execution_mutex.unlock();
}

boost::mutex::scoped_lock lock(m_execution_mutex);
m_is_executing = false;
m_execute_finished_cond_var.notify_one();
}

void Command::waitForCompletion()
{
boost::mutex::scoped_lock lock(m_execution_mutex);
while(m_is_executing)
m_execute_finished_cond_var.wait(lock);
}

bool Command::wasSuccessful() const
Expand Down