Work on making Angelscript work threaded
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-04-17 13:50:43 +02:00
parent 7dff0c90bc
commit a8f3e30049
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 37 additions and 8 deletions

View File

@ -98,6 +98,8 @@ void AngelScriptResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg,
}
_contextPool = new ContextPool(_engine);
asPrepareMultithread();
}
void AngelScriptResolver::RegisterTypes() {

View File

@ -2,34 +2,61 @@
#define PKMNLIB_CONTEXTPOOL_HPP
#include <angelscript.h>
#include <mutex>
#include <thread>
class ContextPool {
std::vector<asIScriptContext*> _pool;
struct CtxThreadedPoolStorage {
std::vector<asIScriptContext*> Pool;
std::mutex Lock;
~CtxThreadedPoolStorage() {
for (auto* ctx : Pool) {
ctx->Release();
}
}
};
ArbUt::Dictionary<std::thread::id, CtxThreadedPoolStorage*> _pool;
asIScriptEngine* _engine;
CtxThreadedPoolStorage* GetThreadPool() {
auto id = std::this_thread::get_id();
if (!_pool.Has(id)) {
auto t = CtxThreadedPoolStorage();
_pool.Insert(id, new CtxThreadedPoolStorage());
}
return _pool[id];
}
public:
ContextPool(asIScriptEngine* engine) : _engine(engine) {}
~ContextPool() {
for (auto ctx : _pool) {
ctx->Release();
for (const auto& kv : _pool) {
delete kv.second;
}
}
asIScriptContext* RequestContext() {
// Get a context from the pool, or create a new
asIScriptContext* ctx = nullptr;
if (!_pool.empty()) {
ctx = *_pool.rbegin();
_pool.pop_back();
} else
auto* pool = GetThreadPool();
std::lock_guard<std::mutex> guard(pool->Lock);
if (!pool->Pool.empty()) {
ctx = *pool->Pool.rbegin();
pool->Pool.pop_back();
} else {
ctx = _engine->CreateContext();
}
return ctx;
}
void ReturnContextToPool(asIScriptContext* ctx) {
ctx->Unprepare();
_pool.push_back(ctx);
auto* pool = GetThreadPool();
std::lock_guard<std::mutex> guard(pool->Lock);
pool->Pool.push_back(ctx);
}
};