aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/vm/RuntimeFiber.hpp
blob: 7e37e27386ec86a07b69aedca7c2939baccf3c3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include <cassert>
#include <optional>

#include "Stacktrace.hpp"
#include "Utility.hpp"
#include <lauf/runtime/process.h>
#include <lauf/runtime/value.h>

namespace OpenVic::Vm {
   struct RuntimeProcess;

   struct RuntimeFiber;

   struct RuntimeFiberRef : utility::HandleBase<lauf_runtime_fiber> {
      using HandleBase::HandleBase;

      lauf_runtime_address lauf_handle() const {
         assert(is_valid());
         return lauf_runtime_get_fiber_handle(_handle);
      }

      lauf_runtime_fiber_status status() const {
         assert(is_valid());
         return lauf_runtime_get_fiber_status(_handle);
      }

      const lauf_runtime_value* get_vstack_base() const;

      RuntimeFiberRef iterate_next() {
         assert(is_valid());
         return lauf_runtime_iterate_fibers_next(_handle);
      }

      bool is_valid() const {
         return _handle != nullptr;
      }
   };

   struct RuntimeFiber : utility::MoveOnlyHandleDerived<RuntimeFiber, RuntimeFiberRef> {
      using MoveOnlyHandleDerived::MoveOnlyHandleDerived;
      using MoveOnlyHandleDerived::operator=;

      ~RuntimeFiber();

      std::optional<Stacktrace> get_stacktrace() const;

      std::optional<RuntimeFiberRef> get_parent();

      const lauf_runtime_value* get_vstack_ptr() const;

      bool resume(const lauf_runtime_value* input, size_t input_count, lauf_runtime_value* output, size_t output_count);
      bool resume_until_completion(
         const lauf_runtime_value* input, size_t input_count, lauf_runtime_value* output, size_t output_count
      );

      void destroy();

   private:
      friend struct RuntimeProcess;
      RuntimeFiber(RuntimeProcess* process, lauf_runtime_fiber* fiber) : MoveOnlyHandleDerived(fiber), _process(process) {}

      RuntimeProcess* _process;
   };
}