Skip to content

Commit 658b8a8

Browse files
committed
Merge pull request #96760 from RandomShaper/wtp_langs_exit_thread
Make use of languages' thread enter/exit more correct
2 parents 63021b0 + c8acf56 commit 658b8a8

6 files changed

+51
-16
lines changed

core/object/script_language.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
4141
int ScriptServer::_language_count = 0;
4242
bool ScriptServer::languages_ready = false;
4343
Mutex ScriptServer::languages_mutex;
44+
thread_local bool ScriptServer::thread_entered = false;
4445

4546
bool ScriptServer::scripting_enabled = true;
4647
bool ScriptServer::reload_scripts_on_save = false;
@@ -326,6 +327,10 @@ bool ScriptServer::are_languages_initialized() {
326327
return languages_ready;
327328
}
328329

330+
bool ScriptServer::thread_is_entered() {
331+
return thread_entered;
332+
}
333+
329334
void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
330335
reload_scripts_on_save = p_enable;
331336
}
@@ -335,23 +340,35 @@ bool ScriptServer::is_reload_scripts_on_save_enabled() {
335340
}
336341

337342
void ScriptServer::thread_enter() {
343+
if (thread_entered) {
344+
return;
345+
}
346+
338347
MutexLock lock(languages_mutex);
339348
if (!languages_ready) {
340349
return;
341350
}
342351
for (int i = 0; i < _language_count; i++) {
343352
_languages[i]->thread_enter();
344353
}
354+
355+
thread_entered = true;
345356
}
346357

347358
void ScriptServer::thread_exit() {
359+
if (!thread_entered) {
360+
return;
361+
}
362+
348363
MutexLock lock(languages_mutex);
349364
if (!languages_ready) {
350365
return;
351366
}
352367
for (int i = 0; i < _language_count; i++) {
353368
_languages[i]->thread_exit();
354369
}
370+
371+
thread_entered = false;
355372
}
356373

357374
HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;

