Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "py/runtime.h" |
| 31 | #include "py/stackctrl.h" |
| 32 | |
| 33 | #if MICROPY_PY_THREAD |
| 34 | |
| 35 | #include "py/mpthread.h" |
| 36 | |
| 37 | #if 0 // print debugging info |
| 38 | #define DEBUG_PRINT (1) |
| 39 | #define DEBUG_printf DEBUG_printf |
| 40 | #else // don't print debugging info |
| 41 | #define DEBUG_PRINT (0) |
| 42 | #define DEBUG_printf(...) (void)0 |
| 43 | #endif |
| 44 | |
| 45 | /****************************************************************/ |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 46 | // Lock object |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 47 | // Note: with the GIL enabled we can easily synthesise a lock object |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 48 | |
| 49 | STATIC const mp_obj_type_t mp_type_thread_lock; |
| 50 | |
| 51 | typedef struct _mp_obj_thread_lock_t { |
| 52 | mp_obj_base_t base; |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 53 | #if !MICROPY_PY_THREAD_GIL |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 54 | mp_thread_mutex_t mutex; |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 55 | #endif |
| 56 | volatile bool locked; |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 57 | } mp_obj_thread_lock_t; |
| 58 | |
| 59 | STATIC mp_obj_thread_lock_t *mp_obj_new_thread_lock(void) { |
| 60 | mp_obj_thread_lock_t *self = m_new_obj(mp_obj_thread_lock_t); |
| 61 | self->base.type = &mp_type_thread_lock; |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 62 | #if !MICROPY_PY_THREAD_GIL |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 63 | mp_thread_mutex_init(&self->mutex); |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 64 | #endif |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 65 | self->locked = false; |
| 66 | return self; |
| 67 | } |
| 68 | |
| 69 | STATIC mp_obj_t thread_lock_acquire(size_t n_args, const mp_obj_t *args) { |
| 70 | mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(args[0]); |
| 71 | bool wait = true; |
| 72 | if (n_args > 1) { |
| 73 | wait = mp_obj_get_int(args[1]); |
| 74 | // TODO support timeout arg |
| 75 | } |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 76 | #if MICROPY_PY_THREAD_GIL |
| 77 | if (self->locked) { |
| 78 | if (!wait) { |
| 79 | return mp_const_false; |
| 80 | } |
| 81 | do { |
| 82 | MP_THREAD_GIL_EXIT(); |
| 83 | MP_THREAD_GIL_ENTER(); |
| 84 | } while (self->locked); |
| 85 | } |
| 86 | self->locked = true; |
| 87 | return mp_const_true; |
| 88 | #else |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 89 | int ret = mp_thread_mutex_lock(&self->mutex, wait); |
| 90 | if (ret == 0) { |
| 91 | return mp_const_false; |
| 92 | } else if (ret == 1) { |
| 93 | self->locked = true; |
| 94 | return mp_const_true; |
| 95 | } else { |
| 96 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-ret))); |
| 97 | } |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 98 | #endif |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 99 | } |
| 100 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thread_lock_acquire_obj, 1, 3, thread_lock_acquire); |
| 101 | |
| 102 | STATIC mp_obj_t thread_lock_release(mp_obj_t self_in) { |
| 103 | mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(self_in); |
| 104 | // TODO check if already unlocked |
| 105 | self->locked = false; |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 106 | #if !MICROPY_PY_THREAD_GIL |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 107 | mp_thread_mutex_unlock(&self->mutex); |
Damien George | c567afc | 2016-05-26 11:24:52 +0000 | [diff] [blame] | 108 | #endif |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 109 | return mp_const_none; |
| 110 | } |
| 111 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(thread_lock_release_obj, thread_lock_release); |
| 112 | |
| 113 | STATIC mp_obj_t thread_lock_locked(mp_obj_t self_in) { |
| 114 | mp_obj_thread_lock_t *self = MP_OBJ_TO_PTR(self_in); |
| 115 | return mp_obj_new_bool(self->locked); |
| 116 | } |
| 117 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(thread_lock_locked_obj, thread_lock_locked); |
| 118 | |
Damien George | 34fc006 | 2016-04-25 11:33:53 +0000 | [diff] [blame] | 119 | STATIC mp_obj_t thread_lock___exit__(size_t n_args, const mp_obj_t *args) { |
Damien George | 7f4658a | 2016-04-25 20:58:22 +0000 | [diff] [blame] | 120 | (void)n_args; // unused |
Damien George | 34fc006 | 2016-04-25 11:33:53 +0000 | [diff] [blame] | 121 | return thread_lock_release(args[0]); |
| 122 | } |
| 123 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(thread_lock___exit___obj, 4, 4, thread_lock___exit__); |
| 124 | |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 125 | STATIC const mp_rom_map_elem_t thread_lock_locals_dict_table[] = { |
| 126 | { MP_ROM_QSTR(MP_QSTR_acquire), MP_ROM_PTR(&thread_lock_acquire_obj) }, |
| 127 | { MP_ROM_QSTR(MP_QSTR_release), MP_ROM_PTR(&thread_lock_release_obj) }, |
| 128 | { MP_ROM_QSTR(MP_QSTR_locked), MP_ROM_PTR(&thread_lock_locked_obj) }, |
Damien George | 34fc006 | 2016-04-25 11:33:53 +0000 | [diff] [blame] | 129 | { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&thread_lock_acquire_obj) }, |
| 130 | { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&thread_lock___exit___obj) }, |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | STATIC MP_DEFINE_CONST_DICT(thread_lock_locals_dict, thread_lock_locals_dict_table); |
| 134 | |
| 135 | STATIC const mp_obj_type_t mp_type_thread_lock = { |
| 136 | { &mp_type_type }, |
| 137 | .name = MP_QSTR_lock, |
| 138 | .locals_dict = (mp_obj_dict_t*)&thread_lock_locals_dict, |
| 139 | }; |
| 140 | |
| 141 | /****************************************************************/ |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 142 | // _thread module |
| 143 | |
Damien George | 707f98f | 2016-04-25 09:02:47 +0000 | [diff] [blame] | 144 | STATIC size_t thread_stack_size = 0; |
| 145 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 146 | STATIC mp_obj_t mod_thread_get_ident(void) { |
| 147 | return mp_obj_new_int_from_uint((uintptr_t)mp_thread_get_state()); |
| 148 | } |
| 149 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_get_ident_obj, mod_thread_get_ident); |
| 150 | |
Damien George | 707f98f | 2016-04-25 09:02:47 +0000 | [diff] [blame] | 151 | STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) { |
| 152 | mp_obj_t ret = mp_obj_new_int_from_uint(thread_stack_size); |
| 153 | if (n_args == 0) { |
| 154 | thread_stack_size = 0; |
| 155 | } else { |
| 156 | thread_stack_size = mp_obj_get_int(args[0]); |
| 157 | } |
| 158 | return ret; |
| 159 | } |
| 160 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_stack_size_obj, 0, 1, mod_thread_stack_size); |
| 161 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 162 | typedef struct _thread_entry_args_t { |
Damien George | df95f52 | 2016-05-30 16:56:51 +0100 | [diff] [blame^] | 163 | size_t stack_size; |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 164 | mp_obj_t fun; |
| 165 | size_t n_args; |
| 166 | size_t n_kw; |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 167 | mp_obj_t args[]; |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 168 | } thread_entry_args_t; |
| 169 | |
| 170 | STATIC void *thread_entry(void *args_in) { |
Damien George | 4cec63a | 2016-05-26 10:42:53 +0000 | [diff] [blame] | 171 | // Execution begins here for a new thread. We do not have the GIL. |
| 172 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 173 | thread_entry_args_t *args = (thread_entry_args_t*)args_in; |
| 174 | |
| 175 | mp_state_thread_t ts; |
| 176 | mp_thread_set_state(&ts); |
| 177 | |
| 178 | mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan |
Damien George | df95f52 | 2016-05-30 16:56:51 +0100 | [diff] [blame^] | 179 | mp_stack_set_limit(args->stack_size); |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 180 | |
Damien George | 4cec63a | 2016-05-26 10:42:53 +0000 | [diff] [blame] | 181 | MP_THREAD_GIL_ENTER(); |
| 182 | |
Damien George | 9172c0c | 2016-05-04 09:52:19 +0000 | [diff] [blame] | 183 | // signal that we are set up and running |
| 184 | mp_thread_start(); |
| 185 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 186 | // TODO set more thread-specific state here: |
| 187 | // mp_pending_exception? (root pointer) |
| 188 | // cur_exception (root pointer) |
| 189 | // dict_locals? (root pointer) uPy doesn't make a new locals dict for functions, just for classes, so it's different to CPy |
| 190 | |
| 191 | DEBUG_printf("[thread] start ts=%p args=%p stack=%p\n", &ts, &args, MP_STATE_THREAD(stack_top)); |
| 192 | |
| 193 | nlr_buf_t nlr; |
| 194 | if (nlr_push(&nlr) == 0) { |
| 195 | mp_call_function_n_kw(args->fun, args->n_args, args->n_kw, args->args); |
| 196 | nlr_pop(); |
| 197 | } else { |
| 198 | // uncaught exception |
| 199 | // check for SystemExit |
Damien George | 3eb7a26 | 2016-04-23 12:24:44 +0000 | [diff] [blame] | 200 | mp_obj_base_t *exc = (mp_obj_base_t*)nlr.ret_val; |
| 201 | if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) { |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 202 | // swallow exception silently |
| 203 | } else { |
| 204 | // print exception out |
| 205 | mp_printf(&mp_plat_print, "Unhandled exception in thread started by "); |
| 206 | mp_obj_print_helper(&mp_plat_print, args->fun, PRINT_REPR); |
| 207 | mp_printf(&mp_plat_print, "\n"); |
Damien George | 3eb7a26 | 2016-04-23 12:24:44 +0000 | [diff] [blame] | 208 | mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(exc)); |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | DEBUG_printf("[thread] finish ts=%p\n", &ts); |
| 213 | |
Damien George | 9172c0c | 2016-05-04 09:52:19 +0000 | [diff] [blame] | 214 | // signal that we are finished |
| 215 | mp_thread_finish(); |
| 216 | |
Damien George | 4cec63a | 2016-05-26 10:42:53 +0000 | [diff] [blame] | 217 | MP_THREAD_GIL_EXIT(); |
| 218 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args) { |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 223 | // This structure holds the Python function and arguments for thread entry. |
| 224 | // We copy all arguments into this structure to keep ownership of them. |
| 225 | // We must be very careful about root pointers because this pointer may |
| 226 | // disappear from our address space before the thread is created. |
| 227 | thread_entry_args_t *th_args; |
| 228 | |
| 229 | // get positional arguments |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 230 | mp_uint_t pos_args_len; |
| 231 | mp_obj_t *pos_args_items; |
| 232 | mp_obj_get_array(args[1], &pos_args_len, &pos_args_items); |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 233 | |
| 234 | // check for keyword arguments |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 235 | if (n_args == 2) { |
| 236 | // just position arguments |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 237 | th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len); |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 238 | th_args->n_kw = 0; |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 239 | } else { |
| 240 | // positional and keyword arguments |
| 241 | if (mp_obj_get_type(args[2]) != &mp_type_dict) { |
| 242 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "expecting a dict for keyword args")); |
| 243 | } |
| 244 | mp_map_t *map = &((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[2]))->map; |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 245 | th_args = m_new_obj_var(thread_entry_args_t, mp_obj_t, pos_args_len + 2 * map->used); |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 246 | th_args->n_kw = map->used; |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 247 | // copy across the keyword arguments |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 248 | for (size_t i = 0, n = pos_args_len; i < map->alloc; ++i) { |
| 249 | if (MP_MAP_SLOT_IS_FILLED(map, i)) { |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 250 | th_args->args[n++] = map->table[i].key; |
| 251 | th_args->args[n++] = map->table[i].value; |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 254 | } |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 255 | |
| 256 | // copy agross the positional arguments |
| 257 | th_args->n_args = pos_args_len; |
| 258 | memcpy(th_args->args, pos_args_items, pos_args_len * sizeof(mp_obj_t)); |
| 259 | |
Damien George | df95f52 | 2016-05-30 16:56:51 +0100 | [diff] [blame^] | 260 | // set the stack size to use |
| 261 | th_args->stack_size = thread_stack_size; |
| 262 | |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 263 | // set the function for thread entry |
| 264 | th_args->fun = args[0]; |
| 265 | |
| 266 | // spawn the thread! |
Damien George | df95f52 | 2016-05-30 16:56:51 +0100 | [diff] [blame^] | 267 | mp_thread_create(thread_entry, th_args, &th_args->stack_size); |
Damien George | 722cff5 | 2016-05-04 09:51:01 +0000 | [diff] [blame] | 268 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 269 | return mp_const_none; |
| 270 | } |
| 271 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread); |
| 272 | |
Damien George | 2dacd60 | 2016-04-25 09:15:21 +0000 | [diff] [blame] | 273 | STATIC mp_obj_t mod_thread_exit(void) { |
| 274 | nlr_raise(mp_obj_new_exception(&mp_type_SystemExit)); |
| 275 | } |
| 276 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_exit_obj, mod_thread_exit); |
| 277 | |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 278 | STATIC mp_obj_t mod_thread_allocate_lock(void) { |
| 279 | return MP_OBJ_FROM_PTR(mp_obj_new_thread_lock()); |
| 280 | } |
| 281 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_allocate_lock_obj, mod_thread_allocate_lock); |
| 282 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 283 | STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = { |
| 284 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__thread) }, |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 285 | { MP_ROM_QSTR(MP_QSTR_LockType), MP_ROM_PTR(&mp_type_thread_lock) }, |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 286 | { MP_ROM_QSTR(MP_QSTR_get_ident), MP_ROM_PTR(&mod_thread_get_ident_obj) }, |
Damien George | 707f98f | 2016-04-25 09:02:47 +0000 | [diff] [blame] | 287 | { MP_ROM_QSTR(MP_QSTR_stack_size), MP_ROM_PTR(&mod_thread_stack_size_obj) }, |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 288 | { MP_ROM_QSTR(MP_QSTR_start_new_thread), MP_ROM_PTR(&mod_thread_start_new_thread_obj) }, |
Damien George | 2dacd60 | 2016-04-25 09:15:21 +0000 | [diff] [blame] | 289 | { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mod_thread_exit_obj) }, |
Damien George | 801d1b3 | 2016-04-25 11:21:48 +0000 | [diff] [blame] | 290 | { MP_ROM_QSTR(MP_QSTR_allocate_lock), MP_ROM_PTR(&mod_thread_allocate_lock_obj) }, |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | STATIC MP_DEFINE_CONST_DICT(mp_module_thread_globals, mp_module_thread_globals_table); |
| 294 | |
| 295 | const mp_obj_module_t mp_module_thread = { |
| 296 | .base = { &mp_type_module }, |
| 297 | .name = MP_QSTR__thread, |
| 298 | .globals = (mp_obj_dict_t*)&mp_module_thread_globals, |
| 299 | }; |
| 300 | |
| 301 | #endif // MICROPY_PY_THREAD |