Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions include/godot_cpp/core/mutex_lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@
namespace godot {

class MutexLock {
const Mutex &mutex;

@Ivorforce Ivorforce Jul 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fix the issue I think, but...

I think we should sync Mutex over directly instead and push Mutex into a CoreBind namespace.
The CoreBind Mutex only really exists for scripting languages that can't do better. I can't think of a reason to use the CoreBind one instead of the template one; it's not allowed to be allocated locally (which was violated here), and it's way slower too.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's an option!

I had thought we were using a lot of OS-specific code in Mutex but reading it now, it's mostly std::mutex (except on mingw) so syncing should be fine

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I submitted #2032 for consideration.

const Ref<Mutex> &mutex;

public:
_ALWAYS_INLINE_ explicit MutexLock(const Mutex &p_mutex) :
_ALWAYS_INLINE_ explicit MutexLock(const Ref<Mutex> &p_mutex) :
mutex(p_mutex) {
const_cast<Mutex *>(&mutex)->lock();
const_cast<Mutex *>(*mutex)->lock();
}

_ALWAYS_INLINE_ ~MutexLock() {
const_cast<Mutex *>(&mutex)->unlock();
const_cast<Mutex *>(*mutex)->unlock();
}
};

#define _THREAD_SAFE_CLASS_ mutable Mutex _thread_safe_;
#define _THREAD_SAFE_CLASS_ mutable Ref<Mutex> _thread_safe_ = Ref<Mutex>(memnew(Mutex));
#define _THREAD_SAFE_METHOD_ MutexLock _thread_safe_method_(_thread_safe_);
#define _THREAD_SAFE_LOCK_ _thread_safe_.lock();
#define _THREAD_SAFE_UNLOCK_ _thread_safe_.unlock();
#define _THREAD_SAFE_LOCK_ _thread_safe_->lock();
#define _THREAD_SAFE_UNLOCK_ _thread_safe_->unlock();

} // namespace godot
1 change: 1 addition & 0 deletions test/build_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Label",
"MultiplayerAPI",
"MultiplayerPeer",
"Mutex",
"OS",
"TileMap",
"TileSet",
Expand Down
6 changes: 6 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ func _ready():
var przykład = ClassDB.instantiate("ExamplePrzykład")
assert_equal(przykład.get_the_word(), "słowo to przykład")

# Test call some methods on a thread safe class.
var example_thread_safe = ExampleThreadSafeClass.new()
assert_equal(example_thread_safe.test(), 123)
assert_equal(example_thread_safe.test_const(), 456)
assert_equal(example_thread_safe.test_manual(), 789)

exit_with_status()

func _on_Example_custom_signal(signal_name, value):
Expand Down
22 changes: 22 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,25 @@ void ExampleInternal::_bind_methods() {
int ExampleInternal::get_the_answer() const {
return 42;
}

void ExampleThreadSafeClass::_bind_methods() {
ClassDB::bind_method(D_METHOD("test"), &ExampleThreadSafeClass::test);
ClassDB::bind_method(D_METHOD("test_const"), &ExampleThreadSafeClass::test_const);
ClassDB::bind_method(D_METHOD("test_manual"), &ExampleThreadSafeClass::test_manual);
}

int ExampleThreadSafeClass::test() {
_THREAD_SAFE_METHOD_
return 123;
}

int ExampleThreadSafeClass::test_const() const {
_THREAD_SAFE_METHOD_
return 456;
}

int ExampleThreadSafeClass::test_manual() {
_THREAD_SAFE_LOCK_
_THREAD_SAFE_UNLOCK_
return 789;
}
15 changes: 15 additions & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <godot_cpp/classes/tile_set.hpp>
#include <godot_cpp/classes/tween.hpp>
#include <godot_cpp/classes/viewport.hpp>
#include <godot_cpp/core/mutex_lock.hpp>
#include <godot_cpp/variant/variant.hpp>
#include <godot_cpp/variant/variant_internal.hpp>

Expand Down Expand Up @@ -313,3 +314,17 @@ class ExampleInternal : public RefCounted {
public:
int get_the_answer() const;
};

class ExampleThreadSafeClass : public RefCounted {
GDCLASS(ExampleThreadSafeClass, RefCounted);

_THREAD_SAFE_CLASS_

protected:
static void _bind_methods();

public:
int test();
int test_const() const;
int test_manual();
};
1 change: 1 addition & 0 deletions test/src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
GDREGISTER_RUNTIME_CLASS(ExampleRuntime);
GDREGISTER_CLASS(ExamplePrzykład);
GDREGISTER_INTERNAL_CLASS(ExampleInternal);
GDREGISTER_CLASS(ExampleThreadSafeClass);
}

void uninitialize_example_module(ModuleInitializationLevel p_level) {
Expand Down
Loading