Skip to content

Commit

Permalink
ALTV-634 [server] Refactor Resource utilization
Browse files Browse the repository at this point in the history
  • Loading branch information
libragliese581 committed Feb 18, 2025
1 parent 4aa3e96 commit e13f8ab
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
66 changes: 66 additions & 0 deletions module/deps/ThreadSafeContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <mutex>
#include <unordered_map>
#include <optional>

/// \brief Safe thread associative container
/// \tparam K type of key
/// \tparam V type of value
template<class K, class V>
class ThreadSafeContainer final
{
public:
ThreadSafeContainer() = default;
~ThreadSafeContainer() noexcept = default;

/// \brief Insert value in container by key
/// \param K key
/// \param V value
void Insert(K key, V value);

/// \brief Check value exists by key
/// \param K key
/// \return true - if value exists, else - false
bool Find(K key) const;

/// \brief Get element from container
/// \param K key
/// \return value
std::optional<V> GetElement(K key) const;

private:
std::unordered_map<K, V> container;
mutable std::mutex mutex;

ThreadSafeContainer(ThreadSafeContainer&& other) = delete;
ThreadSafeContainer(const ThreadSafeContainer& other) = delete;
ThreadSafeContainer& operator=(ThreadSafeContainer&& other) = delete;
ThreadSafeContainer& operator=(const ThreadSafeContainer& other) = delete;
};

template<class K, class V>
void ThreadSafeContainer<K, V>::Insert(K key, V value)
{
std::lock_guard lock{ mutex };
container[key] = value;
}

template<class K, class V>
bool ThreadSafeContainer<K, V>::Find(K key) const
{
std::lock_guard lock{ mutex };
return container.find(key) != container.end();
}

template<class K, class V>
std::optional<V> ThreadSafeContainer<K, V>::GetElement(K key) const
{
std::lock_guard lock{ mutex };
auto it = container.find(key);
if (it == container.end())
{
return std::nullopt;
}
return it->second;
}
23 changes: 19 additions & 4 deletions module/src/CScriptRuntimeInfo.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
#pragma once

#include <thread>
#include <memory>
#include <optional>

#include "v8.h"
#include <libplatform/libplatform.h>
#include "Log.h"
#include "ThreadSafeContainer.h"
#include "libplatform/libplatform.h"

class CScriptRuntimeInfo
{
private:
v8::Isolate* isolate;
ThreadSafeContainer<std::thread::id, v8::Isolate*> isolates;
std::unique_ptr<v8::Platform> platform;

public:
v8::Isolate* GetIsolate() { return isolate; }
v8::Isolate* GetIsolate() {
if (!isolates.Find(std::this_thread::get_id()))
{
Log::Colored << "NOT FOUND" << std::this_thread::get_id() << Log::Endl;
v8::Isolate* isolate = MakeIsolate();
isolates.Insert(std::this_thread::get_id(), isolate);
return isolate;
}
return isolates.GetElement(std::this_thread::get_id()).value_or(nullptr);
}

v8::Isolate* MakeIsolate()
{
Expand All @@ -28,7 +43,7 @@ class CScriptRuntimeInfo

v8::V8::Initialize();

isolate = MakeIsolate();
isolates.Insert(std::this_thread::get_id(), MakeIsolate());
}

static CScriptRuntimeInfo& Instance()
Expand Down
2 changes: 1 addition & 1 deletion module/src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackage* package)
{
v8::Isolate* isolate = CScriptRuntimeInfo::Instance().MakeIsolate();
v8::Isolate* isolate = CScriptRuntimeInfo::Instance().GetIsolate();
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);

Expand Down

0 comments on commit e13f8ab

Please sign in to comment.