Skip to content

Commit c8acf56

Browse files
committed
Make languages' thread enter/exit more resilient
1 parent 2d1dd41 commit c8acf56

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
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

+9-6
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;
@@ -516,6 +513,12 @@ void WorkerThreadPool::yield() {
516513
int th_index = get_thread_index();
517514
ERR_FAIL_COND_MSG(th_index == -1, "This function can only be called from a worker thread.");
518515
_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();
519522
}
520523

521524
void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {

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
};

0 commit comments

Comments
 (0)