-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththread_pool.cpp
479 lines (352 loc) · 12.3 KB
/
thread_pool.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
#include "thread_pool.h"
/*
Copyright (c) 2020-2022 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "core/version.h"
#if VERSION_MAJOR > 3
#include "core/config/engine.h"
#include "core/config/project_settings.h"
#else
#include "core/engine.h"
#include "core/project_settings.h"
#endif
#include "core/os/os.h"
#include "scene/main/scene_tree.h"
#include "core/version.h"
#if VERSION_MAJOR >= 4
#define CONNECT(sig, obj, target_method_class, method) connect(sig, callable_mp(obj, &target_method_class::method))
#define DISCONNECT(sig, obj, target_method_class, method) disconnect(sig, callable_mp(obj, &target_method_class::method))
#define REAL FLOAT
#else
#define CONNECT(sig, obj, target_method_class, method) connect(sig, obj, #method)
#define DISCONNECT(sig, obj, target_method_class, method) disconnect(sig, obj, #method)
#endif
ThreadPool *ThreadPool::_instance;
ThreadPool *ThreadPool::get_singleton() {
return _instance;
}
bool ThreadPool::get_use_threads() const {
return _use_threads;
}
void ThreadPool::set_use_threads(const bool value) {
// Will be applied later in update, so current jobs can be finished first
_use_threads_new = value;
_dirty = true;
}
int ThreadPool::get_thread_count() const {
return _thread_count;
}
void ThreadPool::set_thread_count(const int value) {
_thread_count = value;
_dirty = true;
}
int ThreadPool::get_thread_fallback_count() const {
return _thread_fallback_count;
}
void ThreadPool::set_thread_fallback_count(const int value) {
_thread_fallback_count = value;
_dirty = true;
}
float ThreadPool::get_max_time_per_frame() const {
return _max_time_per_frame;
}
void ThreadPool::set_max_time_per_frame(const float value) {
_max_time_per_frame = value;
}
float ThreadPool::get_max_work_per_frame_percent() const {
return _max_work_per_frame_percent;
}
void ThreadPool::set_max_work_per_frame_percent(const float value) {
_max_work_per_frame_percent = value;
apply_max_work_per_frame_percent();
}
float ThreadPool::get_target_fps() const {
return _target_fps;
}
void ThreadPool::set_target_fps(const float value) {
_target_fps = value;
apply_max_work_per_frame_percent();
}
void ThreadPool::apply_max_work_per_frame_percent() {
_max_time_per_frame = (1.0 / _target_fps) * (_max_work_per_frame_percent / 100.0);
}
bool ThreadPool::is_working() const {
_THREAD_SAFE_LOCK_
if (_queue.size() > 0) {
_THREAD_SAFE_UNLOCK_
return true;
}
for (int i = 0; i < _threads.size(); ++i) {
if (_threads[i]->job.is_valid()) {
_THREAD_SAFE_UNLOCK_
return true;
}
}
_THREAD_SAFE_UNLOCK_
return false;
}
bool ThreadPool::is_working_no_lock() const {
if (_queue.size() > 0) {
return true;
}
for (int i = 0; i < _threads.size(); ++i) {
if (_threads[i]->job.is_valid()) {
return true;
}
}
return false;
}
bool ThreadPool::has_job(const Ref<ThreadPoolJob> &job) {
_THREAD_SAFE_LOCK_
for (int i = 0; i < _threads.size(); ++i) {
ThreadPoolContext *context = _threads.get(i);
if (context->job == job) {
_THREAD_SAFE_UNLOCK_
return true;
}
}
List<Ref<ThreadPoolJob>>::Element *E = _queue.find(job);
_THREAD_SAFE_UNLOCK_
return E;
}
void ThreadPool::add_job(const Ref<ThreadPoolJob> &job) {
_THREAD_SAFE_LOCK_
if (_use_threads) {
for (int i = 0; i < _threads.size(); ++i) {
ThreadPoolContext *context = _threads.get(i);
if (!context->job.is_valid()) {
context->job = job;
context->semaphore->post();
_THREAD_SAFE_UNLOCK_
return;
}
}
}
_queue.push_back(job);
_THREAD_SAFE_UNLOCK_
}
void ThreadPool::cancel_job(Ref<ThreadPoolJob> job) {
ERR_FAIL_COND(!job.is_valid());
job->set_cancelled(true);
_THREAD_SAFE_LOCK_
_queue.erase(job);
_THREAD_SAFE_UNLOCK_
}
void ThreadPool::cancel_job_wait(Ref<ThreadPoolJob> job) {
ERR_FAIL_COND(!job.is_valid());
job->set_cancelled(true);
_THREAD_SAFE_LOCK_
if (_queue.erase(job)) {
_THREAD_SAFE_UNLOCK_
return;
}
_THREAD_SAFE_UNLOCK_
for (int i = 0; i < _threads.size(); ++i) {
Ref<ThreadPoolJob> j = _threads[i]->job;
if (j == job) {
//wait until it's done
while (_threads[i]->job == job) {
OS::get_singleton()->delay_usec(100);
}
return;
}
}
}
void ThreadPool::_thread_finished(ThreadPoolContext *context) {
_THREAD_SAFE_LOCK_
context->job.unref();
while (_queue.size() > 0 && !context->job.is_valid()) {
context->job = _queue.front()->get();
_queue.pop_front();
}
if (context->job.is_valid()) {
context->semaphore->post();
}
_THREAD_SAFE_UNLOCK_
}
void ThreadPool::_worker_thread_func(void *user_data) {
ThreadPoolContext *context = reinterpret_cast<ThreadPoolContext *>(user_data);
while (context->running) {
context->semaphore->wait();
if (!context->job.is_valid()) {
ThreadPool::get_singleton()->_thread_finished(context);
continue;
}
if (context->job->get_cancelled()) {
ThreadPool::get_singleton()->_thread_finished(context);
continue;
}
context->job->execute();
ThreadPool::get_singleton()->_thread_finished(context);
}
}
void ThreadPool::register_update() {
if (!SceneTree::get_singleton()) {
return;
}
if (!SceneTree::get_singleton()->is_connected("idle_frame", this, "update")) {
SceneTree::get_singleton()->CONNECT("idle_frame", this, ThreadPool, update);
}
}
void ThreadPool::unregister_update() {
if (!SceneTree::get_singleton()) {
return;
}
if (SceneTree::get_singleton()->is_connected("idle_frame", this, "update")) {
SceneTree::get_singleton()->DISCONNECT("idle_frame", this, ThreadPool, update);
}
}
void ThreadPool::update() {
if (_dirty) {
apply_settings();
}
if (_use_threads) {
return;
}
if (_queue.size() == 0) {
return;
}
float remaining_time = _max_time_per_frame;
while (remaining_time > 0 && _queue.size() > 0) {
Ref<ThreadPoolJob> job = _queue.front()->get();
if (!job.is_valid()) {
_queue.pop_front();
continue;
}
job->set_max_allocated_time(remaining_time);
job->execute();
remaining_time -= job->get_current_execution_time();
if (job->get_complete() || job->get_cancelled()) {
_queue.pop_front();
}
}
}
void ThreadPool::apply_settings() {
if (!_dirty) {
return;
}
_THREAD_SAFE_LOCK_
if (is_working_no_lock()) {
_THREAD_SAFE_UNLOCK_
return;
}
_dirty = false;
for (int i = 0; i < _threads.size(); ++i) {
ThreadPoolContext *context = _threads[i];
CRASH_COND(context->job.is_valid());
context->running = false;
context->semaphore->post();
context->thread->wait_to_finish();
memdelete(context->thread);
memdelete(context->semaphore);
memdelete(context);
}
_threads.resize(0);
unregister_update();
_use_threads = _use_threads_new;
if (_use_threads) {
_threads.resize(_thread_count);
for (int i = 0; i < _threads.size(); ++i) {
ThreadPoolContext *context = memnew(ThreadPoolContext);
context->running = true;
context->semaphore = memnew(Semaphore);
context->thread = memnew(Thread());
context->thread->start(ThreadPool::_worker_thread_func, context);
_threads.write[i] = context;
}
} else {
call_deferred("register_update");
}
_THREAD_SAFE_UNLOCK_
}
ThreadPool::ThreadPool() {
_instance = this;
_use_threads = GLOBAL_DEF("thread_pool/use_threads", true);
_thread_count = GLOBAL_DEF("thread_pool/thread_count", -1);
_thread_fallback_count = GLOBAL_DEF("thread_pool/thread_fallback_count", 4);
if (_thread_fallback_count <= 0) {
print_error("ThreadPool: thread_fallback_count is invalid! Check ProjectSettings/ThreadPool/thread_fallback_count! Needs to be > 0! Set to 1!");
_thread_fallback_count = 1;
}
_target_fps = GLOBAL_DEF("thread_pool/target_fps", 60);
//Todo Add help text, as this will only come into play if threading is disabled, or not available
_max_work_per_frame_percent = GLOBAL_DEF("thread_pool/max_work_per_frame_percent", 25);
apply_max_work_per_frame_percent();
if (!OS::get_singleton()->can_use_threads()) {
_use_threads = false;
}
if (_use_threads) {
if (_thread_count <= 0) {
_thread_count = OS::get_singleton()->get_processor_count() + _thread_count;
}
//a.k.a OS::get_singleton()->get_processor_count() is not implemented, or returns something unexpected, or too high negative number
if (_thread_count <= 0) {
_thread_count = _thread_fallback_count;
}
}
_use_threads_new = _use_threads;
_dirty = true;
apply_settings();
}
ThreadPool::~ThreadPool() {
for (int i = 0; i < _threads.size(); ++i) {
ThreadPoolContext *context = _threads.get(i);
context->running = false;
context->semaphore->post();
}
for (int i = 0; i < _threads.size(); ++i) {
ThreadPoolContext *context = _threads.get(i);
context->thread->wait_to_finish();
memdelete(context->thread);
memdelete(context->semaphore);
context->job.unref();
memdelete(context);
}
_threads.clear();
_queue.clear();
}
void ThreadPool::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_use_threads"), &ThreadPool::get_use_threads);
ClassDB::bind_method(D_METHOD("set_use_threads", "value"), &ThreadPool::set_use_threads);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "get_use_threads");
ClassDB::bind_method(D_METHOD("get_thread_count"), &ThreadPool::get_thread_count);
ClassDB::bind_method(D_METHOD("set_thread_count", "value"), &ThreadPool::set_thread_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "thread_count"), "set_thread_count", "get_thread_count");
ClassDB::bind_method(D_METHOD("get_thread_fallback_count"), &ThreadPool::get_thread_fallback_count);
ClassDB::bind_method(D_METHOD("set_thread_fallback_count", "value"), &ThreadPool::set_thread_fallback_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "thread_fallback_count"), "set_thread_fallback_count", "get_thread_fallback_count");
ClassDB::bind_method(D_METHOD("get_max_time_per_frame"), &ThreadPool::get_max_time_per_frame);
ClassDB::bind_method(D_METHOD("set_max_time_per_frame", "value"), &ThreadPool::set_max_time_per_frame);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_time_per_frame"), "set_max_time_per_frame", "get_max_time_per_frame");
ClassDB::bind_method(D_METHOD("get_max_work_per_frame_percent"), &ThreadPool::get_max_work_per_frame_percent);
ClassDB::bind_method(D_METHOD("set_max_work_per_frame_percent", "value"), &ThreadPool::set_max_work_per_frame_percent);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_work_per_frame_percent"), "set_max_work_per_frame_percent", "get_max_work_per_frame_percent");
ClassDB::bind_method(D_METHOD("get_target_fps"), &ThreadPool::get_target_fps);
ClassDB::bind_method(D_METHOD("set_target_fps", "value"), &ThreadPool::set_target_fps);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_fps"), "set_target_fps", "get_target_fps");
ClassDB::bind_method(D_METHOD("apply_max_work_per_frame_percent"), &ThreadPool::apply_max_work_per_frame_percent);
ClassDB::bind_method(D_METHOD("is_working"), &ThreadPool::is_working);
ClassDB::bind_method(D_METHOD("is_working_no_lock"), &ThreadPool::is_working_no_lock);
ClassDB::bind_method(D_METHOD("has_job", "job"), &ThreadPool::has_job);
ClassDB::bind_method(D_METHOD("add_job", "job"), &ThreadPool::add_job);
ClassDB::bind_method(D_METHOD("cancel_job", "job"), &ThreadPool::cancel_job);
ClassDB::bind_method(D_METHOD("cancel_job_wait", "job"), &ThreadPool::cancel_job_wait);
ClassDB::bind_method(D_METHOD("register_update"), &ThreadPool::register_update);
ClassDB::bind_method(D_METHOD("unregister_update"), &ThreadPool::unregister_update);
ClassDB::bind_method(D_METHOD("update"), &ThreadPool::update);
}