-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathThread.cpp
More file actions
338 lines (296 loc) · 11.1 KB
/
Thread.cpp
File metadata and controls
338 lines (296 loc) · 11.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "Thread.h"
#include "Fault.h"
#include <iostream>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef __linux__
#include <pthread.h>
#endif
using namespace std;
using namespace std::chrono;
#define MSG_POST_USER_DATA 1
#define MSG_EXIT_THREAD 2
static steady_clock::time_point GetNow()
{
return steady_clock::now();
}
//----------------------------------------------------------------------------
// Thread
//----------------------------------------------------------------------------
Thread::Thread(const std::string& threadName, size_t maxQueueSize, FullPolicy fullPolicy)
: m_thread(std::nullopt)
, m_exit(false)
, THREAD_NAME(threadName)
, MAX_QUEUE_SIZE(maxQueueSize)
, FULL_POLICY(fullPolicy)
, m_watchdogExit(false)
, m_lastAliveTime(steady_clock::time_point{})
, m_watchdogTimeout(steady_clock::duration::zero())
{
}
//----------------------------------------------------------------------------
// ~Thread
//----------------------------------------------------------------------------
Thread::~Thread()
{
ExitThread();
}
//----------------------------------------------------------------------------
// CreateThread
//----------------------------------------------------------------------------
bool Thread::CreateThread(std::optional<std::chrono::milliseconds> watchdogTimeout)
{
if (!m_thread)
{
m_threadStartPromise.emplace();
m_threadStartFuture.emplace(m_threadStartPromise->get_future());
m_exit = false;
m_thread.emplace(&Thread::Process, this);
SetThreadName(m_thread->native_handle(), THREAD_NAME);
// Wait until the worker thread has entered Process()
m_threadStartFuture->get();
m_lastAliveTime.store(GetNow());
if (watchdogTimeout.has_value())
{
m_watchdogTimeout.store(watchdogTimeout.value());
m_watchdogExit.store(false);
m_watchdogThread.emplace(&Thread::WatchdogProcess, this);
}
}
return true;
}
//----------------------------------------------------------------------------
// ExitThread
//----------------------------------------------------------------------------
void Thread::ExitThread()
{
if (!m_thread)
return;
// Stop watchdog first so it doesn't fire during shutdown
if (m_watchdogThread)
{
{
lock_guard<mutex> lk(m_watchdogMutex);
m_watchdogExit.store(true);
}
m_watchdogCv.notify_one();
m_watchdogThread->join();
m_watchdogThread.reset();
}
// Push high-priority exit message, bypassing MAX_QUEUE_SIZE limit
auto exitMsg = make_shared<ThreadMsg>(MSG_EXIT_THREAD, nullptr, Priority::HIGH);
{
lock_guard<mutex> lock(m_mutex);
m_exit.store(true);
m_queue.push(exitMsg);
m_cv.notify_one();
m_cvNotFull.notify_all(); // unblock any blocked producers
}
if (m_thread->joinable())
{
if (std::this_thread::get_id() != m_thread->get_id())
m_thread->join();
else
m_thread->detach(); // called from within the thread itself
}
{
lock_guard<mutex> lock(m_mutex);
m_thread.reset();
while (!m_queue.empty())
m_queue.pop();
m_cvNotFull.notify_all();
}
}
//----------------------------------------------------------------------------
// GetThreadId
//----------------------------------------------------------------------------
std::thread::id Thread::GetThreadId()
{
ASSERT_TRUE(m_thread.has_value());
return m_thread->get_id();
}
//----------------------------------------------------------------------------
// GetCurrentThreadId
//----------------------------------------------------------------------------
std::thread::id Thread::GetCurrentThreadId()
{
return this_thread::get_id();
}
//----------------------------------------------------------------------------
// IsCurrentThread
//----------------------------------------------------------------------------
bool Thread::IsCurrentThread()
{
if (!m_thread.has_value())
return false;
return GetThreadId() == GetCurrentThreadId();
}
//----------------------------------------------------------------------------
// GetQueueSize
//----------------------------------------------------------------------------
size_t Thread::GetQueueSize()
{
lock_guard<mutex> lock(m_mutex);
return m_queue.size();
}
//----------------------------------------------------------------------------
// SetThreadName
//----------------------------------------------------------------------------
void Thread::SetThreadName(std::thread::native_handle_type handle, const std::string& name)
{
#ifdef _WIN32
wstring wstr(name.begin(), name.end());
SetThreadDescription(handle, wstr.c_str());
#elif defined(__linux__)
pthread_setname_np(handle, name.substr(0, 15).c_str());
#endif
}
//----------------------------------------------------------------------------
// PostMsg
//----------------------------------------------------------------------------
void Thread::PostMsg(std::shared_ptr<UserData> data, Priority priority)
{
if (m_exit.load())
return;
ASSERT_TRUE(m_thread.has_value());
unique_lock<mutex> lk(m_mutex);
// [BACK PRESSURE / DROP LOGIC]
if (MAX_QUEUE_SIZE > 0 && m_queue.size() >= MAX_QUEUE_SIZE)
{
if (FULL_POLICY == FullPolicy::DROP)
return; // silently discard — caller is not stalled
// BLOCK: wait until the consumer drains a slot or the thread exits
m_cvNotFull.wait(lk, [this]() {
return m_queue.size() < MAX_QUEUE_SIZE || m_exit.load();
});
}
if (m_exit.load())
return;
auto threadMsg = make_shared<ThreadMsg>(MSG_POST_USER_DATA, data, priority);
m_queue.push(threadMsg);
m_cv.notify_one();
}
//----------------------------------------------------------------------------
// WatchdogProcess
// Runs on a dedicated watchdog thread. Wakes up every timeout/2 and checks
// whether the worker thread has completed a loop iteration recently.
//----------------------------------------------------------------------------
void Thread::WatchdogProcess()
{
auto timeout = m_watchdogTimeout.load();
auto sleepInterval = timeout / 2;
while (true)
{
unique_lock<mutex> lk(m_watchdogMutex);
m_watchdogCv.wait_for(lk, sleepInterval);
if (m_watchdogExit.load())
break;
auto delta = GetNow() - m_lastAliveTime.load();
if (delta > timeout)
{
cerr << "Watchdog: thread '" << THREAD_NAME
<< "' is unresponsive!" << endl;
// Optionally trigger a hard fault: ASSERT();
}
}
}
//----------------------------------------------------------------------------
// Process
// The worker thread event loop. Runs for the lifetime of the thread,
// blocking when there is nothing to do and waking up to process one message
// at a time in priority order (HIGH before NORMAL before LOW).
//
// Watchdog interaction
// --------------------
// m_lastAliveTime is written at the top of every loop iteration. The
// watchdog thread reads this timestamp and raises an alert if it has not
// been refreshed within the configured timeout — indicating the thread is
// stuck inside a message handler (deadlock, infinite loop, etc.).
//
// When a watchdog is active the blocking wait is a timed wait_for() rather
// than an indefinite wait(). The timeout is set to timeout/4 so the loop
// cycles at least four times per watchdog period even when the queue is
// empty, keeping m_lastAliveTime fresh. Without this, an idle thread would
// look identical to a stalled one from the watchdog's perspective.
//
// Back pressure interaction
// -------------------------
// After dequeuing a message, m_cvNotFull is signalled. This wakes any
// producer that blocked in PostMsg() because the queue was full, allowing
// it to enqueue its next message now that a slot is free.
//----------------------------------------------------------------------------
void Thread::Process()
{
// Unblock CreateThread(), which is waiting on this promise to confirm
// the thread is running before it returns to the caller.
m_threadStartPromise->set_value();
while (1)
{
// --- Watchdog heartbeat -------------------------------------------
// Record the current time before blocking. This is the timestamp the
// watchdog checks. As long as the loop keeps cycling (either woken by
// a message or by the timed heartbeat below), this stays current and
// the watchdog stays silent.
m_lastAliveTime.store(GetNow());
// --- Wait for a message -------------------------------------------
shared_ptr<ThreadMsg> msg;
{
unique_lock<mutex> lk(m_mutex);
if (m_watchdogTimeout.load() > steady_clock::duration::zero())
{
// Watchdog is active: use a timed wait so the loop wakes
// periodically even when the queue is empty. Without this,
// m_lastAliveTime would never be refreshed while idle and the
// watchdog would incorrectly report the thread as unresponsive.
auto heartbeat = m_watchdogTimeout.load() / 4;
m_cv.wait_for(lk, heartbeat, [this]() {
return !m_queue.empty() || m_exit.load();
});
}
else
{
// No watchdog: block indefinitely until a message arrives or
// ExitThread() sets m_exit and notifies.
m_cv.wait(lk, [this]() {
return !m_queue.empty() || m_exit.load();
});
}
if (m_queue.empty())
{
// Woken with no message — either the watchdog heartbeat fired
// (loop back to refresh m_lastAliveTime) or ExitThread() was
// called with nothing left in the queue (exit the loop).
if (m_exit.load()) return;
continue;
}
// Dequeue the highest-priority waiting message.
// std::priority_queue::top() returns the greatest element per the
// ThreadMsgComparator, i.e. HIGH > NORMAL > LOW.
msg = m_queue.top();
m_queue.pop();
// --- Back pressure: notify a blocked producer -----------------
// A producer in PostMsg() may be sleeping on m_cvNotFull because
// the queue was at MAX_QUEUE_SIZE. Removing one message opens a
// slot, so wake one waiting producer.
if (MAX_QUEUE_SIZE > 0)
m_cvNotFull.notify_one();
}
// --- Dispatch -------------------------------------------------------
switch (msg->GetId())
{
case MSG_POST_USER_DATA:
{
ASSERT_TRUE(msg->GetData());
auto userData = msg->GetData();
cout << userData->msg << " " << userData->year
<< " on " << THREAD_NAME << endl;
break;
}
case MSG_EXIT_THREAD:
return;
default:
ASSERT();
}
}
}