mirror of
https://github.com/godotengine/godot.git
synced 2026-01-03 18:11:19 +03:00
Modernize Thread
- Based on C++11's `thread` and `thread_local` - No more need to allocate-deallocate or check for null - No pointer anymore, just a member variable - Platform-specific implementations no longer needed (except for the few cases of non-portable functions) - Simpler for `NO_THREADS` - Thread ids are now the same across platforms (main is 1; others follow)
This commit is contained in:
@@ -2787,24 +2787,14 @@ Error _Thread::start(Object *p_instance, const StringName &p_method, const Varia
|
||||
|
||||
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 {
|
||||
@@ -2813,17 +2803,13 @@ bool _Thread::is_active() const {
|
||||
}
|
||||
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);
|
||||
thread.wait_to_finish();
|
||||
Variant r = ret;
|
||||
active = false;
|
||||
target_method = StringName();
|
||||
target_instance = NULL;
|
||||
userdata = Variant();
|
||||
if (thread)
|
||||
memdelete(thread);
|
||||
thread = NULL;
|
||||
|
||||
return r;
|
||||
}
|
||||
@@ -2842,7 +2828,6 @@ void _Thread::_bind_methods() {
|
||||
_Thread::_Thread() {
|
||||
|
||||
active = false;
|
||||
thread = NULL;
|
||||
target_instance = NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -684,7 +684,7 @@ protected:
|
||||
volatile bool active;
|
||||
Object *target_instance;
|
||||
StringName target_method;
|
||||
Thread *thread;
|
||||
Thread thread;
|
||||
static void _bind_methods();
|
||||
static void _start_func(void *ud);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user