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 | |
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')
-rw-r--r-- | firmware/target/hosted/sdl/kernel-sdl.c | 62 | ||||
-rw-r--r-- | firmware/target/hosted/sdl/system-sdl.c | 21 | ||||
-rw-r--r-- | firmware/target/hosted/sdl/system-sdl.h | 3 | ||||
-rw-r--r-- | firmware/target/hosted/sdl/thread-sdl.h | 2 | ||||
-rw-r--r-- | firmware/target/hosted/thread-arm.c (renamed from firmware/target/hosted/android/thread-android-arm.c) | 8 | ||||
-rw-r--r-- | firmware/target/hosted/thread-unix.c | 294 | ||||
-rw-r--r-- | firmware/target/hosted/thread-win32.c | 85 |
7 files changed, 461 insertions, 14 deletions
diff --git a/firmware/target/hosted/sdl/kernel-sdl.c b/firmware/target/hosted/sdl/kernel-sdl.c index d933b9097e..4fb1aede0a 100644 --- a/firmware/target/hosted/sdl/kernel-sdl.c +++ b/firmware/target/hosted/sdl/kernel-sdl.c | |||
@@ -34,6 +34,14 @@ | |||
34 | static SDL_TimerID tick_timer_id; | 34 | static SDL_TimerID tick_timer_id; |
35 | long start_tick; | 35 | long start_tick; |
36 | 36 | ||
37 | #ifndef HAVE_SDL_THREADS | ||
38 | /* for the wait_for_interrupt function */ | ||
39 | static bool do_exit; | ||
40 | static SDL_cond *wfi_cond; | ||
41 | static SDL_mutex *wfi_mutex; | ||
42 | #else | ||
43 | #define do_exit false | ||
44 | #endif | ||
37 | /* Condition to signal that "interrupts" may proceed */ | 45 | /* Condition to signal that "interrupts" may proceed */ |
38 | static SDL_cond *sim_thread_cond; | 46 | static SDL_cond *sim_thread_cond; |
39 | /* Mutex to serialize changing levels and exclude other threads while | 47 | /* Mutex to serialize changing levels and exclude other threads while |
@@ -98,6 +106,9 @@ void sim_exit_irq_handler(void) | |||
98 | 106 | ||
99 | status_reg = 0; | 107 | status_reg = 0; |
100 | SDL_UnlockMutex(sim_irq_mtx); | 108 | SDL_UnlockMutex(sim_irq_mtx); |
109 | #ifndef HAVE_SDL_THREADS | ||
110 | SDL_CondSignal(wfi_cond); | ||
111 | #endif | ||
101 | } | 112 | } |
102 | 113 | ||
103 | static bool sim_kernel_init(void) | 114 | static bool sim_kernel_init(void) |
@@ -115,15 +126,33 @@ static bool sim_kernel_init(void) | |||
115 | panicf("Cannot create sim_thread_cond\n"); | 126 | panicf("Cannot create sim_thread_cond\n"); |
116 | return false; | 127 | return false; |
117 | } | 128 | } |
118 | 129 | #ifndef HAVE_SDL_THREADS | |
130 | wfi_cond = SDL_CreateCond(); | ||
131 | if (wfi_cond == NULL) | ||
132 | { | ||
133 | panicf("Cannot create wfi\n"); | ||
134 | return false; | ||
135 | } | ||
136 | wfi_mutex = SDL_CreateMutex(); | ||
137 | if (wfi_mutex == NULL) | ||
138 | { | ||
139 | panicf("Cannot create wfi mutex\n"); | ||
140 | return false; | ||
141 | } | ||
142 | #endif | ||
119 | return true; | 143 | return true; |
120 | } | 144 | } |
121 | 145 | ||
122 | void sim_kernel_shutdown(void) | 146 | void sim_kernel_shutdown(void) |
123 | { | 147 | { |
124 | SDL_RemoveTimer(tick_timer_id); | 148 | SDL_RemoveTimer(tick_timer_id); |
149 | #ifndef HAVE_SDL_THREADS | ||
150 | do_exit = true; | ||
151 | SDL_CondSignal(wfi_cond); | ||
152 | #endif | ||
153 | disable_irq(); | ||
125 | SDL_DestroyMutex(sim_irq_mtx); | 154 | SDL_DestroyMutex(sim_irq_mtx); |
126 | SDL_DestroyCond(sim_thread_cond); | 155 | SDL_DestroyCond(sim_thread_cond); |
127 | } | 156 | } |
128 | 157 | ||
129 | Uint32 tick_timer(Uint32 interval, void *param) | 158 | Uint32 tick_timer(Uint32 interval, void *param) |
@@ -135,7 +164,7 @@ Uint32 tick_timer(Uint32 interval, void *param) | |||
135 | 164 | ||
136 | new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ); | 165 | new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ); |
137 | 166 | ||
138 | while(new_tick != current_tick) | 167 | while(new_tick != current_tick && !do_exit) |
139 | { | 168 | { |
140 | sim_enter_irq_handler(); | 169 | sim_enter_irq_handler(); |
141 | 170 | ||
@@ -146,7 +175,7 @@ Uint32 tick_timer(Uint32 interval, void *param) | |||
146 | sim_exit_irq_handler(); | 175 | sim_exit_irq_handler(); |
147 | } | 176 | } |
148 | 177 | ||
149 | return interval; | 178 | return do_exit ? 0 : interval; |
150 | } | 179 | } |
151 | 180 | ||
152 | void tick_start(unsigned int interval_in_ms) | 181 | void tick_start(unsigned int interval_in_ms) |
@@ -168,4 +197,29 @@ void tick_start(unsigned int interval_in_ms) | |||
168 | } | 197 | } |
169 | 198 | ||
170 | tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL); | 199 | tick_timer_id = SDL_AddTimer(interval_in_ms, tick_timer, NULL); |
200 | #ifndef HAVE_SDL_THREADS | ||
201 | SDL_LockMutex(wfi_mutex); | ||
202 | #endif | ||
203 | } | ||
204 | |||
205 | #ifndef HAVE_SDL_THREADS | ||
206 | static void check_exit(void) | ||
207 | { | ||
208 | if (UNLIKELY(do_exit)) | ||
209 | { | ||
210 | SDL_DestroyCond(wfi_cond); | ||
211 | SDL_UnlockMutex(wfi_mutex); | ||
212 | SDL_DestroyMutex(wfi_mutex); | ||
213 | sim_do_exit(); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | void wait_for_interrupt(void) | ||
218 | { | ||
219 | /* the exit may come at any time, during the CondWait or before, | ||
220 | * so check it twice */ | ||
221 | check_exit(); | ||
222 | SDL_CondWait(wfi_cond, wfi_mutex); | ||
223 | check_exit(); | ||
171 | } | 224 | } |
225 | #endif | ||
diff --git a/firmware/target/hosted/sdl/system-sdl.c b/firmware/target/hosted/sdl/system-sdl.c index 9ea25c1014..1cc9b2c919 100644 --- a/firmware/target/hosted/sdl/system-sdl.c +++ b/firmware/target/hosted/sdl/system-sdl.c | |||
@@ -184,7 +184,9 @@ static int sdl_event_thread(void * param) | |||
184 | 184 | ||
185 | /* Order here is relevent to prevent deadlocks and use of destroyed | 185 | /* Order here is relevent to prevent deadlocks and use of destroyed |
186 | sync primitives by kernel threads */ | 186 | sync primitives by kernel threads */ |
187 | sim_thread_shutdown(); | 187 | #ifdef HAVE_SDL_THREADS |
188 | sim_thread_shutdown(); /* not needed for native threads */ | ||
189 | #endif | ||
188 | sim_kernel_shutdown(); | 190 | sim_kernel_shutdown(); |
189 | 191 | ||
190 | return 0; | 192 | return 0; |
@@ -199,9 +201,13 @@ void sim_do_exit(void) | |||
199 | exit(EXIT_SUCCESS); | 201 | exit(EXIT_SUCCESS); |
200 | } | 202 | } |
201 | 203 | ||
204 | uintptr_t *stackbegin; | ||
205 | uintptr_t *stackend; | ||
202 | void system_init(void) | 206 | void system_init(void) |
203 | { | 207 | { |
204 | SDL_sem *s; | 208 | SDL_sem *s; |
209 | /* fake stack, OS manages size (and growth) */ | ||
210 | stackbegin = stackend = (uintptr_t*)&s; | ||
205 | 211 | ||
206 | #if (CONFIG_PLATFORM & PLATFORM_MAEMO) | 212 | #if (CONFIG_PLATFORM & PLATFORM_MAEMO) |
207 | /* Make glib thread safe */ | 213 | /* Make glib thread safe */ |
@@ -219,21 +225,24 @@ void system_init(void) | |||
219 | /* wait for sdl_event_thread to run so that it can initialize the surfaces | 225 | /* wait for sdl_event_thread to run so that it can initialize the surfaces |
220 | * and video subsystem needed for SDL events */ | 226 | * and video subsystem needed for SDL events */ |
221 | SDL_SemWait(s); | 227 | SDL_SemWait(s); |
222 | |||
223 | /* cleanup */ | 228 | /* cleanup */ |
224 | SDL_DestroySemaphore(s); | 229 | SDL_DestroySemaphore(s); |
225 | } | 230 | } |
226 | 231 | ||
227 | void system_exception_wait(void) | ||
228 | { | ||
229 | sim_thread_exception_wait(); | ||
230 | } | ||
231 | 232 | ||
232 | void system_reboot(void) | 233 | void system_reboot(void) |
233 | { | 234 | { |
235 | #ifdef HAVE_SDL_THREADS | ||
234 | sim_thread_exception_wait(); | 236 | sim_thread_exception_wait(); |
237 | #else | ||
238 | sim_do_exit(); | ||
239 | #endif | ||
235 | } | 240 | } |
236 | 241 | ||
242 | void system_exception_wait(void) | ||
243 | { | ||
244 | system_reboot(); | ||
245 | } | ||
237 | 246 | ||
238 | void sys_handle_argv(int argc, char *argv[]) | 247 | void sys_handle_argv(int argc, char *argv[]) |
239 | { | 248 | { |
diff --git a/firmware/target/hosted/sdl/system-sdl.h b/firmware/target/hosted/sdl/system-sdl.h index 01952e50a0..bec01ec81d 100644 --- a/firmware/target/hosted/sdl/system-sdl.h +++ b/firmware/target/hosted/sdl/system-sdl.h | |||
@@ -46,6 +46,9 @@ void sys_poweroff(void); | |||
46 | void sys_handle_argv(int argc, char *argv[]); | 46 | void sys_handle_argv(int argc, char *argv[]); |
47 | void gui_message_loop(void); | 47 | void gui_message_loop(void); |
48 | void sim_do_exit(void); | 48 | void sim_do_exit(void); |
49 | #ifndef HAVE_SDL_THREADS | ||
50 | void wait_for_interrupt(void); | ||
51 | #endif | ||
49 | 52 | ||
50 | extern bool background; /* True if the background image is enabled */ | 53 | extern bool background; /* True if the background image is enabled */ |
51 | extern bool showremote; | 54 | extern bool showremote; |
diff --git a/firmware/target/hosted/sdl/thread-sdl.h b/firmware/target/hosted/sdl/thread-sdl.h index 9384e6060d..9edb3ee74e 100644 --- a/firmware/target/hosted/sdl/thread-sdl.h +++ b/firmware/target/hosted/sdl/thread-sdl.h | |||
@@ -22,11 +22,13 @@ | |||
22 | #ifndef __THREADSDL_H__ | 22 | #ifndef __THREADSDL_H__ |
23 | #define __THREADSDL_H__ | 23 | #define __THREADSDL_H__ |
24 | 24 | ||
25 | #ifdef HAVE_SDL_THREADS | ||
25 | /* extra thread functions that only apply when running on hosting platforms */ | 26 | /* extra thread functions that only apply when running on hosting platforms */ |
26 | void sim_thread_lock(void *me); | 27 | void sim_thread_lock(void *me); |
27 | void * sim_thread_unlock(void); | 28 | void * sim_thread_unlock(void); |
28 | void sim_thread_exception_wait(void); | 29 | void sim_thread_exception_wait(void); |
29 | void sim_thread_shutdown(void); /* Shut down all kernel threads gracefully */ | 30 | void sim_thread_shutdown(void); /* Shut down all kernel threads gracefully */ |
31 | #endif | ||
30 | 32 | ||
31 | #endif /* #ifndef __THREADSDL_H__ */ | 33 | #endif /* #ifndef __THREADSDL_H__ */ |
32 | 34 | ||
diff --git a/firmware/target/hosted/android/thread-android-arm.c b/firmware/target/hosted/thread-arm.c index 0bfd2b6c44..d2fa7d1e5d 100644 --- a/firmware/target/hosted/android/thread-android-arm.c +++ b/firmware/target/hosted/thread-arm.c | |||
@@ -22,7 +22,6 @@ | |||
22 | * | 22 | * |
23 | ****************************************************************************/ | 23 | ****************************************************************************/ |
24 | 24 | ||
25 | #include <jni.h> | ||
26 | #include <system.h> | 25 | #include <system.h> |
27 | /*--------------------------------------------------------------------------- | 26 | /*--------------------------------------------------------------------------- |
28 | * Start the thread running and terminate it if it returns | 27 | * Start the thread running and terminate it if it returns |
@@ -84,14 +83,15 @@ static inline void load_context(const void* addr) | |||
84 | * this core sleep suspends the OS thread rockbox runs under, which greatly | 83 | * this core sleep suspends the OS thread rockbox runs under, which greatly |
85 | * reduces cpu usage (~100% to <10%) | 84 | * reduces cpu usage (~100% to <10%) |
86 | * | 85 | * |
87 | * it returns when the RockboxTimer notified us, i.e. at each tick | 86 | * it returns when when the tick timer is called, other interrupt-like |
88 | * (after it called the tick tasks) | 87 | * events occur |
89 | * | 88 | * |
90 | * wait_for_interrupt is implemented in kernel-android.c | 89 | * wait_for_interrupt is implemented in kernel-<platform>.c |
91 | **/ | 90 | **/ |
92 | 91 | ||
93 | static inline void core_sleep(void) | 92 | static inline void core_sleep(void) |
94 | { | 93 | { |
94 | enable_irq(); | ||
95 | wait_for_interrupt(); | 95 | wait_for_interrupt(); |
96 | } | 96 | } |
97 | 97 | ||
diff --git a/firmware/target/hosted/thread-unix.c b/firmware/target/hosted/thread-unix.c new file mode 100644 index 0000000000..a84ac70d56 --- /dev/null +++ b/firmware/target/hosted/thread-unix.c | |||
@@ -0,0 +1,294 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <stdbool.h> | ||
3 | #include <signal.h> | ||
4 | #include <stdio.h> | ||
5 | #include <setjmp.h> | ||
6 | #include <unistd.h> | ||
7 | #include <pthread.h> | ||
8 | #include <errno.h> | ||
9 | #include "debug.h" | ||
10 | |||
11 | static volatile bool sig_handler_called; | ||
12 | static volatile jmp_buf tramp_buf; | ||
13 | static volatile jmp_buf bootstrap_buf; | ||
14 | static void (*thread_func)(void); | ||
15 | static const int trampoline_sig = SIGUSR1; | ||
16 | static pthread_t main_thread; | ||
17 | |||
18 | static struct ctx { | ||
19 | jmp_buf thread_buf; | ||
20 | } thread_bufs[MAXTHREADS]; | ||
21 | static struct ctx* thread_context, *target_context; | ||
22 | static int curr_uc; | ||
23 | |||
24 | static void trampoline(int sig); | ||
25 | static void bootstrap_context(void) __attribute__((noinline)); | ||
26 | |||
27 | /* The *_context functions are heavily based on Gnu pth | ||
28 | * http://www.gnu.org/software/pth/ | ||
29 | * | ||
30 | * adjusted to work in a multi-thread environment to | ||
31 | * offer a ucontext-like API | ||
32 | */ | ||
33 | |||
34 | /* | ||
35 | * VARIANT 2: THE SIGNAL STACK TRICK | ||
36 | * | ||
37 | * This uses sigstack/sigaltstack() and friends and is really the | ||
38 | * most tricky part of Pth. When you understand the following | ||
39 | * stuff you're a good Unix hacker and then you've already | ||
40 | * understood the gory ingredients of Pth. So, either welcome to | ||
41 | * the club of hackers, or do yourself a favor and skip this ;) | ||
42 | * | ||
43 | * The ingenious fact is that this variant runs really on _all_ POSIX | ||
44 | * compliant systems without special platform kludges. But be _VERY_ | ||
45 | * carefully when you change something in the following code. The slightest | ||
46 | * change or reordering can lead to horribly broken code. Really every | ||
47 | * function call in the following case is intended to be how it is, doubt | ||
48 | * me... | ||
49 | * | ||
50 | * For more details we strongly recommend you to read the companion | ||
51 | * paper ``Portable Multithreading -- The Signal Stack Trick for | ||
52 | * User-Space Thread Creation'' from Ralf S. Engelschall. A copy of the | ||
53 | * draft of this paper you can find in the file rse-pmt.ps inside the | ||
54 | * GNU Pth distribution. | ||
55 | */ | ||
56 | |||
57 | static int make_context(struct ctx *ctx, void (*f)(void), char *sp, size_t stack_size) | ||
58 | { | ||
59 | struct sigaction sa; | ||
60 | struct sigaction osa; | ||
61 | stack_t ss; | ||
62 | stack_t oss; | ||
63 | sigset_t osigs; | ||
64 | sigset_t sigs; | ||
65 | |||
66 | disable_irq(); | ||
67 | /* | ||
68 | * Preserve the trampoline_sig signal state, block trampoline_sig, | ||
69 | * and establish our signal handler. The signal will | ||
70 | * later transfer control onto the signal stack. | ||
71 | */ | ||
72 | sigemptyset(&sigs); | ||
73 | sigaddset(&sigs, trampoline_sig); | ||
74 | sigprocmask(SIG_BLOCK, &sigs, &osigs); | ||
75 | sa.sa_handler = trampoline; | ||
76 | sigemptyset(&sa.sa_mask); | ||
77 | sa.sa_flags = SA_ONSTACK; | ||
78 | if (sigaction(trampoline_sig, &sa, &osa) != 0) | ||
79 | { | ||
80 | DEBUGF("%s(): %s\n", __func__, strerror(errno)); | ||
81 | return false; | ||
82 | } | ||
83 | /* | ||
84 | * Set the new stack. | ||
85 | * | ||
86 | * For sigaltstack we're lucky [from sigaltstack(2) on | ||
87 | * FreeBSD 3.1]: ``Signal stacks are automatically adjusted | ||
88 | * for the direction of stack growth and alignment | ||
89 | * requirements'' | ||
90 | * | ||
91 | * For sigstack we have to decide ourself [from sigstack(2) | ||
92 | * on Solaris 2.6]: ``The direction of stack growth is not | ||
93 | * indicated in the historical definition of struct sigstack. | ||
94 | * The only way to portably establish a stack pointer is for | ||
95 | * the application to determine stack growth direction.'' | ||
96 | */ | ||
97 | ss.ss_sp = sp; | ||
98 | ss.ss_size = stack_size; | ||
99 | ss.ss_flags = 0; | ||
100 | if (sigaltstack(&ss, &oss) < 0) | ||
101 | { | ||
102 | DEBUGF("%s(): %s\n", __func__, strerror(errno)); | ||
103 | return false; | ||
104 | } | ||
105 | |||
106 | /* | ||
107 | * Now transfer control onto the signal stack and set it up. | ||
108 | * It will return immediately via "return" after the setjmp() | ||
109 | * was performed. Be careful here with race conditions. The | ||
110 | * signal can be delivered the first time sigsuspend() is | ||
111 | * called. | ||
112 | */ | ||
113 | sig_handler_called = false; | ||
114 | main_thread = pthread_self(); | ||
115 | sigfillset(&sigs); | ||
116 | sigdelset(&sigs, trampoline_sig); | ||
117 | pthread_kill(main_thread, trampoline_sig); | ||
118 | while(!sig_handler_called) | ||
119 | sigsuspend(&sigs); | ||
120 | |||
121 | /* | ||
122 | * Inform the system that we are back off the signal stack by | ||
123 | * removing the alternative signal stack. Be careful here: It | ||
124 | * first has to be disabled, before it can be removed. | ||
125 | */ | ||
126 | sigaltstack(NULL, &ss); | ||
127 | ss.ss_flags = SS_DISABLE; | ||
128 | if (sigaltstack(&ss, NULL) < 0) | ||
129 | { | ||
130 | DEBUGF("%s(): %s\n", __func__, strerror(errno)); | ||
131 | return false; | ||
132 | } | ||
133 | sigaltstack(NULL, &ss); | ||
134 | if (!(ss.ss_flags & SS_DISABLE)) | ||
135 | { | ||
136 | DEBUGF("%s(): %s\n", __func__, strerror(errno)); | ||
137 | return false; | ||
138 | } | ||
139 | if (!(oss.ss_flags & SS_DISABLE)) | ||
140 | sigaltstack(&oss, NULL); | ||
141 | |||
142 | /* | ||
143 | * Restore the old trampoline_sig signal handler and mask | ||
144 | */ | ||
145 | sigaction(trampoline_sig, &osa, NULL); | ||
146 | sigprocmask(SIG_SETMASK, &osigs, NULL); | ||
147 | |||
148 | /* | ||
149 | * Tell the trampoline and bootstrap function where to dump | ||
150 | * the new machine context, and what to do afterwards... | ||
151 | */ | ||
152 | thread_func = f; | ||
153 | thread_context = ctx; | ||
154 | |||
155 | /* | ||
156 | * Now enter the trampoline again, but this time not as a signal | ||
157 | * handler. Instead we jump into it directly. The functionally | ||
158 | * redundant ping-pong pointer arithmentic is neccessary to avoid | ||
159 | * type-conversion warnings related to the `volatile' qualifier and | ||
160 | * the fact that `jmp_buf' usually is an array type. | ||
161 | */ | ||
162 | if (setjmp(*((jmp_buf *)&bootstrap_buf)) == 0) | ||
163 | longjmp(*((jmp_buf *)&tramp_buf), 1); | ||
164 | |||
165 | /* | ||
166 | * Ok, we returned again, so now we're finished | ||
167 | */ | ||
168 | enable_irq(); | ||
169 | return true; | ||
170 | } | ||
171 | |||
172 | static void trampoline(int sig) | ||
173 | { | ||
174 | (void)sig; | ||
175 | /* sanity check, no other thread should be here */ | ||
176 | if (pthread_self() != main_thread) | ||
177 | return; | ||
178 | |||
179 | if (setjmp(*((jmp_buf *)&tramp_buf)) == 0) | ||
180 | { | ||
181 | sig_handler_called = true; | ||
182 | return; | ||
183 | } | ||
184 | /* longjump'd back in */ | ||
185 | bootstrap_context(); | ||
186 | } | ||
187 | |||
188 | void bootstrap_context(void) | ||
189 | { | ||
190 | /* copy to local storage so we can spawn further threads | ||
191 | * in the meantime */ | ||
192 | void (*thread_entry)(void) = thread_func; | ||
193 | struct ctx *t = thread_context; | ||
194 | |||
195 | /* | ||
196 | * Save current machine state (on new stack) and | ||
197 | * go back to caller until we're scheduled for real... | ||
198 | */ | ||
199 | if (setjmp(t->thread_buf) == 0) | ||
200 | longjmp(*((jmp_buf *)&bootstrap_buf), 1); | ||
201 | |||
202 | /* | ||
203 | * The new thread is now running: GREAT! | ||
204 | * Now we just invoke its init function.... | ||
205 | */ | ||
206 | thread_entry(); | ||
207 | DEBUGF("thread left\n"); | ||
208 | thread_exit(); | ||
209 | } | ||
210 | |||
211 | static inline void set_context(struct ctx *c) | ||
212 | { | ||
213 | longjmp(c->thread_buf, 1); | ||
214 | } | ||
215 | |||
216 | static inline void swap_context(struct ctx *old, struct ctx *new) | ||
217 | { | ||
218 | if (setjmp(old->thread_buf) == 0) | ||
219 | longjmp(new->thread_buf, 1); | ||
220 | } | ||
221 | |||
222 | static inline void get_context(struct ctx *c) | ||
223 | { | ||
224 | setjmp(c->thread_buf); | ||
225 | } | ||
226 | |||
227 | |||
228 | static void setup_thread(struct regs *context); | ||
229 | |||
230 | #define INIT_MAIN_THREAD | ||
231 | static void init_main_thread(void *addr) | ||
232 | { | ||
233 | /* get a context for the main thread so that we can jump to it from | ||
234 | * other threads */ | ||
235 | struct regs *context = (struct regs*)addr; | ||
236 | context->uc = &thread_bufs[curr_uc++]; | ||
237 | get_context(context->uc); | ||
238 | } | ||
239 | |||
240 | #define THREAD_STARTUP_INIT(core, thread, function) \ | ||
241 | ({ (thread)->context.stack_size = (thread)->stack_size, \ | ||
242 | (thread)->context.stack = (uintptr_t)(thread)->stack; \ | ||
243 | (thread)->context.start = function; }) | ||
244 | |||
245 | |||
246 | |||
247 | /* | ||
248 | * Prepare context to make the thread runnable by calling swapcontext on it | ||
249 | */ | ||
250 | static void setup_thread(struct regs *context) | ||
251 | { | ||
252 | void (*fn)(void) = context->start; | ||
253 | context->uc = &thread_bufs[curr_uc++]; | ||
254 | while (!make_context(context->uc, fn, (char*)context->stack, context->stack_size)) | ||
255 | DEBUGF("Thread creation failed. Retrying"); | ||
256 | } | ||
257 | |||
258 | |||
259 | /* | ||
260 | * Save the ucontext_t pointer for later use in swapcontext() | ||
261 | * | ||
262 | * Cannot do getcontext() here, because jumping back to the context | ||
263 | * resumes after the getcontext call (i.e. store_context), but we need | ||
264 | * to resume from load_context() | ||
265 | */ | ||
266 | static inline void store_context(void* addr) | ||
267 | { | ||
268 | struct regs *r = (struct regs*)addr; | ||
269 | target_context = r->uc; | ||
270 | } | ||
271 | |||
272 | /* | ||
273 | * Perform context switch | ||
274 | */ | ||
275 | static inline void load_context(const void* addr) | ||
276 | { | ||
277 | struct regs *r = (struct regs*)addr; | ||
278 | if (UNLIKELY(r->start)) | ||
279 | { | ||
280 | setup_thread(r); | ||
281 | r->start = NULL; | ||
282 | } | ||
283 | swap_context(target_context, r->uc); | ||
284 | } | ||
285 | |||
286 | /* | ||
287 | * play nice with the host and sleep while waiting for the tick */ | ||
288 | extern void wait_for_interrupt(void); | ||
289 | static inline void core_sleep(void) | ||
290 | { | ||
291 | enable_irq(); | ||
292 | wait_for_interrupt(); | ||
293 | } | ||
294 | |||
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 | |||