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

@@ -116,7 +116,13 @@ STATIC void core1_entry_wrapper(void) {
// returning from here will loop the core forever (WFI)
}
void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
mp_uint_t mp_thread_get_id(void) {
// On RP2, there are only two threads, one for each core, so the thread id
// is the core number.
return get_core_num();
}
mp_uint_t mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
// Check if core1 is already in use.
if (core1_entry != NULL) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("core1 in use"));
@@ -144,6 +150,8 @@ void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
// Adjust stack_size to provide room to recover from hitting the limit.
*stack_size -= 512;
return 1;
}
void mp_thread_start(void) {