diff --git a/languages/cpp/src/shared/src/Async/Async.cpp b/languages/cpp/src/shared/src/Async/Async.cpp index f9994d7d..5fa96717 100644 --- a/languages/cpp/src/shared/src/Async/Async.cpp +++ b/languages/cpp/src/shared/src/Async/Async.cpp @@ -60,7 +60,7 @@ namespace FireboltSDK { void Async::Clear() { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.begin(); while (index != _methodMap.end()) { CallbackMap::iterator callbackIndex = index->second.begin(); @@ -72,7 +72,5 @@ namespace FireboltSDK { } index = _methodMap.erase(index); } - _adminLock.Unlock(); } } - diff --git a/languages/cpp/src/shared/src/Async/Async.h b/languages/cpp/src/shared/src/Async/Async.h index a141b9e5..ea6f189d 100644 --- a/languages/cpp/src/shared/src/Async/Async.h +++ b/languages/cpp/src/shared/src/Async/Async.h @@ -18,6 +18,7 @@ #pragma once +#include #include "Module.h" namespace FireboltSDK { @@ -111,7 +112,7 @@ namespace FireboltSDK { return (status); }; - _adminLock.Lock(); + _adminLock.lock(); WPEFramework::Core::ProxyType job = WPEFramework::Core::ProxyType(WPEFramework::Core::ProxyType::Create(*this, method, lambda, usercb)); CallbackData callbackData = {lambda, job, DefaultId}; MethodMap::iterator index = _methodMap.find(method); @@ -126,7 +127,7 @@ namespace FireboltSDK { callbackMap.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); _methodMap.emplace(std::piecewise_construct, std::forward_as_tuple(method), std::forward_as_tuple(callbackMap)); } - _adminLock.Unlock(); + _adminLock.unlock(); WPEFramework::Core::IWorkerPool::Instance().Submit(job); } @@ -142,7 +143,7 @@ namespace FireboltSDK { void UpdateEntry(const string& method, void* usercb, uint32_t id) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.find(method); if (index != _methodMap.end()) { CallbackMap::iterator callbackIndex = index->second.find(usercb); @@ -150,12 +151,11 @@ namespace FireboltSDK { callbackIndex->second.id = id; } } - _adminLock.Unlock(); } void RemoveEntry(const string& method, void* usercb) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.find(method); if (index != _methodMap.end()) { CallbackMap::iterator callbackIndex = index->second.find(usercb); @@ -169,13 +169,13 @@ namespace FireboltSDK { } } } - _adminLock.Unlock(); } bool IsActive(const string& method, void* usercb) { bool valid = false; - _adminLock.Lock(); + + std::lock_guard guard(_adminLock); MethodMap::iterator index = _methodMap.find(method); if (index != _methodMap.end()) { CallbackMap::iterator callbackIndex = index->second.find(usercb); @@ -183,7 +183,6 @@ namespace FireboltSDK { valid = true; } } - _adminLock.Unlock(); return valid; } @@ -195,7 +194,7 @@ namespace FireboltSDK { private: MethodMap _methodMap; - WPEFramework::Core::CriticalSection _adminLock; + std::mutex _adminLock; Transport* _transport; static Async* _singleton; diff --git a/languages/cpp/src/shared/src/Event/Event.cpp b/languages/cpp/src/shared/src/Event/Event.cpp index 5d739d70..5469815c 100644 --- a/languages/cpp/src/shared/src/Event/Event.cpp +++ b/languages/cpp/src/shared/src/Event/Event.cpp @@ -16,6 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include "Transport/Transport.h" #include "Event.h" @@ -91,7 +92,7 @@ namespace FireboltSDK { Firebolt::Error Event::Dispatch(const string& eventName, const WPEFramework::Core::ProxyType& jsonResponse) /* override */ { string response = jsonResponse->Result.Value(); - _adminLock.Lock(); + _adminLock.lock(); EventMap::iterator eventIndex = _eventMap.find(eventName); if (eventIndex != _eventMap.end()) { CallbackMap::iterator callbackIndex = eventIndex->second.begin(); @@ -101,11 +102,11 @@ namespace FireboltSDK { callbackIndex->second.state = State::EXECUTING; } state = callbackIndex->second.state; - _adminLock.Unlock(); + _adminLock.unlock(); if (state == State::EXECUTING) { callbackIndex->second.lambda(callbackIndex->first, callbackIndex->second.userdata, (jsonResponse->Result.Value())); } - _adminLock.Lock(); + _adminLock.lock(); if (callbackIndex->second.state == State::REVOKED) { callbackIndex = eventIndex->second.erase(callbackIndex); if (eventIndex->second.size() == 0) { @@ -117,7 +118,7 @@ namespace FireboltSDK { } } } - _adminLock.Unlock(); + _adminLock.unlock(); return Firebolt::Error::None;; } @@ -125,24 +126,33 @@ namespace FireboltSDK { Firebolt::Error Event::Revoke(const string& eventName, void* usercb) { Firebolt::Error status = Firebolt::Error::None; - _adminLock.Lock(); + + std::lock_guard guard(_adminLock); + EventMap::iterator eventIndex = _eventMap.begin(); - if (eventIndex != _eventMap.end()) { + if (eventIndex != _eventMap.end()) + { CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb); - if (callbackIndex->second.state != State::EXECUTING) { - if (callbackIndex != eventIndex->second.end()) { + if (callbackIndex->second.state != State::EXECUTING) + { + if (callbackIndex != eventIndex->second.end()) + { eventIndex->second.erase(callbackIndex); } - } else { + } + else + { callbackIndex->second.state = State::REVOKED; } - if (eventIndex->second.size() == 0) { + if (eventIndex->second.size() == 0) + { _eventMap.erase(eventIndex); - } else { + } + else + { status = Firebolt::Error::General; } } - _adminLock.Unlock(); return status; } @@ -157,7 +167,7 @@ namespace FireboltSDK { } eventIndex = _eventMap.erase(eventIndex); } - _adminLock.Unlock(); + _adminLock.unlock(); } } diff --git a/languages/cpp/src/shared/src/Event/Event.h b/languages/cpp/src/shared/src/Event/Event.h index 7299fde0..6e73cf40 100644 --- a/languages/cpp/src/shared/src/Event/Event.h +++ b/languages/cpp/src/shared/src/Event/Event.h @@ -18,6 +18,7 @@ #pragma once +#include #include "Module.h" namespace FireboltSDK { @@ -126,7 +127,7 @@ namespace FireboltSDK { }; CallbackData callbackData = {implementation, userdata, State::IDLE}; - _adminLock.Lock(); + std::lock_guard guard(_adminLock); EventMap::iterator eventIndex = _eventMap.find(eventName); if (eventIndex != _eventMap.end()) { CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb); @@ -140,10 +141,7 @@ namespace FireboltSDK { callbackMap.emplace(std::piecewise_construct, std::forward_as_tuple(usercb), std::forward_as_tuple(callbackData)); _eventMap.emplace(std::piecewise_construct, std::forward_as_tuple(eventName), std::forward_as_tuple(callbackMap)); status = Firebolt::Error::None; - } - - _adminLock.Unlock(); return status; } Firebolt::Error Revoke(const string& eventName, void* usercb); @@ -155,7 +153,7 @@ namespace FireboltSDK { private: EventMap _eventMap; - WPEFramework::Core::CriticalSection _adminLock; + std::mutex _adminLock; Transport* _transport; static Event* _singleton; diff --git a/languages/cpp/src/shared/src/Transport/Transport.h b/languages/cpp/src/shared/src/Transport/Transport.h index 8c7cb029..abe037f7 100644 --- a/languages/cpp/src/shared/src/Transport/Transport.h +++ b/languages/cpp/src/shared/src/Transport/Transport.h @@ -18,6 +18,7 @@ #pragma once +#include #include "Module.h" #include "error.h" #include "time_new.h" @@ -349,23 +350,23 @@ namespace FireboltSDK { } void Register(CLIENT& client) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); + ASSERT(std::find(_observers.begin(), _observers.end(), &client) == _observers.end()); _observers.push_back(&client); if (_channel.IsOpen() == true) { client.Opened(); } - _adminLock.Unlock(); } void Unregister(CLIENT& client) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); + typename std::list::iterator index(std::find(_observers.begin(), _observers.end(), &client)); if (index != _observers.end()) { _observers.erase(index); } FactoryImpl::Instance().Revoke(&client); - _adminLock.Unlock(); } void Submit(const WPEFramework::Core::ProxyType& message) @@ -392,7 +393,8 @@ namespace FireboltSDK { protected: void StateChange() { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); + typename std::list::iterator index(_observers.begin()); while (index != _observers.end()) { if (_channel.IsOpen() == true) { @@ -403,7 +405,6 @@ namespace FireboltSDK { } index++; } - _adminLock.Unlock(); } bool Open(const uint32_t waitTime) { @@ -422,19 +423,20 @@ namespace FireboltSDK { int32_t Inbound(const WPEFramework::Core::ProxyType& inbound) { int32_t result = WPEFramework::Core::ERROR_UNAVAILABLE; - _adminLock.Lock(); + + std::lock_guard guard(_adminLock); + typename std::list::iterator index(_observers.begin()); while ((result != WPEFramework::Core::ERROR_NONE) && (index != _observers.end())) { result = (*index)->Submit(inbound); index++; } - _adminLock.Unlock(); return (result); } private: - WPEFramework::Core::CriticalSection _adminLock; + std::mutex _adminLock; ChannelImpl _channel; mutable std::atomic _sequence; std::list _observers; @@ -565,9 +567,8 @@ namespace FireboltSDK { void Revoke(const string& eventName) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); _eventMap.erase(eventName); - _adminLock.Unlock(); } void SetEventHandler(IEventHandler* eventHandler) @@ -600,10 +601,11 @@ namespace FireboltSDK { Firebolt::Error WaitForResponse(const uint32_t& id, RESPONSE& response, const uint32_t waitTime) { int32_t result = WPEFramework::Core::ERROR_TIMEDOUT; - _adminLock.Lock(); + + _adminLock.lock(); typename PendingMap::iterator index = _pendingQueue.find(id); Entry& slot(index->second); - _adminLock.Unlock(); + _adminLock.unlock(); if (slot.WaitForResponse(waitTime) == true) { WPEFramework::Core::ProxyType jsonResponse = slot.Response(); @@ -625,18 +627,19 @@ namespace FireboltSDK { } else { result = WPEFramework::Core::ERROR_TIMEDOUT; } - _adminLock.Lock(); - _pendingQueue.erase(id); - _adminLock.Unlock(); + + { + std::lock_guard guard(_adminLock); + _pendingQueue.erase(id); + } return FireboltErrorValue(result); } void Abort(uint32_t id) { - _adminLock.Lock(); + std::lock_guard guard(_adminLock); typename PendingMap::iterator index = _pendingQueue.find(id); Entry& slot(index->second); - _adminLock.Unlock(); slot.Abort(id); } @@ -646,12 +649,14 @@ namespace FireboltSDK { Entry slot; uint32_t id = _channel->Sequence(); Firebolt::Error result = Send(eventName, parameters, id); - if (result == Firebolt::Error::None) { - _adminLock.Lock(); - _eventMap.emplace(std::piecewise_construct, - std::forward_as_tuple(eventName), - std::forward_as_tuple(~0)); - _adminLock.Unlock(); + if (result == Firebolt::Error::None) + { + { + std::lock_guard guard(_adminLock); + _eventMap.emplace(std::piecewise_construct, + std::forward_as_tuple(eventName), + std::forward_as_tuple(~0)); + } result = WaitForEventResponse(id, eventName, response, _waitTime); } @@ -695,14 +700,15 @@ namespace FireboltSDK { friend Channel; inline bool IsEvent(const uint32_t id, string& eventName) { - _adminLock.Lock(); - for (auto& event : _eventMap) { - if (event.second == id) { - eventName = event.first; - break; + { + std::lock_guard guard(_adminLock); + for (auto& event : _eventMap) { + if (event.second == id) { + eventName = event.first; + break; + } } } - _adminLock.Unlock(); return (eventName.empty() != true); } uint64_t Timed() @@ -711,7 +717,7 @@ namespace FireboltSDK { uint64_t currentTime = FireboltSDK::Core::MyTime::Now().Ticks(); // Lets see if some callback are expire. If so trigger and remove... - _adminLock.Lock(); + std::lock_guard guard(_adminLock); typename PendingMap::iterator index = _pendingQueue.begin(); @@ -726,8 +732,6 @@ namespace FireboltSDK { } _scheduledTime = (result != static_cast(~0) ? result : 0); - _adminLock.Unlock(); - return (_scheduledTime); } @@ -742,17 +746,18 @@ namespace FireboltSDK { void Closed() { - // Abort any in progress RPC command: - _adminLock.Lock(); + { + // Abort any in progress RPC command: + std::lock_guard guard(_adminLock); - // See if we issued anything, if so abort it.. - while (_pendingQueue.size() != 0) { + // See if we issued anything, if so abort it.. + while (_pendingQueue.size() != 0) { - _pendingQueue.begin()->second.Abort(_pendingQueue.begin()->first); - _pendingQueue.erase(_pendingQueue.begin()); + _pendingQueue.begin()->second.Abort(_pendingQueue.begin()->first); + _pendingQueue.erase(_pendingQueue.begin()); + } } - _adminLock.Unlock(); if (_connected != false) { _connected = false; _listener(_connected, _status); @@ -778,7 +783,7 @@ namespace FireboltSDK { ASSERT(inbound->Parameters.IsSet() == false); ASSERT(inbound->Designator.IsSet() == false); - _adminLock.Lock(); + _adminLock.lock(); // See if we issued this.. typename PendingMap::iterator index = _pendingQueue.find(inbound->Id.Value()); @@ -790,9 +795,9 @@ namespace FireboltSDK { } result = WPEFramework::Core::ERROR_NONE; - _adminLock.Unlock(); + _adminLock.unlock(); } else { - _adminLock.Unlock(); + _adminLock.unlock(); string eventName; if (IsEvent(inbound->Id.Value(), eventName)) { _eventHandler->Dispatch(eventName, inbound); @@ -822,7 +827,7 @@ namespace FireboltSDK { message->Designator = method; ToMessage(parameters, message); - _adminLock.Lock(); + _adminLock.lock(); typename std::pair< typename PendingMap::iterator, bool> newElement = _pendingQueue.emplace(std::piecewise_construct, @@ -832,7 +837,7 @@ namespace FireboltSDK { if (newElement.second == true) { - _adminLock.Unlock(); + _adminLock.unlock(); _channel->Submit(WPEFramework::Core::ProxyType(message)); @@ -848,10 +853,11 @@ namespace FireboltSDK { Firebolt::Error WaitForEventResponse(const uint32_t& id, const string& eventName, RESPONSE& response, const uint32_t waitTime) { Firebolt::Error result = Firebolt::Error::Timedout; - _adminLock.Lock(); + + _adminLock.lock(); typename PendingMap::iterator index = _pendingQueue.find(id); Entry& slot(index->second); - _adminLock.Unlock(); + _adminLock.unlock(); uint8_t waiting = waitTime; do { @@ -871,13 +877,13 @@ namespace FireboltSDK { result = _eventHandler->ValidateResponse(jsonResponse, enabled); if (result == Firebolt::Error::None) { FromMessage((INTERFACE*)&response, *jsonResponse); - if (enabled) { - _adminLock.Lock(); + if (enabled) + { + std::lock_guard guard(_adminLock); typename EventMap::iterator index = _eventMap.find(eventName); if (index != _eventMap.end()) { index->second = id; } - _adminLock.Unlock(); } } } @@ -888,9 +894,9 @@ namespace FireboltSDK { } waiting -= (waiting == WPEFramework::Core::infinite ? 0 : waitSlot); } while ((result != Firebolt::Error::None) && (waiting > 0 )); - _adminLock.Lock(); + + std::lock_guard guard(_adminLock); _pendingQueue.erase(id); - _adminLock.Unlock(); return result; } @@ -969,7 +975,7 @@ namespace FireboltSDK { } private: - WPEFramework::Core::CriticalSection _adminLock; + std::mutex _adminLock; WPEFramework::Core::NodeId _connectId; WPEFramework::Core::ProxyType _channel; IEventHandler* _eventHandler;