Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of all the multi-threading modernization (3.2) #45618

Merged
merged 5 commits into from
Feb 18, 2021
Merged
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
66 changes: 16 additions & 50 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2689,12 +2689,14 @@ void _Marshalls::_bind_methods() {

Error _Semaphore::wait() {

return semaphore->wait();
semaphore.wait();
return OK; // Can't fail anymore; keep compat
}

Error _Semaphore::post() {

return semaphore->post();
semaphore.post();
return OK; // Can't fail anymore; keep compat
}

void _Semaphore::_bind_methods() {
Expand All @@ -2703,31 +2705,21 @@ void _Semaphore::_bind_methods() {
ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
}

_Semaphore::_Semaphore() {

semaphore = Semaphore::create();
}

_Semaphore::~_Semaphore() {

memdelete(semaphore);
}

///////////////

void _Mutex::lock() {

mutex->lock();
mutex.lock();
}

Error _Mutex::try_lock() {

return mutex->try_lock();
return mutex.try_lock();
}

void _Mutex::unlock() {

mutex->unlock();
mutex.unlock();
}

void _Mutex::_bind_methods() {
Expand All @@ -2737,16 +2729,6 @@ void _Mutex::_bind_methods() {
ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock);
}

_Mutex::_Mutex() {

mutex = Mutex::create();
}

_Mutex::~_Mutex() {

memdelete(mutex);
}

///////////////

void _Thread::_start_func(void *ud) {
Expand Down Expand Up @@ -2790,7 +2772,7 @@ void _Thread::_start_func(void *ud) {

Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {

ERR_FAIL_COND_V_MSG(active, ERR_ALREADY_IN_USE, "Thread already started.");
ERR_FAIL_COND_V_MSG(active.is_set(), ERR_ALREADY_IN_USE, "Thread already started.");
ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
Expand All @@ -2799,49 +2781,35 @@ Error _Thread::start(Object *p_instance, const StringName &p_method, const Varia
target_method = p_method;
target_instance = p_instance;
userdata = p_userdata;
active = true;
active.set();

Ref<_Thread> *ud = memnew(Ref<_Thread>(this));

Thread::Settings s;
s.priority = (Thread::Priority)p_priority;
thread = Thread::create(_start_func, ud, s);
if (!thread) {
active = false;
target_method = StringName();
target_instance = NULL;
userdata = Variant();
return ERR_CANT_CREATE;
}
thread.start(_start_func, ud, s);

return OK;
}

String _Thread::get_id() const {

if (!thread)
return String();

return itos(thread->get_id());
return itos(thread.get_id());
}

bool _Thread::is_active() const {

return active;
return active.is_set();
}
Variant _Thread::wait_to_finish() {

ERR_FAIL_COND_V_MSG(!thread, Variant(), "Thread must exist to wait for its completion.");
ERR_FAIL_COND_V_MSG(!active, Variant(), "Thread must be active to wait for its completion.");
Thread::wait_to_finish(thread);
ERR_FAIL_COND_V_MSG(!active.is_set(), Variant(), "Thread must be active to wait for its completion.");
thread.wait_to_finish();
Variant r = ret;
active = false;
target_method = StringName();
target_instance = NULL;
userdata = Variant();
if (thread)
memdelete(thread);
thread = NULL;
active.clear();

return r;
}
Expand All @@ -2859,14 +2827,12 @@ void _Thread::_bind_methods() {
}
_Thread::_Thread() {

active = false;
thread = NULL;
target_instance = NULL;
}

_Thread::~_Thread() {

ERR_FAIL_COND_MSG(active, "Reference to a Thread object was lost while the thread is still running...");
ERR_FAIL_COND_MSG(active.is_set(), "Reference to a Thread object was lost while the thread is still running...");
}

/////////////////////////////////////
Expand Down
15 changes: 5 additions & 10 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "core/os/os.h"
#include "core/os/semaphore.h"
#include "core/os/thread.h"
#include "core/safe_refcount.h"

class _ResourceLoader : public Object {
GDCLASS(_ResourceLoader, Object);
Expand Down Expand Up @@ -652,32 +653,26 @@ class _Marshalls : public Object {
class _Mutex : public Reference {

GDCLASS(_Mutex, Reference);
Mutex *mutex;
Mutex mutex;

static void _bind_methods();

public:
void lock();
Error try_lock();
void unlock();

_Mutex();
~_Mutex();
};

class _Semaphore : public Reference {

GDCLASS(_Semaphore, Reference);
Semaphore *semaphore;
Semaphore semaphore;

static void _bind_methods();

public:
Error wait();
Error post();

_Semaphore();
~_Semaphore();
};

class _Thread : public Reference {
Expand All @@ -687,10 +682,10 @@ class _Thread : public Reference {
protected:
Variant ret;
Variant userdata;
volatile bool active;
SafeFlag active;
Object *target_instance;
StringName target_method;
Thread *thread;
Thread thread;
static void _bind_methods();
static void _start_func(void *ud);

Expand Down
13 changes: 3 additions & 10 deletions core/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,9 @@ void ClassDB::add_property_group(StringName p_class, const String &p_name, const

void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index) {

lock->read_lock();
lock.read_lock();
ClassInfo *type = classes.getptr(p_class);
lock->read_unlock();
lock.read_unlock();

ERR_FAIL_COND(!type);

Expand Down Expand Up @@ -1447,12 +1447,7 @@ Variant ClassDB::class_get_default_property_value(const StringName &p_class, con
return default_values[p_class][p_property];
}

RWLock *ClassDB::lock = NULL;

void ClassDB::init() {

lock = RWLock::create();
}
RWLock ClassDB::lock;

void ClassDB::cleanup_defaults() {

Expand All @@ -1479,8 +1474,6 @@ void ClassDB::cleanup() {
classes.clear();
resource_base_extensions.clear();
compat_classes.clear();

memdelete(lock);
}

//
3 changes: 1 addition & 2 deletions core/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ClassDB {
return memnew(T);
}

static RWLock *lock;
static RWLock lock;
static HashMap<StringName, ClassInfo> classes;
static HashMap<StringName, StringName> resource_base_extensions;
static HashMap<StringName, StringName> compat_classes;
Expand Down Expand Up @@ -393,7 +393,6 @@ class ClassDB {
static void get_extensions_for_type(const StringName &p_class, List<String> *p_extensions);

static void add_compatibility_class(const StringName &p_class, const StringName &p_fallback);
static void init();

static void set_current_api(APIType p_api);
static APIType get_current_api();
Expand Down
15 changes: 3 additions & 12 deletions core/command_queue_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@

void CommandQueueMT::lock() {

if (mutex)
mutex->lock();
mutex.lock();
}

void CommandQueueMT::unlock() {

if (mutex)
mutex->unlock();
mutex.unlock();
}

void CommandQueueMT::wait_for_flush() {
Expand Down Expand Up @@ -107,7 +105,6 @@ CommandQueueMT::CommandQueueMT(bool p_sync) {
read_ptr_and_epoch = 0;
write_ptr_and_epoch = 0;
dealloc_ptr = 0;
mutex = Mutex::create();

command_mem_size = GLOBAL_DEF_RST("memory/limits/command_queue/multithreading_queue_size_kb", DEFAULT_COMMAND_MEM_SIZE_KB);
ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/command_queue/multithreading_queue_size_kb", PropertyInfo(Variant::INT, "memory/limits/command_queue/multithreading_queue_size_kb", PROPERTY_HINT_RANGE, "1,4096,1,or_greater"));
Expand All @@ -116,11 +113,10 @@ CommandQueueMT::CommandQueueMT(bool p_sync) {

for (int i = 0; i < SYNC_SEMAPHORES; i++) {

sync_sems[i].sem = Semaphore::create();
sync_sems[i].in_use = false;
}
if (p_sync) {
sync = Semaphore::create();
sync = memnew(Semaphore);
} else {
sync = NULL;
}
Expand All @@ -130,10 +126,5 @@ CommandQueueMT::~CommandQueueMT() {

if (sync)
memdelete(sync);
memdelete(mutex);
for (int i = 0; i < SYNC_SEMAPHORES; i++) {

memdelete(sync_sems[i].sem);
}
memfree(command_mem);
}
10 changes: 5 additions & 5 deletions core/command_queue_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
cmd->sync_sem = ss; \
unlock(); \
if (sync) sync->post(); \
ss->sem->wait(); \
ss->sem.wait(); \
ss->in_use = false; \
}

Expand All @@ -267,7 +267,7 @@
cmd->sync_sem = ss; \
unlock(); \
if (sync) sync->post(); \
ss->sem->wait(); \
ss->sem.wait(); \
ss->in_use = false; \
}

Expand All @@ -277,7 +277,7 @@ class CommandQueueMT {

struct SyncSemaphore {

Semaphore *sem;
Semaphore sem;
bool in_use;
};

Expand All @@ -293,7 +293,7 @@ class CommandQueueMT {
SyncSemaphore *sync_sem;

virtual void post() {
sync_sem->sem->post();
sync_sem->sem.post();
}
};

Expand Down Expand Up @@ -321,7 +321,7 @@ class CommandQueueMT {
uint32_t dealloc_ptr;
uint32_t command_mem_size;
SyncSemaphore sync_sems[SYNC_SEMAPHORES];
Mutex *mutex;
Mutex mutex;
Semaphore *sync;

template <class T>
Expand Down
Loading