Bind RWLock, so it's accessible to scripts.

This commit is contained in:
Relintai 2023-07-06 21:17:13 +02:00
parent 4abd5bd2b3
commit be022b610f
4 changed files with 112 additions and 0 deletions

View File

@ -2712,6 +2712,42 @@ void _Mutex::_bind_methods() {
///////////////
void _RWLock::read_lock() {
rw_lock.read_lock();
}
Error _RWLock::read_try_lock() {
return rw_lock.read_try_lock();
}
void _RWLock::read_unlock() {
rw_lock.read_unlock();
}
void _RWLock::write_lock() {
rw_lock.write_lock();
}
Error _RWLock::write_try_lock() {
return rw_lock.write_try_lock();
}
void _RWLock::write_unlock() {
rw_lock.write_unlock();
}
void _RWLock::_bind_methods() {
ClassDB::bind_method(D_METHOD("read_lock"), &_RWLock::read_lock);
ClassDB::bind_method(D_METHOD("read_unlock"), &_RWLock::read_unlock);
ClassDB::bind_method(D_METHOD("read_try_lock"), &_RWLock::read_try_lock);
ClassDB::bind_method(D_METHOD("write_lock"), &_RWLock::write_lock);
ClassDB::bind_method(D_METHOD("write_unlock"), &_RWLock::write_unlock);
ClassDB::bind_method(D_METHOD("write_try_lock"), &_RWLock::write_try_lock);
}
///////////////
void _Thread::_start_func(void *ud) {
Ref<_Thread> *tud = (Ref<_Thread> *)ud;
Ref<_Thread> t = *tud;

View File

@ -38,6 +38,7 @@
#include "core/os/dir_access.h"
#include "core/os/file_access.h"
#include "core/os/os.h"
#include "core/os/rw_lock.h"
#include "core/os/safe_refcount.h"
#include "core/os/semaphore.h"
#include "core/os/thread.h"
@ -699,6 +700,23 @@ public:
void unlock();
};
class _RWLock : public Reference {
GDCLASS(_RWLock, Reference);
RWLock rw_lock;
static void _bind_methods();
public:
void read_lock();
void read_unlock();
Error read_try_lock();
void write_lock();
void write_unlock();
Error write_try_lock();
};
class _Semaphore : public Reference {
GDCLASS(_Semaphore, Reference);
Semaphore semaphore;

View File

@ -206,6 +206,7 @@ void register_core_types() {
ClassDB::register_class<_Directory>();
ClassDB::register_class<_Thread>();
ClassDB::register_class<_Mutex>();
ClassDB::register_class<_RWLock>();
ClassDB::register_class<_Semaphore>();
ClassDB::register_class<XMLParser>();

57
doc/classes/RWLock.xml Normal file
View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RWLock" inherits="Reference" version="3.12">
<brief_description>
A Read Write Lock.
</brief_description>
<description>
A Read Write Lock. This is used to synchronize multiple [Thread]s. This guarantees that only one thread can ever acquire the write lock at a time, and gives any number of threads read access in the meantime. An RWLock can be used to protect a critical section; however, be careful to avoid deadlocks.
</description>
<tutorials>
</tutorials>
<methods>
<method name="read_lock">
<return type="void" />
<description>
Locks this [RWLock] for read access, blocks until it is unlocked by the current owner.
[b]Note:[/b] This function returns without blocking if the thread already has ownership of the rwlock.
</description>
</method>
<method name="read_try_lock">
<return type="int" enum="Error" />
<description>
Tries locking this [RWLock] for read access, but does not block. Returns [constant OK] on success, [constant ERR_BUSY] otherwise.
[b]Note:[/b] This function returns [constant OK] if the thread already has ownership of the rwlock.
</description>
</method>
<method name="read_unlock">
<return type="void" />
<description>
Unlocks this [RWLock] for read access, leaving it to other threads.
[b]Note:[/b] If a thread called [method lock] or [method try_lock] multiple times while already having ownership of the rwlock, it must also call [method unlock] the same number of times in order to unlock it correctly.
</description>
</method>
<method name="write_lock">
<return type="void" />
<description>
Locks this [RWLock] for write access, blocks until it is unlocked by the current owner.
[b]Note:[/b] This function returns without blocking if the thread already has ownership of the rwlock.
</description>
</method>
<method name="write_try_lock">
<return type="int" enum="Error" />
<description>
Tries locking this [RWLock] for write access, but does not block. Returns [constant OK] on success, [constant ERR_BUSY] otherwise.
[b]Note:[/b] This function returns [constant OK] if the thread already has ownership of the rwlock.
</description>
</method>
<method name="write_unlock">
<return type="void" />
<description>
Unlocks this [RWLock] for write access, leaving it to other threads.
[b]Note:[/b] If a thread called [method lock] or [method try_lock] multiple times while already having ownership of the rwlock, it must also call [method unlock] the same number of times in order to unlock it correctly.
</description>
</method>
</methods>
<constants>
</constants>
</class>