Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2014 Damien P. George |
| 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 | */ |
Alexander Steffen | 299bc62 | 2017-06-29 23:14:58 +0200 | [diff] [blame] | 26 | #ifndef MICROPY_INCLUDED_PY_MPSTATE_H |
| 27 | #define MICROPY_INCLUDED_PY_MPSTATE_H |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 28 | |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #include "py/mpconfig.h" |
Damien George | c93d9ca | 2016-04-25 15:28:57 +0000 | [diff] [blame] | 32 | #include "py/mpthread.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 33 | #include "py/misc.h" |
| 34 | #include "py/nlr.h" |
| 35 | #include "py/obj.h" |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 36 | #include "py/objlist.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 37 | #include "py/objexcept.h" |
| 38 | |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 39 | // This file contains structures defining the state of the MicroPython |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 40 | // memory system, runtime and virtual machine. The state is a global |
| 41 | // variable, but in the future it is hoped that the state can become local. |
| 42 | |
Jim Mussared | 7d2ee8a | 2023-06-05 22:38:36 +1000 | [diff] [blame] | 43 | #if MICROPY_PY_SYS_ATTR_DELEGATION |
| 44 | // Must be kept in sync with sys_mutable_keys in modsys.c. |
Damien George | bc18155 | 2021-07-27 00:39:04 +1000 | [diff] [blame] | 45 | enum { |
Jim Mussared | 5e50975 | 2023-06-05 16:52:29 +1000 | [diff] [blame] | 46 | #if MICROPY_PY_SYS_PATH |
| 47 | MP_SYS_MUTABLE_PATH, |
| 48 | #endif |
Damien George | ac22931 | 2021-07-27 00:43:35 +1000 | [diff] [blame] | 49 | #if MICROPY_PY_SYS_PS1_PS2 |
| 50 | MP_SYS_MUTABLE_PS1, |
| 51 | MP_SYS_MUTABLE_PS2, |
| 52 | #endif |
Damien George | cac939d | 2021-07-27 00:41:27 +1000 | [diff] [blame] | 53 | #if MICROPY_PY_SYS_TRACEBACKLIMIT |
| 54 | MP_SYS_MUTABLE_TRACEBACKLIMIT, |
| 55 | #endif |
Damien George | bc18155 | 2021-07-27 00:39:04 +1000 | [diff] [blame] | 56 | MP_SYS_MUTABLE_NUM, |
| 57 | }; |
Jim Mussared | 7d2ee8a | 2023-06-05 22:38:36 +1000 | [diff] [blame] | 58 | #endif // MICROPY_PY_SYS_ATTR_DELEGATION |
Damien George | bc18155 | 2021-07-27 00:39:04 +1000 | [diff] [blame] | 59 | |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 60 | // This structure contains dynamic configuration for the compiler. |
| 61 | #if MICROPY_DYNAMIC_COMPILER |
| 62 | typedef struct mp_dynamic_compiler_t { |
| 63 | uint8_t small_int_bits; // must be <= host small_int_bits |
Damien George | d9d92f2 | 2019-03-09 10:59:25 +1100 | [diff] [blame] | 64 | uint8_t native_arch; |
Damien George | b596638 | 2019-09-18 13:45:20 +1000 | [diff] [blame] | 65 | uint8_t nlr_buf_num_regs; |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 66 | } mp_dynamic_compiler_t; |
| 67 | extern mp_dynamic_compiler_t mp_dynamic_compiler; |
| 68 | #endif |
| 69 | |
Damien George | 6e74d24 | 2017-02-16 18:05:06 +1100 | [diff] [blame] | 70 | // These are the values for sched_state |
| 71 | #define MP_SCHED_IDLE (1) |
| 72 | #define MP_SCHED_LOCKED (-1) |
| 73 | #define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM |
| 74 | |
| 75 | typedef struct _mp_sched_item_t { |
| 76 | mp_obj_t func; |
| 77 | mp_obj_t arg; |
| 78 | } mp_sched_item_t; |
| 79 | |
Ayke van Laethem | bcc827d | 2018-01-24 02:09:58 +0100 | [diff] [blame] | 80 | // This structure holds information about a single contiguous area of |
| 81 | // memory reserved for the memory manager. |
| 82 | typedef struct _mp_state_mem_area_t { |
| 83 | #if MICROPY_GC_SPLIT_HEAP |
| 84 | struct _mp_state_mem_area_t *next; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
| 87 | byte *gc_alloc_table_start; |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 88 | size_t gc_alloc_table_byte_len; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 89 | #if MICROPY_ENABLE_FINALISER |
| 90 | byte *gc_finaliser_table_start; |
| 91 | #endif |
Damien George | 94fe6e5 | 2015-11-27 13:07:48 +0000 | [diff] [blame] | 92 | byte *gc_pool_start; |
| 93 | byte *gc_pool_end; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 94 | |
Ayke van Laethem | bcc827d | 2018-01-24 02:09:58 +0100 | [diff] [blame] | 95 | size_t gc_last_free_atb_index; |
Damien Tournoud | 2dcd745 | 2022-12-15 14:09:19 -0800 | [diff] [blame] | 96 | size_t gc_last_used_block; // The block ID of the highest block allocated in the area |
Ayke van Laethem | bcc827d | 2018-01-24 02:09:58 +0100 | [diff] [blame] | 97 | } mp_state_mem_area_t; |
| 98 | |
Ayke van Laethem | bcc827d | 2018-01-24 02:09:58 +0100 | [diff] [blame] | 99 | // This structure hold information about the memory allocation system. |
| 100 | typedef struct _mp_state_mem_t { |
| 101 | #if MICROPY_MEM_STATS |
| 102 | size_t total_bytes_allocated; |
| 103 | size_t current_bytes_allocated; |
| 104 | size_t peak_bytes_allocated; |
| 105 | #endif |
| 106 | |
| 107 | mp_state_mem_area_t area; |
| 108 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 109 | int gc_stack_overflow; |
Rob Knegjens | 4a48531 | 2022-04-12 21:26:16 -0700 | [diff] [blame] | 110 | MICROPY_GC_STACK_ENTRY_TYPE gc_block_stack[MICROPY_ALLOC_GC_STACK_SIZE]; |
| 111 | #if MICROPY_GC_SPLIT_HEAP |
| 112 | // Array that tracks the area for each block on gc_block_stack. |
| 113 | mp_state_mem_area_t *gc_area_stack[MICROPY_ALLOC_GC_STACK_SIZE]; |
| 114 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 115 | |
| 116 | // This variable controls auto garbage collection. If set to 0 then the |
| 117 | // GC won't automatically run when gc_alloc can't find enough blocks. But |
| 118 | // you can still allocate/free memory and also explicitly call gc_collect. |
| 119 | uint16_t gc_auto_collect_enabled; |
| 120 | |
Paul Sokolovsky | 93e353e | 2016-07-21 00:37:30 +0300 | [diff] [blame] | 121 | #if MICROPY_GC_ALLOC_THRESHOLD |
| 122 | size_t gc_alloc_amount; |
| 123 | size_t gc_alloc_threshold; |
| 124 | #endif |
| 125 | |
Ayke van Laethem | bcc827d | 2018-01-24 02:09:58 +0100 | [diff] [blame] | 126 | #if MICROPY_GC_SPLIT_HEAP |
| 127 | mp_state_mem_area_t *gc_last_free_area; |
| 128 | #endif |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 129 | |
| 130 | #if MICROPY_PY_GC_COLLECT_RETVAL |
Damien George | d977d26 | 2015-12-16 20:09:11 -0500 | [diff] [blame] | 131 | size_t gc_collected; |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 132 | #endif |
Damien George | c93d9ca | 2016-04-25 15:28:57 +0000 | [diff] [blame] | 133 | |
David Lechner | ccc18f0 | 2020-01-22 11:19:37 -0600 | [diff] [blame] | 134 | #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL |
Damien George | c93d9ca | 2016-04-25 15:28:57 +0000 | [diff] [blame] | 135 | // This is a global mutex used to make the GC thread-safe. |
| 136 | mp_thread_mutex_t gc_mutex; |
| 137 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 138 | } mp_state_mem_t; |
| 139 | |
| 140 | // This structure hold runtime and VM information. It includes a section |
| 141 | // which contains root pointers that must be scanned by the GC. |
| 142 | typedef struct _mp_state_vm_t { |
Damien George | 749b161 | 2018-05-12 22:09:34 +1000 | [diff] [blame] | 143 | // |
| 144 | // CONTINUE ROOT POINTER SECTION |
| 145 | // This must start at the start of this structure and follows |
| 146 | // the state in the mp_state_thread_t structure, continuing |
| 147 | // the root pointer section from there. |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 148 | // |
| 149 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 150 | qstr_pool_t *last_pool; |
| 151 | |
Damien George | fca5701 | 2022-05-04 12:12:11 +1000 | [diff] [blame] | 152 | #if MICROPY_TRACKED_ALLOC |
| 153 | struct _m_tracked_node_t *m_tracked_head; |
| 154 | #endif |
| 155 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 156 | // non-heap memory for creating an exception if we can't allocate RAM |
| 157 | mp_obj_exception_t mp_emergency_exception_obj; |
| 158 | |
| 159 | // memory for exception arguments if we can't allocate RAM |
| 160 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF |
| 161 | #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0 |
Damien George | ee86de1 | 2017-04-10 16:02:56 +1000 | [diff] [blame] | 162 | // statically allocated buf (needs to be aligned to mp_obj_t) |
| 163 | mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)]; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 164 | #else |
| 165 | // dynamically allocated buf |
| 166 | byte *mp_emergency_exception_buf; |
| 167 | #endif |
| 168 | #endif |
| 169 | |
Damien George | 7f1da0a | 2016-12-15 13:00:19 +1100 | [diff] [blame] | 170 | #if MICROPY_KBD_EXCEPTION |
| 171 | // exception object of type KeyboardInterrupt |
| 172 | mp_obj_exception_t mp_kbd_exception; |
| 173 | #endif |
| 174 | |
Paul Sokolovsky | 1a1d11f | 2015-12-05 00:09:10 +0200 | [diff] [blame] | 175 | // dictionary with loaded modules (may be exposed as sys.modules) |
| 176 | mp_obj_dict_t mp_loaded_modules_dict; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 177 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 178 | // dictionary for the __main__ module |
| 179 | mp_obj_dict_t dict_main; |
| 180 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 181 | // dictionary for overridden builtins |
| 182 | #if MICROPY_CAN_OVERRIDE_BUILTINS |
| 183 | mp_obj_dict_t *mp_module_builtins_override_dict; |
| 184 | #endif |
| 185 | |
David Lechner | fc3d7ae | 2022-07-01 12:29:08 -0500 | [diff] [blame] | 186 | // Include any root pointers registered with MP_REGISTER_ROOT_POINTER(). |
| 187 | #ifndef NO_QSTR |
| 188 | // Only include root pointer definitions when not doing qstr extraction, because |
| 189 | // the qstr extraction stage also generates the root pointers header file. |
| 190 | #include "genhdr/root_pointers.h" |
| 191 | #endif |
| 192 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 193 | // |
| 194 | // END ROOT POINTER SECTION |
| 195 | //////////////////////////////////////////////////////////// |
| 196 | |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 197 | // pointer and sizes to store interned string data |
| 198 | // (qstr_last_chunk can be root pointer but is also stored in qstr pool) |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 199 | char *qstr_last_chunk; |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 200 | size_t qstr_last_alloc; |
| 201 | size_t qstr_last_used; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 202 | |
David Lechner | edbb73a | 2020-01-22 11:19:37 -0600 | [diff] [blame] | 203 | #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 204 | // This is a global mutex used to make qstr interning thread-safe. |
| 205 | mp_thread_mutex_t qstr_mutex; |
| 206 | #endif |
| 207 | |
Damien George | 3f420c0 | 2018-04-04 14:24:03 +1000 | [diff] [blame] | 208 | #if MICROPY_ENABLE_COMPILER |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 209 | mp_uint_t mp_optimise_value; |
Damien George | af20c2e | 2019-08-23 11:20:50 +1000 | [diff] [blame] | 210 | #if MICROPY_EMIT_NATIVE |
| 211 | uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx |
| 212 | #endif |
Damien George | 3f420c0 | 2018-04-04 14:24:03 +1000 | [diff] [blame] | 213 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 214 | |
| 215 | // size of the emergency exception buf, if it's dynamically allocated |
| 216 | #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0 |
| 217 | mp_int_t mp_emergency_exception_buf_size; |
| 218 | #endif |
Damien George | 4cec63a | 2016-05-26 10:42:53 +0000 | [diff] [blame] | 219 | |
Damien George | 749b161 | 2018-05-12 22:09:34 +1000 | [diff] [blame] | 220 | #if MICROPY_ENABLE_SCHEDULER |
| 221 | volatile int16_t sched_state; |
Damien George | 75506e4 | 2022-03-23 17:13:03 +1100 | [diff] [blame] | 222 | |
| 223 | #if MICROPY_SCHEDULER_STATIC_NODES |
| 224 | // These will usually point to statically allocated memory. They are not |
| 225 | // traced by the GC. They are assumed to be zero'd out before mp_init() is |
| 226 | // called (usually because this struct lives in the BSS). |
| 227 | struct _mp_sched_node_t *sched_head; |
| 228 | struct _mp_sched_node_t *sched_tail; |
| 229 | #endif |
| 230 | |
| 231 | // These index sched_queue. |
Andrew Leech | 8977c7e | 2019-03-21 11:52:10 +1100 | [diff] [blame] | 232 | uint8_t sched_len; |
| 233 | uint8_t sched_idx; |
Damien George | 749b161 | 2018-05-12 22:09:34 +1000 | [diff] [blame] | 234 | #endif |
| 235 | |
Damien George | d54208a | 2022-12-16 17:31:21 +1100 | [diff] [blame] | 236 | #if MICROPY_ENABLE_VM_ABORT |
| 237 | bool vm_abort; |
| 238 | nlr_buf_t *nlr_abort; |
| 239 | #endif |
| 240 | |
Damien George | 4cec63a | 2016-05-26 10:42:53 +0000 | [diff] [blame] | 241 | #if MICROPY_PY_THREAD_GIL |
| 242 | // This is a global mutex used to make the VM/runtime thread-safe. |
| 243 | mp_thread_mutex_t gil_mutex; |
| 244 | #endif |
Jim Mussared | 11ef8f2 | 2021-08-18 14:52:48 +1000 | [diff] [blame] | 245 | |
| 246 | #if MICROPY_OPT_MAP_LOOKUP_CACHE |
| 247 | // See mp_map_lookup. |
| 248 | uint8_t map_lookup_cache[MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE]; |
| 249 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 250 | } mp_state_vm_t; |
| 251 | |
Jim Mussared | 3883f29 | 2023-10-17 12:27:49 +1100 | [diff] [blame] | 252 | // This structure holds state that is specific to a given thread. Everything |
| 253 | // in this structure is scanned for root pointers. Anything added to this |
| 254 | // structure must have corresponding initialisation added to thread_entry (in |
| 255 | // py/modthread.c). |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 256 | typedef struct _mp_state_thread_t { |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 257 | // Stack top at the start of program |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 258 | char *stack_top; |
| 259 | |
| 260 | #if MICROPY_STACK_CHECK |
| 261 | size_t stack_limit; |
| 262 | #endif |
Damien George | 02d830c | 2017-11-26 23:28:40 +1100 | [diff] [blame] | 263 | |
| 264 | #if MICROPY_ENABLE_PYSTACK |
| 265 | uint8_t *pystack_start; |
| 266 | uint8_t *pystack_end; |
| 267 | uint8_t *pystack_cur; |
| 268 | #endif |
Damien George | 749b161 | 2018-05-12 22:09:34 +1000 | [diff] [blame] | 269 | |
Damien George | b6b39bf | 2021-05-04 23:56:43 +1000 | [diff] [blame] | 270 | // Locking of the GC is done per thread. |
| 271 | uint16_t gc_lock_depth; |
| 272 | |
Damien George | 749b161 | 2018-05-12 22:09:34 +1000 | [diff] [blame] | 273 | //////////////////////////////////////////////////////////// |
| 274 | // START ROOT POINTER SECTION |
| 275 | // Everything that needs GC scanning must start here, and |
| 276 | // is followed by state in the mp_state_vm_t structure. |
| 277 | // |
| 278 | |
| 279 | mp_obj_dict_t *dict_locals; |
| 280 | mp_obj_dict_t *dict_globals; |
| 281 | |
| 282 | nlr_buf_t *nlr_top; |
Damien George | 2757acf | 2023-05-09 11:03:04 +1000 | [diff] [blame] | 283 | nlr_jump_callback_node_t *nlr_jump_callback_top; |
Milan Rossa | 310b3d1 | 2019-08-14 16:09:36 +0200 | [diff] [blame] | 284 | |
David Lechner | ca920f7 | 2021-05-10 21:53:22 -0500 | [diff] [blame] | 285 | // pending exception object (MP_OBJ_NULL if not pending) |
| 286 | volatile mp_obj_t mp_pending_exception; |
| 287 | |
Damien George | bb00125 | 2021-06-29 17:34:34 +1000 | [diff] [blame] | 288 | // If MP_OBJ_STOP_ITERATION is propagated then this holds its argument. |
| 289 | mp_obj_t stop_iteration_arg; |
| 290 | |
Milan Rossa | 310b3d1 | 2019-08-14 16:09:36 +0200 | [diff] [blame] | 291 | #if MICROPY_PY_SYS_SETTRACE |
| 292 | mp_obj_t prof_trace_callback; |
| 293 | bool prof_callback_is_executing; |
| 294 | struct _mp_code_state_t *current_code_state; |
| 295 | #endif |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 296 | } mp_state_thread_t; |
| 297 | |
Damien George | 05fe66f | 2017-02-27 23:56:46 +1100 | [diff] [blame] | 298 | // This structure combines the above 3 structures. |
| 299 | // The order of the entries are important for root pointer scanning in the GC to work. |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 300 | typedef struct _mp_state_ctx_t { |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 301 | mp_state_thread_t thread; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 302 | mp_state_vm_t vm; |
| 303 | mp_state_mem_t mem; |
| 304 | } mp_state_ctx_t; |
| 305 | |
| 306 | extern mp_state_ctx_t mp_state_ctx; |
| 307 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 308 | #define MP_STATE_VM(x) (mp_state_ctx.vm.x) |
| 309 | #define MP_STATE_MEM(x) (mp_state_ctx.mem.x) |
David Lechner | 259d9b6 | 2021-05-10 22:18:17 -0500 | [diff] [blame] | 310 | #define MP_STATE_MAIN_THREAD(x) (mp_state_ctx.thread.x) |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 311 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 312 | #if MICROPY_PY_THREAD |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 313 | #define MP_STATE_THREAD(x) (mp_thread_get_state()->x) |
Damien George | 5d4bfce | 2022-12-16 17:30:26 +1100 | [diff] [blame] | 314 | #define mp_thread_is_main_thread() (mp_thread_get_state() == &mp_state_ctx.thread) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 315 | #else |
David Lechner | 259d9b6 | 2021-05-10 22:18:17 -0500 | [diff] [blame] | 316 | #define MP_STATE_THREAD(x) MP_STATE_MAIN_THREAD(x) |
Damien George | 5d4bfce | 2022-12-16 17:30:26 +1100 | [diff] [blame] | 317 | #define mp_thread_is_main_thread() (true) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 318 | #endif |
Damien George | 330165a | 2016-04-22 22:44:56 +0000 | [diff] [blame] | 319 | |
Alexander Steffen | 299bc62 | 2017-06-29 23:14:58 +0200 | [diff] [blame] | 320 | #endif // MICROPY_INCLUDED_PY_MPSTATE_H |