slige/runtime/vm_provider.cpp

64 lines
1.2 KiB
C++
Raw Normal View History

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