|
| 1 | +export module CppUtils.Execution.EventQueue; |
| 2 | + |
| 3 | +import std; |
| 4 | +import CppUtils.String.Hash; |
| 5 | +import CppUtils.Execution.EventDispatcher; |
| 6 | +import CppUtils.Execution.ScopeGuard; |
| 7 | +import CppUtils.Thread.ThreadLoop; |
| 8 | +import CppUtils.Thread.UniqueLocker; |
| 9 | + |
| 10 | +export namespace CppUtils::Execution |
| 11 | +{ |
| 12 | + class EventQueue |
| 13 | + { |
| 14 | + public: |
| 15 | + inline EventQueue(): |
| 16 | + m_worker{ |
| 17 | + [this] { workerThread(); }, |
| 18 | + [this] { m_condition.notify_all(); }} |
| 19 | + { |
| 20 | + m_worker.start(); |
| 21 | + } |
| 22 | + |
| 23 | + inline ~EventQueue() |
| 24 | + { |
| 25 | + waitUntilFinished(); |
| 26 | + } |
| 27 | + |
| 28 | + EventQueue(const EventQueue&) = delete; |
| 29 | + EventQueue& operator=(const EventQueue&) = delete; |
| 30 | + EventQueue(EventQueue&&) = delete; |
| 31 | + EventQueue& operator=(EventQueue&&) = delete; |
| 32 | + |
| 33 | + template<String::Hasher eventName = String::Hash{}, class... Args> |
| 34 | + inline auto emit(Args&&... args) -> void |
| 35 | + { |
| 36 | + emit(static_cast<String::Hash>(eventName), std::forward<Args>(args)...); |
| 37 | + } |
| 38 | + |
| 39 | + template<class... Args> |
| 40 | + inline auto emit(String::Hash eventName, Args&&... args) -> void |
| 41 | + { |
| 42 | + auto task = [this, eventName, payload = std::make_tuple(std::forward<Args>(args)...)]() mutable { |
| 43 | + std::apply([this, eventName](auto&&... args) { |
| 44 | + m_dispatcher.emit(eventName, std::forward<decltype(args)>(args)...); |
| 45 | + }, std::move(payload)); |
| 46 | + }; |
| 47 | + enqueue(std::move(task)); |
| 48 | + } |
| 49 | + |
| 50 | + template<String::Hasher eventName = String::Hash{}> |
| 51 | + inline auto subscribe(auto&& function) -> void |
| 52 | + { |
| 53 | + m_dispatcher.subscribe<eventName>(std::forward<decltype(function)>(function)); |
| 54 | + } |
| 55 | + |
| 56 | + inline auto waitUntilFinished() -> void |
| 57 | + { |
| 58 | + auto accessor = m_queue.access(); |
| 59 | + m_condition.wait(accessor.getLockGuard(), [this, &accessor] { return accessor.value().empty() and not m_isTaskRunning; }); |
| 60 | + } |
| 61 | + |
| 62 | + private: |
| 63 | + inline auto enqueue(std::function<void()> task) -> void |
| 64 | + { |
| 65 | + { |
| 66 | + auto accessor = m_queue.access(); |
| 67 | + accessor.value().push(std::move(task)); |
| 68 | + } |
| 69 | + m_condition.notify_one(); |
| 70 | + } |
| 71 | + |
| 72 | + inline auto workerThread() -> void |
| 73 | + { |
| 74 | + auto task = std::function<void()>{}; |
| 75 | + { |
| 76 | + auto accessor = m_queue.access(); |
| 77 | + m_condition.wait(accessor.getLockGuard(), [this, &accessor] { |
| 78 | + return not accessor.value().empty() or m_worker.isStopRequested(); |
| 79 | + }); |
| 80 | + |
| 81 | + if (accessor.value().empty()) |
| 82 | + return; |
| 83 | + |
| 84 | + task = std::move(accessor.value().front()); |
| 85 | + accessor.value().pop(); |
| 86 | + } |
| 87 | + if (task) |
| 88 | + { |
| 89 | + m_isTaskRunning = true; |
| 90 | + auto _ = ScopeGuard{[this] { m_isTaskRunning = false; }}; |
| 91 | + task(); |
| 92 | + } |
| 93 | + m_condition.notify_all(); |
| 94 | + } |
| 95 | + |
| 96 | + EventDispatcher m_dispatcher; |
| 97 | + Thread::UniqueLocker<std::queue<std::function<void()>>> m_queue; |
| 98 | + std::condition_variable m_condition; |
| 99 | + Thread::ThreadLoop m_worker; |
| 100 | + std::atomic<bool> m_isTaskRunning = false; |
| 101 | + }; |
| 102 | +} |
0 commit comments