Remove userdata from Thread.start()
This commit is contained in:
parent
d1dac8427a
commit
a6f0aba43d
@ -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()));
|
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());
|
::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) {
|
if (ce.error != Callable::CallError::CALL_OK) {
|
||||||
t->running.clear();
|
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();
|
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_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
|
||||||
ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
|
ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
|
||||||
ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
|
ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
|
||||||
|
|
||||||
ret = Variant();
|
ret = Variant();
|
||||||
target_callable = p_callable;
|
target_callable = p_callable;
|
||||||
userdata = p_userdata;
|
|
||||||
running.set();
|
running.set();
|
||||||
|
|
||||||
Ref<Thread> *ud = memnew(Ref<Thread>(this));
|
Ref<Thread> *ud = memnew(Ref<Thread>(this));
|
||||||
@ -1902,13 +1867,12 @@ Variant Thread::wait_to_finish() {
|
|||||||
thread.wait_to_finish();
|
thread.wait_to_finish();
|
||||||
Variant r = ret;
|
Variant r = ret;
|
||||||
target_callable = Callable();
|
target_callable = Callable();
|
||||||
userdata = Variant();
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thread::_bind_methods() {
|
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("get_id"), &Thread::get_id);
|
||||||
ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
|
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("is_alive"), &Thread::is_alive);
|
||||||
|
@ -551,7 +551,6 @@ class Thread : public RefCounted {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
Variant ret;
|
Variant ret;
|
||||||
Variant userdata;
|
|
||||||
SafeFlag running;
|
SafeFlag running;
|
||||||
Callable target_callable;
|
Callable target_callable;
|
||||||
::Thread thread;
|
::Thread thread;
|
||||||
@ -566,7 +565,7 @@ public:
|
|||||||
PRIORITY_MAX
|
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;
|
String get_id() const;
|
||||||
bool is_started() const;
|
bool is_started() const;
|
||||||
bool is_alive() const;
|
bool is_alive() const;
|
||||||
|
@ -35,10 +35,11 @@
|
|||||||
<method name="start">
|
<method name="start">
|
||||||
<return type="int" enum="Error" />
|
<return type="int" enum="Error" />
|
||||||
<argument index="0" name="callable" type="Callable" />
|
<argument index="0" name="callable" type="Callable" />
|
||||||
<argument index="1" name="userdata" type="Variant" default="null" />
|
<argument index="1" name="priority" type="int" enum="Thread.Priority" default="1" />
|
||||||
<argument index="2" name="priority" type="int" enum="Thread.Priority" default="1" />
|
|
||||||
<description>
|
<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.
|
Returns [constant OK] on success, or [constant ERR_CANT_CREATE] on failure.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
Loading…
Reference in New Issue
Block a user