core/object/script_language.h

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ScriptServer {
5454
static int _language_count;
5555
static bool languages_ready;
5656
static Mutex languages_mutex;
57+
static thread_local bool thread_entered;
5758

5859
static bool scripting_enabled;
5960
static bool reload_scripts_on_save;
@@ -101,6 +102,7 @@ class ScriptServer {
101102
static void init_languages();
102103
static void finish_languages();
103104
static bool are_languages_initialized();
105+
static bool thread_is_entered();
104106
};
105107

106108
class PlaceHolderScriptInstance;

core/object/worker_thread_pool.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,14 @@ void WorkerThreadPool::_process_task(Task *p_task) {
6363
// Tasks must start with these at default values. They are free to set-and-forget otherwise.
6464
set_current_thread_safe_for_nodes(false);
6565
MessageQueue::set_thread_singleton_override(nullptr);
66+
6667
// Since the WorkerThreadPool is started before the script server,
6768
// its pre-created threads can't have ScriptServer::thread_enter() called on them early.
6869
// Therefore, we do it late at the first opportunity, so in case the task
6970
// about to be run uses scripting, guarantees are held.
71+
ScriptServer::thread_enter();
72+
7073
task_mutex.lock();
71-
if (!curr_thread.ready_for_scripting && ScriptServer::are_languages_initialized()) {
72-
task_mutex.unlock();
73-
ScriptServer::thread_enter();
74-
task_mutex.lock();
75-
curr_thread.ready_for_scripting = true;
76-
}
7774
p_task->pool_thread_index = pool_thread_index;
7875
prev_task = curr_thread.current_task;
7976
curr_thread.current_task = p_task;
@@ -326,6 +323,8 @@ WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *
326323
}
327324

328325
WorkerThreadPool::TaskID WorkerThreadPool::_add_task(const Callable &p_callable, void (*p_func)(void *), void *p_userdata, BaseTemplateUserdata *p_template_userdata, bool p_high_priority, const String &p_description) {
326+
ERR_FAIL_COND_V_MSG(threads.is_empty(), INVALID_TASK_ID, "Can't add a task because the WorkerThreadPool is either not initialized yet or already terminated.");
327+
329328
task_mutex.lock();
330329
// Get a free task
331330
Task *task = task_allocator.alloc();
@@ -514,6 +513,12 @@ void WorkerThreadPool::yield() {
514513
int th_index = get_thread_index();
515514
ERR_FAIL_COND_MSG(th_index == -1, "This function can only be called from a worker thread.");
516515
_wait_collaboratively(&threads[th_index], ThreadData::YIELDING);
516+
517+
// If this long-lived task started before the scripting server was initialized,
518+
// now is a good time to have scripting languages ready for the current thread.
519+
// Otherwise, such a piece of setup won't happen unless another task has been
520+
// run during the collaborative wait.
521+
ScriptServer::thread_enter();
517522
}
518523

519524
void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
@@ -538,6 +543,7 @@ void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
538543
}
539544

540545
WorkerThreadPool::GroupID WorkerThreadPool::_add_group_task(const Callable &p_callable, void (*p_func)(void *, uint32_t), void *p_userdata, BaseTemplateUserdata *p_template_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
546+
ERR_FAIL_COND_V_MSG(threads.is_empty(), INVALID_TASK_ID, "Can't add a group task because the WorkerThreadPool is either not initialized yet or already terminated.");
541547
ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
542548
if (p_tasks < 0) {
543549
p_tasks = MAX(1u, threads.size());
@@ -749,5 +755,5 @@ WorkerThreadPool::WorkerThreadPool() {
749755
}
750756

751757
WorkerThreadPool::~WorkerThreadPool() {
752-
finish();
758+
DEV_ASSERT(threads.size() == 0 && "finish() hasn't been called!");
753759
}

core/object/worker_thread_pool.h

-2
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,13 @@ class WorkerThreadPool : public Object {
112112

113113
uint32_t index = 0;
114114
Thread thread;
115-
bool ready_for_scripting : 1;
116115
bool signaled : 1;
117116
bool yield_is_over : 1;
118117
Task *current_task = nullptr;
119118
Task *awaited_task = nullptr; // Null if not awaiting the condition variable, or special value (YIELDING).
120119
ConditionVariable cond_var;
121120

122121
ThreadData() :
123-
ready_for_scripting(false),
124122
signaled(false),
125123
yield_is_over(false) {}
126124
};

core/register_core_types.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ static Time *_time = nullptr;
107107
static core_bind::Geometry2D *_geometry_2d = nullptr;
108108
static core_bind::Geometry3D *_geometry_3d = nullptr;
109109

110-
static WorkerThreadPool *worker_thread_pool = nullptr;
111-
112110
extern Mutex _global_mutex;
113111

114112
static GDExtensionManager *gdextension_manager = nullptr;
@@ -297,8 +295,6 @@ void register_core_types() {
297295
GDREGISTER_NATIVE_STRUCT(AudioFrame, "float left;float right");
298296
GDREGISTER_NATIVE_STRUCT(ScriptLanguageExtensionProfilingInfo, "StringName signature;uint64_t call_count;uint64_t total_time;uint64_t self_time");
299297

300-
worker_thread_pool = memnew(WorkerThreadPool);
301-
302298
OS::get_singleton()->benchmark_end_measure("Core", "Register Types");
303299
}
304300

@@ -349,7 +345,7 @@ void register_core_singletons() {
349345
Engine::get_singleton()->add_singleton(Engine::Singleton("Time", Time::get_singleton()));
350346
Engine::get_singleton()->add_singleton(Engine::Singleton("GDExtensionManager", GDExtensionManager::get_singleton()));
351347
Engine::get_singleton()->add_singleton(Engine::Singleton("ResourceUID", ResourceUID::get_singleton()));
352-
Engine::get_singleton()->add_singleton(Engine::Singleton("WorkerThreadPool", worker_thread_pool));
348+
Engine::get_singleton()->add_singleton(Engine::Singleton("WorkerThreadPool", WorkerThreadPool::get_singleton()));
353349

354350
OS::get_singleton()->benchmark_end_measure("Core", "Register Singletons");
355351
}
@@ -382,8 +378,6 @@ void unregister_core_types() {
382378

383379
// Destroy singletons in reverse order to ensure dependencies are not broken.
384380

385-
memdelete(worker_thread_pool);
386-
387381
memdelete(_engine_debugger);
388382
memdelete(_marshalls);
389383
memdelete(_classdb);

main/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static Engine *engine = nullptr;
140140
static ProjectSettings *globals = nullptr;
141141
static Input *input = nullptr;
142142
static InputMap *input_map = nullptr;
143+
static WorkerThreadPool *worker_thread_pool = nullptr;
143144
static TranslationServer *translation_server = nullptr;
144145
static Performance *performance = nullptr;
145146
static PackedData *packed_data = nullptr;
@@ -690,6 +691,8 @@ Error Main::test_setup() {
690691

691692
register_core_settings(); // Here globals are present.
692693

694+
worker_thread_pool = memnew(WorkerThreadPool);
695+
693696
translation_server = memnew(TranslationServer);
694697
tsman = memnew(TextServerManager);
695698

@@ -800,6 +803,8 @@ void Main::test_cleanup() {
800803
ResourceSaver::remove_custom_savers();
801804
PropertyListHelper::clear_base_helpers();
802805

806+
WorkerThreadPool::get_singleton()->finish();
807+
803808
#ifdef TOOLS_ENABLED
804809
GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
805810
uninitialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
@@ -841,6 +846,9 @@ void Main::test_cleanup() {
841846
if (physics_server_2d_manager) {
842847
memdelete(physics_server_2d_manager);
843848
}
849+
if (worker_thread_pool) {
850+
memdelete(worker_thread_pool);
851+
}
844852
if (globals) {
845853
memdelete(globals);
846854
}
@@ -931,6 +939,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
931939

932940
register_core_settings(); //here globals are present
933941

942+
worker_thread_pool = memnew(WorkerThreadPool);
934943
translation_server = memnew(TranslationServer);
935944
performance = memnew(Performance);
936945
GDREGISTER_CLASS(Performance);
@@ -2621,6 +2630,10 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
26212630
if (translation_server) {
26222631
memdelete(translation_server);
26232632
}
2633+
if (worker_thread_pool) {
2634+
worker_thread_pool->finish();
2635+
memdelete(worker_thread_pool);
2636+
}
26242637
if (globals) {
26252638
memdelete(globals);
26262639
}
@@ -4502,6 +4515,8 @@ void Main::cleanup(bool p_force) {
45024515
ResourceLoader::clear_translation_remaps();
45034516
ResourceLoader::clear_path_remaps();
45044517

4518+
WorkerThreadPool::get_singleton()->finish();
4519+
45054520
ScriptServer::finish_languages();
45064521

45074522
// Sync pending commands that may have been queued from a different thread during ScriptServer finalization
@@ -4592,6 +4607,9 @@ void Main::cleanup(bool p_force) {
45924607
if (physics_server_2d_manager) {
45934608
memdelete(physics_server_2d_manager);
45944609
}
4610+
if (worker_thread_pool) {
4611+
memdelete(worker_thread_pool);
4612+
}
45954613
if (globals) {
45964614
memdelete(globals);
45974615
}

0 commit comments

Comments
 (0)