feat(furvm): introduce executor
This commit is contained in:
@@ -13,8 +13,10 @@ namespace furvm {
|
|||||||
class context {
|
class context {
|
||||||
public:
|
public:
|
||||||
using value_type = std::unique_ptr<mod>; /**< An alias to unique pointer of module. */
|
using value_type = std::unique_ptr<mod>; /**< An alias to unique pointer of module. */
|
||||||
|
|
||||||
|
friend class executor;
|
||||||
public:
|
public:
|
||||||
context() = default;
|
context();
|
||||||
~context() = default;
|
~context() = default;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,6 +95,7 @@ public:
|
|||||||
constexpr size_t size() const { return m_modules.size(); }
|
constexpr size_t size() const { return m_modules.size(); }
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<mod>> m_modules;
|
std::vector<std::unique_ptr<mod>> m_modules;
|
||||||
|
std::vector<std::shared_ptr<executor>> m_executors;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace furvm
|
} // namespace furvm
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#ifndef FURVM_EXECUTOR_HPP
|
||||||
|
#define FURVM_EXECUTOR_HPP
|
||||||
|
|
||||||
|
#include "furvm/fwd.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace furvm {
|
||||||
|
|
||||||
|
enum class executor_flags : std::uint32_t {
|
||||||
|
Suspended = (1 << 0), /**< Execution suspended. */
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline executor_flags operator|(executor_flags lhs, executor_flags rhs) {
|
||||||
|
return executor_flags(static_cast<std::uint32_t>(lhs) | static_cast<std::uint32_t>(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline executor_flags operator&(executor_flags lhs, executor_flags rhs) {
|
||||||
|
return executor_flags(static_cast<std::uint32_t>(lhs) & static_cast<std::uint32_t>(rhs));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline executor_flags operator~(executor_flags flags) {
|
||||||
|
return executor_flags(~static_cast<std::uint32_t>(flags));
|
||||||
|
}
|
||||||
|
|
||||||
|
class executor {
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* @brief A private token for the private constructor.
|
||||||
|
*
|
||||||
|
* Also `egzekutor` in Polish translates to `executor` I think.
|
||||||
|
*/
|
||||||
|
struct egzekutor {
|
||||||
|
explicit egzekutor() = default;
|
||||||
|
};
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Private constructor.
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @param context
|
||||||
|
*/
|
||||||
|
executor(egzekutor, executor_handle id, const std::shared_ptr<context>& context);
|
||||||
|
|
||||||
|
~executor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Move constructor.
|
||||||
|
*/
|
||||||
|
executor(executor&&) = default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Move constructor.
|
||||||
|
*/
|
||||||
|
executor& operator=(executor&&) = default;
|
||||||
|
|
||||||
|
executor(const executor&) = delete;
|
||||||
|
executor& operator=(const executor&) = delete;
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns a new executor.
|
||||||
|
*
|
||||||
|
* @param context Furvm context.
|
||||||
|
*/
|
||||||
|
static std::shared_ptr<executor> create(const std::shared_ptr<context>& context);
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Returns an id of this executor.
|
||||||
|
*
|
||||||
|
* @return The id.
|
||||||
|
*/
|
||||||
|
executor_handle id() const { return m_id; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns flags of this executor.
|
||||||
|
*
|
||||||
|
* @return The flags.
|
||||||
|
*/
|
||||||
|
executor_flags flags() const { return m_flags; }
|
||||||
|
private:
|
||||||
|
executor_handle m_id;
|
||||||
|
executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization)
|
||||||
|
std::shared_ptr<context> m_context;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace furvm
|
||||||
|
|
||||||
|
#endif // FURVM_EXECUTOR_HPP
|
||||||
@@ -61,6 +61,27 @@ class mod;
|
|||||||
*/
|
*/
|
||||||
using module_handle = std::uint32_t;
|
using module_handle = std::uint32_t;
|
||||||
|
|
||||||
|
// executor.hpp
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @enum executor_flags
|
||||||
|
* @brief Flags of an executor.
|
||||||
|
*/
|
||||||
|
enum class executor_flags : std::uint32_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class executor
|
||||||
|
* @brief Furvm executor.
|
||||||
|
*
|
||||||
|
* Furvm executors are like threads.
|
||||||
|
*/
|
||||||
|
class executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Furvm executor's index.
|
||||||
|
*/
|
||||||
|
using executor_handle = std::uint32_t;
|
||||||
|
|
||||||
// context.hpp
|
// context.hpp
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
#include "furvm/context.hpp"
|
#include "furvm/context.hpp"
|
||||||
|
|
||||||
namespace furvm {}
|
namespace furvm {
|
||||||
|
|
||||||
|
context::context() {}
|
||||||
|
|
||||||
|
} // namespace furvm
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include "furvm/executor.hpp"
|
||||||
|
|
||||||
|
#include "furvm/context.hpp"
|
||||||
|
|
||||||
|
namespace furvm {
|
||||||
|
|
||||||
|
executor::executor(egzekutor, executor_handle id, const std::shared_ptr<context>& context)
|
||||||
|
: m_id(id), m_context(context) {}
|
||||||
|
|
||||||
|
executor::~executor() {
|
||||||
|
m_context->m_executors[m_id] = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<executor> executor::create(const std::shared_ptr<context>& context) {
|
||||||
|
auto ex = std::make_shared<executor>(egzekutor{}, context->m_executors.size(), context);
|
||||||
|
context->m_executors.push_back(ex);
|
||||||
|
return std::move(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace furvm
|
||||||
+6
-2
@@ -1,10 +1,12 @@
|
|||||||
#ifndef LIBFURVM
|
#ifndef LIBFURVM
|
||||||
|
|
||||||
#include "furvm/context.hpp"
|
#include "furvm/context.hpp"
|
||||||
|
#include "furvm/executor.hpp"
|
||||||
#include "furvm/instruction.hpp"
|
#include "furvm/instruction.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
static constexpr std::array<furvm::byte, 3> s_bytecode = {
|
static constexpr std::array<furvm::byte, 3> s_bytecode = {
|
||||||
furvm::byte(furvm::instruction_t::PushB2I),
|
furvm::byte(furvm::instruction_t::PushB2I),
|
||||||
@@ -13,9 +15,11 @@ static constexpr std::array<furvm::byte, 3> s_bytecode = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
furvm::context context;
|
auto context = std::make_shared<furvm::context>();
|
||||||
|
|
||||||
const auto& mainModule = context.emplace(s_bytecode.begin(), s_bytecode.end());
|
const auto& mainModule = context->emplace(s_bytecode.begin(), s_bytecode.end());
|
||||||
|
|
||||||
|
auto executor = furvm::executor::create(context);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user