From 3f9c7c2ab5edef46346fed58807307ea4499b39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Stenberg?= Date: Tue, 25 Jun 2002 12:04:23 +0000 Subject: [PATCH] Demystified code git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1183 a1c6a512-1295-4272-9138-f99709370657 --- firmware/thread.c | 71 ++++++++++++++++++++--------------------------- firmware/thread.h | 2 +- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/firmware/thread.c b/firmware/thread.c index f5167872de..fc8370b42d 100644 --- a/firmware/thread.c +++ b/firmware/thread.c @@ -18,33 +18,25 @@ ****************************************************************************/ #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 mach; - unsigned int macl; - unsigned int sr; /* Status register */ - void* pr; /* Procedure register */ - } regs; -} ctx_t; + unsigned int r[7]; /* Registers r8 thru r14 */ + void *sp; /* Stack pointer (r15) */ + unsigned int mach; + unsigned int macl; + unsigned int sr; /* Status register */ + void* pr; /* Procedure register */ +}; -typedef struct -{ - int num_threads; - int current; - ctx_t ctx[MAXTHREADS]; -} thread_t; - -static thread_t threads; +static int num_threads; +static int current_thread; +static struct regs thread_contexts[MAXTHREADS]; /*--------------------------------------------------------------------------- * Store non-volatile context. *--------------------------------------------------------------------------- */ -static inline void stctx(void* addr) +static inline void store_context(void* addr) { asm volatile ("add #48, %0\n\t" "sts.l pr, @-%0\n\t" @@ -65,7 +57,7 @@ static inline void stctx(void* addr) * 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" "mov.l @%0+,r9\n\t" @@ -89,16 +81,15 @@ static inline void ldctx(void* addr) */ void switch_thread(void) { - int ct; - int nt; - thread_t* t = &threads; + int current; + int next; - nt = ct = t->current; - if (++nt >= t->num_threads) - nt = 0; - t->current = nt; - stctx(&t->ctx[ct]); - ldctx(&t->ctx[nt]); + next = current = current_thread; + if (++next >= num_threads) + next = 0; + current_thread = next; + store_context(&thread_contexts[current]); + load_context(&thread_contexts[next]); } /*--------------------------------------------------------------------------- @@ -106,27 +97,25 @@ void switch_thread(void) * 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 (t->num_threads >= MAXTHREADS) + if (num_threads >= MAXTHREADS) return -1; else { - ctx_t* ctxp = &t->ctx[t->num_threads++]; - stctx(ctxp); + struct regs* regs = &thread_contexts[num_threads++]; + store_context(regs); /* Subtract 4 to leave room for the PR push in ldctx() Align it on an even 32 bit boundary */ - ctxp->regs.sp = (void*)(((unsigned int)sp + stk_size - 4) & ~3); - ctxp->regs.sr = 0; - ctxp->regs.pr = fp; + regs->sp = (void*)(((unsigned int)stack + stack_size - 4) & ~3); + regs->sr = 0; + regs->pr = function; } return 0; } void init_threads(void) { - threads.num_threads = 1; /* We have 1 thread to begin with */ - threads.current = 0; /* The current thread is number 0 */ + num_threads = 1; /* We have 1 thread to begin with */ + current_thread = 0; /* The current thread is number 0 */ } diff --git a/firmware/thread.h b/firmware/thread.h index dd42c8cd35..62cd4607d2 100644 --- a/firmware/thread.h +++ b/firmware/thread.h @@ -21,7 +21,7 @@ #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 init_threads(void);