Added syncronization to t_send worker thread

Which avoids possible deadlocks
This commit is contained in:
Peter van der Perk
2019-11-08 18:58:38 +01:00
committed by Nuno Marques
parent f0d22d3962
commit 991399f105
5 changed files with 69 additions and 71 deletions
+26 -6
View File
@@ -74,9 +74,12 @@ except AttributeError:
@(topic)_Subscriber::~@(topic)_Subscriber() { Domain::removeParticipant(mp_participant);}
bool @(topic)_Subscriber::init(std::condition_variable* cv)
bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable* t_send_queue_cv, std::mutex* t_send_queue_mutex, std::queue<uint8_t>* t_send_queue)
{
m_listener.cv_msg = cv;
m_listener.topic_ID = topic_ID;
m_listener.t_send_queue_cv = t_send_queue_cv;
m_listener.t_send_queue_mutex = t_send_queue_mutex;
m_listener.t_send_queue = t_send_queue;
// Create RTPSParticipant
ParticipantAttributes PParam;
@@ -132,17 +135,27 @@ void @(topic)_Subscriber::SubListener::onSubscriptionMatched(Subscriber* sub, Ma
void @(topic)_Subscriber::SubListener::onNewDataMessage(Subscriber* sub)
{
std::unique_lock<std::mutex> has_msg_lock(has_msg_mutex);
if(has_msg.load() == true) // Check if msg has been fetched
{
has_msg_cv.wait(has_msg_lock); // Wait till msg has been fetched
}
has_msg_lock.unlock();
// Take data
if(sub->takeNextData(&msg, &m_info))
{
if(m_info.sampleKind == ALIVE)
{
// Print your structure data here.
std::unique_lock<std::mutex> lk(*t_send_queue_mutex);
++n_msg;
//std::cout << "Sample received, count=" << n_msg << std::endl;
has_msg = true;
cv_msg->notify_all();
t_send_queue->push(topic_ID);
lk.unlock();
t_send_queue_cv->notify_one();
}
}
@@ -174,6 +187,13 @@ bool @(topic)_Subscriber::hasMsg()
@[ end if]@
@[end if]@
{
m_listener.has_msg = false;
return m_listener.msg;
}
void @(topic)_Subscriber::unlockMsg()
{
std::unique_lock<std::mutex> has_msg_lock(m_listener.has_msg_mutex);
m_listener.has_msg = false;
has_msg_lock.unlock();
m_listener.has_msg_cv.notify_one();
}