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

@@ -89,6 +89,10 @@ void mp_thread_set_state(mp_state_thread_t *state) {
vTaskSetThreadLocalStoragePointer(NULL, 0, state);
}
mp_uint_t mp_thread_get_id(void) {
return (mp_uint_t)xTaskGetCurrentTaskHandle();
}
void mp_thread_start(void) {
mp_thread_mutex_lock(&thread_mutex, 1);
for (mp_thread_t *th = thread; th != NULL; th = th->next) {
@@ -111,7 +115,7 @@ STATIC void freertos_entry(void *arg) {
}
}
void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
mp_uint_t mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
// store thread entry function into a global variable so we can access it
ext_thread_entry = entry;
@@ -148,6 +152,9 @@ 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;
MP_STATIC_ASSERT(sizeof(mp_uint_t) >= sizeof(TaskHandle_t));
return (mp_uint_t)id;
}
void mp_thread_finish(void) {