Commit 2f66baef authored by Xavier Thompson's avatar Xavier Thompson

Refactor continuation.hpp

parent a45fcb61
......@@ -2,6 +2,7 @@
#define TYPON_CORE_CONTINUATION_HPP_INCLUDED
#include <atomic>
#include <concepts>
#include <coroutine>
#include <cstdint>
#include <exception>
......@@ -11,19 +12,23 @@
namespace typon
{
struct coroutine_node
struct ForkList
{
coroutine_node * _next;
struct Node
{
Node * _next;
std::coroutine_handle<> _coroutine;
std::exception_ptr * _exception;
};
struct coroutine_list
{
coroutine_node * _first = nullptr;
Node * _first = nullptr;
template <typename Promise>
requires requires (Promise p)
{
{ p._exception } -> std::same_as<std::exception_ptr&>;
{ p._node } -> std::same_as<Node&>;
}
void insert(std::coroutine_handle<Promise> coroutine) noexcept
{
std::exception_ptr * exception = &(coroutine.promise()._exception);
......@@ -57,32 +62,33 @@ namespace typon
};
struct continuation_data
struct Continuation
{
struct Data
{
using u64 = std::uint_fast64_t;
static constexpr u64 UMAX = std::numeric_limits<u64>::max();
std::coroutine_handle<> _coroutine;
std::coroutine_handle<> _continuation;
coroutine_list _children;
ForkList _children;
std::atomic<u64> _n = UMAX;
u64 _thefts = 0;
continuation_data(std::coroutine_handle<> coroutine)
Data(std::coroutine_handle<> coroutine) noexcept
: _coroutine(coroutine)
{}
};
Data * _data;
struct continuation_handle
{
continuation_data * _data;
continuation_handle() noexcept {}
Continuation() noexcept {}
template <typename Promise>
continuation_handle(std::coroutine_handle<Promise> coroutine) noexcept
Continuation(std::coroutine_handle<Promise> coroutine) noexcept
: _data(&(coroutine.promise()._data))
{}
......@@ -123,4 +129,5 @@ namespace typon
}
#endif // TYPON_CONTINUATION_HPP_INCLUDED
......@@ -17,7 +17,7 @@ namespace typon
struct [[nodiscard]] Fork
{
struct promise_type;
using u64 = continuation_data::u64;
using u64 = Continuation::Data::u64;
std::coroutine_handle<promise_type> _coroutine;
......@@ -32,8 +32,8 @@ namespace typon
struct promise_type : Result<T>
{
continuation_handle _continuation;
coroutine_node _node;
Continuation _continuation;
ForkList::Node _node;
Fork get_return_object() noexcept
{
......
......@@ -47,10 +47,10 @@ namespace typon
struct promise_type : Result<T>
{
using u64 = continuation_data::u64;
static constexpr u64 UMAX = continuation_data::UMAX;
using u64 = Continuation::Data::u64;
static constexpr u64 UMAX = Continuation::Data::UMAX;
continuation_data _data;
Continuation::Data _data;
promise_type() noexcept
: _data(std::coroutine_handle<promise_type>::from_promise(*this))
......@@ -76,7 +76,7 @@ namespace typon
{
struct awaitable
{
continuation_data & _data;
Continuation::Data & _data;
bool await_ready() noexcept
{
......@@ -119,7 +119,7 @@ namespace typon
{
std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept
{
continuation_data & data = coroutine.promise()._data;
Continuation::Data & data = coroutine.promise()._data;
u64 thefts = data._thefts;
u64 n = data._n.fetch_sub(UMAX - thefts, std::memory_order_acq_rel);
if (n - (UMAX - thefts) == 0)
......
......@@ -27,8 +27,7 @@ namespace typon
struct promise_type : Result<void>
{
// std::atomic_bool _done = false;
continuation_data _data;
Continuation::Data _data;
promise_type() noexcept
: _data(std::coroutine_handle<promise_type>::from_promise(*this))
......@@ -64,7 +63,7 @@ namespace typon
{
Scheduler::get();
Scheduler::schedule(_coroutine);
_coroutine.promise()._data._n.wait(continuation_data::UMAX, std::memory_order_acquire);
_coroutine.promise()._data._n.wait(Continuation::Data::UMAX, std::memory_order_acquire);
}
};
......
......@@ -21,7 +21,7 @@ namespace typon
struct Scheduler
{
using uint = unsigned int;
using Deque = fdt::lock_free::deque<continuation_handle>;
using Deque = fdt::lock_free::deque<Continuation>;
using Task = typename Deque::pop_type;
static inline thread_local uint thread_id;
......@@ -40,12 +40,12 @@ namespace typon
return scheduler;
}
static void push(continuation_handle task) noexcept
static void push(Continuation task) noexcept
{
get()._deque[thread_id].push(task);
}
static void schedule(continuation_handle task) noexcept
static void schedule(Continuation task) noexcept
{
Scheduler & scheduler = get();
scheduler._deque[thread_id].push(task);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment