-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadLoop.mpp
More file actions
172 lines (153 loc) · 4.27 KB
/
ThreadLoop.mpp
File metadata and controls
172 lines (153 loc) · 4.27 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module;
#include <cstdio>
export module CppUtils.Thread.ThreadLoop;
import std;
export namespace CppUtils::Thread
{
class ThreadLoop final
{
public:
inline ThreadLoop() = delete;
inline explicit ThreadLoop(std::function<void()> function,
std::function<void()> interruptFunction = nullptr,
std::function<void(std::exception_ptr)> onError = nullptr) noexcept:
m_function{std::move(function)},
m_interruptFunction{std::move(interruptFunction)},
m_onError{std::move(onError)}
{}
inline ThreadLoop(const ThreadLoop&) = delete;
inline auto operator=(const ThreadLoop&) -> ThreadLoop& = delete;
inline ThreadLoop(ThreadLoop&& other) noexcept
{
auto lockGuard = std::unique_lock{other.m_mutex};
m_function = std::move(other.m_function);
m_interruptFunction = std::move(other.m_interruptFunction);
m_onError = std::move(other.m_onError);
m_thread = std::move(other.m_thread);
}
inline auto operator=(ThreadLoop&& other) noexcept -> ThreadLoop&
{
if (this == std::addressof(other))
return *this;
auto lockGuard = std::scoped_lock{m_mutex, other.m_mutex};
m_function = std::move(other.m_function);
m_interruptFunction = std::move(other.m_interruptFunction);
m_onError = std::move(other.m_onError);
m_thread = std::move(other.m_thread);
return *this;
}
inline ~ThreadLoop() noexcept
{
stop();
}
[[nodiscard]] inline auto isRunning() const noexcept -> bool
{
auto lockGuard = std::unique_lock{m_mutex};
return m_thread.joinable();
}
inline auto setInterruptFunction(std::function<void()> interruptFunction) noexcept -> void
{
auto lockGuard = std::unique_lock{m_mutex};
m_interruptFunction = std::move(interruptFunction);
}
inline auto setOnError(std::function<void(std::exception_ptr)> onError) noexcept -> void
{
auto lockGuard = std::unique_lock{m_mutex};
m_onError = std::move(onError);
}
inline auto start() -> void
{
auto lockGuard = std::unique_lock{m_mutex};
if (m_thread.joinable())
return;
m_thread = std::jthread{[function = m_function, interruptFunction = m_interruptFunction, onError = m_onError](std::stop_token stopToken) mutable -> void {
using namespace std::chrono_literals;
while (not stopToken.stop_requested())
{
try
{
if (function)
function();
}
catch (...)
{
if (onError)
{
try
{
onError(std::current_exception());
}
catch (const std::exception& exception)
{
std::println(stderr, "ThreadLoop: onError threw an exception: {}", exception.what());
std::this_thread::sleep_for(100ms);
}
catch (...)
{
std::println(stderr, "ThreadLoop: onError threw a non-std exception");
std::this_thread::sleep_for(100ms);
}
}
else
{
std::println(stderr, "ThreadLoop: Unhandled exception, cooling down...");
std::this_thread::sleep_for(100ms);
}
}
}
}};
}
inline auto isStopRequested() const -> bool
{
return m_thread.get_stop_token().stop_requested();
}
inline auto requestStop() -> void
{
auto lockGuard = std::unique_lock{m_mutex};
if (not m_thread.joinable() or m_thread.get_stop_token().stop_requested())
return;
m_thread.request_stop();
}
inline auto stop() noexcept -> void
{
try
{
auto interruptFunction = std::function<void()>{};
{
auto lockGuard = std::unique_lock{m_mutex};
if (not m_thread.joinable() or m_thread.get_stop_token().stop_requested())
return;
m_thread.request_stop();
interruptFunction = m_interruptFunction;
}
if (interruptFunction)
{
try
{
interruptFunction();
}
catch (...)
{
std::println(stderr, "ThreadLoop: interruptFunction threw during stop");
}
}
if (m_thread.joinable())
m_thread.join();
}
catch (const std::exception& exception)
{
std::println(stderr, "Exception in ThreadLoop::stop(): {}", exception.what());
}
catch (...)
{
std::println(stderr, "Unknown exception in ThreadLoop::stop()");
}
}
private:
mutable std::mutex m_mutex;
std::function<void()> m_function;
std::function<void()> m_interruptFunction;
std::function<void(std::exception_ptr)> m_onError;
std::jthread m_thread;
};
}