2006-02-03 15:19:58 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Dan Everton
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2006-02-03 15:19:58 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-10 03:49:12 +00:00
|
|
|
#include <stdbool.h>
|
2006-02-03 15:19:58 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_thread.h>
|
|
|
|
#include <stdlib.h>
|
2007-09-08 12:20:53 +00:00
|
|
|
#include <memory.h>
|
2007-09-10 03:49:12 +00:00
|
|
|
#include <setjmp.h>
|
2007-10-26 23:11:18 +00:00
|
|
|
#include "system-sdl.h"
|
2006-02-03 15:19:58 +00:00
|
|
|
#include "thread-sdl.h"
|
2008-03-25 02:34:12 +00:00
|
|
|
#include "system.h"
|
2006-02-03 15:19:58 +00:00
|
|
|
#include "kernel.h"
|
2007-03-24 01:27:29 +00:00
|
|
|
#include "thread.h"
|
2006-02-03 15:19:58 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
/* Define this as 1 to show informational messages that are not errors. */
|
|
|
|
#define THREAD_SDL_DEBUGF_ENABLED 0
|
2006-02-03 15:19:58 +00:00
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
#if THREAD_SDL_DEBUGF_ENABLED
|
|
|
|
#define THREAD_SDL_DEBUGF(...) DEBUGF(__VA_ARGS__)
|
|
|
|
static char __name[32];
|
|
|
|
#define THREAD_SDL_GET_NAME(thread) \
|
2008-03-25 02:34:12 +00:00
|
|
|
({ thread_get_name(__name, ARRAYLEN(__name), thread); __name; })
|
2007-09-08 12:20:53 +00:00
|
|
|
#else
|
|
|
|
#define THREAD_SDL_DEBUGF(...)
|
|
|
|
#define THREAD_SDL_GET_NAME(thread)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define THREAD_PANICF(str...) \
|
|
|
|
({ fprintf(stderr, str); exit(-1); })
|
|
|
|
|
2007-10-26 23:11:18 +00:00
|
|
|
/* Thread/core entries as in rockbox core */
|
|
|
|
struct core_entry cores[NUM_CORES];
|
2007-09-10 03:49:12 +00:00
|
|
|
struct thread_entry threads[MAXTHREADS];
|
|
|
|
/* Jump buffers for graceful exit - kernel threads don't stay neatly
|
|
|
|
* 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];
|
2007-09-08 12:20:53 +00:00
|
|
|
static SDL_mutex *m;
|
2007-09-10 03:49:12 +00:00
|
|
|
static bool threads_exit = false;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2007-09-09 01:59:07 +00:00
|
|
|
extern long start_tick;
|
|
|
|
|
2007-09-10 03:49:12 +00:00
|
|
|
void thread_sdl_shutdown(void)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-09-10 03:49:12 +00:00
|
|
|
/* Take control */
|
2007-09-08 12:20:53 +00:00
|
|
|
SDL_LockMutex(m);
|
2007-09-10 03:49:12 +00:00
|
|
|
|
|
|
|
/* Tell all threads jump back to their start routines, unlock and exit
|
|
|
|
gracefully - we'll check each one in turn for it's status. Threads
|
|
|
|
_could_ terminate via remove_thread or multiple threads could exit
|
|
|
|
on each unlock but that is safe. */
|
|
|
|
threads_exit = true;
|
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
for (i = 0; i < MAXTHREADS; i++)
|
|
|
|
{
|
|
|
|
struct thread_entry *thread = &threads[i];
|
|
|
|
if (thread->context.t != NULL)
|
|
|
|
{
|
2007-09-10 03:49:12 +00:00
|
|
|
/* Signal thread on delay or block */
|
|
|
|
SDL_Thread *t = thread->context.t;
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_SemPost(thread->context.s);
|
2007-09-10 03:49:12 +00:00
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
/* Wait for it to finish */
|
|
|
|
SDL_WaitThread(t, NULL);
|
|
|
|
/* Relock for next thread signal */
|
2007-09-08 12:20:53 +00:00
|
|
|
SDL_LockMutex(m);
|
2007-09-10 03:49:12 +00:00
|
|
|
}
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
2007-09-10 03:49:12 +00:00
|
|
|
|
|
|
|
SDL_UnlockMutex(m);
|
2007-09-08 12:20:53 +00:00
|
|
|
SDL_DestroyMutex(m);
|
|
|
|
}
|
|
|
|
|
2007-09-10 03:49:12 +00:00
|
|
|
/* 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);
|
2008-03-25 02:34:12 +00:00
|
|
|
cores[CURRENT_CORE].running = &threads[0];
|
2007-09-10 03:49:12 +00:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *thread;
|
|
|
|
memset(cores, 0, sizeof(cores));
|
2007-09-10 03:49:12 +00:00
|
|
|
memset(threads, 0, sizeof(threads));
|
|
|
|
|
|
|
|
m = SDL_CreateMutex();
|
|
|
|
|
|
|
|
if (SDL_LockMutex(m) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Couldn't lock mutex\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Slot 0 is reserved for the main thread - initialize it here and
|
|
|
|
then create the SDL thread - it is possible to have a quick, early
|
|
|
|
shutdown try to access the structure. */
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = &threads[0];
|
|
|
|
thread->stack = (uintptr_t *)" ";
|
|
|
|
thread->stack_size = 8;
|
|
|
|
thread->name = "main";
|
|
|
|
thread->state = STATE_RUNNING;
|
|
|
|
thread->context.s = SDL_CreateSemaphore(0);
|
|
|
|
cores[CURRENT_CORE].running = thread;
|
2008-01-19 13:27:47 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread->context.s == NULL)
|
2007-09-10 03:49:12 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
fprintf(stderr, "Failed to create main semaphore\n");
|
2007-09-10 03:49:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->context.t = SDL_CreateThread(thread_sdl_app_main, param);
|
2007-09-10 03:49:12 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread->context.t == NULL)
|
2007-09-10 03:49:12 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_DestroySemaphore(thread->context.s);
|
2007-09-10 03:49:12 +00:00
|
|
|
fprintf(stderr, "Failed to create main thread\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
THREAD_SDL_DEBUGF("Main thread: %p\n", thread);
|
2007-09-10 03:49:12 +00:00
|
|
|
|
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-03 14:01:12 +00:00
|
|
|
/* A way to yield and leave the threading system for extended periods */
|
|
|
|
void thread_sdl_thread_lock(void *me)
|
|
|
|
{
|
|
|
|
SDL_LockMutex(m);
|
2008-03-25 02:34:12 +00:00
|
|
|
cores[CURRENT_CORE].running = (struct thread_entry *)me;
|
2007-12-03 14:01:12 +00:00
|
|
|
|
|
|
|
if (threads_exit)
|
2008-03-25 02:34:12 +00:00
|
|
|
thread_exit();
|
2007-12-03 14:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void * thread_sdl_thread_unlock(void)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2007-12-03 14:01:12 +00:00
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
static struct thread_entry * find_empty_thread_slot(void)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *thread = NULL;
|
2007-09-08 12:20:53 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < MAXTHREADS; n++)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
int state = threads[n].state;
|
|
|
|
|
|
|
|
if (state == STATE_KILLED)
|
2008-03-25 02:34:12 +00:00
|
|
|
{
|
|
|
|
thread = &threads[n];
|
2007-09-08 12:20:53 +00:00
|
|
|
break;
|
2008-03-25 02:34:12 +00:00
|
|
|
}
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
return thread;
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
static void add_to_list_l(struct thread_entry **list,
|
|
|
|
struct thread_entry *thread)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
|
|
|
if (*list == NULL)
|
|
|
|
{
|
|
|
|
/* Insert into unoccupied list */
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->l.next = thread;
|
|
|
|
thread->l.prev = thread;
|
2007-09-08 12:20:53 +00:00
|
|
|
*list = thread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Insert last */
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->l.next = *list;
|
|
|
|
thread->l.prev = (*list)->l.prev;
|
|
|
|
thread->l.prev->l.next = thread;
|
|
|
|
(*list)->l.prev = thread;
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
static void remove_from_list_l(struct thread_entry **list,
|
|
|
|
struct thread_entry *thread)
|
2006-02-03 15:19:58 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
if (thread == thread->l.next)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
|
|
|
/* The only item */
|
|
|
|
*list = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2006-09-03 20:21:21 +00:00
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
if (thread == *list)
|
2006-07-31 15:02:39 +00:00
|
|
|
{
|
2007-09-08 12:20:53 +00:00
|
|
|
/* List becomes next item */
|
2007-10-16 01:25:17 +00:00
|
|
|
*list = thread->l.next;
|
2006-07-31 15:02:39 +00:00
|
|
|
}
|
2007-09-08 12:20:53 +00:00
|
|
|
|
|
|
|
/* Fix links to jump over the removed entry. */
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->l.prev->l.next = thread->l.next;
|
|
|
|
thread->l.next->l.prev = thread->l.prev;
|
2006-02-03 15:19:58 +00:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
struct thread_entry *thread_get_current(void)
|
2006-02-03 15:19:58 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
return cores[CURRENT_CORE].running;
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
void switch_thread(void)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
enable_irq();
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
switch (current->state)
|
|
|
|
{
|
|
|
|
case STATE_RUNNING:
|
|
|
|
{
|
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
/* Any other thread waiting already will get it first */
|
|
|
|
SDL_LockMutex(m);
|
|
|
|
break;
|
|
|
|
} /* STATE_RUNNING: */
|
|
|
|
|
|
|
|
case STATE_BLOCKED:
|
|
|
|
{
|
|
|
|
int oldlevel;
|
|
|
|
|
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
SDL_SemWait(current->context.s);
|
|
|
|
SDL_LockMutex(m);
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2008-03-25 02:34:12 +00:00
|
|
|
current->state = STATE_RUNNING;
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
|
|
|
} /* STATE_BLOCKED: */
|
|
|
|
|
|
|
|
case STATE_BLOCKED_W_TMO:
|
|
|
|
{
|
|
|
|
int result, oldlevel;
|
|
|
|
|
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
result = SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
|
|
|
|
SDL_LockMutex(m);
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
if (current->state == STATE_BLOCKED_W_TMO)
|
|
|
|
{
|
|
|
|
/* Timed out */
|
|
|
|
remove_from_list_l(current->bqp, current);
|
|
|
|
|
|
|
|
#ifdef HAVE_WAKEUP_EXT_CB
|
|
|
|
if (current->wakeup_ext_cb != NULL)
|
|
|
|
current->wakeup_ext_cb(current);
|
|
|
|
#endif
|
|
|
|
current->state = STATE_RUNNING;
|
|
|
|
}
|
2007-09-10 03:49:12 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (result == SDL_MUTEX_TIMEDOUT)
|
|
|
|
{
|
|
|
|
/* Other signals from an explicit wake could have been made before
|
|
|
|
* arriving here if we timed out waiting for the semaphore. Make
|
|
|
|
* sure the count is reset. */
|
|
|
|
while (SDL_SemValue(current->context.s) > 0)
|
|
|
|
SDL_SemTryWait(current->context.s);
|
|
|
|
}
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2008-03-25 02:34:12 +00:00
|
|
|
break;
|
|
|
|
} /* STATE_BLOCKED_W_TMO: */
|
|
|
|
|
|
|
|
case STATE_SLEEPING:
|
|
|
|
{
|
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
SDL_SemWaitTimeout(current->context.s, current->tmo_tick);
|
|
|
|
SDL_LockMutex(m);
|
|
|
|
current->state = STATE_RUNNING;
|
|
|
|
break;
|
|
|
|
} /* STATE_SLEEPING: */
|
|
|
|
}
|
|
|
|
|
|
|
|
cores[CURRENT_CORE].running = current;
|
|
|
|
|
|
|
|
if (threads_exit)
|
|
|
|
thread_exit();
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_thread(int ticks)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2007-09-09 01:59:07 +00:00
|
|
|
int rem;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
current->state = STATE_SLEEPING;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2007-09-09 01:59:07 +00:00
|
|
|
rem = (SDL_GetTicks() - start_tick) % (1000/HZ);
|
|
|
|
if (rem < 0)
|
|
|
|
rem = 0;
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
current->tmo_tick = (1000/HZ) * ticks + ((1000/HZ)-1) - rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void block_thread(struct thread_entry *current)
|
|
|
|
{
|
|
|
|
current->state = STATE_BLOCKED;
|
|
|
|
add_to_list_l(current->bqp, current);
|
|
|
|
}
|
|
|
|
|
|
|
|
void block_thread_w_tmo(struct thread_entry *current, int ticks)
|
|
|
|
{
|
|
|
|
current->state = STATE_BLOCKED_W_TMO;
|
|
|
|
current->tmo_tick = (1000/HZ)*ticks;
|
|
|
|
add_to_list_l(current->bqp, current);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int wakeup_thread(struct thread_entry **list)
|
|
|
|
{
|
|
|
|
struct thread_entry *thread = *list;
|
2007-09-10 03:49:12 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (thread != NULL)
|
2007-09-10 03:49:12 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
switch (thread->state)
|
|
|
|
{
|
|
|
|
case STATE_BLOCKED:
|
|
|
|
case STATE_BLOCKED_W_TMO:
|
|
|
|
remove_from_list_l(list, thread);
|
|
|
|
thread->state = STATE_RUNNING;
|
|
|
|
SDL_SemPost(thread->context.s);
|
|
|
|
return THREAD_OK;
|
|
|
|
}
|
2007-09-10 03:49:12 +00:00
|
|
|
}
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
return THREAD_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int thread_queue_wake(struct thread_entry **list)
|
|
|
|
{
|
|
|
|
unsigned int result = THREAD_NONE;
|
|
|
|
|
|
|
|
for (;;)
|
2007-09-10 03:49:12 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
unsigned int rc = wakeup_thread(list);
|
2007-09-09 01:59:07 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
if (rc == THREAD_NONE)
|
|
|
|
break;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
result |= rc;
|
|
|
|
}
|
2007-09-10 03:49:12 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_thaw(struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
if (thread->state == STATE_FROZEN)
|
|
|
|
{
|
|
|
|
thread->state = STATE_RUNNING;
|
|
|
|
SDL_SemPost(thread->context.s);
|
|
|
|
}
|
2006-02-03 15:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int runthread(void *data)
|
|
|
|
{
|
2007-09-08 12:20:53 +00:00
|
|
|
struct thread_entry *current;
|
2007-09-10 03:49:12 +00:00
|
|
|
jmp_buf *current_jmpbuf;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
|
|
|
/* Cannot access thread variables before locking the mutex as the
|
|
|
|
data structures may not be filled-in yet. */
|
|
|
|
SDL_LockMutex(m);
|
2008-03-25 02:34:12 +00:00
|
|
|
cores[CURRENT_CORE].running = (struct thread_entry *)data;
|
|
|
|
current = cores[CURRENT_CORE].running;
|
|
|
|
current_jmpbuf = &thread_jmpbufs[current - threads];
|
2007-09-10 03:49:12 +00:00
|
|
|
|
|
|
|
/* Setup jump for exit */
|
|
|
|
if (setjmp(*current_jmpbuf) == 0)
|
|
|
|
{
|
|
|
|
/* Run the thread routine */
|
2007-10-16 01:25:17 +00:00
|
|
|
if (current->state == STATE_FROZEN)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
SDL_SemWait(current->context.s);
|
|
|
|
SDL_LockMutex(m);
|
|
|
|
cores[CURRENT_CORE].running = current;
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!threads_exit)
|
|
|
|
{
|
|
|
|
current->context.start();
|
|
|
|
THREAD_SDL_DEBUGF("Thread Done: %d (%s)\n",
|
|
|
|
current - threads, THREAD_SDL_GET_NAME(current));
|
|
|
|
/* Thread routine returned - suicide */
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread_exit();
|
2007-09-10 03:49:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Unlock and exit */
|
|
|
|
SDL_UnlockMutex(m);
|
|
|
|
}
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2006-02-03 15:19:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-24 01:27:29 +00:00
|
|
|
struct thread_entry*
|
2008-03-25 02:34:12 +00:00
|
|
|
create_thread(void (*function)(void), void* stack, size_t stack_size,
|
2007-10-16 01:25:17 +00:00
|
|
|
unsigned flags, const char *name)
|
2006-02-03 15:19:58 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *thread;
|
2007-03-24 01:27:29 +00:00
|
|
|
SDL_Thread* t;
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_sem *s;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
|
|
|
THREAD_SDL_DEBUGF("Creating thread: (%s)\n", name ? name : "");
|
2006-02-03 15:19:58 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = find_empty_thread_slot();
|
|
|
|
if (thread == NULL)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
|
|
|
DEBUGF("Failed to find thread slot\n");
|
2007-03-24 01:27:29 +00:00
|
|
|
return NULL;
|
2006-02-03 15:19:58 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
s = SDL_CreateSemaphore(0);
|
|
|
|
if (s == NULL)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
DEBUGF("Failed to create semaphore\n");
|
2007-09-08 12:20:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
t = SDL_CreateThread(runthread, thread);
|
2007-09-08 12:20:53 +00:00
|
|
|
if (t == NULL)
|
|
|
|
{
|
|
|
|
DEBUGF("Failed to create SDL thread\n");
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_DestroySemaphore(s);
|
2007-09-08 12:20:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->stack = stack;
|
|
|
|
thread->stack_size = stack_size;
|
|
|
|
thread->name = name;
|
|
|
|
thread->state = (flags & CREATE_THREAD_FROZEN) ?
|
2007-10-16 01:25:17 +00:00
|
|
|
STATE_FROZEN : STATE_RUNNING;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread->context.start = function;
|
|
|
|
thread->context.t = t;
|
|
|
|
thread->context.s = s;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
|
|
|
THREAD_SDL_DEBUGF("New Thread: %d (%s)\n",
|
2008-03-25 02:34:12 +00:00
|
|
|
thread - threads, THREAD_SDL_GET_NAME(thread));
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
return thread;
|
2006-02-03 15:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_threads(void)
|
|
|
|
{
|
2007-09-10 03:49:12 +00:00
|
|
|
/* Main thread is already initialized */
|
2008-03-25 02:34:12 +00:00
|
|
|
if (cores[CURRENT_CORE].running != &threads[0])
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
THREAD_PANICF("Wrong main thread in init_threads: %p\n",
|
|
|
|
cores[CURRENT_CORE].running);
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
THREAD_SDL_DEBUGF("First Thread: %d (%s)\n",
|
2007-09-10 03:49:12 +00:00
|
|
|
0, THREAD_SDL_GET_NAME(&threads[0]));
|
2006-02-03 15:19:58 +00:00
|
|
|
}
|
2007-03-30 16:02:42 +00:00
|
|
|
|
|
|
|
void remove_thread(struct thread_entry *thread)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
2007-09-08 12:20:53 +00:00
|
|
|
SDL_Thread *t;
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_sem *s;
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
int oldlevel = disable_irq_save();
|
2007-10-26 23:11:18 +00:00
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
if (thread == NULL)
|
|
|
|
{
|
|
|
|
thread = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = thread->context.t;
|
2008-03-25 02:34:12 +00:00
|
|
|
s = thread->context.s;
|
2007-09-08 12:20:53 +00:00
|
|
|
thread->context.t = NULL;
|
|
|
|
|
|
|
|
if (thread != current)
|
2007-10-16 01:25:17 +00:00
|
|
|
{
|
|
|
|
switch (thread->state)
|
|
|
|
{
|
|
|
|
case STATE_BLOCKED:
|
|
|
|
case STATE_BLOCKED_W_TMO:
|
|
|
|
/* Remove thread from object it's waiting on */
|
2008-03-25 02:34:12 +00:00
|
|
|
remove_from_list_l(thread->bqp, thread);
|
|
|
|
|
|
|
|
#ifdef HAVE_WAKEUP_EXT_CB
|
|
|
|
if (thread->wakeup_ext_cb != NULL)
|
|
|
|
thread->wakeup_ext_cb(thread);
|
|
|
|
#endif
|
2007-10-16 01:25:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_SemPost(s);
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
2007-09-08 12:20:53 +00:00
|
|
|
|
|
|
|
THREAD_SDL_DEBUGF("Removing thread: %d (%s)\n",
|
|
|
|
thread - threads, THREAD_SDL_GET_NAME(thread));
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
thread->state = STATE_KILLED;
|
2008-03-25 02:34:12 +00:00
|
|
|
thread_queue_wake(&thread->queue);
|
2007-09-08 12:20:53 +00:00
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
SDL_DestroySemaphore(s);
|
2007-09-08 12:20:53 +00:00
|
|
|
|
|
|
|
if (thread == current)
|
|
|
|
{
|
2007-09-10 03:49:12 +00:00
|
|
|
/* Do a graceful exit - perform the longjmp back into the thread
|
|
|
|
function to return */
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-09-10 03:49:12 +00:00
|
|
|
longjmp(thread_jmpbufs[current - threads], 1);
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_KillThread(t);
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(oldlevel);
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
2008-03-25 02:34:12 +00:00
|
|
|
void thread_exit(void)
|
|
|
|
{
|
|
|
|
remove_thread(NULL);
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
void thread_wait(struct thread_entry *thread)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
struct thread_entry *current = cores[CURRENT_CORE].running;
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
if (thread == NULL)
|
2008-03-25 02:34:12 +00:00
|
|
|
thread = current;
|
2007-10-16 01:25:17 +00:00
|
|
|
|
|
|
|
if (thread->state != STATE_KILLED)
|
|
|
|
{
|
2008-03-25 02:34:12 +00:00
|
|
|
current->bqp = &thread->queue;
|
|
|
|
block_thread(current);
|
|
|
|
switch_thread();
|
2007-10-16 01:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:20:53 +00:00
|
|
|
int thread_stack_usage(const struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
return 50;
|
|
|
|
(void)thread;
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:25:17 +00:00
|
|
|
unsigned thread_get_status(const struct thread_entry *thread)
|
2007-09-08 12:20:53 +00:00
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
return thread->state;
|
2007-09-08 12:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return name if one or ID if none */
|
|
|
|
void thread_get_name(char *buffer, int size,
|
|
|
|
struct thread_entry *thread)
|
|
|
|
{
|
|
|
|
if (size <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*buffer = '\0';
|
|
|
|
|
|
|
|
if (thread)
|
|
|
|
{
|
|
|
|
/* Display thread name if one or ID if none */
|
|
|
|
bool named = thread->name && *thread->name;
|
|
|
|
const char *fmt = named ? "%s" : "%08lX";
|
|
|
|
intptr_t name = named ?
|
|
|
|
(intptr_t)thread->name : (intptr_t)thread;
|
|
|
|
snprintf(buffer, size, fmt, name);
|
|
|
|
}
|
2007-03-30 16:02:42 +00:00
|
|
|
}
|