Skip to content

Commit

Permalink
Lazily create GDScript thread lambda bookkeeping block
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomShaper committed Dec 26, 2023
1 parent 9d1cbab commit e3887df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,11 +1383,17 @@ String GDScript::debug_get_script_name(const Ref<Script> &p_script) {

GDScript::UpdatableFuncPtr GDScript::func_ptrs_to_update_main_thread;
thread_local GDScript::UpdatableFuncPtr *GDScript::func_ptrs_to_update_thread_local = nullptr;
PagedAllocator<GDScript::UpdatableFuncPtr> GDScript::updatable_func_ptrs_allocator;

GDScript::UpdatableFuncPtrElement GDScript::_add_func_ptr_to_update(GDScriptFunction **p_func_ptr_ptr) {
UpdatableFuncPtrElement result = {};

{
if (!func_ptrs_to_update_thread_local) {
// Creating it lazily here instead of at thread_enter() because non-GDScript threads can live without it.
func_ptrs_to_update_thread_local = updatable_func_ptrs_allocator.alloc();
}

MutexLock lock(func_ptrs_to_update_thread_local->mutex);
result.element = func_ptrs_to_update_thread_local->ptrs.push_back(p_func_ptr_ptr);
result.func_ptr = func_ptrs_to_update_thread_local;
Expand Down Expand Up @@ -2091,13 +2097,10 @@ void GDScriptLanguage::remove_named_global_constant(const StringName &p_name) {
named_globals.erase(p_name);
}

void GDScriptLanguage::thread_enter() {
GDScript::func_ptrs_to_update_thread_local = memnew(GDScript::UpdatableFuncPtr);
}

void GDScriptLanguage::thread_exit() {
// This thread may have been created before GDScript was up
// (which also means it can't have run any GDScript code at all).
// (which also means it can't have run any GDScript code at all),
// or may not even have had the need to allocate this at all.
if (!GDScript::func_ptrs_to_update_thread_local) {
return;
}
Expand All @@ -2114,7 +2117,7 @@ void GDScriptLanguage::thread_exit() {
}
}
if (destroy) {
memdelete(GDScript::func_ptrs_to_update_thread_local);
GDScript::updatable_func_ptrs_allocator.free(GDScript::func_ptrs_to_update_thread_local);
}
}

Expand Down Expand Up @@ -2203,6 +2206,7 @@ void GDScriptLanguage::finish() {
function_list.clear();

DEV_ASSERT(GDScript::func_ptrs_to_update_main_thread.rc == 1);
GDScript::updatable_func_ptrs_allocator.reset();
}

void GDScriptLanguage::profiling_start() {
Expand Down
3 changes: 2 additions & 1 deletion modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "core/io/resource_loader.h"
#include "core/io/resource_saver.h"
#include "core/object/script_language.h"
#include "core/templates/paged_allocator.h"
#include "core/templates/rb_set.h"

class GDScriptNativeClass : public RefCounted {
Expand Down Expand Up @@ -133,6 +134,7 @@ class GDScript : public Script {
};
static UpdatableFuncPtr func_ptrs_to_update_main_thread;
static thread_local UpdatableFuncPtr *func_ptrs_to_update_thread_local;
static PagedAllocator<UpdatableFuncPtr> updatable_func_ptrs_allocator;
List<UpdatableFuncPtr *> func_ptrs_to_update;
Mutex func_ptrs_to_update_mutex;

Expand Down Expand Up @@ -564,7 +566,6 @@ class GDScriptLanguage : public ScriptLanguage {

/* MULTITHREAD FUNCTIONS */

virtual void thread_enter() override;
virtual void thread_exit() override;

/* DEBUGGER FUNCTIONS */
Expand Down

0 comments on commit e3887df

Please sign in to comment.