| 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 | |
| 9 | namespace application::schedule |
| 10 | { |
| 11 | std::atomic_bool Awaitable::active = false; |
| 12 | |
| 13 | Awaitable::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 | |
| 22 | Awaitable::~Awaitable() |
| 23 | { |
| 24 | active.store(i: false); |
| 25 | if (handle) |
| 26 | { |
| 27 | handle.destroy(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void Awaitable::resume() const |
| 32 | { |
| 33 | if (handle) |
| 34 | { |
| 35 | handle.resume(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | bool 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 |
| 46 | void enterNextPhase(Awaitable& awaitable) |
| 47 | { |
| 48 | if (!awaitable.done()) |
| 49 | { |
| 50 | awaitable.resume(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | bool NativeManager::empty() const |
| 55 | { |
| 56 | return nativeCategories.none(); |
| 57 | } |
| 58 | |
| 59 | void NativeManager::reset() |
| 60 | { |
| 61 | nativeCategories.reset(); |
| 62 | } |
| 63 | |
| 64 | bool ExtraManager::empty() const |
| 65 | { |
| 66 | return !extraHelping |
| 67 | && std::ranges::none_of(extraChecklist, [](const auto& pair) { return pair.second.present(); }); |
| 68 | } |
| 69 | |
| 70 | void ExtraManager::reset() |
| 71 | { |
| 72 | extraHelping = false; |
| 73 | std::ranges::for_each(extraChecklist, [](const auto& pair) { pair.second.clear(); }); |
| 74 | } |
| 75 | |
| 76 | bool TaskScheduler::empty() const |
| 77 | { |
| 78 | return NativeManager::empty() && ExtraManager::empty(); |
| 79 | } |
| 80 | |
| 81 | void TaskScheduler::reset() |
| 82 | { |
| 83 | NativeManager::reset(); |
| 84 | ExtraManager::reset(); |
| 85 | } |
| 86 | } // namespace application::schedule |
| 87 |