-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
windows/thread: Add support for win32 micropython _thread. #7796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewleech
wants to merge
1
commit into
micropython:master
Choose a base branch
from
andrewleech:windows-thread
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+356
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 Andrew Leech | ||
* | ||
* 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 <stdlib.h> | ||
#include <errno.h> | ||
|
||
#include "py/runtime.h" | ||
#include "py/mpthread.h" | ||
#include "py/gc.h" | ||
|
||
#if MICROPY_PY_THREAD | ||
|
||
#include <windows.h> | ||
#include "shared/runtime/gchelper.h" | ||
|
||
// This value seems to be about right for both 32-bit and 64-bit builds. | ||
#define THREAD_STACK_OVERFLOW_MARGIN (8192) | ||
|
||
// this structure forms a linked list, one node per active thread. | ||
typedef struct _thread_t { | ||
DWORD id; // system id of thread | ||
HANDLE handle; // handle of thread | ||
int ready; // whether the thread is ready and running | ||
void *(*entry)(void *); // entrypoint, to be passed into thread wrapper | ||
void *arg; // thread Python args, a GC root pointer | ||
struct _thread_t *next; | ||
} thread_t; | ||
|
||
static DWORD tls_key; | ||
static thread_t *thread; | ||
static HANDLE criticalSection = INVALID_HANDLE_VALUE; | ||
|
||
static DWORD wait_for_event(HANDLE event, bool wait) { | ||
// WaitForSingleObjectEx will exit with WAIT_IO_COMPLETION when the thread | ||
// runs an APC function (mp_thread_gc below) so we ignore this and wait | ||
// for a normal event retult. | ||
DWORD ret = WAIT_IO_COMPLETION; | ||
while(WAIT_IO_COMPLETION==ret) { | ||
ret = WaitForSingleObjectEx(event, (wait)?INFINITE:0, TRUE); | ||
} | ||
return ret; | ||
} | ||
|
||
void mp_thread_windows_begin_atomic_section(void) { | ||
if (criticalSection != INVALID_HANDLE_VALUE) { | ||
wait_for_event(criticalSection, TRUE); | ||
} | ||
} | ||
|
||
void mp_thread_windows_end_atomic_section(void) { | ||
if (criticalSection != INVALID_HANDLE_VALUE) { | ||
ReleaseMutex(criticalSection); | ||
} | ||
} | ||
|
||
void mp_thread_init(void) { | ||
tls_key = TlsAlloc(); | ||
TlsSetValue(tls_key, (LPVOID) &mp_state_ctx.thread); | ||
|
||
// Needs to be a recursive mutex to emulate the behavior of | ||
// BEGIN_ATOMIC_SECTION on bare metal. This is the default | ||
// behavior of win32 Mutex and CriticalSection objects. | ||
criticalSection = CreateMutex(NULL, FALSE, NULL); | ||
|
||
// create first entry in linked list of all threads. | ||
thread = malloc(sizeof(thread_t)); | ||
thread->id = GetCurrentThreadId(); | ||
thread->ready = 1; | ||
thread->arg = NULL; | ||
thread->next = NULL; | ||
|
||
// The handle is used for running gc via QueueUserAPC below. | ||
thread->handle = OpenThread(THREAD_SET_CONTEXT, FALSE, thread->id); | ||
} | ||
|
||
void mp_thread_deinit(void) { | ||
mp_thread_windows_begin_atomic_section(); | ||
while (thread->next != NULL) { | ||
thread_t *th = thread; | ||
thread = thread->next; | ||
TerminateThread(th->handle, -1); | ||
free(th); | ||
} | ||
mp_thread_windows_end_atomic_section(); | ||
assert(thread->id == GetCurrentThreadId()); | ||
free(thread); | ||
} | ||
|
||
// this signal handler is used to scan the regs and stack of a thread. | ||
static void WINAPI mp_thread_gc(ULONG_PTR parameter) { | ||
HANDLE thread_signal_done = (HANDLE)parameter; | ||
gc_helper_collect_regs_and_stack(); | ||
#if MICROPY_ENABLE_PYSTACK | ||
void **ptrs = (void **)(void *)MP_STATE_THREAD(pystack_start); | ||
gc_collect_root(ptrs, (MP_STATE_THREAD(pystack_cur) - MP_STATE_THREAD(pystack_start)) / sizeof(void *)); | ||
#endif | ||
SetEvent(thread_signal_done); | ||
} | ||
|
||
// This function scans all pointers that are external to the current thread. | ||
// It does this by signalling all other threads and getting them to scan their | ||
// own registers and stack. Note that there may still be some edge cases left | ||
// with race conditions and root-pointer scanning: a given thread may manipulate | ||
// the global root pointers (in mp_state_ctx) while another thread is doing a | ||
// garbage collection and tracing these pointers. | ||
void mp_thread_gc_others(void) { | ||
mp_thread_windows_begin_atomic_section(); | ||
for (thread_t *th = thread; th != NULL; th = th->next) { | ||
gc_collect_root(&th->arg, 1); | ||
if (th->id == GetCurrentThreadId()) { | ||
continue; | ||
} | ||
if (!th->ready) { | ||
continue; | ||
} | ||
|
||
// Used to signal when targetted thread gc is complete. | ||
HANDLE thread_signal_done = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
|
||
// Run mp_thread_gc function on target thread (soon). | ||
QueueUserAPC(mp_thread_gc, th->handle, (ULONG_PTR)thread_signal_done); | ||
|
||
// wait for it to finish running. | ||
wait_for_event(thread_signal_done, TRUE); | ||
} | ||
mp_thread_windows_end_atomic_section(); | ||
} | ||
|
||
mp_state_thread_t *mp_thread_get_state(void) { | ||
return (mp_state_thread_t *)TlsGetValue(tls_key); | ||
} | ||
|
||
void mp_thread_set_state(mp_state_thread_t *state) { | ||
TlsSetValue(tls_key, (LPVOID) state); | ||
} | ||
|
||
void mp_thread_start(void) { | ||
mp_thread_windows_begin_atomic_section(); | ||
for (thread_t *th = thread; th != NULL; th = th->next) { | ||
if (th->id == GetCurrentThreadId()) { | ||
th->ready = 1; | ||
break; | ||
} | ||
} | ||
mp_thread_windows_end_atomic_section(); | ||
} | ||
|
||
static DWORD WINAPI win32_entry(PVOID args) { | ||
// Win32 threads require different function form to posix. | ||
thread_t *th = (thread_t *)args; | ||
th->entry(th->arg); | ||
return 0; | ||
} | ||
|
||
void mp_thread_create(void *(*entry)(void *), void *arg, size_t *stack_size) { | ||
// default stack size is 8k machine-words. | ||
if (*stack_size == 0) { | ||
*stack_size = 8192 * sizeof(void *); | ||
} | ||
|
||
// ensure there is enough stack to include a stack-overflow margin. | ||
if (*stack_size < 2 * THREAD_STACK_OVERFLOW_MARGIN) { | ||
*stack_size = 2 * THREAD_STACK_OVERFLOW_MARGIN; | ||
} | ||
|
||
mp_thread_windows_begin_atomic_section(); | ||
|
||
// create thread | ||
DWORD id; | ||
|
||
thread_t *th = malloc(sizeof(thread_t)); | ||
th->entry = entry; | ||
th->arg = arg; | ||
|
||
HANDLE thread_handle = CreateThread(NULL, *stack_size, win32_entry, th, 0, &id); | ||
|
||
if (thread_handle == NULL) { | ||
free(th); | ||
mp_thread_windows_end_atomic_section(); | ||
goto er; | ||
} | ||
|
||
// adjust stack_size to provide room to recover from hitting the limit. | ||
*stack_size -= THREAD_STACK_OVERFLOW_MARGIN; | ||
|
||
// add thread to linked list of all threads. | ||
th->id = id; | ||
th->handle = thread_handle; | ||
th->ready = 0; | ||
th->next = thread; | ||
thread = th; | ||
|
||
mp_thread_windows_end_atomic_section(); | ||
|
||
return; | ||
|
||
er: | ||
mp_raise_OSError(-1); | ||
} | ||
|
||
void mp_thread_finish(void) { | ||
mp_thread_windows_begin_atomic_section(); | ||
thread_t *prev = NULL; | ||
for (thread_t *th = thread; th != NULL; th = th->next) { | ||
if (th->id == GetCurrentThreadId()) { | ||
if (prev == NULL) { | ||
thread = th->next; | ||
} else { | ||
prev->next = th->next; | ||
} | ||
free(th); | ||
break; | ||
} | ||
prev = th; | ||
} | ||
mp_thread_windows_end_atomic_section(); | ||
} | ||
|
||
void mp_thread_mutex_init(mp_thread_mutex_t *mutex) { | ||
// The win32 Mutex is re-entrant, unlike the posix equivalent. | ||
// To avoid this a sepaphore with size of 1 is used. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spelling: "semaphore" |
||
*mutex = CreateSemaphore(NULL, 1, 1, NULL); | ||
} | ||
|
||
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) { | ||
if (wait_for_event(*mutex, wait) == 0) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) { | ||
ReleaseSemaphore(*mutex, 1, NULL); | ||
} | ||
|
||
#endif // MICROPY_PY_THREAD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 Andrew Leech | ||
* | ||
* 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. | ||
*/ | ||
|
||
typedef void* mp_thread_mutex_t; // win32 HANDLE | ||
|
||
void mp_thread_init(void); | ||
void mp_thread_deinit(void); | ||
void mp_thread_gc_others(void); | ||
|
||
// Windows version of "enable/disable IRQs". | ||
// Functions as a port-global lock for any code that must be serialised. | ||
void mp_thread_windows_begin_atomic_section(void); | ||
void mp_thread_windows_end_atomic_section(void); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.