Skip to content

Commit e13f8ab

Browse files
ALTV-634 [server] Refactor Resource utilization
1 parent 4aa3e96 commit e13f8ab

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

module/deps/ThreadSafeContainer.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
#include <unordered_map>
5+
#include <optional>
6+
7+
/// \brief Safe thread associative container
8+
/// \tparam K type of key
9+
/// \tparam V type of value
10+
template<class K, class V>
11+
class ThreadSafeContainer final
12+
{
13+
public:
14+
ThreadSafeContainer() = default;
15+
~ThreadSafeContainer() noexcept = default;
16+
17+
/// \brief Insert value in container by key
18+
/// \param K key
19+
/// \param V value
20+
void Insert(K key, V value);
21+
22+
/// \brief Check value exists by key
23+
/// \param K key
24+
/// \return true - if value exists, else - false
25+
bool Find(K key) const;
26+
27+
/// \brief Get element from container
28+
/// \param K key
29+
/// \return value
30+
std::optional<V> GetElement(K key) const;
31+
32+
private:
33+
std::unordered_map<K, V> container;
34+
mutable std::mutex mutex;
35+
36+
ThreadSafeContainer(ThreadSafeContainer&& other) = delete;
37+
ThreadSafeContainer(const ThreadSafeContainer& other) = delete;
38+
ThreadSafeContainer& operator=(ThreadSafeContainer&& other) = delete;
39+
ThreadSafeContainer& operator=(const ThreadSafeContainer& other) = delete;
40+
};
41+
42+
template<class K, class V>
43+
void ThreadSafeContainer<K, V>::Insert(K key, V value)
44+
{
45+
std::lock_guard lock{ mutex };
46+
container[key] = value;
47+
}
48+
49+
template<class K, class V>
50+
bool ThreadSafeContainer<K, V>::Find(K key) const
51+
{
52+
std::lock_guard lock{ mutex };
53+
return container.find(key) != container.end();
54+
}
55+
56+
template<class K, class V>
57+
std::optional<V> ThreadSafeContainer<K, V>::GetElement(K key) const
58+
{
59+
std::lock_guard lock{ mutex };
60+
auto it = container.find(key);
61+
if (it == container.end())
62+
{
63+
return std::nullopt;
64+
}
65+
return it->second;
66+
}

module/src/CScriptRuntimeInfo.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
#pragma once
22

3+
#include <thread>
4+
#include <memory>
5+
#include <optional>
6+
37
#include "v8.h"
4-
#include <libplatform/libplatform.h>
8+
#include "Log.h"
9+
#include "ThreadSafeContainer.h"
10+
#include "libplatform/libplatform.h"
511

612
class CScriptRuntimeInfo
713
{
814
private:
9-
v8::Isolate* isolate;
15+
ThreadSafeContainer<std::thread::id, v8::Isolate*> isolates;
1016
std::unique_ptr<v8::Platform> platform;
1117

1218
public:
13-
v8::Isolate* GetIsolate() { return isolate; }
19+
v8::Isolate* GetIsolate() {
20+
if (!isolates.Find(std::this_thread::get_id()))
21+
{
22+
Log::Colored << "NOT FOUND" << std::this_thread::get_id() << Log::Endl;
23+
v8::Isolate* isolate = MakeIsolate();
24+
isolates.Insert(std::this_thread::get_id(), isolate);
25+
return isolate;
26+
}
27+
return isolates.GetElement(std::this_thread::get_id()).value_or(nullptr);
28+
}
1429

1530
v8::Isolate* MakeIsolate()
1631
{
@@ -28,7 +43,7 @@ class CScriptRuntimeInfo
2843

2944
v8::V8::Initialize();
3045

31-
isolate = MakeIsolate();
46+
isolates.Insert(std::this_thread::get_id(), MakeIsolate());
3247
}
3348

3449
static CScriptRuntimeInfo& Instance()

module/src/runtime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackage* package)
99
{
10-
v8::Isolate* isolate = CScriptRuntimeInfo::Instance().MakeIsolate();
10+
v8::Isolate* isolate = CScriptRuntimeInfo::Instance().GetIsolate();
1111
v8::Isolate::Scope isolateScope(isolate);
1212
v8::HandleScope handleScope(isolate);
1313

0 commit comments

Comments
 (0)