From a3827243ff147df164f21cd0f739e961344f4c0c Mon Sep 17 00:00:00 2001 From: sfja Date: Sun, 15 Dec 2024 01:32:21 +0100 Subject: [PATCH] run in thread --- runtime/actions.cpp | 27 ++++++++++++++++++--------- runtime/actions.hpp | 10 ++++++++-- runtime/main.cpp | 5 ++--- runtime/vm.hpp | 9 ++------- runtime/vm_provider.cpp | 35 ++++++++++++++++++++++++++++++++--- runtime/vm_provider.hpp | 9 +++++++++ 6 files changed, 71 insertions(+), 24 deletions(-) diff --git a/runtime/actions.cpp b/runtime/actions.cpp index c57594b..ff32273 100644 --- a/runtime/actions.cpp +++ b/runtime/actions.cpp @@ -7,17 +7,16 @@ using namespace sliger::rpc::action; -auto sliger::rpc::action::RunDebug::perform_action( - std::unique_ptr writer, +auto Status::perform_action(std::unique_ptr writer, vm_provider::VmProvider& vm) -> void { - auto program = this->instructions; - vm.load_and_run(program); - writer->write("{ \"ok\": true }"); + bool running = not vm.done(); + + writer->write(std::format("{{ \"ok\": true, \"running\": {} }}", running)); writer->flush(); }; -auto sliger::rpc::action::FlameGraph::perform_action( +auto FlameGraph::perform_action( std::unique_ptr writer, vm_provider::VmProvider& vm) -> void { @@ -31,7 +30,7 @@ auto sliger::rpc::action::FlameGraph::perform_action( writer->flush(); }; -auto sliger::rpc::action::CodeCoverage::perform_action( +auto CodeCoverage::perform_action( std::unique_ptr writer, vm_provider::VmProvider& vm) -> void { @@ -45,8 +44,18 @@ auto sliger::rpc::action::CodeCoverage::perform_action( writer->flush(); }; -auto sliger::rpc::action::action_from_json(const sliger::json::Value& value) - -> std::unique_ptr +auto RunDebug::perform_action( + std::unique_ptr writer, + vm_provider::VmProvider& vm) -> void +{ + auto program = this->instructions; + vm.load_and_run(program); + writer->write("{ \"ok\": true }"); + writer->flush(); +}; + +auto sliger::rpc::action::action_from_json( + const sliger::json::Value& value) -> std::unique_ptr { auto& obj = value.as(); auto type = obj.fields.at("type")->as(); diff --git a/runtime/actions.hpp b/runtime/actions.hpp index 80916c7..9515f3d 100644 --- a/runtime/actions.hpp +++ b/runtime/actions.hpp @@ -6,11 +6,17 @@ namespace sliger::rpc::action { struct Action { virtual auto perform_action(std::unique_ptr writer, - vm_provider::VmProvider& vm_provider) -> void - = 0; + vm_provider::VmProvider& vm_provider) -> void = 0; virtual ~Action() = default; }; +class Status : public Action { +public: + Status() { } + auto perform_action(std::unique_ptr writer, + vm_provider::VmProvider& vm_provider) -> void; +}; + class FlameGraph : public Action { public: FlameGraph() { } diff --git a/runtime/main.cpp b/runtime/main.cpp index 52e63b6..b3a26cd 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -3,7 +3,6 @@ #include "rpc_server.hpp" #include "vm.hpp" #include "vm_provider.hpp" -#include #include #include #include @@ -68,13 +67,13 @@ int main(int argc, char** argv) return execute_file_and_exit(argv[2], print_debug); } - auto state = sliger::rpc::vm_provider::VmProvider(); + auto vm_provider = sliger::rpc::vm_provider::VmProvider(); auto rpc = sliger::rpc::RpcServer( [&](std::unique_ptr req, std::unique_ptr writer) { auto action = sliger::rpc::action::action_from_json(*req); - action->perform_action(std::move(writer), state); + action->perform_action(std::move(writer), vm_provider); }); std::cout << "binding on 127.0.0.1:13370\n"; diff --git a/runtime/vm.hpp b/runtime/vm.hpp index 7e7e3ae..fec3772 100644 --- a/runtime/vm.hpp +++ b/runtime/vm.hpp @@ -157,6 +157,8 @@ public: void run_n_instructions(size_t amount); void run_instruction(); + inline auto done() const -> bool { return this->pc >= this->program_size; } + inline auto flame_graph_json() const -> std::string { return json::to_json(this->flame_graph); @@ -215,11 +217,6 @@ private: return this->program[this->pc]; } - inline auto done() const -> bool - { - return not this->halt and this->pc >= this->program_size; - } - inline auto fn_stack_at(size_t idx) -> Value& { return this->stack.at(this->bp + idx); @@ -254,8 +251,6 @@ private: { 2, stderr }, }; - bool halt = false; - FlameGraphBuilder flame_graph; CodeCoverageBuilder code_coverage; }; diff --git a/runtime/vm_provider.cpp b/runtime/vm_provider.cpp index 79c0c18..ea5c6bf 100644 --- a/runtime/vm_provider.cpp +++ b/runtime/vm_provider.cpp @@ -1,22 +1,32 @@ #include "vm_provider.hpp" #include "vm.hpp" +#include +#include using namespace sliger::rpc::vm_provider; auto VmProvider::load_and_run(std::vector instructions) -> void { - auto vm = VM(instructions, + std::lock_guard lock(this->mutex); + + this->vm = VM(instructions, { .flame_graph = true, .code_coverage = true, .print_debug = false, }); - vm.run_until_done(); - this->vm = vm; + + this->running_thread = std::thread([&]() { + while (not this->done()) { + this->run_timeslot(); + } + }); } auto VmProvider::flame_graph_json() -> std::optional { + std::lock_guard lock(this->mutex); + if (this->vm) { return this->vm->flame_graph_json(); } else { @@ -26,9 +36,28 @@ auto VmProvider::flame_graph_json() -> std::optional auto VmProvider::code_coverage_json() -> std::optional { + 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(); +} diff --git a/runtime/vm_provider.hpp b/runtime/vm_provider.hpp index ad49507..7d8a72b 100644 --- a/runtime/vm_provider.hpp +++ b/runtime/vm_provider.hpp @@ -1,6 +1,8 @@ #pragma once #include "vm.hpp" +#include +#include namespace sliger::rpc::vm_provider { class VmProvider { @@ -11,8 +13,15 @@ public: auto flame_graph_json() -> std::optional; auto code_coverage_json() -> std::optional; + auto done() -> bool; + private: + void run_timeslot(); + + std::mutex mutex; + std::optional vm; + std::optional running_thread; }; }