-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.h
More file actions
190 lines (176 loc) · 7.33 KB
/
task.h
File metadata and controls
190 lines (176 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
/*
task.h - Implementation of a C++20 async coroutines task.
Copyright (c) 2023 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <atomic>
#include <memory>
#include <coroutine>
namespace ghostl
{
template<class T = void>
struct task
{
struct promise_type final
{
auto get_return_object() noexcept
{
return task(std::coroutine_handle<promise_type>::from_promise(*this));
}
constexpr std::suspend_always initial_suspend() const { return {}; }
struct final_awaiter final
{
constexpr bool await_ready() const noexcept { return false; }
constexpr void await_resume() const noexcept {}
std::coroutine_handle<>
await_suspend(std::coroutine_handle<promise_type> h) const noexcept
{
// final_awaiter::await_suspend is called when the execution of the
// current coroutine (referred to by 'h') is about to finish.
// If the current coroutine was resumed by another coroutine via
// co_await get_task(), a handle to that coroutine has been stored
// as h.promise().previous. In that case, return the handle to resume
// the previous coroutine.
// Otherwise, return noop_coroutine(), whose resumption does nothing.
if (auto previous = h.promise().previous; previous)
return previous;
else
return std::noop_coroutine();
}
};
final_awaiter final_suspend() const noexcept { return {}; }
void unhandled_exception() const { std::rethrow_exception(std::current_exception()); }
void return_value(T value) { result = std::move(value); }
std::coroutine_handle<> previous;
T result{};
};
task() noexcept : coroutine(nullptr) { }
explicit task(std::coroutine_handle<promise_type> h) : coroutine(h) { }
task(const task&) = delete;
task(task&& other) noexcept : coroutine(std::exchange(other.coroutine, nullptr)) { }
~task() { if (coroutine) coroutine.destroy(); }
task& operator=(const task&) = delete;
task& operator=(task&& other) noexcept
{
if (std::addressof(other) != this)
{
if (coroutine) coroutine.destroy();
coroutine = std::exchange(other.coroutine, nullptr);
}
return *this;
}
struct awaiter final
{
awaiter() = delete;
explicit awaiter(std::coroutine_handle<promise_type> h) : coroutine(h) { }
constexpr bool await_ready() const { return false; }
T await_resume() { return std::move(coroutine.promise().result); }
auto await_suspend(std::coroutine_handle<> h)
{
coroutine.promise().previous = h;
return coroutine;
}
private:
std::coroutine_handle<promise_type> coroutine;
};
awaiter operator co_await() { return awaiter{ coroutine }; }
T resume()
{
coroutine.resume();
return std::move(coroutine.promise().result);
}
T operator()()
{
return resume();
}
private:
std::coroutine_handle<promise_type> coroutine;
};
template<>
struct task<void>
{
struct promise_type final
{
auto get_return_object() noexcept
{
return task(std::coroutine_handle<promise_type>::from_promise(*this));
}
constexpr std::suspend_always initial_suspend() const { return {}; }
struct final_awaiter final
{
constexpr bool await_ready() const noexcept { return false; }
constexpr void await_resume() const noexcept {}
std::coroutine_handle<>
await_suspend(std::coroutine_handle<promise_type> h) const noexcept
{
// final_awaiter::await_suspend is called when the execution of the
// current coroutine (referred to by 'h') is about to finish.
// If the current coroutine was resumed by another coroutine via
// co_await get_task(), a handle to that coroutine has been stored
// as h.promise().previous. In that case, return the handle to resume
// the previous coroutine.
// Otherwise, return noop_coroutine(), whose resumption does nothing.
if (auto previous = h.promise().previous; previous)
return previous;
else
return std::noop_coroutine();
}
};
final_awaiter final_suspend() const noexcept { return {}; }
void unhandled_exception() const { std::rethrow_exception(std::current_exception()); }
constexpr void return_void() const { }
std::coroutine_handle<> previous;
};
task() noexcept : coroutine(nullptr) { }
explicit task(std::coroutine_handle<promise_type> h) : coroutine(h) { }
task(const task&) = delete;
task(task&& other) noexcept : coroutine(std::exchange(other.coroutine, nullptr)) { }
~task() { if (coroutine) coroutine.destroy(); }
task& operator=(const task&) = delete;
task& operator=(task&& other) noexcept
{
if (std::addressof(other) != this)
{
if (coroutine) coroutine.destroy();
coroutine = std::exchange(other.coroutine, nullptr);
}
return *this;
}
struct awaiter final
{
awaiter() = delete;
explicit awaiter(std::coroutine_handle<promise_type> h) : coroutine(h) { }
constexpr bool await_ready() const { return false; }
constexpr void await_resume() const noexcept {}
auto await_suspend(std::coroutine_handle<> h)
{
coroutine.promise().previous = h;
return coroutine;
}
private:
std::coroutine_handle<promise_type> coroutine;
};
awaiter operator co_await() { return awaiter{ coroutine }; }
void resume()
{
coroutine.resume();
}
void operator()()
{
resume();
}
private:
std::coroutine_handle<promise_type> coroutine;
};
} // namespace ghostl