intial commit

This commit is contained in:
2023-05-29 14:51:44 +02:00
commit da781b87f2
38 changed files with 4842 additions and 0 deletions

View File

@@ -0,0 +1,213 @@
#include "./coroutine.hpp"
#include <array>
#include <cstdio>
#include "../util/os.hpp"
namespace mijin
{
//
// internal defines
//
//
// internal constants
//
//
// internal types
//
//
// internal variables
//
thread_local TaskLoop::StoredTask* MultiThreadedTaskLoop::currentTask_ = nullptr;
//
// internal functions
//
void MultiThreadedTaskLoop::managerThread(std::stop_token stopToken) // NOLINT(performance-unnecessary-value-param)
{
setCurrentThreadName("Task Manager");
while (!stopToken.stop_requested())
{
// first clear out any parked tasks that are actually finished
auto it = std::remove_if(parkedTasks_.begin(), parkedTasks_.end(), [](StoredTask& task) {
return !task.task || task.task->status() == TaskStatus::FINISHED;
});
parkedTasks_.erase(it, parkedTasks_.end());
// then try to push any task from the buffer into the queue, if possible
for (auto it = parkedTasks_.begin(); it != parkedTasks_.end();)
{
if (!it->task->canResume())
{
++it;
continue;
}
if (readyTasks_.tryPushMaybeMove(*it)) {
it = parkedTasks_.erase(it);
}
else {
break;
}
}
// then clear the incoming task queue
while (true)
{
std::optional<StoredTask> task = queuedTasks_.tryPop();
if (!task.has_value()) {
break;
}
// try to directly move it into the next queue
if (readyTasks_.tryPushMaybeMove(*task)) {
continue;
}
// otherwise park it
parkedTasks_.push_back(std::move(*task));
}
// next collect tasks returning from the worker threads
while (true)
{
std::optional<StoredTask> task = returningTasks_.tryPop();
if (!task.has_value()) {
break;
}
if (task->task == nullptr || task->task->status() == TaskStatus::FINISHED) {
continue; // task has been transferred or finished
}
if (task->task->canResume() && readyTasks_.tryPushMaybeMove(*task)) {
continue; // instantly resume, no questions asked
}
// otherwise park it for future processing
parkedTasks_.push_back(std::move(*task));
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void MultiThreadedTaskLoop::workerThread(std::stop_token stopToken, std::size_t workerId) // NOLINT(performance-unnecessary-value-param)
{
currentLoopStorage() = this; // forever (on this thread)
std::array<char, 16> threadName;
(void) std::snprintf(threadName.data(), 16, "Task Worker %lu", static_cast<unsigned long>(workerId));
setCurrentThreadName(threadName.data());
while (!stopToken.stop_requested())
{
// try to fetch a task to run
std::optional<StoredTask> task = readyTasks_.tryPop();
if (!task.has_value())
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
// run it
currentTask_ = &*task;
tickTask(*task);
currentTask_ = nullptr;
// and give it back
returningTasks_.push(std::move(*task));
}
}
//
// public functions
//
void SimpleTaskLoop::transferCurrentTask(TaskLoop& otherLoop) noexcept
{
assertCorrectThread();
if (&otherLoop == this) {
return;
}
MIJIN_ASSERT_FATAL(currentTask_ != tasks_.end(), "Trying to call transferCurrentTask() while not running a task!");
// now start the transfer, first disown the task
StoredTask storedTask = std::move(*currentTask_);
currentTask_->task = nullptr; // just to be sure
// then send it over to the other loop
otherLoop.addStoredTask(std::move(storedTask));
}
void SimpleTaskLoop::addStoredTask(StoredTask&& storedTask) noexcept
{
storedTask.task->setLoop(this);
if (threadId_ == std::thread::id() || threadId_ == std::this_thread::get_id())
{
// same thread, just copy it over
if (currentLoopStorage() != nullptr) {
// currently running, can't append to tasks_ directly
newTasks_.push_back(std::move(storedTask));
}
else {
tasks_.push_back(std::move(storedTask));
}
}
else
{
// other thread, better be safe
queuedTasks_.push(std::move(storedTask));
}
}
void MultiThreadedTaskLoop::transferCurrentTask(TaskLoop& otherLoop) noexcept
{
if (&otherLoop == this) {
return;
}
MIJIN_ASSERT_FATAL(currentTask_ != nullptr, "Trying to call transferCurrentTask() while not running a task!");
// now start the transfer, first disown the task
StoredTask storedTask = std::move(*currentTask_);
currentTask_->task = nullptr; // just to be sure
// then send it over to the other loop
otherLoop.addStoredTask(std::move(storedTask));
}
void MultiThreadedTaskLoop::addStoredTask(StoredTask&& storedTask) noexcept
{
storedTask.task->setLoop(this);
// just assume we are not on the manager thread, as that wouldn't make sense
queuedTasks_.push(std::move(storedTask));
}
void MultiThreadedTaskLoop::start(std::size_t numWorkerThreads)
{
managerThread_ = std::jthread([this](std::stop_token stopToken) { managerThread(std::move(stopToken)); });
workerThreads_.reserve(numWorkerThreads);
for (std::size_t workerId = 0; workerId < numWorkerThreads; ++workerId) {
workerThreads_.emplace_back([this, workerId](std::stop_token stopToken) { workerThread(std::move(stopToken), workerId); });
}
}
void MultiThreadedTaskLoop::stop()
{
workerThreads_.clear(); // will also set the stop token
managerThread_ = {}; // this too
}
} // namespace mijin

View File

@@ -0,0 +1,648 @@
#pragma once
#ifndef MIJIN_ASYNC_COROUTINE_HPP_INCLUDED
#define MIJIN_ASYNC_COROUTINE_HPP_INCLUDED 1
#include <any>
#include <coroutine>
#include <memory>
#include <thread>
#include <tuple>
#include "./future.hpp"
#include "./message_queue.hpp"
#include "../container/optional.hpp"
#include "../util/flag.hpp"
#include "../util/traits.hpp"
namespace mijin
{
//
// public defines
//
#if !defined(MIJIN_COROUTINE_ENABLE_DEBUG_INFO)
#define MIJIN_COROUTINE_ENABLE_DEBUG_INFO 0
#endif
//
// public types
//
enum class TaskStatus
{
SUSPENDED = 0,
RUNNING = 1,
WAITING = 2,
FINISHED = 3,
YIELDED = 4
};
// forward declarations
template<typename T>
struct TaskState;
class TaskLoop;
template<typename TResult = void>
class TaskBase;
namespace impl
{
template<typename TReturn, typename TPromise>
struct TaskReturn
{
template<typename... TArgs>
constexpr void return_value(TArgs&&... args) noexcept {
*(static_cast<TPromise&>(*this).state_) = TaskState<TReturn>(TReturn(std::forward<TArgs>(args)...), TaskStatus::FINISHED);
}
constexpr void return_value(TReturn value) noexcept {
*(static_cast<TPromise&>(*this).state_) = TaskState<TReturn>(TReturn(std::move(value)), TaskStatus::FINISHED);
}
};
template<typename TPromise>
struct TaskReturn<void, TPromise>
{
constexpr void return_void() noexcept {
static_cast<TPromise&>(*this).state_->status = TaskStatus::FINISHED;
}
};
} // namespace impl
template<typename T>
struct TaskState
{
Optional<T> value;
TaskStatus status = TaskStatus::SUSPENDED;
TaskState() = default;
TaskState(const TaskState&) = default;
TaskState(TaskState&&) noexcept = default;
constexpr TaskState(T _value, TaskStatus _status) noexcept : value(std::move(_value)), status(_status) {}
TaskState& operator=(const TaskState&) = default;
TaskState& operator=(TaskState&&) noexcept = default;
};
template<>
struct TaskState<void>
{
TaskStatus status = TaskStatus::SUSPENDED;
TaskState() = default;
TaskState(const TaskState&) = default;
TaskState(TaskState&&) noexcept = default;
constexpr TaskState(TaskStatus _status) noexcept : status(_status) {}
TaskState& operator=(const TaskState&) = default;
TaskState& operator=(TaskState&&) noexcept = default;
};
template<typename TValue>
struct TaskAwaitableFuture
{
FuturePtr<TValue> future;
[[nodiscard]] constexpr bool await_ready() const noexcept { return future->ready(); }
constexpr void await_suspend(std::coroutine_handle<>) const noexcept {}
constexpr TValue await_resume() const noexcept {
if constexpr (std::is_same_v<TValue, void>) {
return;
}
else {
return std::move(future->get());
}
}
};
template<typename... TArgs>
struct TaskAwaitableSignal
{
std::shared_ptr<std::tuple<TArgs...>> data;
[[nodiscard]] constexpr bool await_ready() const noexcept { return false; }
constexpr void await_suspend(std::coroutine_handle<>) const noexcept {}
constexpr auto& await_resume() const noexcept {
return *data;
}
};
template<typename TSingleArg>
struct TaskAwaitableSignal<TSingleArg>
{
std::shared_ptr<TSingleArg> data;
[[nodiscard]] constexpr bool await_ready() const noexcept { return false; }
constexpr void await_suspend(std::coroutine_handle<>) const noexcept {}
constexpr auto& await_resume() const noexcept {
return *data;
}
};
template<>
struct TaskAwaitableSignal<>
{
[[nodiscard]] constexpr bool await_ready() const noexcept { return false; }
constexpr void await_suspend(std::coroutine_handle<>) const noexcept {}
constexpr void await_resume() const noexcept {}
};
template<typename TTraits>
struct TaskPromise : impl::TaskReturn<typename TTraits::result_t, TaskPromise<TTraits>>
{
using handle_t = std::coroutine_handle<TaskPromise>;
using task_t = typename TTraits::task_t;
using result_t = typename TTraits::result_t;
std::shared_ptr<TaskState<result_t>> state_ = std::make_shared<TaskState<result_t>>();
TaskLoop* loop_ = nullptr;
constexpr task_t get_return_object() noexcept { return task_t(handle_t::from_promise(*this)); }
constexpr std::suspend_always initial_suspend() noexcept { return {}; }
constexpr std::suspend_always final_suspend() noexcept { return {}; }
// template<typename TValue>
// constexpr std::suspend_always yield_value(TValue value) noexcept {
// *state_ = TaskState<result_t>(std::move(value), TaskStatus::YIELDED);
// return {};
// }
// TODO: implement yielding (can't use futures for this)
constexpr void unhandled_exception() noexcept {}
template<typename TValue>
auto await_transform(FuturePtr<TValue> future) noexcept
{
MIJIN_ASSERT(loop_ != nullptr, "Cannot await future outside of a loop!");
TaskAwaitableFuture<TValue> awaitable{future};
if (!awaitable.await_ready())
{
state_->status = TaskStatus::WAITING;
future->sigSet.connect([this, future]() mutable
{
state_->status = TaskStatus::SUSPENDED;
}, Oneshot::YES);
}
return awaitable;
}
template<typename TResultOther>
auto await_transform(TaskBase<TResultOther> task) noexcept
{
MIJIN_ASSERT(loop_ != nullptr, "Cannot await another task outside of a loop!");
auto future = delayEvaluation<TResultOther>(loop_)->addTask(std::move(task)); // hackidyhack: delay evaluation of the type of loop_ as it is only forward-declared here
return await_transform(future);
}
template<typename TFirstArg, typename TSecondArg, typename... TArgs>
auto await_transform(Signal<TFirstArg, TSecondArg, TArgs...>& signal) noexcept
{
auto data = std::make_shared<std::tuple<TFirstArg, TSecondArg, TArgs...>>();
signal.connect([this, data](TFirstArg arg0, TSecondArg arg1, TArgs... args) mutable
{
*data = std::make_tuple(std::move(arg0), std::move(arg1), std::move(args)...);
state_->status = TaskStatus::SUSPENDED;
}, Oneshot::YES);
TaskAwaitableSignal<TFirstArg, TSecondArg, TArgs...> awaitable{data};
state_->status = TaskStatus::WAITING;
return awaitable;
}
template<typename TFirstArg>
auto await_transform(Signal<TFirstArg>& signal) noexcept
{
auto data = std::make_shared<TFirstArg>();
signal.connect([this, data](TFirstArg arg0) mutable
{
*data = std::move(arg0);
state_->status = TaskStatus::SUSPENDED;
}, Oneshot::YES);
TaskAwaitableSignal<TFirstArg> awaitable{data};
state_->status = TaskStatus::WAITING;
return awaitable;
}
auto await_transform(Signal<>& signal) noexcept
{
signal.connect([this]()
{
state_->status = TaskStatus::SUSPENDED;
}, Oneshot::YES);
TaskAwaitableSignal<> awaitable{};
state_->status = TaskStatus::WAITING;
return awaitable;
}
std::suspend_always await_transform(std::suspend_always) noexcept
{
state_->status = TaskStatus::SUSPENDED;
return std::suspend_always();
}
std::suspend_never await_transform(std::suspend_never) noexcept {
return std::suspend_never();
}
};
template<typename TResult>
class TaskBase
{
public:
using task_t = TaskBase;
using result_t = TResult;
struct Traits
{
using task_t = TaskBase;
using result_t = TResult;
};
public:
using promise_type = TaskPromise<Traits>;
using handle_t = typename promise_type::handle_t;
private:
handle_t handle_;
std::shared_ptr<TaskState<result_t>> state_;
public:
constexpr explicit TaskBase(handle_t handle) noexcept : handle_(handle), state_(handle.promise().state_) {}
TaskBase(const TaskBase&) = default;
TaskBase(TaskBase&& other) noexcept = default;
~TaskBase() noexcept;
public:
TaskBase& operator=(const TaskBase&) = default;
TaskBase& operator=(TaskBase&& other) noexcept = default;
[[nodiscard]]
constexpr bool operator==(const TaskBase& other) const noexcept { return handle_ == other.handle_; }
[[nodiscard]]
constexpr bool operator!=(const TaskBase& other) const noexcept { return handle_ != other.handle_; }
public:
constexpr TaskState<TResult>& resume() noexcept
{
state_->status = TaskStatus::RUNNING;
handle_.resume();
return *state_;
}
[[nodiscard]]
constexpr TaskState<TResult>& state() noexcept
{
return *state_;
}
private:
[[nodiscard]]
constexpr handle_t handle() const noexcept { return handle_; }
[[nodiscard]]
constexpr TaskLoop* getLoop() noexcept
{
return handle_.promise().loop_;
}
constexpr void setLoop(TaskLoop* loop) noexcept
{
// MIJIN_ASSERT(handle_.promise().loop_ == nullptr
// || handle_.promise().loop_ == loop
// || loop == nullptr, "Task already has a loop assigned!");
handle_.promise().loop_ = loop;
}
friend class TaskLoop;
template<typename TTask>
friend class WrappedTask;
};
class WrappedTaskBase
{
public:
virtual ~WrappedTaskBase() = default;
public:
virtual TaskStatus status() noexcept = 0;
// virtual std::any result() noexcept = 0;
virtual void resume() noexcept = 0;
virtual void* raw() noexcept = 0;
virtual std::coroutine_handle<> handle() noexcept = 0;
virtual void setLoop(TaskLoop* loop) noexcept = 0;
[[nodiscard]] inline bool canResume() {
const TaskStatus stat = status();
return (stat == TaskStatus::SUSPENDED || stat == TaskStatus::YIELDED);
}
};
template<typename TTask>
class WrappedTask : public WrappedTaskBase
{
private:
TTask task_;
public:
constexpr explicit WrappedTask(TTask&& task) noexcept : task_(std::move(task)) {}
WrappedTask(const WrappedTask&) = delete;
WrappedTask(WrappedTask&&) noexcept = default;
public:
WrappedTask& operator=(const WrappedTask&) = delete;
WrappedTask& operator=(WrappedTask&&) noexcept = default;
public:
TaskStatus status() noexcept override { return task_.state().status; }
// std::any result() noexcept override
// {
// if constexpr (std::is_same_v<typename TTask::result_t, void>) {
// return {};
// }
// else {
// return std::any(task_.state().value);
// }
// }
void resume() noexcept override { task_.resume(); }
void* raw() noexcept override { return &task_; }
std::coroutine_handle<> handle() noexcept override { return task_.handle(); }
void setLoop(TaskLoop* loop) noexcept override { task_.setLoop(loop); }
};
template<typename TTask>
std::unique_ptr<WrappedTask<TTask>> wrapTask(TTask&& task) noexcept
{
return std::make_unique<WrappedTask<TTask>>(std::forward<TTask>(task));
}
class TaskLoop
{
public:
MIJIN_DEFINE_FLAG(CanContinue);
MIJIN_DEFINE_FLAG(IgnoreWaiting);
protected:
using wrapped_task_t = WrappedTaskBase;
using wrapped_task_base_ptr_t = std::unique_ptr<wrapped_task_t>;
struct StoredTask
{
wrapped_task_base_ptr_t task;
std::function<void(StoredTask&)> setFuture;
std::any resultData;
};
using task_vector_t = std::vector<StoredTask>;
template<typename TTask>
using wrapped_task_ptr_t = std::unique_ptr<WrappedTask<TTask>>;
public:
TaskLoop() = default;
TaskLoop(const TaskLoop&) = delete;
TaskLoop(TaskLoop&&) = delete;
virtual ~TaskLoop() = default;
TaskLoop& operator=(const TaskLoop&) = delete;
TaskLoop& operator=(TaskLoop&&) = delete;
template<typename TResult>
inline FuturePtr<TResult> addTask(TaskBase<TResult> task) noexcept;
virtual void transferCurrentTask(TaskLoop& otherLoop) noexcept = 0;
virtual void addStoredTask(StoredTask&& storedTask) noexcept = 0;
[[nodiscard]] static TaskLoop& current() noexcept;
protected:
inline TaskStatus tickTask(StoredTask& task) noexcept;
protected:
static inline TaskLoop*& currentLoopStorage() noexcept;
template<typename TResult>
static inline void setFutureHelper(StoredTask& storedTask) noexcept;
};
template<typename TResult = void>
using Task = TaskBase<TResult>;
class SimpleTaskLoop : public TaskLoop
{
private:
task_vector_t tasks_;
task_vector_t newTasks_;
task_vector_t::iterator currentTask_;
MessageQueue<StoredTask> queuedTasks_;
std::thread::id threadId_;
public: // TaskLoop implementation
void transferCurrentTask(TaskLoop& otherLoop) noexcept override;
void addStoredTask(StoredTask&& storedTask) noexcept override;
public: // public interface
[[nodiscard]] constexpr bool empty() const noexcept { return tasks_.empty() && newTasks_.empty(); }
inline CanContinue tick() noexcept;
inline void runUntilDone(IgnoreWaiting ignoreWaiting = IgnoreWaiting::NO) noexcept;
private:
inline void assertCorrectThread() { MIJIN_ASSERT(threadId_ == std::thread::id() || threadId_ == std::this_thread::get_id(), "Unsafe to TaskLoop from different thread!"); }
};
class MultiThreadedTaskLoop : public TaskLoop
{
private:
task_vector_t parkedTasks_; // buffer for tasks that don't fit into readyTasks_
MessageQueue<StoredTask> queuedTasks_; // tasks that should be appended to parked tasks
MessageQueue<StoredTask> readyTasks_; // task queue to send tasks to a worker thread
MessageQueue<StoredTask> returningTasks_; // task that have executed on a worker thread and return for further processing
std::jthread managerThread_;
std::vector<std::jthread> workerThreads_;
public: // TaskLoop implementation
void transferCurrentTask(TaskLoop& otherLoop) noexcept override;
void addStoredTask(StoredTask&& storedTask) noexcept override;
public: // public interface
void start(std::size_t numWorkerThreads);
void stop();
private: // private stuff
void managerThread(std::stop_token stopToken);
void workerThread(std::stop_token stopToken, std::size_t workerId);
static thread_local StoredTask* currentTask_;
};
//
// public functions
//
template<typename TResult>
TaskBase<TResult>::~TaskBase() noexcept
{
if (handle_)
{
// handle_.destroy();
}
}
template<typename TResult>
inline FuturePtr<TResult> TaskLoop::addTask(TaskBase<TResult> task) noexcept
{
MIJIN_ASSERT(!task.getLoop(), "Attempting to add task that already has a loop!");
task.setLoop(this);
auto future = std::make_shared<Future<TResult>>();
auto setFuture = &setFutureHelper<TResult>;
// add tasks to a seperate vector first as we might be running another task right now
addStoredTask(StoredTask{
.task = wrapTask(std::move(task)),
.setFuture = setFuture,
.resultData = future
});
return future;
}
inline TaskStatus TaskLoop::tickTask(StoredTask& task) noexcept
{
TaskStatus status = {};
do
{
task.task->resume();
status = task.task ? task.task->status() : TaskStatus::WAITING; // no inner task -> task switch context (and will be removed later)
}
while (status == TaskStatus::RUNNING);
if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED)
{
task.setFuture(task);
}
return status;
}
/* static */ inline auto TaskLoop::current() noexcept -> TaskLoop&
{
MIJIN_ASSERT(currentLoopStorage() != nullptr, "Attempting to fetch current loop while no coroutine is running!");
return *currentLoopStorage();
}
/* static */ auto TaskLoop::currentLoopStorage() noexcept -> TaskLoop*&
{
static thread_local TaskLoop* storage = nullptr;
return storage;
}
template<typename TResult>
/* static */ inline void TaskLoop::setFutureHelper(StoredTask& storedTask) noexcept
{
TaskBase<TResult>& task = *static_cast<TaskBase<TResult>*>(storedTask.task->raw());
auto future = std::any_cast<FuturePtr<TResult>>(storedTask.resultData);
if constexpr (!std::is_same_v<TResult, void>)
{
MIJIN_ASSERT(!task.state().value.empty(), "Task did not produce a value?");
future->set(std::move(task.state().value.get()));
}
else {
future->set();
}
}
inline std::suspend_always switchContext(TaskLoop& taskLoop)
{
TaskLoop& currentTaskLoop = TaskLoop::current();
if (&currentTaskLoop == &taskLoop) {
return {};
}
currentTaskLoop.transferCurrentTask(taskLoop);
return {};
}
inline auto SimpleTaskLoop::tick() noexcept -> CanContinue
{
// set current taskloop
MIJIN_ASSERT(currentLoopStorage() == nullptr, "Trying to tick a loop from a coroutine, this is not supported.");
currentLoopStorage() = this;
threadId_ = std::this_thread::get_id();
// move over all tasks from newTasks
for (StoredTask& task : newTasks_)
{
tasks_.push_back(std::move(task));
}
newTasks_.clear();
// also pick up tasks from other threads
while(true)
{
std::optional<StoredTask> task = queuedTasks_.tryPop();
if (!task.has_value()) {
break;
}
tasks_.push_back(std::move(*task));
}
// remove any tasks that are finished executing
auto it = std::remove_if(tasks_.begin(), tasks_.end(), [](StoredTask& task) {
return task.task->status() == TaskStatus::FINISHED;
});
tasks_.erase(it, tasks_.end());
CanContinue canContinue = CanContinue::NO;
// then execute all tasks that can be executed
for (currentTask_ = tasks_.begin(); currentTask_ != tasks_.end(); ++currentTask_)
{
StoredTask& task = *currentTask_;
TaskStatus status = task.task->status();
if (status != TaskStatus::SUSPENDED && status != TaskStatus::YIELDED)
{
MIJIN_ASSERT(status == TaskStatus::WAITING, "Task with invalid status in task list!");
continue;
}
status = tickTask(task);
if (status == TaskStatus::SUSPENDED || status == TaskStatus::YIELDED)
{
canContinue = CanContinue::YES;
}
}
// reset current loop
currentLoopStorage() = nullptr;
// remove any tasks that have been transferred to another queue
it = std::remove_if(tasks_.begin(), tasks_.end(), [](const StoredTask& task) {
return task.task == nullptr;
});
tasks_.erase(it, tasks_.end());
return canContinue;
}
inline void SimpleTaskLoop::runUntilDone(IgnoreWaiting ignoreWaiting) noexcept
{
while (!tasks_.empty() || !newTasks_.empty())
{
const CanContinue canContinue = tick();
if (ignoreWaiting && !canContinue)
{
break;
}
}
}
// utility stuff
inline std::suspend_always c_suspend() {
return std::suspend_always();
}
template<template<typename...> typename TCollection, typename TType, typename... TTemplateArgs>
Task<> c_allDone(const TCollection<FuturePtr<TType>, TTemplateArgs...>& futures)
{
bool allDone = true;
do
{
allDone = true;
for (const FuturePtr<TType>& future : futures)
{
if (future && !future->ready()) {
allDone = false;
break;
}
}
co_await c_suspend();
} while (!allDone);
}
#if MIJIN_COROUTINE_ENABLE_DEBUG_INFO
#endif
}
#endif // MIJIN_ASYNC_COROUTINE_HPP_INCLUDED

View File

@@ -0,0 +1,140 @@
#pragma once
#if !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)
#define MIJIN_ASYNC_FUTURE_HPP_INCLUDED 1
#include <optional>
#include <memory>
#include <type_traits>
#include "./signal.hpp"
#include "../debug/assert.hpp"
#include "../container/optional.hpp"
namespace mijin
{
//
// public defines
//
//
// public constants
//
//
// public types
//
template<typename TValue>
class Future;
// TODO: add support for mutexes and waiting for futures
namespace impl
{
template<typename TValue>
struct FutureStorage
{
std::optional<TValue> value;
void setValue(TValue value_) noexcept { value = std::move(value_); }
[[nodiscard]] TValue& getValue() noexcept { return value.value(); }
};
template<typename TValue>
struct FutureStorage<TValue&>
{
std::optional<TValue*> value;
void setValue(TValue& value_) noexcept { value = &value_; }
[[nodiscard]] TValue& getValue() const noexcept { return *value.value(); }
};
template<>
struct FutureStorage<void>
{
};
} // namespace impl
template<typename TValue>
class Future
{
private:
impl::FutureStorage<TValue> value_;
bool isSet_ = false;
public:
Future() = default;
Future(const Future&) = delete;
Future(Future&&) noexcept = default;
public:
Future& operator=(const Future&) = delete;
Future& operator=(Future&&) noexcept = default;
[[nodiscard]]
constexpr explicit operator bool() const noexcept { return ready(); }
[[nodiscard]]
constexpr bool operator!() const noexcept { return !ready(); }
public: // access
[[nodiscard]]
constexpr decltype(auto) get() noexcept
{
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
if constexpr(std::is_same_v<TValue, void>) {
return;
}
else {
return value_.getValue();
}
}
[[nodiscard]]
constexpr decltype(auto) get() const noexcept
{
MIJIN_ASSERT(isSet_, "Attempting to get from future that is not ready.");
if constexpr(std::is_same_v<TValue, void>) {
return;
}
else {
return value_.getValue();
}
}
[[nodiscard]]
constexpr bool ready() const noexcept
{
return isSet_;
}
public: // modification
template<typename TArg> requires (!std::is_same_v<TValue, void>)
constexpr void set(TArg&& value) noexcept
{
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
value_.setValue(std::move(value));
isSet_ = true;
sigSet.emit();
}
constexpr void set() noexcept
{
MIJIN_ASSERT(!isSet_, "Trying to set a future twice!");
isSet_ = true;
if constexpr (std::is_same_v<TValue, void>) {
sigSet.emit();
}
else {
// would love to make this a compile-time error :/
MIJIN_ERROR("Attempting to call set(void) on future with value.");
}
}
public: // signals
Signal<> sigSet;
};
template<typename TValue>
using FuturePtr = std::shared_ptr<Future<TValue>>;
//
// public functions
//
} // namespace mijin
#endif // !defined(MIJIN_ASYNC_FUTURE_HPP_INCLUDED)

View File

@@ -0,0 +1,132 @@
#pragma once
#if !defined(MIJIN_MESSAGE_QUEUE_HPP_INCLUDED)
#define MIJIN_MESSAGE_QUEUE_HPP_INCLUDED 1
#include <array>
#include <atomic>
#include <optional>
#include <thread>
#include "../util/bitarray.hpp"
namespace mijin
{
//
// public defines
//
//
// public constants
//
//
// public types
//
template<typename TMessage, std::size_t bufferSize = 32>
class MessageQueue
{
private:
std::array<TMessage, bufferSize> messages;
mijin::BitArray<bufferSize, true> messageReady;
std::atomic_uint writePos = 0;
std::atomic_uint readPos = 0;
public:
MessageQueue() = default;
MessageQueue(const MessageQueue&) = delete;
MessageQueue(MessageQueue&&) noexcept = delete;
MessageQueue& operator=(const MessageQueue&) = delete;
MessageQueue& operator=(MessageQueue&&) noexcept = delete;
[[nodiscard]] bool tryPushMaybeMove(TMessage& message);
[[nodiscard]] bool tryPush(TMessage message) {
return tryPushMaybeMove(message);
}
void push(TMessage message);
[[nodiscard]] std::optional<TMessage> tryPop();
[[nodiscard]] TMessage wait();
};
template<typename TRequest, typename TResponse, std::size_t requestBufferSize = 32, std::size_t responseBufferSize = 32>
struct TaskMessageQueue
{
MessageQueue<TRequest, requestBufferSize> requests;
MessageQueue<TResponse, responseBufferSize> responses;
};
//
// public functions
//
template<typename TMessage, std::size_t bufferSize>
bool MessageQueue<TMessage, bufferSize>::tryPushMaybeMove(TMessage& message)
{
unsigned oldWritePos = writePos.load(std::memory_order_relaxed);
unsigned newWritePos = 0;
do
{
newWritePos = (oldWritePos + 1) % bufferSize;
if (newWritePos == readPos) {
return false;
}
} while (!writePos.compare_exchange_weak(oldWritePos, newWritePos, std::memory_order_release, std::memory_order_relaxed));
while (messageReady.get(oldWritePos)) {
std::this_thread::yield(); // someone is still reading, wait...
}
messages[oldWritePos] = std::move(message);
messageReady.set(oldWritePos, true);
return true;
}
template<typename TMessage, std::size_t bufferSize>
void MessageQueue<TMessage, bufferSize>::push(TMessage message)
{
while (!tryPushMaybeMove(message)) {
std::this_thread::yield();
}
}
template<typename TMessage, std::size_t bufferSize>
std::optional<TMessage> MessageQueue<TMessage, bufferSize>::tryPop()
{
unsigned oldReadPos = readPos.load(std::memory_order_relaxed);
unsigned newReadPos = 0;
do
{
if (oldReadPos == writePos) {
return std::nullopt;
}
newReadPos = (oldReadPos + 1) % bufferSize;
} while (!readPos.compare_exchange_weak(oldReadPos, newReadPos, std::memory_order_release, std::memory_order_relaxed));
while (!messageReady.get(oldReadPos)) {
std::this_thread::yield(); // no harm in busy-waiting here, should be fast
};
TMessage message = std::move(messages[oldReadPos]);
messageReady.set(oldReadPos, false);
return message;
}
template<typename TMessage, std::size_t bufferSize>
TMessage MessageQueue<TMessage, bufferSize>::wait()
{
while (true)
{
std::optional<TMessage> message = tryPop();
if (message.has_value()) {
return message.value();
}
std::this_thread::yield();
}
}
} // namespace mijin
#endif // !defined(MIJIN_MESSAGE_QUEUE_HPP_INCLUDED)

View File

@@ -0,0 +1,126 @@
#pragma once
#if !defined(MIJIN_ASYNC_SIGNAL_HPP_INCLUDED)
#define MIJIN_ASYNC_SIGNAL_HPP_INCLUDED 1
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <vector>
#include "../util/flag.hpp"
namespace mijin
{
//
// public defines
//
//
// public constants
//
//
// public types
//
MIJIN_DEFINE_FLAG(Oneshot);
template<typename... TArgs>
class Signal
{
public:
using handler_t = std::function<void(TArgs...)>;
using token_t = std::uint32_t;
private:
struct RegisteredHandler
{
handler_t callable;
std::weak_ptr<void> referenced;
token_t token;
Oneshot oneshot = Oneshot::NO;
};
using handler_vector_t = std::vector<RegisteredHandler>;
private:
handler_vector_t handlers_;
token_t nextToken = 1;
std::mutex handlersMutex_;
public:
Signal() = default;
Signal(const Signal&) = delete;
Signal(Signal&&) noexcept = default;
public:
Signal& operator=(const Signal&) = delete;
Signal& operator=(Signal&&) noexcept = default;
public:
template<typename THandler, typename TWeak = void>
inline token_t connect(THandler handler, Oneshot oneshot = Oneshot::NO, std::weak_ptr<TWeak> referenced = std::weak_ptr<TWeak>()) noexcept;
inline void disconnect(token_t token) noexcept;
inline void emit(TArgs&&... args) noexcept;
};
//
// public functions
//
template<typename... TArgs>
template<typename THandler, typename TWeak>
inline auto Signal<TArgs...>::connect(THandler handler, Oneshot oneshot, std::weak_ptr<TWeak> referenced) noexcept -> token_t
{
std::lock_guard lock(handlersMutex_);
auto callable = handler_t(handler);
handlers_.push_back({
.callable = std::move(callable),
.referenced = std::move(referenced),
.token = nextToken,
.oneshot = oneshot
});
return nextToken++;
}
template<typename... TArgs>
inline void Signal<TArgs...>::disconnect(token_t token) noexcept
{
std::lock_guard lock(handlersMutex_);
auto it = std::remove_if(handlers_.begin(), handlers_.end(), [token](const RegisteredHandler& handler)
{
return handler.token == token;
});
handlers_.erase(it, handlers_.end());
}
template<typename... TArgs>
inline void Signal<TArgs...>::emit(TArgs&&... args) noexcept
{
std::lock_guard lock(handlersMutex_);
// first erase any handlers with expired references
// auto it = std::remove_if(handlers_.begin(), handlers_.end(), [](const RegisteredHandler& handler)
// {
// return handler.referenced.expired();
// });
// handlers_.erase(it, handlers_.end());
// TODO: this doesn't really work since expired() also returns true if the pointer was never set
// invoke all handlers
for (RegisteredHandler& handler : handlers_)
{
handler.callable(forward<TArgs>(args)...);
}
// remove any oneshot
auto it = std::remove_if(handlers_.begin(), handlers_.end(), [](const RegisteredHandler& handler)
{
return handler.oneshot;
});
handlers_.erase(it, handlers_.end());
}
} // namespace mijin
#endif // !defined(MIJIN_ASYNC_SIGNAL_HPP_INCLUDED)