-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFEThreadPool.cpp
More file actions
575 lines (465 loc) · 15 KB
/
Copy pathFEThreadPool.cpp
File metadata and controls
575 lines (465 loc) · 15 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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include "FEThreadPool.h"
using namespace FocalEngine;
#ifdef FEBASICAPPLICATION_SHARED
extern "C" __declspec(dllexport) void* GetThreadPool()
{
return FEThreadPool::GetInstancePointer();
}
#endif
JobThread::JobThread()
{
bJobFinished = false;
bHaveNewJob = false;
bJobCollected = true;
bNeedToExit = false;
bReadyForDeletion = false;
ThreadHandler = std::thread(&JobThread::ExecuteJob, this);
}
JobThread::~JobThread()
{
bNeedToExit = true;
if (ThreadHandler.joinable())
ThreadHandler.join();
}
void JobThread::ExecuteJob()
{
while (true)
{
if (bNeedToExit)
{
bReadyForDeletion = true;
return;
}
bHaveNewJob = false;
if (Job != nullptr)
{
try
{
Job(CurrentInputData, CurrentOutputData);
}
catch (const std::exception& Exception)
{
LOG.Add(std::string("Exception in thread pool job: ") + Exception.what(), "FE_THREAD_POOL", FE_LOG_ERROR);
}
catch (...)
{
LOG.Add("Unknown exception in thread pool job.", "FE_THREAD_POOL", FE_LOG_ERROR);
}
Job = nullptr;
}
bJobFinished = true;
while (true)
{
if (bNeedToExit)
{
bReadyForDeletion = true;
return;
}
Sleep(5);
if (bHaveNewJob.load())
break;
}
}
}
bool JobThread::IsJobFinished() const
{
return bJobFinished.load();
}
bool JobThread::IsJobCollected() const
{
return bJobCollected.load();
}
bool JobThread::AssignJob(const FEUnexecutedJob* NewJob)
{
if (!IsJobFinished())
return false;
CurrentInputData = NewJob->InputData;
CurrentOutputData = NewJob->OutputData;
Job = NewJob->Job;
CurrentCallBack = NewJob->CallBack;
delete NewJob;
bJobFinished = false;
bJobCollected = false;
bHaveNewJob = true;
return true;
}
DedicatedJobThread::DedicatedJobThread()
{
ThreadID = UNIQUE_ID.GetUniqueHexID();
bShutdownRequested = false;
}
DedicatedJobThread::~DedicatedJobThread()
{
for (size_t i = 0; i < JobsList.size(); i++)
delete JobsList[i];
JobsList.clear();
}
LightThread::LightThread()
{
ThreadID = UNIQUE_ID.GetUniqueHexID();
}
LightThread::~LightThread() {}
FEThreadPool::FEThreadPool()
{
Threads.resize(4);
for (size_t i = 0; i < Threads.size(); i++)
Threads[i] = new JobThread();
}
FEThreadPool::~FEThreadPool()
{
for (size_t i = 0; i < Threads.size(); i++)
delete Threads[i];
Threads.clear();
for (size_t i = 0; i < JobsList.size(); i++)
delete JobsList[i];
JobsList.clear();
for (size_t i = 0; i < DedicatedThreads.size(); i++)
delete DedicatedThreads[i];
DedicatedThreads.clear();
DedicatedThreadsToShutdown.clear();
for (size_t i = 0; i < LightThreads.size(); i++)
{
if (LightThreads[i]->ThreadHandler.joinable())
LightThreads[i]->ThreadHandler.join();
delete LightThreads[i];
}
LightThreads.clear();
}
bool FEThreadPool::IsAnyThreadHaveActiveJob() const
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
if (!JobsList.empty())
return true;
for (size_t i = 0; i < Threads.size(); i++)
{
if (!Threads[i]->IsJobCollected() || Threads[i]->CallBacksInFlight.load() > 0)
return true;
}
return false;
}
bool FEThreadPool::SetConcurrentThreadCount(size_t NewValue)
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
if (NewValue > FE_MAX_CONCURRENT_THREADS)
NewValue = FE_MAX_CONCURRENT_THREADS;
if (NewValue <= Threads.size())
return false;
const size_t NewThreadsToAdd = NewValue - Threads.size();
for (size_t i = 0; i < NewThreadsToAdd; i++)
{
Threads.push_back(new JobThread());
}
return true;
}
void FEThreadPool::Execute(const FE_THREAD_JOB_FUNC Job, void* InputData, void* OutputData, const FE_THREAD_CALLBACK_FUNC CallBack)
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
FEUnexecutedJob* NewJob = new FEUnexecutedJob();
NewJob->Job = Job;
NewJob->InputData = InputData;
NewJob->OutputData = OutputData;
NewJob->CallBack = CallBack;
// Check if we can start it right away
for (size_t i = 0; i < Threads.size(); i++)
{
if (Threads[i]->IsJobFinished() && Threads[i]->IsJobCollected())
{
if (Threads[i]->AssignJob(NewJob))
return;
}
}
// If all threads in the pool are working, we should save the new job for later execution.
JobsList.push_back(NewJob);
}
FEThreadPool::FECollectedCallBack FEThreadPool::CollectJob(JobThread* FromThread)
{
// Copy the callback data and mark the job as collected BEFORE the callback can run.
FECollectedCallBack CollectedCallBack;
CollectedCallBack.CallBack = FromThread->CurrentCallBack;
CollectedCallBack.OutputData = FromThread->CurrentOutputData;
CollectedCallBack.FromThread = FromThread;
FromThread->CurrentCallBack = nullptr;
FromThread->CallBacksInFlight.fetch_add(1);
FromThread->bJobCollected.store(true);
return CollectedCallBack;
}
void FEThreadPool::InvokeCollectedCallBack(const FECollectedCallBack& CollectedCallBack)
{
// bNeedToExit after collection can only mean a force shutdown, and its contract says the callback must not fire.
if (CollectedCallBack.CallBack != nullptr && !CollectedCallBack.FromThread->bNeedToExit)
{
try
{
CollectedCallBack.CallBack(CollectedCallBack.OutputData);
}
catch (const std::exception& Exception)
{
LOG.Add(std::string("Exception in thread pool callback: ") + Exception.what(), "FE_THREAD_POOL", FE_LOG_ERROR);
}
catch (...)
{
LOG.Add("Unknown exception in thread pool callback.", "FE_THREAD_POOL", FE_LOG_ERROR);
}
}
CollectedCallBack.FromThread->CallBacksInFlight.fetch_sub(1);
}
void FEThreadPool::Update()
{
// Callbacks are gathered under the lock but invoked after it is released, so user callbacks do not block other threads that are using the pool.
std::vector<FECollectedCallBack> CallBacksToInvoke;
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
for (size_t i = 0; i < Threads.size(); i++)
{
if (Threads[i]->IsJobFinished() && !Threads[i]->IsJobCollected())
{
CallBacksToInvoke.push_back(CollectJob(Threads[i]));
}
else if (!JobsList.empty() && Threads[i]->IsJobFinished() && Threads[i]->IsJobCollected())
{
if (Threads[i]->AssignJob(JobsList[0]))
JobsList.erase(JobsList.begin());
}
}
for (size_t i = 0; i < DedicatedThreadsToShutdown.size(); i++)
{
// A thread with a callback still in flight can not be deleted yet, the invoker holds a pointer to it.
if (DedicatedThreadsToShutdown[i]->bNeedToExit && DedicatedThreadsToShutdown[i]->bReadyForDeletion && DedicatedThreadsToShutdown[i]->CallBacksInFlight.load() == 0)
{
for (size_t j = 0; j < DedicatedThreads.size(); j++)
{
if (DedicatedThreads[j]->ThreadID == DedicatedThreadsToShutdown[i]->ThreadID)
{
DedicatedThreads.erase(DedicatedThreads.begin() + j, DedicatedThreads.begin() + j + 1);
break;
}
}
delete DedicatedThreadsToShutdown[i];
DedicatedThreadsToShutdown.erase(DedicatedThreadsToShutdown.begin() + i, DedicatedThreadsToShutdown.begin() + i + 1);
i--;
if (DedicatedThreads.empty())
break;
}
}
for (size_t i = 0; i < DedicatedThreads.size(); i++)
{
DedicatedJobThread* CurrentDedicatedThread = DedicatedThreads[i];
if (CurrentDedicatedThread->bNeedToExit || CurrentDedicatedThread->bReadyForDeletion)
continue;
if (CurrentDedicatedThread->IsJobFinished() && !CurrentDedicatedThread->IsJobCollected())
{
CallBacksToInvoke.push_back(CollectJob(CurrentDedicatedThread));
}
else if (!CurrentDedicatedThread->JobsList.empty() && CurrentDedicatedThread->IsJobFinished() && CurrentDedicatedThread->IsJobCollected())
{
if (CurrentDedicatedThread->AssignJob(CurrentDedicatedThread->JobsList[0]))
CurrentDedicatedThread->JobsList.erase(CurrentDedicatedThread->JobsList.begin());
}
// If a graceful shutdown was requested and the thread has finished its queue, promote the request to a real exit signal.
if (CurrentDedicatedThread->bShutdownRequested
&& CurrentDedicatedThread->JobsList.empty()
&& CurrentDedicatedThread->IsJobFinished()
&& CurrentDedicatedThread->IsJobCollected()
&& CurrentDedicatedThread->CallBacksInFlight.load() == 0)
{
CurrentDedicatedThread->bNeedToExit = true;
MarkDedicatedThreadForShutdown(CurrentDedicatedThread);
}
}
}
for (size_t i = 0; i < CallBacksToInvoke.size(); i++)
InvokeCollectedCallBack(CallBacksToInvoke[i]);
}
unsigned int FEThreadPool::GetLogicalCoreCount() const
{
return std::thread::hardware_concurrency();
}
unsigned int FEThreadPool::GetThreadCount() const
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
return static_cast<int>(Threads.size());
}
std::string FEThreadPool::CreateDedicatedThread()
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
DedicatedThreads.push_back(new DedicatedJobThread());
return DedicatedThreads.back()->ThreadID;
}
bool FEThreadPool::IsAnyDedicatedThreadHaveActiveJob() const
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
for (size_t i = 0; i < DedicatedThreads.size(); i++)
{
if (!DedicatedThreads[i]->JobsList.empty() || !DedicatedThreads[i]->IsJobCollected() || DedicatedThreads[i]->CallBacksInFlight.load() > 0)
return true;
}
return false;
}
DedicatedJobThread* FEThreadPool::GetDedicatedThread(const std::string& ThreadID)
{
for (size_t i = 0; i < DedicatedThreads.size(); i++)
{
if (DedicatedThreads[i]->ThreadID == ThreadID)
return DedicatedThreads[i];
}
return nullptr;
}
void FEThreadPool::Execute(const std::string& DedicatedThreadID, const FE_THREAD_JOB_FUNC Job, void* InputData, void* OutputData, const FE_THREAD_CALLBACK_FUNC CallBack)
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
DedicatedJobThread* Thread = GetDedicatedThread(DedicatedThreadID);
if (!Thread)
return;
if (Thread->bShutdownRequested || Thread->bNeedToExit || Thread->bReadyForDeletion)
return;
FEUnexecutedJob* NewJob = new FEUnexecutedJob();
NewJob->Job = Job;
NewJob->InputData = InputData;
NewJob->OutputData = OutputData;
NewJob->CallBack = CallBack;
// Check if we can start it right away, but never ahead of already queued jobs, dedicated thread must run jobs in submission order.
if (Thread->JobsList.empty() && Thread->IsJobFinished() && Thread->IsJobCollected())
{
if (Thread->AssignJob(NewJob))
return;
}
// The thread is busy or older jobs are waiting, so save the new job for later execution.
Thread->JobsList.push_back(NewJob);
// If the thread is idle while older jobs are waiting, start the oldest one now instead of waiting for the next Update().
if (Thread->IsJobFinished() && Thread->IsJobCollected())
{
if (Thread->AssignJob(Thread->JobsList[0]))
Thread->JobsList.erase(Thread->JobsList.begin());
}
}
void FEThreadPool::MarkDedicatedThreadForShutdown(DedicatedJobThread* DedicatedThread)
{
for (size_t i = 0; i < DedicatedThreadsToShutdown.size(); i++)
{
if (DedicatedThreadsToShutdown[i]->ThreadID == DedicatedThread->ThreadID)
return;
}
DedicatedThreadsToShutdown.push_back(DedicatedThread);
}
bool FEThreadPool::ShutdownDedicatedThread(const std::string& DedicatedThreadID)
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
DedicatedJobThread* Thread = GetDedicatedThread(DedicatedThreadID);
if (!Thread)
return false;
// Request graceful shutdown. The dedicated-thread loop in Update() drains active jobs and any queued ones (firing their callbacks) before
// promoting this to bNeedToExit and freeing the slot.
Thread->bShutdownRequested = true;
return true;
}
bool FEThreadPool::ForceShutdownDedicatedThread(const std::string& DedicatedThreadID)
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
DedicatedJobThread* Thread = GetDedicatedThread(DedicatedThreadID);
if (!Thread)
return false;
// Reject any subsequent Execute(ID, ...) calls.
Thread->bShutdownRequested = true;
// Drop queued work without running it. Active job runs to completion but its pending callback is intentionally not fired.
for (size_t i = 0; i < Thread->JobsList.size(); i++)
delete Thread->JobsList[i];
Thread->JobsList.clear();
// Signal the worker to exit on its next inner sleep wake (every ~5 ms).
Thread->bNeedToExit = true;
MarkDedicatedThreadForShutdown(Thread);
return true;
}
bool FEThreadPool::WaitForDedicatedThread(const std::string& DedicatedThreadID)
{
while (true)
{
FECollectedCallBack CollectedCallBack;
{
std::lock_guard<std::recursive_mutex> Lock(MainMutex);
// Look up again under the lock so a concurrent Shutdown+Update can not free the thread out from under us between iterations.
DedicatedJobThread* Thread = GetDedicatedThread(DedicatedThreadID);
if (!Thread)
return false;
// The thread is shutting down so, it is no longer waitable and its pending callback is intentionally suppressed.
if (Thread->bNeedToExit || Thread->bReadyForDeletion)
return false;
// A callback collected by a concurrent Update() may still be running, completion includes its delivery.
if (Thread->JobsList.empty() && Thread->IsJobFinished() && Thread->IsJobCollected() && Thread->CallBacksInFlight.load() == 0)
return true;
if (Thread->IsJobFinished() && !Thread->IsJobCollected())
{
CollectedCallBack = CollectJob(Thread);
}
else if (!Thread->JobsList.empty() && Thread->IsJobFinished() && Thread->IsJobCollected())
{
if (Thread->AssignJob(Thread->JobsList[0]))
Thread->JobsList.erase(Thread->JobsList.begin());
}
}
if (CollectedCallBack.FromThread != nullptr)
{
// Invoke the callback outside of the lock, so user callbacks do not block other threads that are using the pool.
InvokeCollectedCallBack(CollectedCallBack);
continue;
}
// Sleep is in a separate block so we do not hold the lock while sleeping.
Sleep(10);
}
}
std::string FEThreadPool::CreateLightThread()
{
std::lock_guard<std::recursive_mutex> Lock(LightThreadsMutex);
LightThread* NewThread = new LightThread();
LightThreads.push_back(NewThread);
return NewThread->ThreadID;
}
LightThread* FEThreadPool::GetLightThread(const std::string& ThreadID)
{
for (size_t i = 0; i < LightThreads.size(); i++)
{
if (LightThreads[i]->ThreadID == ThreadID)
return LightThreads[i];
}
return nullptr;
}
bool FEThreadPool::WaitForLightThread(const std::string& LightThreadID)
{
std::thread ThreadToJoin;
{
std::lock_guard<std::recursive_mutex> Lock(LightThreadsMutex);
LightThread* Thread = GetLightThread(LightThreadID);
if (!Thread)
return false;
if (!Thread->ThreadHandler.joinable())
return false;
// Take ownership of the handle so the join can happen without holding the mutex.
ThreadToJoin = std::move(Thread->ThreadHandler);
}
ThreadToJoin.join();
return true;
}
bool FEThreadPool::RemoveLightThread(const std::string& LightThreadID)
{
std::thread ThreadToJoin;
bool bWasFound = false;
{
std::lock_guard<std::recursive_mutex> Lock(LightThreadsMutex);
for (size_t i = 0; i < LightThreads.size(); i++)
{
if (LightThreads[i]->ThreadID == LightThreadID)
{
ThreadToJoin = std::move(LightThreads[i]->ThreadHandler);
delete LightThreads[i];
LightThreads.erase(LightThreads.begin() + i, LightThreads.begin() + i + 1);
bWasFound = true;
break;
}
}
}
if (!bWasFound)
return false;
// Join outside the mutex, to not deadlock with a light thread that uses the light thread API.
if (ThreadToJoin.joinable())
ThreadToJoin.join();
return true;
}