From 74a1f76282fee1b42407820b3cf18deb09bf7052 Mon Sep 17 00:00:00 2001 From: Antonio Ramos Date: Mon, 8 Jan 2018 10:57:01 +0100 Subject: [PATCH] Avoid multithreading collision --- src/algorithms/channel/adapters/channel.cc | 8 +- src/algorithms/channel/libs/channel_fsm.cc | 103 ++++++++++++------ src/algorithms/channel/libs/channel_fsm.h | 10 +- .../channel/libs/channel_msg_receiver_cc.cc | 13 ++- 4 files changed, 90 insertions(+), 44 deletions(-) diff --git a/src/algorithms/channel/adapters/channel.cc b/src/algorithms/channel/adapters/channel.cc index f4c7fa77d..353f5a080 100644 --- a/src/algorithms/channel/adapters/channel.cc +++ b/src/algorithms/channel/adapters/channel.cc @@ -197,7 +197,13 @@ void Channel::set_signal(const Gnss_Signal& gnss_signal) void Channel::start_acquisition() { - channel_fsm_.Event_start_acquisition(); + bool result = false; + result = channel_fsm_.Event_start_acquisition(); + if(!result) + { + LOG(WARNING) << "Invalid channel event"; + return; + } DLOG(INFO) << "Channel start_acquisition()"; } diff --git a/src/algorithms/channel/libs/channel_fsm.cc b/src/algorithms/channel/libs/channel_fsm.cc index 499d55881..935c696ce 100644 --- a/src/algorithms/channel/libs/channel_fsm.cc +++ b/src/algorithms/channel/libs/channel_fsm.cc @@ -53,53 +53,88 @@ ChannelFsm::ChannelFsm(std::shared_ptr acquisition) : } - -void ChannelFsm::Event_start_acquisition() +bool ChannelFsm::Event_start_acquisition() { - mx.lock(); - d_state = 1; - start_acquisition(); - DLOG(INFO) << "CH = " << channel_ << ". Ev start acquisition"; - mx.unlock(); + std::lock_guard lk(mx); + if((d_state == 1) || (d_state == 2)) + { + return false; + } + else + { + d_state = 1; + start_acquisition(); + DLOG(INFO) << "CH = " << channel_ << ". Ev start acquisition"; + return true; + } } -void ChannelFsm::Event_valid_acquisition() +bool ChannelFsm::Event_valid_acquisition() { - mx.lock(); - d_state = 2; - start_tracking(); - DLOG(INFO) << "CH = " << channel_ << ". Ev valid acquisition"; - mx.unlock(); + std::lock_guard lk(mx); + if(d_state != 1) + { + return false; + } + else + { + d_state = 2; + start_tracking(); + DLOG(INFO) << "CH = " << channel_ << ". Ev valid acquisition"; + return true; + } } -void ChannelFsm::Event_failed_acquisition_repeat() +bool ChannelFsm::Event_failed_acquisition_repeat() { - mx.lock(); - d_state = 1; - start_acquisition(); - DLOG(INFO) << "CH = " << channel_ << ". Ev failed acquisition repeat"; - mx.unlock(); -} - -void ChannelFsm::Event_failed_acquisition_no_repeat() -{ - mx.lock(); - d_state = 3; - request_satellite(); - DLOG(INFO) << "CH = " << channel_ << ". Ev failed acquisition no repeat"; - mx.unlock(); + std::lock_guard lk(mx); + if(d_state != 1) + { + return false; + } + else + { + d_state = 1; + start_acquisition(); + DLOG(INFO) << "CH = " << channel_ << ". Ev failed acquisition repeat"; + return true; + } } -void ChannelFsm::Event_failed_tracking_standby() +bool ChannelFsm::Event_failed_acquisition_no_repeat() { - mx.lock(); - d_state = 0; - notify_stop_tracking(); - DLOG(INFO) << "CH = " << channel_ << ". Ev failed tracking standby"; - mx.unlock(); + std::lock_guard lk(mx); + if(d_state != 1) + { + return false; + } + else + { + d_state = 3; + request_satellite(); + DLOG(INFO) << "CH = " << channel_ << ". Ev failed acquisition no repeat"; + return true; + } +} + + +bool ChannelFsm::Event_failed_tracking_standby() +{ + std::lock_guard lk(mx); + if(d_state != 2) + { + return false; + } + else + { + d_state = 0; + notify_stop_tracking(); + DLOG(INFO) << "CH = " << channel_ << ". Ev failed tracking standby"; + return true; + } } void ChannelFsm::set_acquisition(std::shared_ptr acquisition) diff --git a/src/algorithms/channel/libs/channel_fsm.h b/src/algorithms/channel/libs/channel_fsm.h index 29f741bfc..23b92c394 100644 --- a/src/algorithms/channel/libs/channel_fsm.h +++ b/src/algorithms/channel/libs/channel_fsm.h @@ -54,11 +54,11 @@ public: void set_channel(unsigned int channel); //FSM EVENTS - void Event_start_acquisition(); - void Event_valid_acquisition(); - void Event_failed_acquisition_repeat(); - void Event_failed_acquisition_no_repeat(); - void Event_failed_tracking_standby(); + bool Event_start_acquisition(); + bool Event_valid_acquisition(); + bool Event_failed_acquisition_repeat(); + bool Event_failed_acquisition_no_repeat(); + bool Event_failed_tracking_standby(); private: diff --git a/src/algorithms/channel/libs/channel_msg_receiver_cc.cc b/src/algorithms/channel/libs/channel_msg_receiver_cc.cc index d22f2dd73..a88fc6d88 100644 --- a/src/algorithms/channel/libs/channel_msg_receiver_cc.cc +++ b/src/algorithms/channel/libs/channel_msg_receiver_cc.cc @@ -44,26 +44,27 @@ channel_msg_receiver_cc_sptr channel_msg_receiver_make_cc(ChannelFsm* channel_fs void channel_msg_receiver_cc::msg_handler_events(pmt::pmt_t msg) { + bool result = false; try { long int message = pmt::to_long(msg); switch (message) { case 1: //positive acquisition - d_channel_fsm->Event_valid_acquisition(); + result = d_channel_fsm->Event_valid_acquisition(); break; case 2: //negative acquisition if (d_repeat == true) { - d_channel_fsm->Event_failed_acquisition_repeat(); + result = d_channel_fsm->Event_failed_acquisition_repeat(); } else { - d_channel_fsm->Event_failed_acquisition_no_repeat(); + result = d_channel_fsm->Event_failed_acquisition_no_repeat(); } break; case 3: // tracking loss of lock event - d_channel_fsm->Event_failed_tracking_standby(); + result = d_channel_fsm->Event_failed_tracking_standby(); break; default: LOG(WARNING) << "Default case, invalid message."; @@ -74,6 +75,10 @@ void channel_msg_receiver_cc::msg_handler_events(pmt::pmt_t msg) { LOG(WARNING) << "msg_handler_telemetry Bad any cast!"; } + if(!result) + { + LOG(WARNING) << "msg_handler_telemetry invalid event"; + } }