diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-10-06 21:20:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-06 21:20:33 +0200 |
commit | 26f4848d013c5a234c2399717372ef72985cb655 (patch) | |
tree | a0af837922e0940b7639b9d6d2c25ac1da8ffcb2 /core/core_bind.cpp | |
parent | 164dc11e04b9cb5d0758dc310c624340355f32a3 (diff) | |
parent | f28c677f3dacf0077cf2ace0f0dff9c991d1dd2d (diff) |
Merge pull request #53455 from briansemrau/thread-is-executing
[core_bind] Add `Thread::is_alive`. Replace `is_active` with `is_started` to align with core/os/Thread API.
Diffstat (limited to 'core/core_bind.cpp')
-rw-r--r-- | core/core_bind.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/core/core_bind.cpp b/core/core_bind.cpp index f630adc39e..e11d9ab9c1 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -1771,6 +1771,7 @@ void Thread::_start_func(void *ud) { Object *target_instance = t->target_callable.get_object(); if (!target_instance) { + t->running.clear(); ERR_FAIL_MSG(vformat("Could not call function '%s' on previously freed instance to start thread %s.", t->target_callable.get_method(), t->get_id())); } @@ -1813,19 +1814,22 @@ void Thread::_start_func(void *ud) { t->target_callable.call(arg, argc, t->ret, ce); if (ce.error != Callable::CallError::CALL_OK) { + t->running.clear(); ERR_FAIL_MSG("Could not call function '" + t->target_callable.get_method().operator String() + "' to start thread " + t->get_id() + ": " + Variant::get_callable_error_text(t->target_callable, arg, argc, ce) + "."); } + + t->running.clear(); } Error Thread::start(const Callable &p_callable, const Variant &p_userdata, Priority p_priority) { - ERR_FAIL_COND_V_MSG(active.is_set(), ERR_ALREADY_IN_USE, "Thread already started."); + ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started."); ERR_FAIL_COND_V(p_callable.is_null(), ERR_INVALID_PARAMETER); ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER); ret = Variant(); target_callable = p_callable; userdata = p_userdata; - active.set(); + running.set(); Ref<Thread> *ud = memnew(Ref<Thread>(this)); @@ -1840,15 +1844,18 @@ String Thread::get_id() const { return itos(thread.get_id()); } -bool Thread::is_active() const { - return active.is_set(); +bool Thread::is_started() const { + return thread.is_started(); +} + +bool Thread::is_alive() const { + return running.is_set(); } Variant Thread::wait_to_finish() { - ERR_FAIL_COND_V_MSG(!active.is_set(), Variant(), "Thread must be active to wait for its completion."); + ERR_FAIL_COND_V_MSG(!is_started(), Variant(), "Thread must have been started to wait for its completion."); thread.wait_to_finish(); Variant r = ret; - active.clear(); target_callable = Callable(); userdata = Variant(); @@ -1858,7 +1865,8 @@ Variant Thread::wait_to_finish() { void Thread::_bind_methods() { ClassDB::bind_method(D_METHOD("start", "callable", "userdata", "priority"), &Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL)); ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id); - ClassDB::bind_method(D_METHOD("is_active"), &Thread::is_active); + ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started); + ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive); ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish); BIND_ENUM_CONSTANT(PRIORITY_LOW); |