1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2017 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 */
26
27 #include <stdio.h>
28
29 #include "py/runtime.h"
30
31 // Schedules an exception on the main thread (for exceptions "thrown" by async
32 // sources such as interrupts and UNIX signal handlers).
MICROPY_WRAP_MP_SCHED_EXCEPTION(mp_sched_exception)33 void MICROPY_WRAP_MP_SCHED_EXCEPTION(mp_sched_exception)(mp_obj_t exc) {
34 MP_STATE_MAIN_THREAD(mp_pending_exception) = exc;
35 #if MICROPY_ENABLE_SCHEDULER
36 if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
37 MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
38 }
39 #endif
40 }
41
42 #if MICROPY_KBD_EXCEPTION
43 // This function may be called asynchronously at any time so only do the bare minimum.
MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(mp_sched_keyboard_interrupt)44 void MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(mp_sched_keyboard_interrupt)(void) {
45 MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
46 mp_sched_exception(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
47 }
48 #endif
49
50 #if MICROPY_ENABLE_SCHEDULER
51
52 #define IDX_MASK(i) ((i) & (MICROPY_SCHEDULER_DEPTH - 1))
53
54 // This is a macro so it is guaranteed to be inlined in functions like
55 // mp_sched_schedule that may be located in a special memory region.
56 #define mp_sched_full() (mp_sched_num_pending() == MICROPY_SCHEDULER_DEPTH)
57
mp_sched_empty(void)58 static inline bool mp_sched_empty(void) {
59 MP_STATIC_ASSERT(MICROPY_SCHEDULER_DEPTH <= 255); // MICROPY_SCHEDULER_DEPTH must fit in 8 bits
60 MP_STATIC_ASSERT((IDX_MASK(MICROPY_SCHEDULER_DEPTH) == 0)); // MICROPY_SCHEDULER_DEPTH must be a power of 2
61
62 return mp_sched_num_pending() == 0;
63 }
64
65 // A variant of this is inlined in the VM at the pending exception check
mp_handle_pending(bool raise_exc)66 void mp_handle_pending(bool raise_exc) {
67 if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
68 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
69 // Re-check state is still pending now that we're in the atomic section.
70 if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
71 mp_obj_t obj = MP_STATE_THREAD(mp_pending_exception);
72 if (obj != MP_OBJ_NULL) {
73 MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
74 if (!mp_sched_num_pending()) {
75 MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
76 }
77 if (raise_exc) {
78 MICROPY_END_ATOMIC_SECTION(atomic_state);
79 nlr_raise(obj);
80 }
81 }
82 mp_handle_pending_tail(atomic_state);
83 } else {
84 MICROPY_END_ATOMIC_SECTION(atomic_state);
85 }
86 }
87 }
88
89 // This function should only be called by mp_handle_pending,
90 // or by the VM's inlined version of that function.
mp_handle_pending_tail(mp_uint_t atomic_state)91 void mp_handle_pending_tail(mp_uint_t atomic_state) {
92 MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
93 if (!mp_sched_empty()) {
94 mp_sched_item_t item = MP_STATE_VM(sched_queue)[MP_STATE_VM(sched_idx)];
95 MP_STATE_VM(sched_idx) = IDX_MASK(MP_STATE_VM(sched_idx) + 1);
96 --MP_STATE_VM(sched_len);
97 MICROPY_END_ATOMIC_SECTION(atomic_state);
98 mp_call_function_1_protected(item.func, item.arg);
99 } else {
100 MICROPY_END_ATOMIC_SECTION(atomic_state);
101 }
102 mp_sched_unlock();
103 }
104
mp_sched_lock(void)105 void mp_sched_lock(void) {
106 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
107 if (MP_STATE_VM(sched_state) < 0) {
108 --MP_STATE_VM(sched_state);
109 } else {
110 MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
111 }
112 MICROPY_END_ATOMIC_SECTION(atomic_state);
113 }
114
mp_sched_unlock(void)115 void mp_sched_unlock(void) {
116 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
117 assert(MP_STATE_VM(sched_state) < 0);
118 if (++MP_STATE_VM(sched_state) == 0) {
119 // vm became unlocked
120 if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL || mp_sched_num_pending()) {
121 MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
122 } else {
123 MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
124 }
125 }
126 MICROPY_END_ATOMIC_SECTION(atomic_state);
127 }
128
MICROPY_WRAP_MP_SCHED_SCHEDULE(mp_sched_schedule)129 bool MICROPY_WRAP_MP_SCHED_SCHEDULE(mp_sched_schedule)(mp_obj_t function, mp_obj_t arg) {
130 mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
131 bool ret;
132 if (!mp_sched_full()) {
133 if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
134 MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
135 }
136 uint8_t iput = IDX_MASK(MP_STATE_VM(sched_idx) + MP_STATE_VM(sched_len)++);
137 MP_STATE_VM(sched_queue)[iput].func = function;
138 MP_STATE_VM(sched_queue)[iput].arg = arg;
139 MICROPY_SCHED_HOOK_SCHEDULED;
140 ret = true;
141 } else {
142 // schedule queue is full
143 ret = false;
144 }
145 MICROPY_END_ATOMIC_SECTION(atomic_state);
146 return ret;
147 }
148
149 #else // MICROPY_ENABLE_SCHEDULER
150
151 // A variant of this is inlined in the VM at the pending exception check
mp_handle_pending(bool raise_exc)152 void mp_handle_pending(bool raise_exc) {
153 if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
154 mp_obj_t obj = MP_STATE_THREAD(mp_pending_exception);
155 MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
156 if (raise_exc) {
157 nlr_raise(obj);
158 }
159 }
160 }
161
162 #endif // MICROPY_ENABLE_SCHEDULER
163