Make languages' thread enter/exit more resilient

This commit is contained in:
Pedro J. Estébanez 2024-09-10 11:29:54 +02:00
parent 2d1dd41ef5
commit c8acf561ef
4 changed files with 28 additions and 8 deletions

View file

@ -41,6 +41,7 @@ ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
int ScriptServer::_language_count = 0;
bool ScriptServer::languages_ready = false;
Mutex ScriptServer::languages_mutex;
thread_local bool ScriptServer::thread_entered = false;
bool ScriptServer::scripting_enabled = true;
bool ScriptServer::reload_scripts_on_save = false;
@ -326,6 +327,10 @@ bool ScriptServer::are_languages_initialized() {
return languages_ready;
}
bool ScriptServer::thread_is_entered() {
return thread_entered;
}
void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
reload_scripts_on_save = p_enable;
}
@ -335,6 +340,10 @@ bool ScriptServer::is_reload_scripts_on_save_enabled() {
}
void ScriptServer::thread_enter() {
if (thread_entered) {
return;
}
MutexLock lock(languages_mutex);
if (!languages_ready) {
return;
@ -342,9 +351,15 @@ void ScriptServer::thread_enter() {
for (int i = 0; i < _language_count; i++) {
_languages[i]->thread_enter();
}
thread_entered = true;
}
void ScriptServer::thread_exit() {
if (!thread_entered) {
return;
}
MutexLock lock(languages_mutex);
if (!languages_ready) {
return;
@ -352,6 +367,8 @@ void ScriptServer::thread_exit() {
for (int i = 0; i < _language_count; i++) {
_languages[i]->thread_exit();
}
thread_entered = false;
}
HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;

View file

@ -54,6 +54,7 @@ class ScriptServer {
static int _language_count;
static bool languages_ready;
static Mutex languages_mutex;
static thread_local bool thread_entered;
static bool scripting_enabled;
static bool reload_scripts_on_save;
@ -101,6 +102,7 @@ public:
static void init_languages();
static void finish_languages();
static bool are_languages_initialized();
static bool thread_is_entered();
};
class PlaceHolderScriptInstance;

View file

@ -63,17 +63,14 @@ void WorkerThreadPool::_process_task(Task *p_task) {
// Tasks must start with these at default values. They are free to set-and-forget otherwise.
set_current_thread_safe_for_nodes(false);
MessageQueue::set_thread_singleton_override(nullptr);
// Since the WorkerThreadPool is started before the script server,
// its pre-created threads can't have ScriptServer::thread_enter() called on them early.
// Therefore, we do it late at the first opportunity, so in case the task
// about to be run uses scripting, guarantees are held.
ScriptServer::thread_enter();
task_mutex.lock();
if (!curr_thread.ready_for_scripting && ScriptServer::are_languages_initialized()) {
task_mutex.unlock();
ScriptServer::thread_enter();
task_mutex.lock();
curr_thread.ready_for_scripting = true;
}
p_task->pool_thread_index = pool_thread_index;
prev_task = curr_thread.current_task;
curr_thread.current_task = p_task;
@ -516,6 +513,12 @@ void WorkerThreadPool::yield() {
int th_index = get_thread_index();
ERR_FAIL_COND_MSG(th_index == -1, "This function can only be called from a worker thread.");
_wait_collaboratively(&threads[th_index], ThreadData::YIELDING);
// If this long-lived task started before the scripting server was initialized,
// now is a good time to have scripting languages ready for the current thread.
// Otherwise, such a piece of setup won't happen unless another task has been
// run during the collaborative wait.
ScriptServer::thread_enter();
}
void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {

View file

@ -112,7 +112,6 @@ private:
uint32_t index = 0;
Thread thread;
bool ready_for_scripting : 1;
bool signaled : 1;
bool yield_is_over : 1;
Task *current_task = nullptr;
@ -120,7 +119,6 @@ private:
ConditionVariable cond_var;
ThreadData() :
ready_for_scripting(false),
signaled(false),
yield_is_over(false) {}
};