52 : state_(std::move(other.state_))
53 , scheduler_(other.scheduler_) {
54 other.scheduler_ =
nullptr;
60 if (state_ && scheduler_ && !state_->retrieved) {
63 state_ = std::move(other.state_);
64 scheduler_ = other.scheduler_;
65 other.scheduler_ =
nullptr;
72 if (state_ && scheduler_ && !state_->retrieved) {
86 throw std::logic_error(
"Future has no state");
99 throw std::logic_error(
"Future has no state");
101 if (state_->retrieved) {
102 throw std::logic_error(
"Future result already retrieved");
106 state_->retrieved =
true;
109 if (state_->exception) {
110 std::rethrow_exception(state_->exception);
113 if (!state_->result.has_value()) {
114 throw std::logic_error(
"Fiber completed without result");
117 return std::move(*state_->result);
124 if (!state_ || !scheduler_) {
127 auto* fiber = scheduler_->GetFiber(state_->fiber_id);
128 return !fiber || fiber->
IsDone();
132 template <
typename U>
135 template <
typename U>
138 template <
typename F>
141 template <
typename F>
145 : state_(std::move(state))
146 , scheduler_(scheduler) {}
152 auto* fiber = scheduler_->GetFiber(state_->fiber_id);
153 if (!fiber || fiber->IsDone()) {
159 auto* current = scheduler_->GetCurrentFiber();
162 scheduler_->SuspendCurrent();
167 std::shared_ptr<detail::FutureState<T>> state_;
168 Scheduler* scheduler_ {
nullptr};
181 : state_(std::move(other.state_))
182 , scheduler_(other.scheduler_) {
183 other.scheduler_ =
nullptr;
187 if (
this != &other) {
188 if (state_ && scheduler_ && !state_->retrieved) {
191 state_ = std::move(other.state_);
192 scheduler_ = other.scheduler_;
193 other.scheduler_ =
nullptr;
199 if (state_ && scheduler_ && !state_->retrieved) {
215 throw std::logic_error(
"Future has no state");
218 state_->retrieved =
true;
221 if (state_->exception) {
222 std::rethrow_exception(state_->exception);
230 if (!state_ || !scheduler_) {
233 auto* fiber = scheduler_->GetFiber(state_->fiber_id);
234 return !fiber || fiber->
IsDone();
238 template <
typename U>
241 template <
typename U>
244 template <
typename F>
247 template <
typename F>
251 : state_(std::move(state))
252 , scheduler_(scheduler) {}
258 auto* fiber = scheduler_->GetFiber(state_->fiber_id);
259 if (!fiber || fiber->IsDone()) {
264 auto* current = scheduler_->GetCurrentFiber();
267 scheduler_->SuspendCurrent();
272 std::shared_ptr<detail::FutureState<void>> state_;
273 Scheduler* scheduler_ {
nullptr};
285 using ResultType = std::invoke_result_t<F>;
288 auto state = std::make_shared<detail::FutureState<ResultType>>();
290 if constexpr (std::is_void_v<ResultType>) {
291 auto fiber_id = scheduler.SpawnFiberInternal(
292 [state, f = std::forward<F>(func)]()
mutable {
297 state->exception = std::current_exception();
302 state->fiber_id = fiber_id;
304 auto fiber_id = scheduler.SpawnFiberInternal(
305 [state, f = std::forward<F>(func)]()
mutable {
310 state->exception = std::current_exception();
315 state->fiber_id = fiber_id;
bool IsDone() const noexcept
Checks if the coroutine has finished its execution.
Definition base_coroutine.hpp:37
Future(const Future &)=delete
~Future()
Definition future.hpp:198
bool IsReady() const noexcept
Check if the fiber has completed.
Definition future.hpp:229
Future(Future &&other) noexcept
Definition future.hpp:180
friend Future< U > Spawn(std::function< U()> func)
Future & operator=(const Future &)=delete
void Wait()
Block current fiber until this fiber completes.
Definition future.hpp:213
friend Future< U > Spawn(std::function< U()> func, std::size_t stack_size)
Future & operator=(Future &&other) noexcept
Definition future.hpp:186
Handle to a spawned fiber that returns a value.
Definition future.hpp:46
~Future()
Definition future.hpp:70
T Get()
Block until fiber completes and return the result.
Definition future.hpp:97
Future & operator=(Future &&other) noexcept
Definition future.hpp:57
friend Future< U > Spawn(std::function< U()> func)
Future & operator=(const Future &)=delete
bool IsReady() const noexcept
Check if the fiber has completed.
Definition future.hpp:123
Future(const Future &)=delete
friend Future< U > Spawn(std::function< U()> func, std::size_t stack_size)
void Wait()
Block current fiber until this fiber completes.
Definition future.hpp:84
Future(Future &&other) noexcept
Definition future.hpp:51
Manages cooperative execution of fibers.
Definition scheduler.hpp:28
static Scheduler & Current()
Get the current scheduler.
void AddWaiter(Fiber *waiter)
std::uint64_t Id
Definition fiber.hpp:34
Cooperative multitasking primitives built on cortex::Coroutine.
Definition condition_variable.hpp:13
auto Spawn(F &&func, std::size_t stack_size) -> Future< std::invoke_result_t< F > >
Spawn a new fiber with custom stack size.
Definition future.hpp:284
Cooperative fiber scheduler for tiny_fiber.
std::exception_ptr exception
Definition future.hpp:33
Fiber::Id fiber_id
Definition future.hpp:24
std::optional< T > result
Definition future.hpp:25
std::exception_ptr exception
Definition future.hpp:26
bool retrieved
Definition future.hpp:27