diff options
author | Thomas Martitz <kugel@rockbox.org> | 2011-02-18 22:46:01 +0000 |
---|---|---|
committer | Thomas Martitz <kugel@rockbox.org> | 2011-02-18 22:46:01 +0000 |
commit | 6d85de341928aef8178465c60122f3cbe76f5dd6 (patch) | |
tree | ff86c384a574ac20d3418c1b904ed4d0de1f6980 /firmware/target/hosted/thread-win32.c | |
parent | 3926c30705cc7235122e2f2e35ab506b53238cdf (diff) | |
download | rockbox-6d85de341928aef8178465c60122f3cbe76f5dd6.tar.gz rockbox-6d85de341928aef8178465c60122f3cbe76f5dd6.zip |
Implement cooperative threads on hosted platforms using C code.
This replaces SDL threads with real cooperative threads, which are less cpu intensive and allow priority scheduling.
The backend for context switching is dependant on the host (sigaltstack/longjmp on Unix, Fibers on Windows).
configure has options to force or disallow SDL threads.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29327 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/target/hosted/thread-win32.c')
-rw-r--r-- | firmware/target/hosted/thread-win32.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/firmware/target/hosted/thread-win32.c b/firmware/target/hosted/thread-win32.c new file mode 100644 index 0000000000..a60198494a --- /dev/null +++ b/firmware/target/hosted/thread-win32.c | |||
@@ -0,0 +1,85 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 by Thomas Martitz | ||
11 | * | ||
12 | * Generic ARM threading support | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
22 | ****************************************************************************/ | ||
23 | |||
24 | |||
25 | #include <windows.h> | ||
26 | #include "system.h" | ||
27 | |||
28 | #define INIT_MAIN_THREAD | ||
29 | |||
30 | #define THREAD_STARTUP_INIT(core, thread, function) \ | ||
31 | ({ (thread)->context.stack_size = (thread)->stack_size, \ | ||
32 | (thread)->context.stack = (uintptr_t)(thread)->stack; \ | ||
33 | (thread)->context.start = function; }) | ||
34 | |||
35 | static void init_main_thread(void *addr) | ||
36 | { | ||
37 | struct regs *context = (struct regs*)addr; | ||
38 | /* we must convert the current main thread to a fiber to be able to | ||
39 | * schedule other fibers */ | ||
40 | context->uc = ConvertThreadToFiber(NULL); | ||
41 | context->stack_size = 0; | ||
42 | } | ||
43 | |||
44 | static inline void store_context(void* addr) | ||
45 | { | ||
46 | (void)addr; | ||
47 | /* nothing to do here, Fibers continue after the SwitchToFiber call */ | ||
48 | } | ||
49 | |||
50 | static void start_thread(void) | ||
51 | { | ||
52 | void (*func)(void) = GetFiberData(); | ||
53 | func(); | ||
54 | /* go out if thread function returns */ | ||
55 | thread_exit(); | ||
56 | } | ||
57 | |||
58 | /* | ||
59 | * Load context and run it | ||
60 | * | ||
61 | * Resume execution from the last load_context call for the thread | ||
62 | */ | ||
63 | |||
64 | static inline void load_context(const void* addr) | ||
65 | { | ||
66 | struct regs *context = (struct regs*)addr; | ||
67 | if (UNLIKELY(context->start)) | ||
68 | { /* need setup before switching to it */ | ||
69 | context->uc = CreateFiber(context->stack_size, | ||
70 | (LPFIBER_START_ROUTINE)start_thread, context->start); | ||
71 | /* can't assign stack pointer, only stack size */ | ||
72 | context->stack_size = 0; | ||
73 | context->start = NULL; | ||
74 | } | ||
75 | SwitchToFiber(context->uc); | ||
76 | } | ||
77 | |||
78 | /* | ||
79 | * play nice with the host and sleep while waiting for the tick */ | ||
80 | static inline void core_sleep(void) | ||
81 | { | ||
82 | enable_irq(); | ||
83 | wait_for_interrupt(); | ||
84 | } | ||
85 | |||