summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-03-02 23:23:32 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2017-03-02 23:23:32 +0100
commit87e9e640972e2833490b56c0a87897c7529b0f87 (patch)
tree3413cbba13f46ef1ad73a8efd3c2de2fc57d6511
parent0961202bff0758ffdccf7bcd0dc9b721e20b2edb (diff)
Fix: unsynchronization between queues.
Ressources not enough protected with mutex, I extend the scope of the lock to make sure that event flow go through synchronously. Change-Id: I77a80ab69eb768a211c537e0f33580379832a465 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--src/can-bus.cpp74
-rw-r--r--src/can-bus.hpp1
-rw-r--r--src/can-signals.cpp1
3 files changed, 40 insertions, 36 deletions
diff --git a/src/can-bus.cpp b/src/can-bus.cpp
index 865c19d..a3dd2d4 100644
--- a/src/can-bus.cpp
+++ b/src/can-bus.cpp
@@ -64,34 +64,34 @@ void can_bus_t::can_decode_message()
std::unique_lock<std::mutex> can_message_lock(can_message_mutex_);
new_can_message_.wait(can_message_lock);
can_message = next_can_message();
- }
-
- /* First we have to found which CanSignal it is */
- search_key = build_DynamicField((double)can_message.get_id());
- signals = find_can_signals(search_key);
+
+ /* First we have to found which CanSignal it is */
+ search_key = build_DynamicField((double)can_message.get_id());
+ signals = find_can_signals(search_key);
- /* Decoding the message ! Don't kill the messenger ! */
- for(auto& sig : signals)
- {
+ /* Decoding the message ! Don't kill the messenger ! */
+ for(auto& sig : signals)
{
- std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
- std::map<std::string, struct afb_event>& s = get_subscribed_signals();
-
- /* DEBUG message to make easier debugger STL containers...
- DEBUG(binder_interface, "Operator[] key char: %s, event valid? %d", sig.genericName, afb_event_is_valid(s[sig.genericName]));
- DEBUG(binder_interface, "Operator[] key string: %s, event valid? %d", sig.genericName, afb_event_is_valid(s[std::string(sig.genericName)]));
- DEBUG(binder_interface, "Nb elt matched char: %d", (int)s.count(sig.genericName));
- DEBUG(binder_interface, "Nb elt matched string: %d", (int)s.count(std::string(sig.genericName))); */
- if( s.find(sig.genericName) != s.end() && afb_event_is_valid(s[sig.genericName]))
{
- decoded_message = decoder.translateSignal(sig, can_message, getSignals());
-
- 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_);
- push_new_vehicle_message(vehicle_message);
- new_decoded_can_message_.notify_one();
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = get_subscribed_signals();
+
+ /* DEBUG message to make easier debugger STL containers...
+ DEBUG(binder_interface, "Operator[] key char: %s, event valid? %d", sig.genericName, afb_event_is_valid(s[sig.genericName]));
+ DEBUG(binder_interface, "Operator[] key string: %s, event valid? %d", sig.genericName, afb_event_is_valid(s[std::string(sig.genericName)]));
+ DEBUG(binder_interface, "Nb elt matched char: %d", (int)s.count(sig.genericName));
+ DEBUG(binder_interface, "Nb elt matched string: %d", (int)s.count(std::string(sig.genericName))); */
+ if( s.find(sig.genericName) != s.end() && afb_event_is_valid(s[sig.genericName]))
+ {
+ decoded_message = decoder.translateSignal(sig, can_message, getSignals());
+
+ 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_);
+ push_new_vehicle_message(vehicle_message);
+ new_decoded_can_message_.notify_one();
+ }
}
}
}
@@ -110,18 +110,17 @@ void can_bus_t::can_event_push()
std::unique_lock<std::mutex> decoded_can_message_lock(decoded_can_message_mutex_);
new_decoded_can_message_.wait(decoded_can_message_lock);
v_message = next_vehicle_message();
- }
-
- s_message = get_simple_message(v_message);
- {
- std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
- std::map<std::string, struct afb_event>& s = get_subscribed_signals();
- if(s.find(std::string(s_message.name)) != s.end() && afb_event_is_valid(s[std::string(s_message.name)]))
+ s_message = get_simple_message(v_message);
{
- jo = json_object_new_object();
- jsonify_simple(s_message, jo);
- afb_event_push(s[std::string(s_message.name)], jo);
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = get_subscribed_signals();
+ if(s.find(std::string(s_message.name)) != s.end() && afb_event_is_valid(s[std::string(s_message.name)]))
+ {
+ jo = json_object_new_object();
+ jsonify_simple(s_message, jo);
+ afb_event_push(s[std::string(s_message.name)], jo);
+ }
}
}
}
@@ -255,6 +254,11 @@ void can_bus_t::push_new_can_message(const can_message_t& can_msg)
can_message_q_.push(can_msg);
}
+bool can_bus_t::has_can_message()
+{
+ return has_can_message_;
+}
+
openxc_VehicleMessage can_bus_t::next_vehicle_message()
{
openxc_VehicleMessage v_msg;
diff --git a/src/can-bus.hpp b/src/can-bus.hpp
index b80b68d..41d3f36 100644
--- a/src/can-bus.hpp
+++ b/src/can-bus.hpp
@@ -169,6 +169,7 @@ class can_bus_t {
*/
std::condition_variable& get_new_can_message();
+ bool has_can_message();
/**
* @brief Return first openxc_VehicleMessage on the queue
diff --git a/src/can-signals.cpp b/src/can-signals.cpp
index 10935ab..d761e1e 100644
--- a/src/can-signals.cpp
+++ b/src/can-signals.cpp
@@ -64,7 +64,6 @@ std::mutex& get_subscribed_signals_mutex()
std::map<std::string, struct afb_event>& get_subscribed_signals()
{
- DEBUG(binder_interface, "Here are the first subscribed_signals: %s", subscribed_signals.begin()->first.c_str() );
return subscribed_signals;
}