diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-02-24 14:14:48 +0000 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-02-24 14:14:48 +0000 |
commit | 9a8dcb99c3efb5b00b0a014e4699655d479d9925 (patch) | |
tree | 912d8e9923b4adf71f973239500743e459bed4d3 /src/can_decode_message.cpp | |
parent | 5c0c41e87ed5d1a2de7d336465c3beb3987a299f (diff) |
Improve mutex lock logic.
- Use of bracket instead of unlock method
- Change some mutex lock scope.
- Added subscribed_signals map object mutex to
manipulate it safely.
Change-Id: I770c0b5701db6b1151511f7360ec31ae6dcc1de9
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src/can_decode_message.cpp')
-rw-r--r-- | src/can_decode_message.cpp | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/can_decode_message.cpp b/src/can_decode_message.cpp index 8d81f8b..3c1825d 100644 --- a/src/can_decode_message.cpp +++ b/src/can_decode_message.cpp @@ -31,41 +31,44 @@ void can_decode_message(can_bus_t &can_bus) std::vector <CanSignal> signals; std::vector <CanSignal>::iterator signals_i; openxc_VehicleMessage vehicle_message; - openxc_DynamicField search_key, ret; + openxc_DynamicField search_key, decoded_message; bool send = true; decoder_t decoder; - while(can_bus.has_can_message()) + 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_message_mutex); + new_can_message.wait(can_message_lock); can_message = can_bus.next_can_message(); - can_message_mutex.unlock(); + } - std::lock_guard<std::mutex> decoded_can_message_lock(decoded_can_message_mutex); - /* First we have to found which CanSignal 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(const auto& sig : signals) - { + /* 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(const auto& sig : signals) + { + { + std::lock_guard<std::mutex> subscribed_signals_lock(subscribed_signals_mutex); std::map<std::string, struct afb_event> subscribed_signals = get_subscribed_signals(); const auto& it_event = subscribed_signals.find(sig.genericName); if(it_event != subscribed_signals.end() && afb_event_is_valid(it_event->second)) { - ret = decoder.decodeSignal(sig, can_message, getSignals(), &send); - - openxc_SimpleMessage s_message = build_SimpleMessage(sig.genericName, ret); + decoded_message = decoder.decodeSignal(sig, can_message, getSignals(), &send); + 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); can_bus.push_new_vehicle_message(vehicle_message); } + new_decoded_can_message.notify_one(); } - decoded_can_message_mutex.unlock(); - new_decoded_can_message.notify_one(); + } } } |