slige/runtime/vm_provider.cpp
2024-12-15 01:32:21 +01:00

64 lines
1.2 KiB
C++

#include "vm_provider.hpp"
#include "vm.hpp"
#include <mutex>
#include <thread>
using namespace sliger::rpc::vm_provider;
auto VmProvider::load_and_run(std::vector<uint32_t> instructions) -> void
{
std::lock_guard lock(this->mutex);
this->vm = VM(instructions,
{
.flame_graph = true,
.code_coverage = true,
.print_debug = false,
});
this->running_thread = std::thread([&]() {
while (not this->done()) {
this->run_timeslot();
}
});
}
auto VmProvider::flame_graph_json() -> std::optional<std::string>
{
std::lock_guard lock(this->mutex);
if (this->vm) {
return this->vm->flame_graph_json();
} else {
return {};
}
}
auto VmProvider::code_coverage_json() -> std::optional<std::string>
{
std::lock_guard lock(this->mutex);
if (this->vm) {
return this->vm->code_coverage_json();
} else {
return {};
}
}
void VmProvider::run_timeslot()
{
std::lock_guard lock(this->mutex);
if (!this->vm.has_value())
return;
this->vm->run_n_instructions(100);
}
auto VmProvider::done() -> bool
{
std::lock_guard lock(this->mutex);
return not this->vm.has_value() or this->vm->done();
}