slige/runtime/vm_provider.cpp

69 lines
1.3 KiB
C++
Raw Normal View History

#include "vm_provider.hpp"
#include "vm.hpp"
2024-12-15 00:32:21 +00:00
#include <mutex>
#include <thread>
2024-12-15 02:07:33 +00:00
using namespace sliger::rpc;
2024-12-15 02:07:33 +00:00
auto VmProvider::load_and_start(std::vector<uint32_t> instructions) -> void
{
2024-12-15 00:32:21 +00:00
std::lock_guard lock(this->mutex);
2024-12-16 09:05:20 +00:00
if (this->running_thread) {
this->vm = {};
this->running_thread = {};
}
2024-12-15 02:07:33 +00:00
this->vm.emplace(instructions,
VMOpts {
.flame_graph = true,
.code_coverage = true,
2024-12-13 19:17:22 +00:00
.print_debug = false,
});
2024-12-15 00:32:21 +00:00
this->running_thread = std::thread([&]() {
while (not this->done()) {
this->run_timeslot();
}
});
}
auto VmProvider::flame_graph_json() -> std::optional<std::string>
{
2024-12-15 00:32:21 +00:00
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>
{
2024-12-15 00:32:21 +00:00
std::lock_guard lock(this->mutex);
if (this->vm) {
return this->vm->code_coverage_json();
} else {
return {};
}
}
2024-12-15 00:32:21 +00:00
void VmProvider::run_timeslot()
{
std::lock_guard lock(this->mutex);
2024-12-15 02:07:33 +00:00
if (not this->vm)
2024-12-15 00:32:21 +00:00
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();
}