|
| 1 | +export module CppUtils.Thread.Planner; |
| 2 | + |
| 3 | +import std; |
| 4 | +import CppUtils.Chrono.Concept; |
| 5 | +import CppUtils.Thread.UniqueLocker; |
| 6 | +import CppUtils.Thread.SharedLocker; |
| 7 | +import CppUtils.Thread.ThreadLoop; |
| 8 | + |
| 9 | +export namespace CppUtils::Thread |
| 10 | +{ |
| 11 | + class Planner final |
| 12 | + { |
| 13 | + using Clock = std::chrono::steady_clock; |
| 14 | + using TimePoint = Clock::time_point; |
| 15 | + using Task = std::function<void()>; |
| 16 | + |
| 17 | + struct Item final |
| 18 | + { |
| 19 | + TimePoint time; |
| 20 | + Task task; |
| 21 | + std::atomic_bool cancelled = false; |
| 22 | + }; |
| 23 | + |
| 24 | + struct Compare final |
| 25 | + { |
| 26 | + auto operator()(const std::shared_ptr<Item>& a, |
| 27 | + const std::shared_ptr<Item>& b) const noexcept -> bool |
| 28 | + { |
| 29 | + return a->time < b->time; |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + using Set = std::multiset<std::shared_ptr<Item>, Compare>; |
| 34 | + using Map = std::unordered_map<std::size_t, std::shared_ptr<Item>>; |
| 35 | + |
| 36 | + public: |
| 37 | + inline Planner(): |
| 38 | + m_loop{ |
| 39 | + [this] { runLoop(); }, |
| 40 | + [this] { m_condition.notify_all(); }} |
| 41 | + { |
| 42 | + m_loop.start(); |
| 43 | + } |
| 44 | + |
| 45 | + inline ~Planner() noexcept |
| 46 | + { |
| 47 | + cancelAll(); |
| 48 | + m_loop.requestStop(); |
| 49 | + } |
| 50 | + |
| 51 | + inline auto schedule(Task task, TimePoint when) -> std::size_t |
| 52 | + { |
| 53 | + auto id = m_nextId++; |
| 54 | + auto item = std::make_shared<Item>(when, std::move(task)); |
| 55 | + { |
| 56 | + auto mapAccessor = m_set.access(); |
| 57 | + mapAccessor->insert(item); |
| 58 | + } |
| 59 | + { |
| 60 | + auto mapAccessor = m_map.access(); |
| 61 | + mapAccessor->emplace(id, item); |
| 62 | + } |
| 63 | + m_condition.notify_all(); |
| 64 | + return id; |
| 65 | + } |
| 66 | + |
| 67 | + inline auto schedule(Task task, Chrono::Duration auto delay) -> std::size_t |
| 68 | + { |
| 69 | + return schedule(std::move(task), Clock::now() + delay); |
| 70 | + } |
| 71 | + |
| 72 | + inline auto waitUntilFinished() -> void |
| 73 | + { |
| 74 | + auto setAccessor = m_set.access(); |
| 75 | + m_condition.wait(setAccessor.getLockGuard(), [&setAccessor] { |
| 76 | + return std::empty(setAccessor.value()); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + inline auto cancel(std::size_t id) -> void |
| 81 | + { |
| 82 | + auto mapAccessor = m_map.access(); |
| 83 | + if (auto it = mapAccessor->find(id); it != std::end(mapAccessor.value())) |
| 84 | + { |
| 85 | + it->second->cancelled = true; |
| 86 | + mapAccessor->erase(it); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + inline auto cancelAll() -> void |
| 91 | + { |
| 92 | + auto mapAccessor = m_map.access(); |
| 93 | + for (auto& [_, item] : mapAccessor.value()) |
| 94 | + item->cancelled = true; |
| 95 | + mapAccessor->clear(); |
| 96 | + { |
| 97 | + auto setAccessor = m_set.access(); |
| 98 | + setAccessor->clear(); |
| 99 | + } |
| 100 | + m_condition.notify_all(); |
| 101 | + } |
| 102 | + |
| 103 | + private: |
| 104 | + inline auto runLoop() -> void |
| 105 | + { |
| 106 | + auto setAccessor = m_set.access(); |
| 107 | + |
| 108 | + if (std::empty(setAccessor.value())) |
| 109 | + { |
| 110 | + m_condition.notify_all(); |
| 111 | + m_condition.wait(setAccessor.getLockGuard(), [this, &setAccessor] { |
| 112 | + return not std::empty(setAccessor.value()) or m_loop.isStopRequested(); |
| 113 | + }); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + auto item = *std::begin(setAccessor.value()); |
| 118 | + |
| 119 | + if (item->cancelled) |
| 120 | + { |
| 121 | + setAccessor->erase(std::begin(setAccessor.value())); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (item->time > Clock::now()) |
| 126 | + { |
| 127 | + m_condition.wait_until(setAccessor.getLockGuard(), item->time); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + setAccessor->erase(std::begin(setAccessor.value())); |
| 132 | + if (item->cancelled) |
| 133 | + return; |
| 134 | + |
| 135 | + setAccessor.getLockGuard().unlock(); |
| 136 | + std::invoke(item->task); |
| 137 | + setAccessor.getLockGuard().lock(); |
| 138 | + } |
| 139 | + |
| 140 | + private: |
| 141 | + std::atomic_size_t m_nextId = 0; |
| 142 | + UniqueLocker<Set> m_set; |
| 143 | + UniqueLocker<Map> m_map; |
| 144 | + |
| 145 | + std::condition_variable m_condition; |
| 146 | + ThreadLoop m_loop; |
| 147 | + }; |
| 148 | +} |
0 commit comments