2016-04-22 18:52:33 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
|
2016-04-22 18:52:33 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stackctrl.h"
|
|
|
|
|
2018-08-08 21:24:49 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
#if MICROPY_PY_THREAD
|
|
|
|
|
|
|
|
#include "py/mpthread.h"
|
|
|
|
|
2017-07-24 12:55:14 -04:00
|
|
|
#if MICROPY_DEBUG_VERBOSE // print debugging info
|
2016-04-22 18:52:33 -04:00
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#define DEBUG_printf DEBUG_printf
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_PRINT (0)
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2016-04-25 07:21:48 -04:00
|
|
|
/****************************************************************/
|
|
|
|
// Lock object
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_thread_lock;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_thread_lock_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_thread_mutex_t mutex;
|
2016-05-26 07:24:52 -04:00
|
|
|
volatile bool locked;
|
2016-04-25 07:21:48 -04:00
|
|
|
} mp_obj_thread_lock_t;
|
|
|
|
|
|
|
|
STATIC mp_obj_thread_lock_t *mp_obj_new_thread_lock(void) {
|
|
|
|
mp_obj_thread_lock_t *self = m_new_obj(mp_obj_thread_lock_t);
|
|
|
|
self->base.type = &mp_type_thread_lock;
|
|
|
|
mp_thread_mutex_init(&self->mutex);
|
|
|
|
self->locked = false;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t thread_lock_acquire(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
bool wait = true;
|
|
|
|
if (n_args > 1) {
|
|
|
|
wait = mp_obj_get_int(args[1]);
|
|
|
|
// TODO support timeout arg
|
|
|
|
}
|
2017-02-05 18:47:20 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2016-04-25 07:21:48 -04:00
|
|
|
int ret = mp_thread_mutex_lock(&self->mutex, wait);
|
2017-02-05 18:47:20 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2016-04-25 07:21:48 -04:00
|
|
|
if (ret == 0) {
|
|
|
|
return mp_const_false;
|
|
|
|
} else if (ret == 1) {
|
|
|
|
self->locked = true;
|
|
|
|
return mp_const_true;
|
|
|
|
} else {
|
2016-10-06 22:31:59 -04:00
|
|
|
mp_raise_OSError(-ret);
|
2016-04-25 07:21:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thread_lock_acquire_obj, 1, 3, thread_lock_acquire);
|
|
|
|
|
|
|
|
STATIC mp_obj_t thread_lock_release(mp_obj_t self_in) {
|
|
|
|
mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(self_in);
|
2017-06-14 00:43:50 -04:00
|
|
|
if (!self->locked) {
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NULL);
|
|
|
|
}
|
2016-04-25 07:21:48 -04:00
|
|
|
self->locked = false;
|
2017-02-05 18:47:20 -05:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2016-04-25 07:21:48 -04:00
|
|
|
mp_thread_mutex_unlock(&self->mutex);
|
2017-02-05 18:47:20 -05:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2016-04-25 07:21:48 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(thread_lock_release_obj, thread_lock_release);
|
|
|
|
|
|
|
|
STATIC mp_obj_t thread_lock_locked(mp_obj_t self_in) {
|
|
|
|
mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_bool(self->locked);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(thread_lock_locked_obj, thread_lock_locked);
|
|
|
|
|
2016-04-25 07:33:53 -04:00
|
|
|
STATIC mp_obj_t thread_lock___exit__(size_t n_args, const mp_obj_t *args) {
|
2016-04-25 16:58:22 -04:00
|
|
|
(void)n_args; // unused
|
2016-04-25 07:33:53 -04:00
|
|
|
return thread_lock_release(args[0]);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thread_lock___exit___obj, 4, 4, thread_lock___exit__);
|
|
|
|
|
2016-04-25 07:21:48 -04:00
|
|
|
STATIC const mp_rom_map_elem_t thread_lock_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_acquire), MP_ROM_PTR(&thread_lock_acquire_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_release), MP_ROM_PTR(&thread_lock_release_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_locked), MP_ROM_PTR(&thread_lock_locked_obj) },
|
2016-04-25 07:33:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&thread_lock_acquire_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&thread_lock___exit___obj) },
|
2016-04-25 07:21:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(thread_lock_locals_dict, thread_lock_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_thread_lock = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_lock,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&thread_lock_locals_dict,
|
2016-04-25 07:21:48 -04:00
|
|
|
};
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
/****************************************************************/
|
|
|
|
// _thread module
|
|
|
|
|
2016-04-25 05:02:47 -04:00
|
|
|
STATIC size_t thread_stack_size = 0;
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
STATIC mp_obj_t mod_thread_get_ident(void) {
|
|
|
|
return mp_obj_new_int_from_uint((uintptr_t)mp_thread_get_state());
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_get_ident_obj, mod_thread_get_ident);
|
|
|
|
|
2016-04-25 05:02:47 -04:00
|
|
|
STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_t ret = mp_obj_new_int_from_uint(thread_stack_size);
|
|
|
|
if (n_args == 0) {
|
|
|
|
thread_stack_size = 0;
|
|
|
|
} else {
|
|
|
|
thread_stack_size = mp_obj_get_int(args[0]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_stack_size_obj, 0, 1, mod_thread_stack_size);
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
typedef struct _thread_entry_args_t {
|
2017-02-27 07:56:46 -05:00
|
|
|
mp_obj_dict_t *dict_locals;
|
|
|
|
mp_obj_dict_t *dict_globals;
|
2016-05-30 11:56:51 -04:00
|
|
|
size_t stack_size;
|
2016-04-22 18:52:33 -04:00
|
|
|
mp_obj_t fun;
|
|
|
|
size_t n_args;
|
|
|
|
size_t n_kw;
|
2016-05-04 05:51:01 -04:00
|
|
|
mp_obj_t args[];
|
2016-04-22 18:52:33 -04:00
|
|
|
} thread_entry_args_t;
|
|
|
|
|
|
|
|
STATIC void *thread_entry(void *args_in) {
|
2016-05-26 06:42:53 -04:00
|
|
|
// Execution begins here for a new thread. We do not have the GIL.
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
thread_entry_args_t *args = (thread_entry_args_t *)args_in;
|
2016-04-22 18:52:33 -04:00
|
|
|
|
|
|
|
mp_state_thread_t ts;
|
|
|
|
mp_thread_set_state(&ts);
|
|
|
|
|
|
|
|
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
|
2016-05-30 11:56:51 -04:00
|
|
|
mp_stack_set_limit(args->stack_size);
|
2016-04-22 18:52:33 -04:00
|
|
|
|
py: Introduce a Python stack for scoped allocation.
This patch introduces the MICROPY_ENABLE_PYSTACK option (disabled by
default) which enables a "Python stack" that allows to allocate and free
memory in a scoped, or Last-In-First-Out (LIFO) way, similar to alloca().
A new memory allocation API is introduced along with this Py-stack. It
includes both "local" and "nonlocal" LIFO allocation. Local allocation is
intended to be equivalent to using alloca(), whereby the same function must
free the memory. Nonlocal allocation is where another function may free
the memory, so long as it's still LIFO.
Follow-up patches will convert all uses of alloca() and VLA to the new
scoped allocation API. The old behaviour (using alloca()) will still be
available, but when MICROPY_ENABLE_PYSTACK is enabled then alloca() is no
longer required or used.
The benefits of enabling this option are (or will be once subsequent
patches are made to convert alloca()/VLA):
- Toolchains without alloca() can use this feature to obtain correct and
efficient scoped memory allocation (compared to using the heap instead
of alloca(), which is slower).
- Even if alloca() is available, enabling the Py-stack gives slightly more
efficient use of stack space when calling nested Python functions, due to
the way that compilers implement alloca().
- Enabling the Py-stack with the stackless mode allows for even more
efficient stack usage, as well as retaining high performance (because the
heap is no longer used to build and destroy stackless code states).
- With Py-stack and stackless enabled, Python-calling-Python is no longer
recursive in the C mp_execute_bytecode function.
The micropython.pystack_use() function is included to measure usage of the
Python stack.
2017-11-26 07:28:40 -05:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
// TODO threading and pystack is not fully supported, for now just make a small stack
|
|
|
|
mp_obj_t mini_pystack[128];
|
|
|
|
mp_pystack_init(mini_pystack, &mini_pystack[128]);
|
|
|
|
#endif
|
|
|
|
|
2017-02-27 07:56:46 -05:00
|
|
|
// set locals and globals from the calling context
|
|
|
|
mp_locals_set(args->dict_locals);
|
|
|
|
mp_globals_set(args->dict_globals);
|
|
|
|
|
2016-05-26 06:42:53 -04:00
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
|
2016-05-04 05:52:19 -04:00
|
|
|
// signal that we are set up and running
|
|
|
|
mp_thread_start();
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
// TODO set more thread-specific state here:
|
|
|
|
// mp_pending_exception? (root pointer)
|
|
|
|
// cur_exception (root pointer)
|
|
|
|
|
|
|
|
DEBUG_printf("[thread] start ts=%p args=%p stack=%p\n", &ts, &args, MP_STATE_THREAD(stack_top));
|
|
|
|
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_call_function_n_kw(args->fun, args->n_args, args->n_kw, args->args);
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
// uncaught exception
|
|
|
|
// check for SystemExit
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_base_t *exc = (mp_obj_base_t *)nlr.ret_val;
|
2016-04-23 08:24:44 -04:00
|
|
|
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
|
2016-04-22 18:52:33 -04:00
|
|
|
// swallow exception silently
|
|
|
|
} else {
|
|
|
|
// print exception out
|
2017-09-24 21:15:48 -04:00
|
|
|
mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in thread started by ");
|
|
|
|
mp_obj_print_helper(MICROPY_ERROR_PRINTER, args->fun, PRINT_REPR);
|
|
|
|
mp_printf(MICROPY_ERROR_PRINTER, "\n");
|
|
|
|
mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(exc));
|
2016-04-22 18:52:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_printf("[thread] finish ts=%p\n", &ts);
|
|
|
|
|
2016-05-04 05:52:19 -04:00
|
|
|
// signal that we are finished
|
|
|
|
mp_thread_finish();
|
|
|
|
|
2016-05-26 06:42:53 -04:00
|
|
|
MP_THREAD_GIL_EXIT();
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args) {
|
2016-05-04 05:51:01 -04:00
|
|
|
// This structure holds the Python function and arguments for thread entry.
|
|
|
|
// We copy all arguments into this structure to keep ownership of them.
|
|
|
|
// We must be very careful about root pointers because this pointer may
|
|
|
|
// disappear from our address space before the thread is created.
|
|
|
|
thread_entry_args_t *th_args;
|
|
|
|
|
|
|
|
// get positional arguments
|
2017-03-25 04:35:08 -04:00
|
|
|
size_t pos_args_len;
|
2016-04-22 18:52:33 -04:00
|
|
|
mp_obj_t *pos_args_items;
|
|
|
|
mp_obj_get_array(args[1], &pos_args_len, &pos_args_items);
|
2016-05-04 05:51:01 -04:00
|
|
|
|
|
|
|
// check for keyword arguments
|
2016-04-22 18:52:33 -04:00
|
|
|
if (n_args == 2) {
|
|
|
|
// just position arguments
|
2016-05-04 05:51:01 -04:00
|
|
|
th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len);
|
2016-04-22 18:52:33 -04:00
|
|
|
th_args->n_kw = 0;
|
|
|
|
} else {
|
|
|
|
// positional and keyword arguments
|
|
|
|
if (mp_obj_get_type(args[2]) != &mp_type_dict) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("expecting a dict for keyword args"));
|
2016-04-22 18:52:33 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_map_t *map = &((mp_obj_dict_t *)MP_OBJ_TO_PTR(args[2]))->map;
|
2016-05-04 05:51:01 -04:00
|
|
|
th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len + 2 * map->used);
|
2016-04-22 18:52:33 -04:00
|
|
|
th_args->n_kw = map->used;
|
2016-05-04 05:51:01 -04:00
|
|
|
// copy across the keyword arguments
|
2016-04-22 18:52:33 -04:00
|
|
|
for (size_t i = 0, n = pos_args_len; i < map->alloc; ++i) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_map_slot_is_filled(map, i)) {
|
2016-05-04 05:51:01 -04:00
|
|
|
th_args->args[n++] = map->table[i].key;
|
|
|
|
th_args->args[n++] = map->table[i].value;
|
2016-04-22 18:52:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 05:51:01 -04:00
|
|
|
|
2020-01-24 16:23:44 -05:00
|
|
|
// copy across the positional arguments
|
2016-05-04 05:51:01 -04:00
|
|
|
th_args->n_args = pos_args_len;
|
|
|
|
memcpy(th_args->args, pos_args_items, pos_args_len * sizeof(mp_obj_t));
|
|
|
|
|
2017-02-27 07:56:46 -05:00
|
|
|
// pass our locals and globals into the new thread
|
|
|
|
th_args->dict_locals = mp_locals_get();
|
|
|
|
th_args->dict_globals = mp_globals_get();
|
|
|
|
|
2016-05-30 11:56:51 -04:00
|
|
|
// set the stack size to use
|
|
|
|
th_args->stack_size = thread_stack_size;
|
|
|
|
|
2016-05-04 05:51:01 -04:00
|
|
|
// set the function for thread entry
|
|
|
|
th_args->fun = args[0];
|
|
|
|
|
|
|
|
// spawn the thread!
|
2016-05-30 11:56:51 -04:00
|
|
|
mp_thread_create(thread_entry, th_args, &th_args->stack_size);
|
2016-05-04 05:51:01 -04:00
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread);
|
|
|
|
|
2016-04-25 05:15:21 -04:00
|
|
|
STATIC mp_obj_t mod_thread_exit(void) {
|
2020-02-10 21:17:41 -05:00
|
|
|
mp_raise_type(&mp_type_SystemExit);
|
2016-04-25 05:15:21 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_exit_obj, mod_thread_exit);
|
|
|
|
|
2016-04-25 07:21:48 -04:00
|
|
|
STATIC mp_obj_t mod_thread_allocate_lock(void) {
|
|
|
|
return MP_OBJ_FROM_PTR(mp_obj_new_thread_lock());
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_allocate_lock_obj, mod_thread_allocate_lock);
|
|
|
|
|
2016-04-22 18:52:33 -04:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__thread) },
|
2016-04-25 07:21:48 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_LockType), MP_ROM_PTR(&mp_type_thread_lock) },
|
2016-04-22 18:52:33 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get_ident), MP_ROM_PTR(&mod_thread_get_ident_obj) },
|
2016-04-25 05:02:47 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stack_size), MP_ROM_PTR(&mod_thread_stack_size_obj) },
|
2016-04-22 18:52:33 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_start_new_thread), MP_ROM_PTR(&mod_thread_start_new_thread_obj) },
|
2016-04-25 05:15:21 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mod_thread_exit_obj) },
|
2016-04-25 07:21:48 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_allocate_lock), MP_ROM_PTR(&mod_thread_allocate_lock_obj) },
|
2016-04-22 18:52:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_thread_globals, mp_module_thread_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_thread = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_thread_globals,
|
2016-04-22 18:52:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_THREAD
|