Remove userdata from Thread.start()
This commit is contained in:
parent
d1dac8427a
commit
a6f0aba43d
3 changed files with 10 additions and 46 deletions
|
@ -1820,60 +1820,25 @@ void Thread::_start_func(void *ud) {
|
|||
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()));
|
||||
}
|
||||
|
||||
Callable::CallError ce;
|
||||
const Variant *arg[1] = { &t->userdata };
|
||||
int argc = 0;
|
||||
if (arg[0]->get_type() != Variant::NIL) {
|
||||
// Just pass to the target function whatever came as user data
|
||||
argc = 1;
|
||||
} else {
|
||||
// There are two cases of null user data:
|
||||
// a) The target function has zero parameters and the caller is just honoring that.
|
||||
// b) The target function has at least one parameter with no default and the caller is
|
||||
// leveraging the fact that user data defaults to null in Thread.start().
|
||||
// We care about the case of more than one parameter because, even if a thread
|
||||
// function can have one at most, out mindset here is to do our best with the
|
||||
// only/first one and let the call handle any other error conditions, like too
|
||||
// much arguments.
|
||||
// We must check if we are in case b).
|
||||
int target_param_count = 0;
|
||||
int target_default_arg_count = 0;
|
||||
Ref<Script> script = target_instance->get_script();
|
||||
if (script.is_valid()) {
|
||||
MethodInfo mi = script->get_method_info(t->target_callable.get_method());
|
||||
target_param_count = mi.arguments.size();
|
||||
target_default_arg_count = mi.default_arguments.size();
|
||||
} else {
|
||||
MethodBind *method = ClassDB::get_method(target_instance->get_class_name(), t->target_callable.get_method());
|
||||
if (method) {
|
||||
target_param_count = method->get_argument_count();
|
||||
target_default_arg_count = method->get_default_argument_count();
|
||||
}
|
||||
}
|
||||
if (target_param_count >= 1 && target_default_arg_count < target_param_count) {
|
||||
argc = 1;
|
||||
}
|
||||
}
|
||||
|
||||
::Thread::set_name(t->target_callable.get_method());
|
||||
|
||||
t->target_callable.call(arg, argc, t->ret, ce);
|
||||
Callable::CallError ce;
|
||||
t->target_callable.call(nullptr, 0, 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) + ".");
|
||||
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, nullptr, 0, ce) + ".");
|
||||
}
|
||||
|
||||
t->running.clear();
|
||||
}
|
||||
|
||||
Error Thread::start(const Callable &p_callable, const Variant &p_userdata, Priority p_priority) {
|
||||
Error Thread::start(const Callable &p_callable, Priority p_priority) {
|
||||
ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
|
||||
ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
|
||||
|
||||
ret = Variant();
|
||||
target_callable = p_callable;
|
||||
userdata = p_userdata;
|
||||
running.set();
|
||||
|
||||
Ref<Thread> *ud = memnew(Ref<Thread>(this));
|
||||
|
@ -1902,13 +1867,12 @@ Variant Thread::wait_to_finish() {
|
|||
thread.wait_to_finish();
|
||||
Variant r = ret;
|
||||
target_callable = Callable();
|
||||
userdata = Variant();
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
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("start", "callable", "priority"), &Thread::start, DEFVAL(PRIORITY_NORMAL));
|
||||
ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
|
||||
ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
|
||||
ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive);
|
||||
|
|
|
@ -551,7 +551,6 @@ class Thread : public RefCounted {
|
|||
|
||||
protected:
|
||||
Variant ret;
|
||||
Variant userdata;
|
||||
SafeFlag running;
|
||||
Callable target_callable;
|
||||
::Thread thread;
|
||||
|
@ -566,7 +565,7 @@ public:
|
|||
PRIORITY_MAX
|
||||
};
|
||||
|
||||
Error start(const Callable &p_callable, const Variant &p_userdata = Variant(), Priority p_priority = PRIORITY_NORMAL);
|
||||
Error start(const Callable &p_callable, Priority p_priority = PRIORITY_NORMAL);
|
||||
String get_id() const;
|
||||
bool is_started() const;
|
||||
bool is_alive() const;
|
||||
|
|
|
@ -35,10 +35,11 @@
|
|||
<method name="start">
|
||||
<return type="int" enum="Error" />
|
||||
<argument index="0" name="callable" type="Callable" />
|
||||
<argument index="1" name="userdata" type="Variant" default="null" />
|
||||
<argument index="2" name="priority" type="int" enum="Thread.Priority" default="1" />
|
||||
<argument index="1" name="priority" type="int" enum="Thread.Priority" default="1" />
|
||||
<description>
|
||||
Starts a new [Thread] that calls [code]callable[/code] with [code]userdata[/code] passed as an argument. Even if no userdata is passed, [code]callable[/code] must accept one argument and it will be null. The [code]priority[/code] of the [Thread] can be changed by passing a value from the [enum Priority] enum.
|
||||
Starts a new [Thread] that calls [code]callable[/code].
|
||||
If the method takes some arguments, you can pass them using [method Callable.bind].
|
||||
The [code]priority[/code] of the [Thread] can be changed by passing a value from the [enum Priority] enum.
|
||||
Returns [constant OK] on success, or [constant ERR_CANT_CREATE] on failure.
|
||||
</description>
|
||||
</method>
|
||||
|
|
Loading…
Reference in a new issue