AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
Coroutine.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include <coroutine>
11#include "../core/AnyData.h"
12#include "../core/function.hpp"
13
14namespace AsyncFw {
15struct CoroutineAwait;
16
18struct CoroutineTask {
19 CoroutineTask();
20 virtual ~CoroutineTask();
22 struct promise_type : public AnyData {
23 friend CoroutineAwait;
24 friend CoroutineTask;
25 struct Private;
26 promise_type();
27 ~promise_type();
28 CoroutineTask get_return_object();
29 std::suspend_never initial_suspend() noexcept;
30 std::suspend_always final_suspend() noexcept;
31 void unhandled_exception() {}
32 void return_void();
34 void resume_queued();
35
36 private:
37 Private *private_;
38 };
39
40 bool finished();
42 void wait();
43
44private:
45 promise_type *promise;
46};
47
48using CoroutineHandle = std::coroutine_handle<CoroutineTask::promise_type>;
49
51struct CoroutineAwait {
52 template <typename T>
53 CoroutineAwait(T _f) : f_(new FunctionArgs<const CoroutineHandle>::Function<T>(_f)) {}
54 CoroutineAwait() = default;
55 CoroutineAwait(const CoroutineAwait &) = delete;
56 CoroutineAwait &operator=(const CoroutineAwait &) = delete;
57 virtual ~CoroutineAwait();
58 virtual void await_suspend(CoroutineHandle) const noexcept;
59 virtual bool await_ready() const noexcept;
60 virtual CoroutineHandle await_resume() const noexcept;
61
62private:
63 mutable CoroutineHandle h_;
64 AbstractFunction<const CoroutineHandle> *f_ = nullptr;
65};
66} // namespace AsyncFw
The CoroutineAwait struct.
Definition Coroutine.h:51
The CoroutineTask::promise_type struct.
Definition Coroutine.h:22
void resume_queued()
Execute coroutine_handle::resume on the thread where the promise_type is created.
Definition Coroutine.cpp:87
bool finished()
Return true if task finished.
Definition Coroutine.cpp:55
void wait()
Runs nested Thread::exec() and wait for task finished.
Definition Coroutine.cpp:45