Handle properly when use_threads is switched from false to true. Also make sure to check whether the update signal is connected or not before trying to set it up.

This commit is contained in:
Relintai 2022-09-18 10:40:06 +02:00
parent 5f5ac160c5
commit 6990f8201a
2 changed files with 23 additions and 1 deletions

View File

@ -264,7 +264,23 @@ void ThreadPool::_worker_thread_func(void *user_data) {
}
void ThreadPool::register_update() {
SceneTree::get_singleton()->CONNECT("idle_frame", this, ThreadPool, 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() {
@ -330,6 +346,8 @@ void ThreadPool::apply_settings() {
_threads.resize(0);
unregister_update();
_use_threads = _use_threads_new;
if (_use_threads) {
@ -455,5 +473,7 @@ void ThreadPool::_bind_methods() {
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);
}

View File

@ -99,6 +99,8 @@ public:
static void _worker_thread_func(void *user_data);
void register_update();
void unregister_update();
void update();
void apply_settings();