|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Andrew Leech |
| 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 <stdlib.h> |
| 29 | +#include <errno.h> |
| 30 | + |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "py/mpthread.h" |
| 33 | +#include "py/gc.h" |
| 34 | + |
| 35 | +#if MICROPY_PY_THREAD |
| 36 | + |
| 37 | +#include <windows.h> |
| 38 | +#include "shared/runtime/gchelper.h" |
| 39 | + |
| 40 | +// This value seems to be about right for both 32-bit and 64-bit builds.
F438
td> |
| 41 | +#define THREAD_STACK_OVERFLOW_MARGIN (8192) |
| 42 | + |
| 43 | +// this structure forms a linked list, one node per active thread |
| 44 | +typedef struct _thread_t { |
| 45 | + DWORD id; // system id of thread |
| 46 | + HANDLE handle; // handle of thread |
| 47 | + int ready; // whether the thread is ready and running |
| 48 | + void *(*entry)(void *); // entrypoint, to be passed into thread wrapper |
| 49 | + void *arg; // thread Python args, a GC root pointer |
| 50 | + struct _thread_t *next; |
| 51 | +} thread_t; |
| 52 | + |
| 53 | +STATIC DWORD tls_key; |
| 54 | +STATIC thread_t *thread; |
| 55 | +STATIC HANDLE criticalSection = INVALID_HANDLE_VALUE; |
| 56 | + |
| 57 | +STATIC DWORD wait_for_event(HANDLE event, bool wait) { |
| 58 | + // WaitForSingleObjectEx will exit with WAIT_IO_COMPLETION when the thread |
| 59 | + // runs an APC function (mp_thread_gc below) so we ignore this and wait |
| 60 | + // for a normal event retult; |
| 61 | + DWORD ret = WAIT_IO_COMPLETION; |
| 62 | + while(WAIT_IO_COMPLETION==ret) { |
| 63 | + ret = WaitForSingleObjectEx(event, (wait)?INFINITE:0, TRUE); |
| 64 | + } |
| 65 | + return ret; |
| 66 | +} |
| 67 | + |
| 68 | +void mp_thread_windows_begin_atomic_section(void) { |
| 69 | + if (criticalSection != INVALID_HANDLE_VALUE) { |
| 70 | + wait_for_event(criticalSection, TRUE); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void mp_thread_windows_end_atomic_section(void) { |
| 75 | + if (criticalSection != INVALID_HANDLE_VALUE) { |
| 76 | + ReleaseMutex(criticalSection); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void mp_thread_init(void) { |
| 81 | + tls_key = TlsAlloc(); |
| 82 | + TlsSetValue(tls_key, (LPVOID) &mp_state_ctx.thread); |
| 83 | + |
| 84 | + // Needs to be a recursive mutex to emulate the behavior of |
| 85 | + // BEGIN_ATOMIC_SECTION on bare metal. This is the default |
| 86 | + // behavior of win32 Mutex and CriticalSection objects. |
| 87 | + criticalSection = CreateMutex(NULL, FALSE, NULL); |
| 88 | + |
| 89 | + // create first entry in linked list of all threads |
| 90 | + thread = malloc(sizeof(thread_t)); |
| 91 | + thread->id = GetCurrentThreadId(); |
| 92 | + thread->ready = 1; |
| 93 | + thread->arg = NULL; |
| 94 | + thread->next = NULL; |
| 95 | + |
| 96 | + // The handle is used for running gc via QueueUserAPC below |
| 97 | + thread->handle = OpenThread(THREAD_SET_CONTEXT, FALSE, thread->id); |
| 98 | +} |
| 99 | + |
| 100 | +void mp_thread_deinit(void) { |
| 101 | + mp_thread_windows_begin_atomic_section(); |
| 102 | + while (thread->next != NULL) { |
| 103 | + thread_t *th = thread; |
| 104 | + thread = thread->next; |
| 105 | + TerminateThread(th->handle, -1); |
| 106 | + free(th); |
| 107 | + } |
| 108 | + mp_thread_windows_end_atomic_section(); |
| 109 | + assert(thread->id == GetCurrentThreadId()); |
| 110 | + free(thread); |
| 111 | +} |
| 112 | + |
| 113 | +// this signal handler is used to scan the regs and stack of a thread |
| 114 | +STATIC void WINAPI mp_thread_gc(ULONG_PTR parameter) { |
| 115 | + HANDLE thread_signal_done = (HANDLE)parameter; |
| 116 | + gc_helper_collect_regs_and_stack(); |
| 117 | + #if MICROPY_ENABLE_PYSTACK |
| 118 | + void **ptrs = (void **)(void *)MP_STATE_THREAD(pystack_start); |
| 119 | + gc_collect_root(ptrs, (MP_STATE_THREAD(pystack_cur) - MP_STATE_THREAD(pystack_start)) / sizeof(void *)); |
| 120 | + #endif |
| 121 | + SetEvent(thread_signal_done); |
| 122 | +} |
| 123 | + |
| 124 | +// This function scans all pointers that are external to the current thread. |
| 125 | +// It does this by signalling all other threads and getting them to scan their |
| 126 | +// own registers and stack. Note that there may still be some edge cases left |
| 127 | +// with race conditions and root-pointer scanning: a given thread may manipulate |
| 128 | +// the global root pointers (in mp_state_ctx) while another thread is doing a |
| 129 | +// garbage collection and tracing these pointers. |
| 130 | +void mp_thread_gc_others(void) { |
| 131 | + mp_thread_windows_begin_atomic_section(); |
| 132 | + for (thread_t *th = thread; th != NULL; th = th->next) { |
| 133 | + gc_collect_root(&th->arg, 1); |
| 134 | + if (th->id == GetCurrentThreadId()) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + if (!th->ready) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + // Used to signal when targetted thread gc is complete |
| 142 | + HANDLE thread_signal_done = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 143 | + |
| 144 | + // Run mp_thread_gc function on target thread (soon) |
| 145 | + QueueUserAPC(mp_thread_gc, th->handle, (ULONG_PTR)thread_signal_done); |
| 146 | + |
| 147 | + // wait for it to finish running |
| 148 | + wait_for_event(thread_signal_done, TRUE); |
| 149 | + } |
| 150 | + mp_thread_windows_end_atomic_section(); |
| 151 | +} |
| 152 | + |
| 153 | +mp_state_thread_t *mp_thread_get_state(void) { |
| 154 | + return (mp_state_thread_t *)TlsGetValue(tls_key); |
| 155 | +} |
| 156 | + |
| 157 | +void mp_thread_set_state(mp_state_thread_t *state) { |
| 158 | + TlsSetValue(tls_key, (LPVOID) state); |
| 159 | +} |
| 160 | + |
| 161 | +void mp_thread_start(void) { |
| 162 | + mp_thread_windows_begin_atomic_section(); |
| 163 | + for (thread_t *th = thread; th != NULL; th = th->next) { |
| 164 | + if (th->id == GetCurrentThreadId()) { |
| 165 | + th->ready = 1; |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + mp_thread_windows_end_atomic_section(); |
| 170 | +} |
| 171 | + |
| 172 | +STATIC DWORD WINAPI win32_entry(PVOID args) { |
| 173 | + // Win32 threads require different function form to posix. |
| 174 | + thread_t *th = (thread_t *)args; |
| 175 | + th->entry(th->arg); |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) { |
| 180 | + // default stack size is 8k machine-words |
| 181 | + if (*stack_size == 0) { |
| 182 | + *stack_size = 8192 * sizeof(void *); |
| 183 | + } |
| 184 | + |
| 185 | + // ensure there is enough stack to include a stack-overflow margin |
| 186 | + if (*stack_size < 2 * THREAD_STACK_OVERFLOW_MARGIN) { |
| 187 | + *stack_size = 2 * THREAD_STACK_OVERFLOW_MARGIN; |
| 188 | + } |
| 189 | + |
| 190 | + mp_thread_windows_begin_atomic_section(); |
| 191 | + |
| 192 | + // create thread |
| 193 | + DWORD id; |
| 194 | + |
| 195 | + thread_t *th = malloc(sizeof(thread_t)); |
| 196 | + th->entry = entry; |
| 197 | + th->arg = arg; |
| 198 | + |
| 199 | + HANDLE thread_handle = CreateThread(NULL, *stack_size, win32_entry, th, 0, &id); |
| 200 | + |
| 201 | + if (thread_handle == NULL) { |
| 202 | + free(th); |
| 203 | + mp_thread_windows_end_atomic_section(); |
| 204 | + goto er; |
| 205 | + } |
| 206 | + |
| 207 | + // adjust stack_size to provide room to recover from hitting the limit |
| 208 | + *stack_size -= THREAD_STACK_OVERFLOW_MARGIN; |
| 209 | + |
| 210 | + // add thread to linked list of all threads |
| 211 | + th->id = id; |
| 212 | + th->handle = thread_handle; |
| 213 | + th->ready = 0; |
| 214 | + th->next = thread; |
| 215 | + thread = th; |
| 216 | + |
| 217 | + mp_thread_windows_end_atomic_section(); |
| 218 | + |
| 219 | + return; |
| 220 | + |
| 221 | +er: |
| 222 | + mp_raise_OSError(-1); |
| 223 | +} |
| 224 | + |
| 225 | +void mp_thread_finish(void) { |
| 226 | + mp_thread_windows_begin_atomic_section(); |
| 227 | + thread_t *prev = NULL; |
| 228 | + for (thread_t *th = thread; th != NULL; th = th->next) { |
| 229 | + if (th->id == GetCurrentThreadId()) { |
| 230 | + if (prev == NULL) { |
| 231 | + thread = th->next; |
| 232 | + } else { |
| 233 | + prev->next = th->next; |
| 234 | + } |
| 235 | + free(th); |
| 236 | + break; |
| 237 | + } |
| 238 | + prev = th; |
| 239 | + } |
| 240 | + mp_thread_windows_end_atomic_section(); |
| 241 | +} |
| 242 | + |
| 243 | +void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { |
| 244 | + // The win32 Mutex is re-entrant, unlike the posix equivalent. |
| 245 | + // To avoid this a sepaphore with size of 1 is used. |
| 246 | + *mutex = CreateSemaphore(NULL, 1, 1, NULL); |
| 247 | +} |
| 248 | + |
| 249 | +int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { |
| 250 | + if (wait_for_event(*mutex, wait) == 0) { |
| 251 | + return 1; |
| 252 | + } else { |
| 253 | + return 0; |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) { |
| 258 | + ReleaseSemaphore(*mutex, 1, NULL); |
| 259 | +} |
| 260 | + |
| 261 | +#endif // MICROPY_PY_THREAD |
0 commit comments