py/modthread: Return thread id from start_new_thread().

In CPython, `_thread.start_new_thread()` returns an ID that is the same ID
that is returned by `_thread.get_ident()`.  The current MicroPython
implementation of `_thread.start_new_thread()` always returns `None`.

This modifies the required functions to return a value. The native thread
id is returned since this can be used for interop with other functions, for
example, `pthread_kill()` on *nix. `_thread.get_ident()` is also modified
to return the native thread id so that the values match and avoids the need
for a separate `native_id` attribute.

Fixes issue #12153.

Signed-off-by: David Lechner <david@pybricks.com>
This commit is contained in:
David Lechner
2023-08-03 15:20:30 -05:00
committed by Damien George
parent c0d4c604e6
commit ffb43b2dd3
9 changed files with 58 additions and 16 deletions

View File

@@ -129,7 +129,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
STATIC size_t thread_stack_size = 0;
STATIC mp_obj_t mod_thread_get_ident(void) {
return mp_obj_new_int_from_uint((uintptr_t)mp_thread_get_state());
return mp_obj_new_int_from_uint(mp_thread_get_id());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_get_ident_obj, mod_thread_get_ident);
@@ -268,9 +268,7 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
th_args->fun = args[0];
// spawn the thread!
mp_thread_create(thread_entry, th_args, &th_args->stack_size);
return mp_const_none;
return mp_obj_new_int_from_uint(mp_thread_create(thread_entry, th_args, &th_args->stack_size));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread);