From f9d6b3b462fa3db5a70e8bd1e1625d35f9e0f21b Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 27 Feb 2017 21:29:07 +0100 Subject: In class mutex and condition variable except for subscribed_signals map For now... Change-Id: I133deb39fcd0660064b3b3c2a52f86ad37cb29c2 Signed-off-by: Romain Forlot --- src/can-signals.cpp | 12 ++++++++++++ src/can-signals.hpp | 10 ++-------- src/can-utils.cpp | 20 ++++++++++++++++++++ src/can-utils.hpp | 17 ++++++++++++++--- src/can_decode_message.cpp | 10 +++++----- src/can_decode_message.hpp | 6 ------ src/can_event_push.cpp | 6 +++--- src/can_reader.cpp | 4 ++-- src/can_reader.hpp | 6 ------ src/low-can-binding.cpp | 2 +- 10 files changed, 59 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/can-signals.cpp b/src/can-signals.cpp index 0a6a829..54c0342 100644 --- a/src/can-signals.cpp +++ b/src/can-signals.cpp @@ -29,6 +29,18 @@ std::vector> SIGNALS { {}// message set: example }; +/** +* @brief Mutex allowing safe manipulation on subscribed_signals map. +* @desc To ensure that the map object isn't modified when we read it, you +* have to set this mutex before use subscribed_signals map object. +*/ +std::mutex subscribed_signals_mutex; + +std::mutex& get_subscribed_signals_mutex() +{ + return subscribed_signals_mutex; +} + const std::vector getSignals() { return SIGNALS[MESSAGE_SET_ID]; diff --git a/src/can-signals.hpp b/src/can-signals.hpp index fb77df5..c2eaea4 100644 --- a/src/can-signals.hpp +++ b/src/can-signals.hpp @@ -18,13 +18,12 @@ #pragma once #include +#include #include #include #include #include #include -#include -#include #include "timer.hpp" #include "openxc.pb.h" @@ -48,12 +47,7 @@ extern "C" */ static std::map subscribed_signals; -/** -* @brief Mutex allowing safe manipulation on subscribed_signals map. -* @desc To ensure that the map object isn't modified when we read it, you -* have to set this mutex before use subscribed_signals map object. -*/ -extern std::mutex subscribed_signals_mutex; +std::mutex& get_subscribed_signals_mutex(); /** Public: Return the currently active CAN configuration. */ CanMessageSet* getActiveMessageSet(); diff --git a/src/can-utils.cpp b/src/can-utils.cpp index 0d52bbe..977d344 100644 --- a/src/can-utils.cpp +++ b/src/can-utils.cpp @@ -258,6 +258,26 @@ std::vector can_bus_t::read_conf() return ret; } +std::condition_variable& can_bus_t::get_new_can_message() +{ + return new_can_message_; +} + +std::mutex& can_bus_t::get_can_message_mutex() +{ + return can_message_mutex_; +} + +std::condition_variable& can_bus_t::get_new_decoded_can_message() +{ + return new_decoded_can_message_; +} + +std::mutex& can_bus_t::get_decoded_can_message_mutex() +{ + return decoded_can_message_mutex_; +} + can_message_t can_bus_t::next_can_message() { can_message_t can_msg; diff --git a/src/can-utils.hpp b/src/can-utils.hpp index 5938169..50d5eb9 100644 --- a/src/can-utils.hpp +++ b/src/can-utils.hpp @@ -17,9 +17,11 @@ #pragma once +#include #include #include #include +#include #include "timer.hpp" #include "openxc.pb.h" @@ -225,9 +227,13 @@ class can_bus_t { std::thread th_pushing_; /*!< thread that'll handle pushing decoded can frame to subscribers */ bool is_pushing_; /*!< boolean member controling thread while loop*/ + std::condition_variable new_can_message_; + std::mutex can_message_mutex_; bool has_can_message_; /*!< boolean members that control whether or not there is can_message into the queue */ std::queue can_message_q_; /*!< queue that'll store can_message_t to decoded */ + std::condition_variable new_decoded_can_message_; + std::mutex decoded_can_message_mutex_; bool has_vehicle_message_; /*!< boolean members that control whether or not there is openxc_VehicleMessage into the queue */ std::queue vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */ @@ -245,15 +251,20 @@ class can_bus_t { * the configuration file passed in the constructor. */ int init_can_dev(); - + /** * @brief read the conf_file_ and will parse json objects * in it searching for canbus objects devices name. * * @return Vector of can bus device name string. */ - std::vector read_conf(); + std::vector read_conf(); + std::condition_variable& get_new_can_message(); + std::mutex& get_can_message_mutex(); + std::condition_variable& get_new_decoded_can_message(); + std::mutex& get_decoded_can_message_mutex(); + /** * @brief Will initialize threads that will decode * and push subscribed events. @@ -341,7 +352,7 @@ class can_bus_dev_t { int can_socket_; /*!< socket handler for the can device */ bool is_fdmode_on_; /*!< boolean telling if whether or not the can socket use fdmode. */ struct sockaddr_can txAddress_; /*!< internal member using to bind to the socket */ - + std::thread th_reading_; /*!< Thread handling read the socket can device filling can_message_q_ queue of can_bus_t */ bool is_running_; /*!< boolean telling whether or not reading is running or not */ diff --git a/src/can_decode_message.cpp b/src/can_decode_message.cpp index c311e98..ed518f1 100644 --- a/src/can_decode_message.cpp +++ b/src/can_decode_message.cpp @@ -38,8 +38,8 @@ void can_decode_message(can_bus_t &can_bus) while(can_bus.is_decoding()) { { - std::unique_lock can_message_lock(can_message_mutex); - new_can_message.wait(can_message_lock); + std::unique_lock can_message_lock(can_bus.get_can_message_mutex()); + can_bus.get_new_can_message().wait(can_message_lock); can_message = can_bus.next_can_message(); } @@ -51,7 +51,7 @@ void can_decode_message(can_bus_t &can_bus) for(auto& sig : signals) { { - std::lock_guard subscribed_signals_lock(subscribed_signals_mutex); + std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); std::map subscribed_signals = get_subscribed_signals(); const auto& it_event = subscribed_signals.find(sig.genericName); @@ -63,10 +63,10 @@ void can_decode_message(can_bus_t &can_bus) openxc_SimpleMessage s_message = build_SimpleMessage(sig.genericName, decoded_message); vehicle_message = build_VehicleMessage_with_SimpleMessage(openxc_DynamicField_Type::openxc_DynamicField_Type_NUM, s_message); - std::lock_guard decoded_can_message_lock(decoded_can_message_mutex); + std::lock_guard decoded_can_message_lock(can_bus.get_decoded_can_message_mutex()); can_bus.push_new_vehicle_message(vehicle_message); } - new_decoded_can_message.notify_one(); + can_bus.get_new_decoded_can_message().notify_one(); } } } diff --git a/src/can_decode_message.hpp b/src/can_decode_message.hpp index 73ea2ef..e597241 100644 --- a/src/can_decode_message.hpp +++ b/src/can_decode_message.hpp @@ -17,9 +17,3 @@ */ #pragma once - -#include -#include - -extern std::condition_variable new_decoded_can_message; -extern std::mutex decoded_can_message_mutex; \ No newline at end of file diff --git a/src/can_event_push.cpp b/src/can_event_push.cpp index fc403ee..b371973 100644 --- a/src/can_event_push.cpp +++ b/src/can_event_push.cpp @@ -33,15 +33,15 @@ void can_event_push(can_bus_t& can_bus) while(can_bus.is_pushing()) { { - std::unique_lock decoded_can_message_lock(decoded_can_message_mutex); - new_decoded_can_message.wait(decoded_can_message_lock); + std::unique_lock decoded_can_message_lock(can_bus.get_decoded_can_message_mutex()); + can_bus.get_new_decoded_can_message().wait(decoded_can_message_lock); v_message = can_bus.next_vehicle_message(); } s_message = get_simple_message(v_message); { - std::lock_guard subscribed_signals_lock(subscribed_signals_mutex); + std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); std::map subscribed_signals = get_subscribed_signals(); const auto& it_event = subscribed_signals.find(s_message.name); if(it_event != subscribed_signals.end() && afb_event_is_valid(it_event->second)) diff --git a/src/can_reader.cpp b/src/can_reader.cpp index 7ae981b..4706d92 100644 --- a/src/can_reader.cpp +++ b/src/can_reader.cpp @@ -30,9 +30,9 @@ void can_reader(can_bus_dev_t &can_bus_dev, can_bus_t& can_bus) can_message.convert_from_canfd_frame(can_bus_dev.read()); { - std::lock_guard can_message_lock(can_message_mutex); + std::lock_guard can_message_lock(can_bus.get_can_message_mutex()); can_bus.push_new_can_message(can_message); } - new_can_message.notify_one(); + can_bus.get_new_can_message().notify_one(); } } \ No newline at end of file diff --git a/src/can_reader.hpp b/src/can_reader.hpp index 3d73103..e597241 100644 --- a/src/can_reader.hpp +++ b/src/can_reader.hpp @@ -17,9 +17,3 @@ */ #pragma once - -#include -#include - -extern std::condition_variable new_can_message; -extern std::mutex can_message_mutex; \ No newline at end of file diff --git a/src/low-can-binding.cpp b/src/low-can-binding.cpp index be2d373..cae86f5 100644 --- a/src/low-can-binding.cpp +++ b/src/low-can-binding.cpp @@ -77,7 +77,7 @@ static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, { int ret; - std::lock_guard subscribed_signals_lock(subscribed_signals_mutex); + std::lock_guard subscribed_signals_lock(get_subscribed_signals_mutex()); auto ss_i = subscribed_signals.find(sig.genericName); if (ss_i != subscribed_signals.end() && !afb_event_is_valid(ss_i->second)) { -- cgit 1.2.3-korg