1
0
Fork 0
forked from len0rd/rockbox

Demystified code

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1183 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-06-25 12:04:23 +00:00
parent ef15d9983a
commit 3f9c7c2ab5
2 changed files with 31 additions and 42 deletions

View file

@ -18,33 +18,25 @@
****************************************************************************/ ****************************************************************************/
#include "thread.h" #include "thread.h"
typedef union struct regs
{ {
struct regs_t unsigned int r[7]; /* Registers r8 thru r14 */
{ void *sp; /* Stack pointer (r15) */
unsigned int r[7]; /* Registers r8 thru r14 */ unsigned int mach;
void *sp; /* Stack pointer (r15) */ unsigned int macl;
unsigned int mach; unsigned int sr; /* Status register */
unsigned int macl; void* pr; /* Procedure register */
unsigned int sr; /* Status register */ };
void* pr; /* Procedure register */
} regs;
} ctx_t;
typedef struct static int num_threads;
{ static int current_thread;
int num_threads; static struct regs thread_contexts[MAXTHREADS];
int current;
ctx_t ctx[MAXTHREADS];
} thread_t;
static thread_t threads;
/*--------------------------------------------------------------------------- /*---------------------------------------------------------------------------
* Store non-volatile context. * Store non-volatile context.
*--------------------------------------------------------------------------- *---------------------------------------------------------------------------
*/ */
static inline void stctx(void* addr) static inline void store_context(void* addr)
{ {
asm volatile ("add #48, %0\n\t" asm volatile ("add #48, %0\n\t"
"sts.l pr, @-%0\n\t" "sts.l pr, @-%0\n\t"
@ -65,7 +57,7 @@ static inline void stctx(void* addr)
* Load non-volatile context. * Load non-volatile context.
*--------------------------------------------------------------------------- *---------------------------------------------------------------------------
*/ */
static inline void ldctx(void* addr) static inline void load_context(void* addr)
{ {
asm volatile ("mov.l @%0+,r8\n\t" asm volatile ("mov.l @%0+,r8\n\t"
"mov.l @%0+,r9\n\t" "mov.l @%0+,r9\n\t"
@ -89,16 +81,15 @@ static inline void ldctx(void* addr)
*/ */
void switch_thread(void) void switch_thread(void)
{ {
int ct; int current;
int nt; int next;
thread_t* t = &threads;
nt = ct = t->current; next = current = current_thread;
if (++nt >= t->num_threads) if (++next >= num_threads)
nt = 0; next = 0;
t->current = nt; current_thread = next;
stctx(&t->ctx[ct]); store_context(&thread_contexts[current]);
ldctx(&t->ctx[nt]); load_context(&thread_contexts[next]);
} }
/*--------------------------------------------------------------------------- /*---------------------------------------------------------------------------
@ -106,27 +97,25 @@ void switch_thread(void)
* Return 0 if context area could be allocated, else -1. * Return 0 if context area could be allocated, else -1.
*--------------------------------------------------------------------------- *---------------------------------------------------------------------------
*/ */
int create_thread(void* fp, void* sp, int stk_size) int create_thread(void* function, void* stack, int stack_size)
{ {
thread_t* t = &threads; if (num_threads >= MAXTHREADS)
if (t->num_threads >= MAXTHREADS)
return -1; return -1;
else else
{ {
ctx_t* ctxp = &t->ctx[t->num_threads++]; struct regs* regs = &thread_contexts[num_threads++];
stctx(ctxp); store_context(regs);
/* Subtract 4 to leave room for the PR push in ldctx() /* Subtract 4 to leave room for the PR push in ldctx()
Align it on an even 32 bit boundary */ Align it on an even 32 bit boundary */
ctxp->regs.sp = (void*)(((unsigned int)sp + stk_size - 4) & ~3); regs->sp = (void*)(((unsigned int)stack + stack_size - 4) & ~3);
ctxp->regs.sr = 0; regs->sr = 0;
ctxp->regs.pr = fp; regs->pr = function;
} }
return 0; return 0;
} }
void init_threads(void) void init_threads(void)
{ {
threads.num_threads = 1; /* We have 1 thread to begin with */ num_threads = 1; /* We have 1 thread to begin with */
threads.current = 0; /* The current thread is number 0 */ current_thread = 0; /* The current thread is number 0 */
} }

View file

@ -21,7 +21,7 @@
#define MAXTHREADS 16 #define MAXTHREADS 16
int create_thread(void* fp, void* sp, int stk_size); int create_thread(void* function, void* stack, int stack_size);
void switch_thread(void); void switch_thread(void);
void init_threads(void); void init_threads(void);