1//! @file schedule.cpp
2//! @author ryftchen
3//! @brief The definitions (schedule) in the application module.
4//! @version 0.1.0
5//! @copyright Copyright (c) 2022-2026 ryftchen. All rights reserved.
6
7#include "schedule.hpp"
8
9namespace application::schedule
10{
11std::atomic_bool Awaitable::active = false;
12
13Awaitable::Awaitable(const std::coroutine_handle<promise_type>& handle) : handle{handle}
14{
15 if (active.load())
16 {
17 throw std::runtime_error{"There can only be one awaitable instance active at any given time."};
18 }
19 active.store(i: true);
20}
21
22Awaitable::~Awaitable()
23{
24 active.store(i: false);
25 if (handle)
26 {
27 handle.destroy();
28 }
29}
30
31void Awaitable::resume() const
32{
33 if (handle)
34 {
35 handle.resume();
36 }
37}
38
39bool Awaitable::done() const
40{
41 return !handle || handle.done();
42}
43
44//! @brief Enter the next phase of the coroutine.
45//! @param awaitable - awaitable instance
46void enterNextPhase(Awaitable& awaitable)
47{
48 if (!awaitable.done())
49 {
50 awaitable.resume();
51 }
52}
53
54bool NativeManager::empty() const
55{
56 return nativeCategories.none();
57}
58
59void NativeManager::reset()
60{
61 nativeCategories.reset();
62}
63
64bool ExtraManager::empty() const
65{
66 return !extraHelping
67 && std::ranges::none_of(extraChecklist, [](const auto& pair) { return pair.second.present(); });
68}
69
70void ExtraManager::reset()
71{
72 extraHelping = false;
73 std::ranges::for_each(extraChecklist, [](const auto& pair) { pair.second.clear(); });
74}
75
76bool TaskScheduler::empty() const
77{
78 return NativeManager::empty() && ExtraManager::empty();
79}
80
81void TaskScheduler::reset()
82{
83 NativeManager::reset();
84 ExtraManager::reset();
85}
86} // namespace application::schedule
87