summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Gilleron <marc.gilleron@gmail.com>2023-07-22 15:48:11 +0100
committerMarc Gilleron <marc.gilleron@gmail.com>2023-07-22 16:31:28 +0100
commit548c7586772adc76b62f5cfd13cc6fad3abc0549 (patch)
tree38b8536334140b2ad6d03e98b8a79f743fe8672b
parent3162be28e594bf5b17889117670fc6f2d75f2f0c (diff)
downloadredot-cpp-548c7586772adc76b62f5cfd13cc6fad3abc0549.tar.gz
Don't cache `null` forever if a singleton isn't available yet
# Conflicts: # binding_generator.py
-rw-r--r--binding_generator.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/binding_generator.py b/binding_generator.py
index acc625d..6e47e7b 100644
--- a/binding_generator.py
+++ b/binding_generator.py
@@ -1453,15 +1453,22 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
if is_singleton:
result.append(f"{class_name} *{class_name}::get_singleton() {{")
+ # We assume multi-threaded access is OK because each assignment will assign the same value every time
+ result.append(f"\tstatic {class_name} *singleton = nullptr;")
+ result.append("\tif (unlikely(singleton == nullptr)) {")
result.append(
- f"\tstatic GDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton({class_name}::get_class_static()._native_ptr());"
+ f"\t\tGDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton({class_name}::get_class_static()._native_ptr());"
)
result.append("#ifdef DEBUG_ENABLED")
- result.append("\tERR_FAIL_COND_V(singleton_obj == nullptr, nullptr);")
+ result.append("\t\tERR_FAIL_COND_V(singleton_obj == nullptr, nullptr);")
result.append("#endif // DEBUG_ENABLED")
result.append(
- f"\tstatic {class_name} *singleton = reinterpret_cast<{class_name} *>(internal::gdextension_interface_object_get_instance_binding(singleton_obj, internal::token, &{class_name}::_gde_binding_callbacks));"
+ f"\t\tsingleton = reinterpret_cast<{class_name} *>(internal::gdextension_interface_object_get_instance_binding(singleton_obj, internal::token, &{class_name}::_gde_binding_callbacks));"
)
+ result.append("#ifdef DEBUG_ENABLED")
+ result.append("\t\tERR_FAIL_COND_V(singleton == nullptr, nullptr);")
+ result.append("#endif // DEBUG_ENABLED")
+ result.append("\t}")
result.append("\treturn singleton;")
result.append("}")
result.append("")