Skip to content

Commit

Permalink
Lock resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaszm committed Dec 12, 2024
1 parent c220cb9 commit 45056bc
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions Source/plugins/JSONRPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,8 @@ namespace JSONRPCErrorAssessorTypes {
class LookupStorageType {
public:
LookupStorageType()
: _storage()
: _lock()
, _storage()
, _nextId(1)
{
}
Expand All @@ -1132,12 +1133,15 @@ namespace JSONRPCErrorAssessorTypes {

obj->AddRef();

_lock.Lock();

id = _nextId++;

_storage.emplace(std::piecewise_construct,
std::forward_as_tuple(id),
std::forward_as_tuple(context.ChannelId(), obj));

_lock.Unlock();
}

return (id);
Expand All @@ -1147,48 +1151,63 @@ namespace JSONRPCErrorAssessorTypes {
{
T* obj{};

_lock.Lock();

auto it = _storage.find(id);

if ((it != _storage.end()) && ((*it).second.first == context.ChannelId())) {
obj = (*it).second.second;
obj->AddRef();
}

_lock.Unlock();

return (obj);
}

const T* Lookup(const Core::JSONRPC::Context& context, const ID id) const
{
const T* obj{};

_lock.Lock();

auto const it = _storage.cfind(id);

if ((it != _storage.cend()) && ((*it).second.first == context.ChannelId())) {
obj = (*it).second.second;
obj->AddRef();
}

_lock.Unlock();

return (obj);
}

T* Dispose(const Core::JSONRPC::Context& context, const ID id)
{
T* obj{};

_lock.Lock();

auto it = _storage.find(id);

if (it != _storage.end() && ((*it).second.first == context.ChannelId())) {
obj = (*it).second.second;
_storage.erase(it);
}

_lock.Unlock();

return (obj);
}

public:
using OnCloseCallback = std::function<void(const ID, const uint32_t, T*)>;
void Closed(const uint32_t& channel, const OnCloseCallback& callback = nullptr)

void Closed(const uint32_t channel, const OnCloseCallback& callback = nullptr)
{
_lock.Lock();

auto it = _storage.begin();

while (it != _storage.end()) {
Expand All @@ -1208,14 +1227,18 @@ namespace JSONRPCErrorAssessorTypes {
++it;
}
}

_lock.Unlock();

}

void Closed(const Core::JSONRPC::Context& context, const std::function<void(ID, T*)>& callback = nullptr)
void Closed(const Core::JSONRPC::Context& context, const OnCloseCallback& callback = nullptr)
{
Closed(context.ChannelId());
}

private:
mutable Core::CriticalSection _lock;
std::map<ID, std::pair<uint32_t, T*>> _storage;
ID _nextId;
};
Expand Down

0 comments on commit 45056bc

Please sign in to comment.