From e45b9164ef487978415818d89e076876abb4ed74 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Tue, 11 May 2010 21:48:15 +0200 Subject: [PATCH] Move main into main.c and use the implicit sdl main thread as the rockbox main thread (instead of creating one). --- apps/main.c | 13 ++++++-- firmware/target/hosted/sdl/system-sdl.c | 5 +-- firmware/target/hosted/sdl/system-sdl.h | 1 + uisimulator/sdl/thread-sdl.c | 58 +++++---------------------------- uisimulator/sdl/uisdl.c | 52 +++-------------------------- 5 files changed, 28 insertions(+), 101 deletions(-) diff --git a/apps/main.c b/apps/main.c index b043daced..e4b9286b4 100644 --- a/apps/main.c +++ b/apps/main.c @@ -124,8 +124,14 @@ const char appsversion[]=APPSVERSION; static void init(void); -#ifdef SIMULATOR -void app_main(void) +#ifdef HAVE_SDL +#if defined(WIN32) && defined(main) +/* Don't use SDL_main on windows -> no more stdio redirection */ +#undef main +#endif +int main(int argc, char *argv[]) +{ + sys_handle_argv(argc, argv); #else /* main(), and various functions called by main() and init() may be * be INIT_ATTR. These functions must not be called after the final call @@ -133,8 +139,8 @@ void app_main(void) * see definition of INIT_ATTR in config.h */ int main(void) INIT_ATTR __attribute__((noreturn)); int main(void) -#endif { +#endif int i; CHART(">init"); init(); @@ -313,6 +319,7 @@ static void init_tagcache(void) static void init(void) { + system_init(); kernel_init(); buffer_init(); enable_irq(); diff --git a/firmware/target/hosted/sdl/system-sdl.c b/firmware/target/hosted/sdl/system-sdl.c index 71a45dae5..d7a3de673 100644 --- a/firmware/target/hosted/sdl/system-sdl.c +++ b/firmware/target/hosted/sdl/system-sdl.c @@ -40,11 +40,12 @@ /* * these 3 are set in uisdl.c:main()*/ -extern SDL_Surface *gui_surface; +SDL_Surface *gui_surface; extern bool background; /* use backgrounds by default */ #ifdef HAVE_REMOTE_LCD extern bool showremote = true; /* include remote by default */ #endif +extern int display_zoom; void sys_poweroff(void) @@ -72,7 +73,7 @@ void system_init(void) DEBUGF("warn: %s\n", SDL_GetError()); } } - + /* Set things up */ if (background) { diff --git a/firmware/target/hosted/sdl/system-sdl.h b/firmware/target/hosted/sdl/system-sdl.h index 8ecffee5c..73c2a4b0b 100644 --- a/firmware/target/hosted/sdl/system-sdl.h +++ b/firmware/target/hosted/sdl/system-sdl.h @@ -43,6 +43,7 @@ void sim_enter_irq_handler(void); void sim_exit_irq_handler(void); void sim_kernel_shutdown(void); void sys_poweroff(void); +void sys_handle_argv(int argc, char *argv[]); extern long start_tick; diff --git a/uisimulator/sdl/thread-sdl.c b/uisimulator/sdl/thread-sdl.c index 459c17406..46c1e9883 100644 --- a/uisimulator/sdl/thread-sdl.c +++ b/uisimulator/sdl/thread-sdl.c @@ -56,6 +56,9 @@ struct thread_entry threads[MAXTHREADS]; * in their start routines responding to messages so this is the only * way to get them back in there so they may exit */ static jmp_buf thread_jmpbufs[MAXTHREADS]; +/* this mutex locks out other Rockbox threads while one runs, + * that enables us to simulate a cooperative environment even if + * the host is preemptive */ static SDL_mutex *m; static volatile bool threads_exit = false; @@ -79,6 +82,7 @@ void thread_sdl_shutdown(void) for (i = 0; i < MAXTHREADS; i++) { struct thread_entry *thread = &threads[i]; + /* exit all current threads, except the main one */ if (thread->context.t != NULL) { /* Signal thread on delay or block */ @@ -128,30 +132,9 @@ static struct thread_entry * find_empty_thread_slot(void) return thread; } -/* Do main thread creation in this file scope to avoid the need to double- - return to a prior call-level which would be unaware of the fact setjmp - was used */ -extern void app_main(void *param); -static int thread_sdl_app_main(void *param) -{ - SDL_LockMutex(m); - cores[CURRENT_CORE].running = &threads[0]; - - /* Set the jump address for return */ - if (setjmp(thread_jmpbufs[0]) == 0) - { - app_main(param); - /* should not ever be reached but... */ - THREAD_PANICF("app_main returned!\n"); - } - - /* Unlock and exit */ - SDL_UnlockMutex(m); - return 0; -} /* Initialize SDL threading */ -bool thread_sdl_init(void *param) +void init_threads(void) { struct thread_entry *thread; int n; @@ -164,7 +147,7 @@ bool thread_sdl_init(void *param) if (SDL_LockMutex(m) == -1) { fprintf(stderr, "Couldn't lock mutex\n"); - return false; + return; } /* Initialize all IDs */ @@ -180,28 +163,18 @@ bool thread_sdl_init(void *param) thread->name = "main"; thread->state = STATE_RUNNING; thread->context.s = SDL_CreateSemaphore(0); + thread->context.t = NULL; /* NULL for the implicit main thread */ cores[CURRENT_CORE].running = thread; if (thread->context.s == NULL) { fprintf(stderr, "Failed to create main semaphore\n"); - return false; - } - - thread->context.t = SDL_CreateThread(thread_sdl_app_main, param); - - if (thread->context.t == NULL) - { - SDL_DestroySemaphore(thread->context.s); - fprintf(stderr, "Failed to create main thread\n"); - return false; + return; } THREAD_SDL_DEBUGF("Main thread: %p\n", thread); - SDL_UnlockMutex(m); - *(struct thread_entry**)param = thread; - return true; + return; } void thread_sdl_exception_wait(void) @@ -530,19 +503,6 @@ unsigned int create_thread(void (*function)(void), return thread->id; } -void init_threads(void) -{ - /* Main thread is already initialized */ - if (cores[CURRENT_CORE].running != &threads[0]) - { - THREAD_PANICF("Wrong main thread in init_threads: %p\n", - cores[CURRENT_CORE].running); - } - - THREAD_SDL_DEBUGF("First Thread: %d (%s)\n", - 0, THREAD_SDL_GET_NAME(&threads[0])); -} - #ifndef ALLOW_REMOVE_THREAD static void remove_thread(unsigned int thread_id) #else diff --git a/uisimulator/sdl/uisdl.c b/uisimulator/sdl/uisdl.c index b1448e1d3..680fb3025 100644 --- a/uisimulator/sdl/uisdl.c +++ b/uisimulator/sdl/uisdl.c @@ -19,36 +19,14 @@ * ****************************************************************************/ +#include #include +#include #include -#include -#include "autoconf.h" -#include "button.h" -#include "system.h" -#include "thread.h" -#include "kernel.h" -#include "uisdl.h" -#include "lcd-sdl.h" -#ifdef HAVE_LCD_BITMAP -#include "lcd-bitmap.h" -#elif defined(HAVE_LCD_CHARCELLS) -#include "lcd-charcells.h" -#endif -#ifdef HAVE_REMOTE_LCD -#include "lcd-remote-bitmap.h" -#endif -#include "thread-sdl.h" -#include "SDL_mutex.h" -#include "SDL_thread.h" -#include "math.h" - +#include "config.h" /* extern functions */ -extern void new_key(int key); -extern int xy2button( int x, int y); -void button_event(int key, bool pressed); -SDL_Surface *gui_surface; bool background = true; /* use backgrounds by default */ #ifdef HAVE_REMOTE_LCD bool showremote = true; /* include remote by default */ @@ -60,6 +38,7 @@ bool lcd_display_redraw = true; /* Used for player simulator */ char having_new_lcd = true; /* Used for player simulator */ bool sim_alarm_wakeup = false; const char *sim_root_dir = NULL; +extern int display_zoom; #ifdef DEBUG bool debug_audio = false; @@ -68,11 +47,7 @@ bool debug_audio = false; bool debug_wps = false; int wps_verbose_level = 3; -#if defined(WIN32) && defined(main) -/* Don't use SDL_main on windows -> no more stdio redirection */ -#undef main -#endif -int main(int argc, char *argv[]) +int sys_handle_argv(int argc, char *argv[]) { if (argc >= 1) { @@ -164,26 +139,9 @@ int main(int argc, char *argv[]) } } } - if (display_zoom > 1) { background = false; } - - /* need to call it here for the time being, before enabling threads */ - system_init(); - struct thread_entry *t = NULL; - int status = 0; - /* app_main will be called by the new main thread */ - if (!thread_sdl_init(&t)) { - fprintf(stderr, "thread_sdl_init failed\n"); - return -1; - } - - /* HACK!! */ - if (t) - SDL_WaitThread(t->context.t, &status); - - sys_poweroff(); return 0; } -- 2.11.4.GIT