summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/thread-win32.c
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2011-02-18 22:46:01 +0000
committerThomas Martitz <kugel@rockbox.org>2011-02-18 22:46:01 +0000
commit6d85de341928aef8178465c60122f3cbe76f5dd6 (patch)
treeff86c384a574ac20d3418c1b904ed4d0de1f6980 /firmware/target/hosted/thread-win32.c
parent3926c30705cc7235122e2f2e35ab506b53238cdf (diff)
downloadrockbox-6d85de341928aef8178465c60122f3cbe76f5dd6.tar.gz
rockbox-6d85de341928aef8178465c60122f3cbe76f5dd6.tar.bz2
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.c85
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 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2010 by Thomas Martitz
+ *
+ * Generic ARM threading support
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+
+#include <windows.h>
+#include "system.h"
+
+#define INIT_MAIN_THREAD
+
+#define THREAD_STARTUP_INIT(core, thread, function) \
+ ({ (thread)->context.stack_size = (thread)->stack_size, \
+ (thread)->context.stack = (uintptr_t)(thread)->stack; \
+ (thread)->context.start = function; })
+
+static void init_main_thread(void *addr)
+{
+ struct regs *context = (struct regs*)addr;
+ /* we must convert the current main thread to a fiber to be able to
+ * schedule other fibers */
+ context->uc = ConvertThreadToFiber(NULL);
+ context->stack_size = 0;
+}
+
+static inline void store_context(void* addr)
+{
+ (void)addr;
+ /* nothing to do here, Fibers continue after the SwitchToFiber call */
+}
+
+static void start_thread(void)
+{
+ void (*func)(void) = GetFiberData();
+ func();
+ /* go out if thread function returns */
+ thread_exit();
+}
+
+/*
+ * Load context and run it
+ *
+ * Resume execution from the last load_context call for the thread
+ */
+
+static inline void load_context(const void* addr)
+{
+ struct regs *context = (struct regs*)addr;
+ if (UNLIKELY(context->start))
+ { /* need setup before switching to it */
+ context->uc = CreateFiber(context->stack_size,
+ (LPFIBER_START_ROUTINE)start_thread, context->start);
+ /* can't assign stack pointer, only stack size */
+ context->stack_size = 0;
+ context->start = NULL;
+ }
+ SwitchToFiber(context->uc);
+}
+
+/*
+ * play nice with the host and sleep while waiting for the tick */
+static inline void core_sleep(void)
+{
+ enable_irq();
+ wait_for_interrupt();
+}
+