mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-23 04:33:34 +08:00
Added syncronization to t_send worker thread
Which avoids possible deadlocks
This commit is contained in:
committed by
Nuno Marques
parent
f0d22d3962
commit
991399f105
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user