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

@@ -191,6 +191,10 @@ void mp_thread_set_state(mp_state_thread_t *state) {
pthread_setspecific(tls_key, state);
}
mp_uint_t mp_thread_get_id(void) {
return (mp_uint_t)pthread_self();
}
void mp_thread_start(void) {
// enable realtime priority if `-X realtime` command line parameter was set
#if defined(__APPLE__)
@@ -210,7 +214,7 @@ void mp_thread_start(void) {
mp_thread_unix_end_atomic_section();
}
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) {
// default stack size is 8k machine-words
if (*stack_size == 0) {
*stack_size = 8192 * sizeof(void *);
@@ -265,7 +269,8 @@ void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) {
mp_thread_unix_end_atomic_section();
return;
MP_STATIC_ASSERT(sizeof(mp_uint_t) >= sizeof(pthread_t));
return (mp_uint_t)id;
er:
mp_raise_OSError(ret);