diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-02-27 21:29:07 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-02-27 21:29:07 +0100 |
commit | f9d6b3b462fa3db5a70e8bd1e1625d35f9e0f21b (patch) | |
tree | e272d292c0e0df9803be2ccb66ac43c8f77bfe8e | |
parent | 7c40ad1076fa608863eb62991d436267dab43dbb (diff) |
In class mutex and condition variable except for subscribed_signals map
For now...
Change-Id: I133deb39fcd0660064b3b3c2a52f86ad37cb29c2
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | can_buses.json.example | 7 | ||||
-rw-r--r-- | src/can-signals.cpp | 12 | ||||
-rw-r--r-- | src/can-signals.hpp | 10 | ||||
-rw-r--r-- | src/can-utils.cpp | 20 | ||||
-rw-r--r-- | src/can-utils.hpp | 17 | ||||
-rw-r--r-- | src/can_decode_message.cpp | 10 | ||||
-rw-r--r-- | src/can_decode_message.hpp | 6 | ||||
-rw-r--r-- | src/can_event_push.cpp | 6 | ||||
-rw-r--r-- | src/can_reader.cpp | 4 | ||||
-rw-r--r-- | src/can_reader.hpp | 6 | ||||
-rw-r--r-- | src/low-can-binding.cpp | 2 |
11 files changed, 59 insertions, 41 deletions
diff --git a/can_buses.json.example b/can_buses.json.example deleted file mode 100644 index a6a3ed26..00000000 --- a/can_buses.json.example +++ /dev/null @@ -1,7 +0,0 @@ -{ - "canbus": { - "dev_name": [ "can0", - "can1" - ] - } -}
\ No newline at end of file diff --git a/src/can-signals.cpp b/src/can-signals.cpp index 0a6a829c..54c03425 100644 --- a/src/can-signals.cpp +++ b/src/can-signals.cpp @@ -29,6 +29,18 @@ std::vector<std::vector<CanSignal>> 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<CanSignal> getSignals() { return SIGNALS[MESSAGE_SET_ID]; diff --git a/src/can-signals.hpp b/src/can-signals.hpp index fb77df5c..c2eaea4c 100644 --- a/src/can-signals.hpp +++ b/src/can-signals.hpp @@ -18,13 +18,12 @@ #pragma once #include <map> +#include <mutex> #include <queue> #include <vector> #include <string> #include <thread> #include <linux/can.h> -#include <mutex> -#include <condition_variable> #include "timer.hpp" #include "openxc.pb.h" @@ -48,12 +47,7 @@ extern "C" */ static std::map<std::string, struct afb_event> 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 0d52bbe4..977d344c 100644 --- a/src/can-utils.cpp +++ b/src/can-utils.cpp @@ -258,6 +258,26 @@ std::vector<std::string> 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 5938169a..50d5eb99 100644 --- a/src/can-utils.hpp +++ b/src/can-utils.hpp @@ -17,9 +17,11 @@ #pragma once +#include <mutex> #include <queue> #include <thread> #include <linux/can.h> +#include <condition_variable> #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_t> 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 <openxc_VehicleMessage> 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<std::string> read_conf(); + std::vector<std::string> 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 c311e98a..ed518f1e 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<std::mutex> can_message_lock(can_message_mutex); - new_can_message.wait(can_message_lock); + std::unique_lock<std::mutex> 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<std::mutex> subscribed_signals_lock(subscribed_signals_mutex); + std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex()); std::map<std::string, struct afb_event> 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<std::mutex> decoded_can_message_lock(decoded_can_message_mutex); + std::lock_guard<std::mutex> 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 73ea2ef3..e5972416 100644 --- a/src/can_decode_message.hpp +++ b/src/can_decode_message.hpp @@ -17,9 +17,3 @@ */ #pragma once - -#include <mutex> -#include <condition_variable> - -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 fc403ee1..b3719734 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<std::mutex> decoded_can_message_lock(decoded_can_message_mutex); - new_decoded_can_message.wait(decoded_can_message_lock); + std::unique_lock<std::mutex> 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<std::mutex> subscribed_signals_lock(subscribed_signals_mutex); + std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex()); std::map<std::string, struct afb_event> 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 7ae981b8..4706d92c 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<std::mutex> can_message_lock(can_message_mutex); + std::lock_guard<std::mutex> 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 3d73103a..e5972416 100644 --- a/src/can_reader.hpp +++ b/src/can_reader.hpp @@ -17,9 +17,3 @@ */ #pragma once - -#include <mutex> -#include <condition_variable> - -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 be2d3737..cae86f5a 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<std::mutex> subscribed_signals_lock(subscribed_signals_mutex); + std::lock_guard<std::mutex> 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)) { |