-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreading.cpp
487 lines (404 loc) · 15.3 KB
/
Threading.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//--------------------------------------------------------------------------------------
// File: Threading.cpp
//
// Distributed as part of NVIDIA Nsight serialization output.
//
// Copyright (c) NVIDIA Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "Threading.h"
#if defined(WIN32)
//--------------------------------------------------------------------------------------
// ThreadStartParams - Struct used to pass the params to the thread startup function
//--------------------------------------------------------------------------------------
struct ThreadStartParams
{
ThreadRunner* threadRunner;
HANDLE sequenceEvent;
HANDLE doneEvent;
bool* exitReplayerThreads;
bool* releaseReplayerThreads;
};
#define WAIT_FOR_SIGNAL() \
WaitForSingleObject(params->sequenceEvent, INFINITE)
#define SIGNAL_THREAD_DONE() \
SetEvent(params->doneEvent)
#define RELEASE_THREAD_RESOURCES() \
CloseHandle(params->sequenceEvent); \
CloseHandle(params->doneEvent)
//--------------------------------------------------------------------------------------
// ThreadStart - The method each replayer thread begins running
//--------------------------------------------------------------------------------------
DWORD WINAPI ThreadStart(LPVOID lpParameter)
{
ThreadStartParams* params = (ThreadStartParams*)lpParameter;
bool firstTime = true;
while (true)
{
WAIT_FOR_SIGNAL();
if (firstTime)
{
params->threadRunner->Init();
firstTime = false;
}
if (*params->releaseReplayerThreads)
{
params->threadRunner->Release();
SIGNAL_THREAD_DONE();
continue;
}
if (*params->exitReplayerThreads)
{
SIGNAL_THREAD_DONE();
break;
}
params->threadRunner->RunThread();
SIGNAL_THREAD_DONE();
}
RELEASE_THREAD_RESOURCES();
delete params;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------
// CreateThreadEvents - Create Windows events used to synchronize the threads
//--------------------------------------------------------------------------------------
bool CreateThreadEvents(unsigned int numThreads, HANDLE* events)
{
for (unsigned int currentEvent = 0; currentEvent < numThreads; ++currentEvent)
{
HANDLE eventHandle = CreateEvent(NULL, // Cannot be inherited
FALSE, // Auto reset object
FALSE, // Initially not signaled
NULL); // No need for a name
if (!eventHandle)
{
for (unsigned int existingHandle = 0; existingHandle < currentEvent; ++existingHandle)
{
CloseHandle(events[existingHandle]);
}
return false;
}
events[currentEvent] = eventHandle;
}
return true;
}
//--------------------------------------------------------------------------------------
// CreateReplayerThreads - Create and start the replayer threads
//--------------------------------------------------------------------------------------
bool CreateReplayerThreads(ThreadRunner** threadRunners, unsigned int numThreads, HANDLE* sequenceEvents, HANDLE* doneEvents, bool& exitThreads, bool& releaseThreads)
{
for (unsigned int currentThread = 0; currentThread < numThreads; ++currentThread)
{
ThreadStartParams* params = new ThreadStartParams();
{
params->threadRunner = threadRunners[currentThread];
params->sequenceEvent = sequenceEvents[currentThread];
params->doneEvent = doneEvents[currentThread];
params->exitReplayerThreads = &exitThreads;
params->releaseReplayerThreads = &releaseThreads;
}
// Create the thread
HANDLE threadHandle = CreateThread(NULL, // Cannot be inherited
0, // Default stack size
ThreadStart, // The function to run
(LPVOID)params, // The arguments
0, // Start immediately
NULL); // No need for the Windows thread id
if (!threadHandle)
{
for (unsigned int existingHandle = currentThread; existingHandle < numThreads; ++existingHandle)
{
CloseHandle(sequenceEvents[existingHandle]);
CloseHandle(doneEvents[existingHandle]);
}
delete params;
ExitReplayerThreads(currentThread, sequenceEvents, doneEvents, exitThreads);
return false;
}
CloseHandle(threadHandle);
}
return true;
}
//--------------------------------------------------------------------------------------
// ReleaseReplayerThreads - Release all of the replayer threads
//--------------------------------------------------------------------------------------
void ReleaseReplayerThreads(unsigned int numThreads, HANDLE* sequenceEvents, HANDLE* doneEvents, bool& releaseThreads)
{
// Set the flag telling the threads to release
releaseThreads = true;
// Wake up each thread
for (unsigned int i = 0; i < numThreads; ++i)
{
SetEvent(sequenceEvents[i]);
}
// Wait for each thread to release
WaitForMultipleObjects(numThreads, doneEvents, TRUE, INFINITE);
releaseThreads = false;
}
//--------------------------------------------------------------------------------------
// ExitReplayerThreads - Exit all of the replayer threads
//--------------------------------------------------------------------------------------
void ExitReplayerThreads(unsigned int numThreads, HANDLE* sequenceEvents, HANDLE* doneEvents, bool& exitThreads)
{
// Set the flag telling the threads to exit
exitThreads = true;
// Wake up each thread
for (unsigned int i = 0; i < numThreads; ++i)
{
SetEvent(sequenceEvents[i]);
}
// Wait for each thread to exit
WaitForMultipleObjects(numThreads, doneEvents, TRUE, INFINITE);
}
//--------------------------------------------------------------------------------------
// RunReplayerThreads - Tell each replayer thread to run a frame and wait for
// them to complete
//--------------------------------------------------------------------------------------
void RunReplayerThreads(unsigned int numThreads, unsigned int startThread, HANDLE* doneEvents, HANDLE* sequenceEvents)
{
// Tell the first thread to begin the frame
SetEvent(sequenceEvents[startThread]);
// Wait for the threads to report that they are ready to run the post frame method
WaitForMultipleObjects(numThreads, doneEvents, TRUE, INFINITE);
// Wake up the threads so they can run their post frame methods
for (unsigned int i = 0; i < numThreads; ++i)
{
SetEvent(sequenceEvents[i]);
}
// Wait for all of the post frame methods to finish
WaitForMultipleObjects(numThreads, doneEvents, TRUE, INFINITE);
}
//--------------------------------------------------------------------------------------
// ThreadRunner implementations - Implementation of ThreadRunner class and derived
// classes
//--------------------------------------------------------------------------------------
ThreadRunner::ThreadRunner(HANDLE sequenceEvent, HANDLE doneEvent, RunFrameFunc runFrameFunc)
: m_sequenceEvent(sequenceEvent)
, m_doneEvent(doneEvent)
, m_runFrameFunc(runFrameFunc)
{
}
void ThreadRunner::RunThread()
{
PreRunThread();
m_runFrameFunc();
PostRunThread();
}
void ThreadRunner::SignalMainThreadAndWait() const
{
SetEvent(m_doneEvent);
WaitForSingleObject(m_sequenceEvent, INFINITE);
}
BasicThreadRunner::BasicThreadRunner(HANDLE sequenceEvent, HANDLE doneEvent, RunFrameFunc runFrameFunc, ResetFrameFunc resetFrameFunc)
: ThreadRunner(sequenceEvent, doneEvent, runFrameFunc)
, m_resetFrameFunc(resetFrameFunc)
{
}
void BasicThreadRunner::PostRunThread()
{
// Make sure to let the main thread know we are here and wait for its permission
// to continue.
SignalMainThreadAndWait();
if (hasFrameReset)
{
m_resetFrameFunc(false);
}
}
#else // defined(WIN32)
#include <thread>
#define WAIT_FOR_SIGNAL(_ThreadID) \
threadSequenceEvents[_ThreadID]->Wait()
#define SIGNAL_THREAD_DONE(_ThreadID) \
threadDoneEvents[_ThreadID]->Signal()
//--------------------------------------------------------------------------------------
// ThreadStart - The method each replayer thread begins running
//--------------------------------------------------------------------------------------
static void ThreadStart(unsigned int threadId, std::shared_ptr<ThreadRunner> spThreadRunner)
{
bool firstTime = true;
while (true)
{
WAIT_FOR_SIGNAL(threadId);
if (firstTime)
{
spThreadRunner->Init();
firstTime = false;
}
if (releaseReplayerThreads)
{
spThreadRunner->Release();
SIGNAL_THREAD_DONE(threadId);
continue;
}
if (exitReplayerThreads)
{
SIGNAL_THREAD_DONE(threadId);
break;
}
spThreadRunner->RunThread();
SIGNAL_THREAD_DONE(threadId);
}
}
//--------------------------------------------------------------------------------------
// CreateThreadEvents - Create events used to synchronize the threads
//--------------------------------------------------------------------------------------
bool CreateThreadEvents(EventsArray& events)
{
for (auto& event : events)
{
event = std::make_shared<AutoResetEvent>();
}
return true;
}
//--------------------------------------------------------------------------------------
// CreateReplayerThreads - Create and start the replayer threads
//--------------------------------------------------------------------------------------
bool CreateReplayerThreads(ThreadRunnerArray& threadRunners)
{
unsigned int numCreatedThreads = 0;
try
{
for (auto& threadRunner : threadRunners)
{
// Create a new replayer thread
threadRunner->CreateThread(ThreadStart, numCreatedThreads);
++numCreatedThreads;
}
}
catch (...)
{
ExitReplayerThreads(numCreatedThreads);
return false;
}
return true;
}
//--------------------------------------------------------------------------------------
// WaitForReplayerThreadsToComplete - Helper to wake up the main thread once all
// replayer threads have signaled they are done
//--------------------------------------------------------------------------------------
static void WaitForReplayerThreadsToComplete(unsigned int numThreads)
{
for (unsigned int i = 0; i < numThreads; ++i)
{
threadDoneEvents[i]->Wait();
}
}
//--------------------------------------------------------------------------------------
// ReleaseReplayerThreads - Release all of the replayer threads, the context is still
// avialable at the time.
//--------------------------------------------------------------------------------------
void ReleaseReplayerThreads(unsigned int numThreads)
{
// Set the flag telling the threads to release
releaseReplayerThreads = true;
// Wake up each thread
for (unsigned int i = 0; i < numThreads; ++i)
{
threadSequenceEvents[i]->Signal();
}
WaitForReplayerThreadsToComplete(numThreads);
// Reset the flag in case we are started again
releaseReplayerThreads = false;
}
//--------------------------------------------------------------------------------------
// ExitReplayerThreads - Exit all of the replayer threads
//--------------------------------------------------------------------------------------
void ExitReplayerThreads(unsigned int numThreads)
{
// Set the flag telling the threads to exit
exitReplayerThreads = true;
// Wake up each thread
for (unsigned int i = 0; i < numThreads; ++i)
{
threadSequenceEvents[i]->Signal();
}
WaitForReplayerThreadsToComplete(numThreads);
// Reset the flag in case we are started again
exitReplayerThreads = false;
}
//--------------------------------------------------------------------------------------
// RunReplayerThreads - Tell each replayer thread to run a frame and wait for
// them to complete
//--------------------------------------------------------------------------------------
void RunReplayerThreads()
{
// Tell the first thread to begin the frame
threadSequenceEvents[0]->Signal();
// Wait for the threads to report that they are ready to run the post frame method
WaitForReplayerThreadsToComplete(NUM_REPLAYER_THREADS);
// Wake up the threads so they can run their post frame methods
for (auto& sequenceEvent : threadSequenceEvents)
{
sequenceEvent->Signal();
}
// Wait for all of the post frame methods to finish
WaitForReplayerThreadsToComplete(NUM_REPLAYER_THREADS);
}
//--------------------------------------------------------------------------------------
// AutoResetEvent implementation
//--------------------------------------------------------------------------------------
AutoResetEvent::AutoResetEvent()
: m_signaled(false)
, m_mutex()
, m_condtionVariable()
{
}
void AutoResetEvent::Signal()
{
// Manage scope of lock
{
std::lock_guard<std::mutex> lock(m_mutex);
m_signaled = true;
}
m_condtionVariable.notify_one();
}
void AutoResetEvent::Wait()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_condtionVariable.wait(lock, [this]() { return m_signaled; });
m_signaled = false;
}
//--------------------------------------------------------------------------------------
// ThreadRunner implementations - Implementation of ThreadRunner class and derived
// classes
//--------------------------------------------------------------------------------------
ThreadRunner::ThreadRunner(unsigned int threadId, RunFrameFunc runFrameFunc)
: m_threadId(threadId)
, m_runFrameFunc(runFrameFunc)
{
}
void ThreadRunner::CreateThread(ThreadStartFunc threadStartFunc, unsigned int threadId)
{
std::thread newthread(threadStartFunc, threadId, shared_from_this());
newthread.detach();
}
void ThreadRunner::RunThread()
{
PreRunThread();
m_runFrameFunc();
PostRunThread();
}
void ThreadRunner::SignalMainThreadAndWait() const
{
SIGNAL_THREAD_DONE(m_threadId);
WAIT_FOR_SIGNAL(m_threadId);
}
unsigned int ThreadRunner::GetThreadId() const
{
return m_threadId;
}
BasicThreadRunner::BasicThreadRunner(unsigned int threadId, RunFrameFunc runFrameFunc, ResetFrameFunc resetFrameFunc)
: ThreadRunner(threadId, runFrameFunc)
, m_resetFrameFunc(resetFrameFunc)
{
}
void BasicThreadRunner::PostRunThread()
{
// Make sure to let the main thread know we are here and wait for its permission
// to continue.
SignalMainThreadAndWait();
if (hasFrameReset)
{
m_resetFrameFunc(false);
}
}
#endif // defined(WIN32)