Skip to content

Commit

Permalink
refactor: Replace WPEFramework::Core::CriticalSection dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaKasar committed Sep 10, 2024
1 parent 62e9fe4 commit f44af2d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 83 deletions.
4 changes: 1 addition & 3 deletions languages/cpp/src/shared/src/Async/Async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace FireboltSDK {

void Async::Clear()
{
_adminLock.Lock();
std::lock_guard<std::mutex> guard(_adminLock);
MethodMap::iterator index = _methodMap.begin();
while (index != _methodMap.end()) {
CallbackMap::iterator callbackIndex = index->second.begin();
Expand All @@ -72,7 +72,5 @@ namespace FireboltSDK {
}
index = _methodMap.erase(index);
}
_adminLock.Unlock();
}
}

17 changes: 8 additions & 9 deletions languages/cpp/src/shared/src/Async/Async.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <mutex>
#include "Module.h"

namespace FireboltSDK {
Expand Down Expand Up @@ -111,7 +112,7 @@ namespace FireboltSDK {
return (status);
};

_adminLock.Lock();
_adminLock.lock();
WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch> job = WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch>(WPEFramework::Core::ProxyType<Async::Job>::Create(*this, method, lambda, usercb));
CallbackData callbackData = {lambda, job, DefaultId};
MethodMap::iterator index = _methodMap.find(method);
Expand All @@ -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);
}
Expand All @@ -142,20 +143,19 @@ namespace FireboltSDK {

void UpdateEntry(const string& method, void* usercb, uint32_t id)
{
_adminLock.Lock();
std::lock_guard<std::mutex> guard(_adminLock);
MethodMap::iterator index = _methodMap.find(method);
if (index != _methodMap.end()) {
CallbackMap::iterator callbackIndex = index->second.find(usercb);
if (callbackIndex != index->second.end()) {
callbackIndex->second.id = id;
}
}
_adminLock.Unlock();
}

void RemoveEntry(const string& method, void* usercb)
{
_adminLock.Lock();
std::lock_guard<std::mutex> guard(_adminLock);
MethodMap::iterator index = _methodMap.find(method);
if (index != _methodMap.end()) {
CallbackMap::iterator callbackIndex = index->second.find(usercb);
Expand All @@ -169,21 +169,20 @@ namespace FireboltSDK {
}
}
}
_adminLock.Unlock();
}

bool IsActive(const string& method, void* usercb)
{
bool valid = false;
_adminLock.Lock();

std::lock_guard<std::mutex> guard(_adminLock);
MethodMap::iterator index = _methodMap.find(method);
if (index != _methodMap.end()) {
CallbackMap::iterator callbackIndex = index->second.find(usercb);
if (callbackIndex != index->second.end()) {
valid = true;
}
}
_adminLock.Unlock();
return valid;
}

Expand All @@ -195,7 +194,7 @@ namespace FireboltSDK {

private:
MethodMap _methodMap;
WPEFramework::Core::CriticalSection _adminLock;
std::mutex _adminLock;
Transport<WPEFramework::Core::JSON::IElement>* _transport;

static Async* _singleton;
Expand Down
36 changes: 23 additions & 13 deletions languages/cpp/src/shared/src/Event/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <mutex>
#include "Transport/Transport.h"
#include "Event.h"

Expand Down Expand Up @@ -91,7 +92,7 @@ namespace FireboltSDK {
Firebolt::Error Event::Dispatch(const string& eventName, const WPEFramework::Core::ProxyType<WPEFramework::Core::JSONRPC::Message>& 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();
Expand All @@ -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) {
Expand All @@ -117,32 +118,41 @@ namespace FireboltSDK {
}
}
}
_adminLock.Unlock();
_adminLock.unlock();

return Firebolt::Error::None;;
}

Firebolt::Error Event::Revoke(const string& eventName, void* usercb)
{
Firebolt::Error status = Firebolt::Error::None;
_adminLock.Lock();

std::lock_guard<std::mutex> 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;
}
Expand All @@ -157,7 +167,7 @@ namespace FireboltSDK {
}
eventIndex = _eventMap.erase(eventIndex);
}
_adminLock.Unlock();
_adminLock.unlock();
}

}
8 changes: 3 additions & 5 deletions languages/cpp/src/shared/src/Event/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <mutex>
#include "Module.h"

namespace FireboltSDK {
Expand Down Expand Up @@ -126,7 +127,7 @@ namespace FireboltSDK {
};
CallbackData callbackData = {implementation, userdata, State::IDLE};

_adminLock.Lock();
std::lock_guard<std::mutex> guard(_adminLock);
EventMap::iterator eventIndex = _eventMap.find(eventName);
if (eventIndex != _eventMap.end()) {
CallbackMap::iterator callbackIndex = eventIndex->second.find(usercb);
Expand All @@ -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);
Expand All @@ -155,7 +153,7 @@ namespace FireboltSDK {

private:
EventMap _eventMap;
WPEFramework::Core::CriticalSection _adminLock;
std::mutex _adminLock;
Transport<WPEFramework::Core::JSON::IElement>* _transport;

static Event* _singleton;
Expand Down
Loading

0 comments on commit f44af2d

Please sign in to comment.