Commit 3f3c9f45 authored by Xavier Thompson's avatar Xavier Thompson

Rename parent.hpp into span.hpp

parent c0202255
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
#include <cstdint> #include <cstdint>
#include <typon/forked.hpp> #include <typon/forked.hpp>
#include <typon/parent.hpp>
#include <typon/result.hpp> #include <typon/result.hpp>
#include <typon/scheduler.hpp> #include <typon/scheduler.hpp>
#include <typon/span.hpp>
namespace typon namespace typon
...@@ -17,7 +17,7 @@ namespace typon ...@@ -17,7 +17,7 @@ namespace typon
struct [[nodiscard]] Fork struct [[nodiscard]] Fork
{ {
struct promise_type; struct promise_type;
using u64 = Parent::u64; using u64 = Span::u64;
std::coroutine_handle<promise_type> _coroutine; std::coroutine_handle<promise_type> _coroutine;
...@@ -33,7 +33,7 @@ namespace typon ...@@ -33,7 +33,7 @@ namespace typon
struct promise_type : Result<T> struct promise_type : Result<T>
{ {
Parent * _parent; Span * _span;
ForkList::Node _node; ForkList::Node _node;
Fork get_return_object() noexcept Fork get_return_object() noexcept
...@@ -52,15 +52,15 @@ namespace typon ...@@ -52,15 +52,15 @@ namespace typon
{ {
std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept
{ {
auto parent = coroutine.promise()._parent; auto span = coroutine.promise()._span;
if (Scheduler::pop()) if (Scheduler::pop())
{ {
return parent->_coroutine; return span->_coroutine;
} }
u64 n = parent->_n.fetch_sub(1, std::memory_order_acq_rel); u64 n = span->_n.fetch_sub(1, std::memory_order_acq_rel);
if (n == 1) if (n == 1)
{ {
return parent->continuation(); return span->continuation();
} }
return std::noop_coroutine(); return std::noop_coroutine();
} }
...@@ -87,22 +87,22 @@ namespace typon ...@@ -87,22 +87,22 @@ namespace typon
template <typename Promise> template <typename Promise>
auto await_suspend(std::coroutine_handle<Promise> continuation) noexcept auto await_suspend(std::coroutine_handle<Promise> continuation) noexcept
{ {
Parent * parent = &(continuation.promise()._data); Span * span = &(continuation.promise()._span);
_coroutine.promise()._parent = parent; _coroutine.promise()._span = span;
_thefts = parent->_thefts; _thefts = span->_thefts;
std::coroutine_handle<> on_stack_handle = _coroutine; std::coroutine_handle<> on_stack_handle = _coroutine;
Scheduler::push(parent); Scheduler::push(span);
return on_stack_handle; return on_stack_handle;
} }
auto await_resume() auto await_resume()
{ {
auto parent = _coroutine.promise()._parent; auto span = _coroutine.promise()._span;
bool stolen = parent->_thefts > _thefts; bool stolen = span->_thefts > _thefts;
if (stolen) if (stolen)
{ {
parent->_children.insert(_coroutine); span->_children.insert(_coroutine);
} }
return Forked<T>(_coroutine, !stolen); return Forked<T>(_coroutine, !stolen);
} }
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
#include <coroutine> #include <coroutine>
#include <utility> #include <utility>
#include <typon/parent.hpp>
#include <typon/result.hpp> #include <typon/result.hpp>
#include <typon/span.hpp>
namespace typon namespace typon
...@@ -40,20 +40,20 @@ namespace typon ...@@ -40,20 +40,20 @@ namespace typon
{ {
if (_coroutine) if (_coroutine)
{ {
_coroutine.promise()._data._children.clear(); _coroutine.promise()._span._children.clear();
_coroutine.destroy(); _coroutine.destroy();
} }
} }
struct promise_type : Result<T> struct promise_type : Result<T>
{ {
using u64 = Parent::u64; using u64 = Span::u64;
static constexpr u64 UMAX = Parent::UMAX; static constexpr u64 UMAX = Span::UMAX;
Parent _data; Span _span;
promise_type() noexcept promise_type() noexcept
: _data(std::coroutine_handle<promise_type>::from_promise(*this)) : _span(std::coroutine_handle<promise_type>::from_promise(*this))
{} {}
Join get_return_object() noexcept Join get_return_object() noexcept
...@@ -76,13 +76,13 @@ namespace typon ...@@ -76,13 +76,13 @@ namespace typon
{ {
struct awaitable struct awaitable
{ {
Parent & _data; Span & _span;
bool await_ready() noexcept bool await_ready() noexcept
{ {
if (u64 thefts = _data._thefts) if (u64 thefts = _span._thefts)
{ {
u64 n = _data._n.load(std::memory_order_acquire); u64 n = _span._n.load(std::memory_order_acquire);
if (n - (UMAX - thefts) == 0) if (n - (UMAX - thefts) == 0)
{ {
return true; return true;
...@@ -94,8 +94,8 @@ namespace typon ...@@ -94,8 +94,8 @@ namespace typon
std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept
{ {
u64 thefts = _data._thefts; u64 thefts = _span._thefts;
u64 n = _data._n.fetch_sub(UMAX - thefts, std::memory_order_acq_rel); u64 n = _span._n.fetch_sub(UMAX - thefts, std::memory_order_acq_rel);
if (n - (UMAX - thefts) == 0) if (n - (UMAX - thefts) == 0)
{ {
return coroutine; return coroutine;
...@@ -105,13 +105,13 @@ namespace typon ...@@ -105,13 +105,13 @@ namespace typon
void await_resume() void await_resume()
{ {
_data._thefts = 0; _span._thefts = 0;
_data._n.store(UMAX, std::memory_order_release); _span._n.store(UMAX, std::memory_order_release);
_data._children.check_exceptions(); _span._children.check_exceptions();
} }
}; };
return awaitable { _data }; return awaitable { _span };
} }
auto final_suspend() noexcept auto final_suspend() noexcept
...@@ -120,16 +120,16 @@ namespace typon ...@@ -120,16 +120,16 @@ namespace typon
{ {
std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept
{ {
Parent & data = coroutine.promise()._data; Span & span = coroutine.promise()._span;
u64 thefts = data._thefts; u64 thefts = span._thefts;
if (thefts == 0) if (thefts == 0)
{ {
return data._continuation; return span._continuation;
} }
u64 n = data._n.fetch_sub(UMAX - thefts, std::memory_order_acq_rel); u64 n = span._n.fetch_sub(UMAX - thefts, std::memory_order_acq_rel);
if (n - (UMAX - thefts) == 0) if (n - (UMAX - thefts) == 0)
{ {
return data._continuation; return span._continuation;
} }
return std::noop_coroutine(); return std::noop_coroutine();
} }
...@@ -152,13 +152,13 @@ namespace typon ...@@ -152,13 +152,13 @@ namespace typon
std::coroutine_handle<> await_suspend(std::coroutine_handle<> continuation) noexcept std::coroutine_handle<> await_suspend(std::coroutine_handle<> continuation) noexcept
{ {
_coroutine.promise()._data._continuation = continuation; _coroutine.promise()._span._continuation = continuation;
return _coroutine; return _coroutine;
} }
decltype(auto) await_resume() decltype(auto) await_resume()
{ {
_coroutine.promise()._data._children.check_exceptions(); _coroutine.promise()._span._children.check_exceptions();
return _coroutine.promise().get(); return _coroutine.promise().get();
} }
}; };
......
#ifndef TYPON_PARENT_HPP_INCLUDED #ifndef TYPON_SPAN_HPP_INCLUDED
#define TYPON_PARENT_HPP_INCLUDED #define TYPON_SPAN_HPP_INCLUDED
#include <atomic> #include <atomic>
#include <concepts> #include <concepts>
...@@ -65,7 +65,7 @@ namespace typon ...@@ -65,7 +65,7 @@ namespace typon
}; };
struct Parent : Continuation struct Span : Continuation
{ {
using u64 = Continuation::u64; using u64 = Continuation::u64;
...@@ -77,7 +77,7 @@ namespace typon ...@@ -77,7 +77,7 @@ namespace typon
std::atomic<u64> _n = UMAX; std::atomic<u64> _n = UMAX;
Parent(std::coroutine_handle<> coroutine) noexcept Span(std::coroutine_handle<> coroutine) noexcept
: Continuation(coroutine) : Continuation(coroutine)
{} {}
...@@ -104,4 +104,4 @@ namespace typon ...@@ -104,4 +104,4 @@ namespace typon
} }
#endif // TYPON_PARENT_HPP_INCLUDED #endif // TYPON_SPAN_HPP_INCLUDED
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