-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
CoRoutine.hpp
486 lines (418 loc) · 16 KB
/
CoRoutine.hpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
MIT License
DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
Copyright 2022, 2023 Chris M. (RealTimeChris)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/// CoRoutine.hpp - Header for the coroutine class.
/// Oct 23, 2021
/// https://discordcoreapi.com
/// \file CoRoutine.hpp
#pragma once
#include <discordcoreapi/FoundationEntities.hpp>
#include <discordcoreapi/Utilities/CoRoutineThreadPool.hpp>
#include <discordcoreapi/Utilities/HttpsClient.hpp>
namespace discord_core_api {
/**
* \addtogroup utilities
* @{
*/
/// @brief The current status of the associated co_routine.
enum class co_routine_status {
idle = 0,///< Idle.
running = 1,///< Running.
complete = 2,///< complete.
cancelled = 3///< cancelled.
};
/// @brief An error type for co_routines.
struct co_routine_error : public dca_exception {
DCA_INLINE co_routine_error(const jsonifier::string_view& message, std::source_location location = std::source_location::current()) : dca_exception{ message, location } {};
};
template<typename value_type> class result_holder {
public:
template<typename value_type_new> DCA_INLINE void setResult(value_type_new&& newResult) {
result = makeUnique<value_type>(std::forward<value_type_new>(newResult));
sentYet.store(true, std::memory_order_release);
}
DCA_INLINE value_type getResult() {
if (sentYet.load(std::memory_order_acquire) && result) {
sentYet.store(false, std::memory_order_release);
return std::move(*result);
} else {
return {};
}
}
DCA_INLINE bool checkForResult() {
return sentYet.load(std::memory_order_acquire);
}
protected:
unique_ptr<value_type> result{};
std::atomic_bool sentYet{};
};
/// @brief A co_routine - representing a potentially asynchronous operation/function.
/// \tparam return_type the type of parameter that is returned by the co_routine.
template<typename return_type_new, bool timeOut> class co_routine {
public:
using return_type = return_type_new;///< The return type of this co_routine.
class promise_type {
public:
template<typename return_type02, bool timeOut02> friend class co_routine;
DCA_INLINE void requestStop() {
areWeStoppedBool.store(true, std::memory_order_release);
}
DCA_INLINE bool stopRequested() {
return areWeStoppedBool.load(std::memory_order_acquire);
}
template<typename return_type_newer> DCA_INLINE void return_value(return_type_newer&& returnValue) {
if (resultBuffer) {
resultBuffer->setResult(std::forward<return_type_newer>(returnValue));
}
}
DCA_INLINE auto get_return_object() {
return co_routine<return_type, timeOut>{ std::coroutine_handle<promise_type>::from_promise(*this) };
}
DCA_INLINE std::suspend_never initial_suspend() {
while (!resultBuffer) {
std::this_thread::sleep_for(1ms);
}
return {};
}
DCA_INLINE std::suspend_always final_suspend() noexcept {
return {};
}
DCA_INLINE void unhandled_exception() {
if (exceptionBuffer) {
exceptionBuffer->setResult(std::current_exception());
}
}
DCA_INLINE ~promise_type() {
exceptionBuffer = nullptr;
resultBuffer = nullptr;
}
protected:
result_holder<std::exception_ptr>* exceptionBuffer{};
result_holder<return_type>* resultBuffer{};
std::atomic_bool areWeStoppedBool{};
};
DCA_INLINE co_routine() = default;
DCA_INLINE co_routine& operator=(co_routine<return_type, timeOut>&& other) noexcept {
if (this != &other) {
coroutineHandle = other.coroutineHandle;
other.coroutineHandle = nullptr;
coroutineHandle.promise().exceptionBuffer = &exceptionBuffer;
coroutineHandle.promise().resultBuffer = &resultBuffer;
currentStatus.store(other.currentStatus.load(std::memory_order_acquire), std::memory_order_release);
other.currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
}
return *this;
}
DCA_INLINE co_routine(co_routine<return_type, timeOut>&& other) noexcept {
*this = std::move(other);
}
DCA_INLINE co_routine& operator=(const co_routine<return_type, timeOut>& other) = delete;
DCA_INLINE co_routine(const co_routine<return_type, timeOut>& other) = delete;
DCA_INLINE co_routine& operator=(std::coroutine_handle<promise_type> coroutineHandleNew) {
coroutineHandle = coroutineHandleNew;
coroutineHandle.promise().exceptionBuffer = &exceptionBuffer;
coroutineHandle.promise().resultBuffer = &resultBuffer;
return *this;
}
DCA_INLINE explicit co_routine(std::coroutine_handle<promise_type> coroutineHandleNew) {
*this = coroutineHandleNew;
};
DCA_INLINE ~co_routine() {
if (coroutineHandle) {
coroutineHandle.promise().exceptionBuffer = nullptr;
coroutineHandle.promise().resultBuffer = nullptr;
}
}
/// @brief Collects the status of the co_routine.
/// @return co_routine_status the status of the co_routine.
DCA_INLINE co_routine_status getStatus() {
if (!coroutineHandle) {
currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
} else if (coroutineHandle && !coroutineHandle.done()) {
currentStatus.store(co_routine_status::running, std::memory_order_release);
} else if (coroutineHandle && coroutineHandle.done()) {
currentStatus.store(co_routine_status::complete, std::memory_order_release);
}
return currentStatus.load(std::memory_order_acquire);
}
/// @brief Gets the resulting value of the co_routine.
/// @return the final value resulting from the co_routine's execution.
DCA_INLINE return_type get() {
if (coroutineHandle) {
if (!coroutineHandle.done()) {
stop_watch<milliseconds> stopWatch{ 15000 };
stopWatch.reset();
while (!resultBuffer.checkForResult()) {
checkForExceptions();
if constexpr (timeOut) {
if (stopWatch.hasTimeElapsed()) {
return resultBuffer.getResult();
}
}
std::this_thread::sleep_for(1ms);
}
}
checkForExceptions();
currentStatus.store(co_routine_status::complete, std::memory_order_release);
return resultBuffer.getResult();
} else {
throw co_routine_error{ "co_routine::get(), you called get() on a co_routine that is "
"not in a valid state." };
}
}
/// @brief Cancels the currently running co_routine, while blocking to wait for it to complete.
/// @return the final value resulting from the co_routine's execution.
DCA_INLINE return_type cancelAndWait() {
if (coroutineHandle) {
if (!coroutineHandle.done()) {
coroutineHandle.promise().requestStop();
stop_watch<milliseconds> stopWatch{ 15000 };
stopWatch.reset();
while (!resultBuffer.checkForResult()) {
checkForExceptions();
if constexpr (timeOut) {
if (stopWatch.hasTimeElapsed()) {
return resultBuffer.getResult();
}
}
std::this_thread::sleep_for(1ms);
}
}
checkForExceptions();
currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
return resultBuffer.getResult();
} else {
throw co_routine_error{ "co_routine::cancelAndWait(), you called get() on a co_routine that is "
"not in a valid state." };
}
}
/// @brief Cancels the currently executing co_routine and returns the current result.
/// @return the final value resulting from the co_routine's execution.
DCA_INLINE return_type cancel() {
if (coroutineHandle) {
if (!coroutineHandle.done()) {
coroutineHandle.promise().requestStop();
}
checkForExceptions();
currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
return resultBuffer.getResult();
} else {
throw co_routine_error{ "co_routine::cancel(), you called cancel() on a co_routine that is "
"not in a valid state." };
}
}
protected:
std::atomic<co_routine_status> currentStatus{ co_routine_status::idle };
std::coroutine_handle<promise_type> coroutineHandle{};
result_holder<std::exception_ptr> exceptionBuffer{};
result_holder<return_type> resultBuffer{};
DCA_INLINE void checkForExceptions() {
if (exceptionBuffer.checkForResult()) {
std::rethrow_exception(exceptionBuffer.getResult());
}
}
};
/// @brief A co_routine - representing a potentially asynchronous operation/function.
/// \tparam void the type of parameter that is returned by the co_routine.
template<jsonifier::concepts::void_t return_type_new, bool timeOut> class co_routine<return_type_new, timeOut> {
public:
using return_type = return_type_new;///< The return type of this co_routine.
class promise_type {
public:
template<typename return_type02, bool timeOut02> friend class co_routine;
DCA_INLINE void requestStop() {
areWeStoppedBool.store(true, std::memory_order_release);
}
DCA_INLINE bool stopRequested() {
return areWeStoppedBool.load(std::memory_order_acquire);
}
DCA_INLINE void return_void() {
return;
};
DCA_INLINE auto get_return_object() {
return co_routine<return_type, timeOut>{ std::coroutine_handle<promise_type>::from_promise(*this) };
}
DCA_INLINE std::suspend_never initial_suspend() {
while (!resultBuffer) {
std::this_thread::sleep_for(1ms);
}
return {};
}
DCA_INLINE std::suspend_always final_suspend() noexcept {
if (resultBuffer) {
resultBuffer->store(true);
}
return {};
}
DCA_INLINE void unhandled_exception() {
if (exceptionBuffer) {
exceptionBuffer->setResult(std::current_exception());
}
}
DCA_INLINE ~promise_type() {
exceptionBuffer = nullptr;
resultBuffer = nullptr;
}
protected:
result_holder<std::exception_ptr>* exceptionBuffer{};
std::atomic_bool areWeStoppedBool{};
std::atomic_bool* resultBuffer{};
};
DCA_INLINE co_routine() = default;
DCA_INLINE co_routine& operator=(co_routine<return_type, timeOut>&& other) noexcept {
if (this != &other) {
coroutineHandle = other.coroutineHandle;
other.coroutineHandle = nullptr;
coroutineHandle.promise().exceptionBuffer = &exceptionBuffer;
coroutineHandle.promise().resultBuffer = &resultBuffer;
currentStatus.store(other.currentStatus.load(std::memory_order_acquire), std::memory_order_release);
other.currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
}
return *this;
}
DCA_INLINE co_routine(co_routine<return_type, timeOut>&& other) noexcept {
*this = std::move(other);
}
DCA_INLINE co_routine& operator=(const co_routine<return_type, timeOut>& other) = delete;
DCA_INLINE co_routine(const co_routine<return_type, timeOut>& other) = delete;
DCA_INLINE co_routine& operator=(std::coroutine_handle<promise_type> coroutineHandleNew) {
coroutineHandle = coroutineHandleNew;
coroutineHandle.promise().exceptionBuffer = &exceptionBuffer;
coroutineHandle.promise().resultBuffer = &resultBuffer;
return *this;
}
DCA_INLINE explicit co_routine(std::coroutine_handle<promise_type> coroutineHandleNew) {
*this = coroutineHandleNew;
};
DCA_INLINE ~co_routine() {
if (coroutineHandle) {
coroutineHandle.promise().exceptionBuffer = nullptr;
coroutineHandle.promise().resultBuffer = nullptr;
}
}
/// @brief Collects the status of the co_routine.
/// @return co_routine_status the status of the co_routine.
DCA_INLINE co_routine_status getStatus() {
if (!coroutineHandle) {
currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
} else if (coroutineHandle && !coroutineHandle.done()) {
currentStatus.store(co_routine_status::running, std::memory_order_release);
} else if (coroutineHandle && coroutineHandle.done()) {
currentStatus.store(co_routine_status::complete, std::memory_order_release);
}
return currentStatus.load(std::memory_order_acquire);
}
/// @brief Gets the resulting value of the co_routine.
DCA_INLINE void get() {
if (coroutineHandle) {
if (!coroutineHandle.done()) {
stop_watch<milliseconds> stopWatch{ 15000 };
stopWatch.reset();
while (!resultBuffer.load()) {
checkForExceptions();
if constexpr (timeOut) {
if (stopWatch.hasTimeElapsed()) {
return;
}
}
std::this_thread::sleep_for(1ms);
}
}
checkForExceptions();
currentStatus.store(co_routine_status::complete, std::memory_order_release);
return;
} else {
throw co_routine_error{ "co_routine::get(), you called get() on a co_routine that is "
"not in a valid state." };
}
}
/// @brief Cancels the currently running co_routine, while blocking to wait for it to complete.
DCA_INLINE void cancelAndWait() {
if (coroutineHandle) {
if (!coroutineHandle.done()) {
coroutineHandle.promise().requestStop();
stop_watch<milliseconds> stopWatch{ 15000 };
stopWatch.reset();
while (!resultBuffer.load()) {
checkForExceptions();
if constexpr (timeOut) {
if (stopWatch.hasTimeElapsed()) {
return;
}
}
std::this_thread::sleep_for(1ms);
}
}
checkForExceptions();
currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
return;
} else {
throw co_routine_error{ "co_routine::cancelAndWait(), you called get() on a co_routine that is "
"not in a valid state." };
}
}
/// @brief Cancels the currently executing co_routine and returns the current result.
DCA_INLINE void cancel() {
if (coroutineHandle) {
if (!coroutineHandle.done()) {
coroutineHandle.promise().requestStop();
}
checkForExceptions();
currentStatus.store(co_routine_status::cancelled, std::memory_order_release);
return;
} else {
throw co_routine_error{ "co_routine::cancel(), you called cancel() on a co_routine that is "
"not in a valid state." };
}
}
protected:
std::atomic<co_routine_status> currentStatus{ co_routine_status::idle };
std::coroutine_handle<promise_type> coroutineHandle{};
result_holder<std::exception_ptr> exceptionBuffer{};
std::atomic_bool resultBuffer{};
DCA_INLINE void checkForExceptions() {
if (exceptionBuffer.checkForResult()) {
std::rethrow_exception(exceptionBuffer.getResult());
}
}
};
class new_thread_awaiter_base {
public:
DCA_INLINE static discord_core_internal::co_routine_thread_pool threadPool{};
};
/// @brief An awaitable that can be used to launch the co_routine onto a new thread - as well as return the handle for stoppping its execution.
/// \tparam return_type the type of value returned by the containing co_routine.
template<typename return_type, bool timeOut> class new_thread_awaiter : public new_thread_awaiter_base {
public:
DCA_INLINE bool await_ready() const {
return false;
}
DCA_INLINE void await_suspend(std::coroutine_handle<typename co_routine<return_type, timeOut>::promise_type> coroHandleNew) {
new_thread_awaiter_base::threadPool.submitTask(coroHandleNew);
coroHandle = coroHandleNew;
}
DCA_INLINE auto await_resume() {
return coroHandle;
}
protected:
std::coroutine_handle<typename co_routine<return_type, timeOut>::promise_type> coroHandle{};
};
/**@}*/
};