-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventQueue.mpp
More file actions
133 lines (114 loc) · 3.46 KB
/
Copy pathEventQueue.mpp
File metadata and controls
133 lines (114 loc) · 3.46 KB
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
export module CppUtils.Execution.EventQueue;
import std;
import CppUtils.String.Hash;
import CppUtils.Execution.EventDispatcher;
import CppUtils.Execution.ScopeGuard;
import CppUtils.Thread.ThreadLoop;
import CppUtils.Thread.UniqueLocker;
import CppUtils.Chrono.Concept;
export namespace CppUtils::Execution
{
class EventQueue final
{
public:
using SubscriptionIdentifier = EventDispatcher::SubscriptionIdentifier;
inline EventQueue():
m_worker{
[this] { workerThread(); },
[this] { m_workCondition.notify_all(); }}
{
m_worker.start();
}
inline ~EventQueue()
{
waitUntilFinished();
m_worker.stop();
}
EventQueue(const EventQueue&) = delete;
EventQueue& operator=(const EventQueue&) = delete;
EventQueue(EventQueue&&) = delete;
EventQueue& operator=(EventQueue&&) = delete;
template<String::Hasher eventName = Type::Hash{}, class... Args>
inline auto emit(Args&&... args) -> void
{
emit(static_cast<Type::Hash>(eventName), std::forward<Args>(args)...);
}
template<class... Args>
inline auto emit(Type::Hash eventName, Args&&... args) -> void
{
auto task = [this, eventName, ... args = std::forward<Args>(args)]() mutable {
m_dispatcher.emit(eventName, std::move(args)...);
};
enqueue(std::move(task));
}
template<String::Hasher... eventNames>
inline auto subscribe(auto&& function)
{
return m_dispatcher.template subscribe<eventNames...>(std::forward<decltype(function)>(function));
}
template<String::Hasher... eventNames>
inline auto unsubscribe() -> bool
{
return m_dispatcher.template unsubscribe<eventNames...>();
}
inline auto unsubscribe(auto&& subscriptionIdentifierOrEventHash)
{
return m_dispatcher.unsubscribe(std::forward<decltype(subscriptionIdentifierOrEventHash)>(subscriptionIdentifierOrEventHash));
}
inline auto clearSubscribers() -> void
{
m_dispatcher.clearSubscribers();
}
template<Chrono::Duration Duration = std::chrono::milliseconds>
inline auto waitUntilFinished(Duration timeout = Duration::zero()) -> bool
{
auto accessor = m_queue.access();
auto predicate = [this, &accessor] { return std::ranges::empty(accessor.value()) and not m_isTaskRunning; };
if (timeout == Duration::zero())
{
m_finishedCondition.wait(accessor.getLockGuard(), predicate);
return true;
}
return m_finishedCondition.wait_for(accessor.getLockGuard(), timeout, predicate);
}
private:
inline auto enqueue(std::function<void()> task) -> void
{
{
auto accessor = m_queue.access();
accessor.value().push(std::move(task));
}
m_workCondition.notify_one();
}
inline auto workerThread() -> void
{
auto task = std::function<void()>{};
{
auto accessor = m_queue.access();
m_workCondition.wait(accessor.getLockGuard(), [this, &accessor] {
return not std::ranges::empty(accessor.value()) or m_worker.isStopRequested();
});
if (std::ranges::empty(accessor.value()))
return;
m_isTaskRunning = true;
task = std::move(accessor.value().front());
accessor.value().pop();
}
auto _ = ScopeGuard{[this] {
{
auto accessor = m_queue.access();
m_isTaskRunning = false;
}
m_finishedCondition.notify_all();
}};
if (task)
task();
}
EventDispatcher m_dispatcher;
Thread::UniqueLocker<std::queue<std::function<void()>>> m_queue;
std::condition_variable m_workCondition;
std::condition_variable m_finishedCondition;
Thread::ThreadLoop m_worker;
bool m_isTaskRunning = false;
};
}