Skip to content

Commit

Permalink
Add multi thread dc vm
Browse files Browse the repository at this point in the history
  • Loading branch information
qubka committed Sep 23, 2024
1 parent 7f674dc commit 34af2ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,28 @@
using namespace plugify;
namespace fs = std::filesystem;

void std::default_delete<DCaggr>::operator()(DCaggr* p) const {
dcFreeAggr(p);
}

void std::default_delete<DCCallVM>::operator()(DCCallVM* p) const {
dcFree(p);
}

namespace py3lm {
extern Python3LanguageModule g_py3lm;

static thread_local VirtualMachine s_vm;

DCCallVM& VirtualMachine::operator()() {
if (_callVirtMachine == nullptr) {
DCCallVM* vm = dcNewCallVM(4096);
dcMode(vm, DC_CALL_C_DEFAULT);
_callVirtMachine = std::unique_ptr<DCCallVM>(vm);
}
return *_callVirtMachine;
}

namespace {
void ReplaceAll(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos{};
Expand Down Expand Up @@ -1457,9 +1476,8 @@ namespace py3lm {
std::vector<std::pair<void*, ValueType>> storage; // used to store array temp memory
DCaggr* ag = nullptr;

ArgsScope(uint8_t size) {
vm = dcNewCallVM(4096);
dcMode(vm, DC_CALL_C_DEFAULT);
explicit ArgsScope(uint8_t size) {
vm = &s_vm();
dcReset(vm);
if (size) {
storage.reserve(size);
Expand Down Expand Up @@ -1616,7 +1634,6 @@ namespace py3lm {
if (ag) {
dcFreeAggr(ag);
}
dcFree(vm);
}
};

Expand Down
21 changes: 21 additions & 0 deletions src/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,28 @@ namespace plugify {
struct Matrix4x4;
}

extern "C" {
typedef struct DCCallVM_ DCCallVM;
typedef struct DCaggr_ DCaggr;
}

template <>
struct std::default_delete<DCaggr> {
void operator()(DCaggr* p) const;
};

template <>
struct std::default_delete<DCCallVM> {
void operator()(DCCallVM* p) const;
};

namespace py3lm {
struct VirtualMachine {
DCCallVM& operator()();
private:
std::unique_ptr<DCCallVM> _callVirtMachine;
};

struct PythonMethodData {
plugify::Function jitFunction;
PyObject* pythonFunction{};
Expand Down

0 comments on commit 34af2ec

Please sign in to comment.