1
0
Fork 0
forked from len0rd/rockbox

Threading changes in preparation for multiple core support

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@10681 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Daniel Ankers 2006-08-21 17:35:35 +00:00
parent e09dc8d513
commit 0aec12f3fd
6 changed files with 309 additions and 207 deletions

View file

@ -78,8 +78,8 @@
/*---------------------------------------------------*/ /*---------------------------------------------------*/
extern char ata_device; extern char ata_device;
extern int ata_io_address; extern int ata_io_address;
extern int num_threads; extern int num_threads[];
extern const char *thread_name[]; extern const char *thread_name[][MAXTHREADS];
#ifdef HAVE_LCD_BITMAP #ifdef HAVE_LCD_BITMAP
/* Test code!!! */ /* Test code!!! */
@ -88,6 +88,8 @@ bool dbg_os(void)
char buf[32]; char buf[32];
int i; int i;
int usage; int usage;
unsigned int core;
int line;
lcd_setmargins(0, 0); lcd_setmargins(0, 0);
lcd_setfont(FONT_SYSFIXED); lcd_setfont(FONT_SYSFIXED);
@ -95,12 +97,16 @@ bool dbg_os(void)
while(1) while(1)
{ {
lcd_puts(0, 0, "Stack usage:"); lcd_puts(0, 0, "Core and stack usage:");
for(i = 0; i < num_threads;i++) line = 0;
for(core = 0; core < NUM_CORES; core++)
{ {
usage = thread_stack_usage(i); for(i = 0; i < num_threads[core]; i++)
snprintf(buf, 32, "%s: %d%%", thread_name[i], usage); {
lcd_puts(0, 1+i, buf); usage = thread_stack_usage_on_core(core, i);
snprintf(buf, 32, "(%d) %s: %d%%", core, thread_name[core][i], usage);
lcd_puts(0, ++line, buf);
}
} }
lcd_update(); lcd_update();
@ -124,6 +130,7 @@ bool dbg_os(void)
{ {
lcd_puts(0, 0, "Stack usage"); lcd_puts(0, 0, "Stack usage");
/* Only Archos Player uses this - so assume a single core */
usage = thread_stack_usage(currval); usage = thread_stack_usage(currval);
snprintf(buf, 32, "%d: %d%% ", currval, usage); snprintf(buf, 32, "%d: %d%% ", currval, usage);
lcd_puts(0, 1, buf); lcd_puts(0, 1, buf);

View file

@ -151,6 +151,10 @@
#define USBOTG_ISP1362 1362 #define USBOTG_ISP1362 1362
#define USBOTG_M5636 5636 #define USBOTG_M5636 5636
/* Multiple cores */
#define CPU 0
#define COP 1
/* now go and pick yours */ /* now go and pick yours */
#if defined(ARCHOS_PLAYER) #if defined(ARCHOS_PLAYER)
#include "config-player.h" #include "config-player.h"
@ -219,6 +223,19 @@
/* define for all cpus from PP family */ /* define for all cpus from PP family */
#if (CONFIG_CPU == PP5002) || (CONFIG_CPU == PP5020) || (CONFIG_CPU == PP5024) #if (CONFIG_CPU == PP5002) || (CONFIG_CPU == PP5020) || (CONFIG_CPU == PP5024)
#define CPU_PP #define CPU_PP
/* PP family has dual cores */
#if 0
/* Keep it as single core until dual core support is ready */
#define NUM_CORES 2
#define CURRENT_CORE current_core()
#endif
#define NUM_CORES 1
#define CURRENT_CORE 0
#else
#define NUM_CORES 1
#define CURRENT_CORE 0
#endif #endif
/* define for all cpus from ARM family */ /* define for all cpus from ARM family */

View file

@ -43,6 +43,8 @@ static inline void udelay(unsigned usecs)
unsigned start = USEC_TIMER; unsigned start = USEC_TIMER;
while ((USEC_TIMER - start) < usecs); while ((USEC_TIMER - start) < usecs);
} }
unsigned int current_core(void);
#endif #endif
struct flash_header { struct flash_header {

View file

@ -31,12 +31,16 @@
int create_thread(void (*function)(void), void* stack, int stack_size, int create_thread(void (*function)(void), void* stack, int stack_size,
const char *name); const char *name);
int create_thread_on_core(unsigned int core, void (*function)(void), void* stack, int stack_size,
const char *name);
void remove_thread(int threadnum); void remove_thread(int threadnum);
void remove_thread_on_core(unsigned int core, int threadnum);
void switch_thread(void); void switch_thread(void);
void sleep_thread(void); void sleep_thread(void);
void wake_up_thread(void); void wake_up_thread(void);
void init_threads(void); void init_threads(void);
int thread_stack_usage(int threadnum); int thread_stack_usage(int threadnum);
int thread_stack_usage_on_core(unsigned int core, int threadnum);
#ifdef RB_PROFILE #ifdef RB_PROFILE
void profile_thread(void); void profile_thread(void);
#endif #endif

View file

@ -1187,6 +1187,16 @@ void irq(void)
#endif #endif
#endif /* BOOTLOADER */ #endif /* BOOTLOADER */
unsigned int current_core(void)
{
if(((*(volatile unsigned long *)(0x60000000)) & 0xff) == 0x55)
{
return CPU;
}
return COP;
}
/* TODO: The following two function have been lifted straight from IPL, and /* TODO: The following two function have been lifted straight from IPL, and
hence have a lot of numeric addresses used straight. I'd like to use hence have a lot of numeric addresses used straight. I'd like to use
#defines for these, but don't know what most of them are for or even what #defines for these, but don't know what most of them are for or even what
@ -1329,6 +1339,16 @@ void irq(void)
#endif #endif
unsigned int current_core(void)
{
if(((*(volatile unsigned long *)(0xc4000000)) & 0xff) == 0x55)
{
return CPU;
}
return COP;
}
/* TODO: The following two function have been lifted straight from IPL, and /* TODO: The following two function have been lifted straight from IPL, and
hence have a lot of numeric addresses used straight. I'd like to use hence have a lot of numeric addresses used straight. I'd like to use
#defines for these, but don't know what most of them are for or even what #defines for these, but don't know what most of them are for or even what

View file

@ -62,18 +62,31 @@ struct regs
/* Cast to the the machine int type, whose size could be < 4. */ /* Cast to the the machine int type, whose size could be < 4. */
int num_threads; int num_threads[NUM_CORES];
static volatile int num_sleepers; static volatile int num_sleepers[NUM_CORES];
static int current_thread; static int current_thread[NUM_CORES];
static struct regs thread_contexts[MAXTHREADS] IBSS_ATTR; static struct regs thread_contexts[NUM_CORES][MAXTHREADS] IBSS_ATTR;
const char *thread_name[MAXTHREADS]; const char *thread_name[NUM_CORES][MAXTHREADS];
void *thread_stack[MAXTHREADS]; void *thread_stack[NUM_CORES][MAXTHREADS];
int thread_stack_size[MAXTHREADS]; int thread_stack_size[NUM_CORES][MAXTHREADS];
static const char main_thread_name[] = "main"; static const char main_thread_name[] = "main";
extern int stackbegin[]; extern int stackbegin[];
extern int stackend[]; extern int stackend[];
#ifdef CPU_PP
#ifndef BOOTLOADER
extern int cop_stackbegin[];
extern int cop_stackend[];
#else
/* The coprocessor stack is not set up in the bootloader code, but the
threading is. No threads are run on the coprocessor, so set up some dummy
stack */
int *cop_stackbegin = stackbegin;
int *cop_stackend = stackend;
#endif
#endif
void switch_thread(void) ICODE_ATTR; void switch_thread(void) ICODE_ATTR;
static inline void store_context(void* addr) __attribute__ ((always_inline)); static inline void store_context(void* addr) __attribute__ ((always_inline));
static inline void load_context(const void* addr) __attribute__ ((always_inline)); static inline void load_context(const void* addr) __attribute__ ((always_inline));
@ -260,7 +273,7 @@ void switch_thread(void)
#ifdef SIMULATOR #ifdef SIMULATOR
/* Do nothing */ /* Do nothing */
#else #else
while (num_sleepers == num_threads) while (num_sleepers[CURRENT_CORE] == num_threads[CURRENT_CORE])
{ {
/* Enter sleep mode, woken up on interrupt */ /* Enter sleep mode, woken up on interrupt */
#ifdef CPU_COLDFIRE #ifdef CPU_COLDFIRE
@ -284,21 +297,21 @@ void switch_thread(void)
#endif #endif
} }
#endif #endif
current = current_thread; current = current_thread[CURRENT_CORE];
store_context(&thread_contexts[current]); store_context(&thread_contexts[CURRENT_CORE][current]);
#if CONFIG_CPU != TCC730 #if CONFIG_CPU != TCC730
/* Check if the current thread stack is overflown */ /* Check if the current thread stack is overflown */
stackptr = thread_stack[current]; stackptr = thread_stack[CURRENT_CORE][current];
if(stackptr[0] != DEADBEEF) if(stackptr[0] != DEADBEEF)
panicf("Stkov %s", thread_name[current]); panicf("Stkov %s", thread_name[CURRENT_CORE][current]);
#endif #endif
if (++current >= num_threads) if (++current >= num_threads[CURRENT_CORE])
current = 0; current = 0;
current_thread = current; current_thread[CURRENT_CORE] = current;
load_context(&thread_contexts[current]); load_context(&thread_contexts[CURRENT_CORE][current]);
#ifdef RB_PROFILE #ifdef RB_PROFILE
profile_thread_started(current_thread); profile_thread_started(current_thread);
#endif #endif
@ -306,29 +319,42 @@ void switch_thread(void)
void sleep_thread(void) void sleep_thread(void)
{ {
++num_sleepers; ++num_sleepers[CURRENT_CORE];
switch_thread(); switch_thread();
} }
void wake_up_thread(void) void wake_up_thread(void)
{ {
num_sleepers = 0; num_sleepers[CURRENT_CORE] = 0;
} }
/*--------------------------------------------------------------------------- /*---------------------------------------------------------------------------
* Create thread. * Create thread on the current core.
* Return ID if context area could be allocated, else -1. * Return ID if context area could be allocated, else -1.
*--------------------------------------------------------------------------- *---------------------------------------------------------------------------
*/ */
int create_thread(void (*function)(void), void* stack, int stack_size, int create_thread(void (*function)(void), void* stack, int stack_size,
const char *name) const char *name)
{
return create_thread_on_core(CURRENT_CORE, function, stack, stack_size,
name);
}
/*---------------------------------------------------------------------------
* Create thread on a specific core.
* Return ID if context area could be allocated, else -1.
*---------------------------------------------------------------------------
*/
int create_thread_on_core(unsigned int core, void (*function)(void), void* stack, int stack_size,
const char *name)
{ {
unsigned int i; unsigned int i;
unsigned int stacklen; unsigned int stacklen;
unsigned int *stackptr; unsigned int *stackptr;
struct regs *regs; struct regs *regs;
if (num_threads >= MAXTHREADS) if (num_threads[core] >= MAXTHREADS)
return -1; return -1;
/* Munge the stack to make it easy to spot stack overflows */ /* Munge the stack to make it easy to spot stack overflows */
@ -340,10 +366,10 @@ int create_thread(void (*function)(void), void* stack, int stack_size,
} }
/* Store interesting information */ /* Store interesting information */
thread_name[num_threads] = name; thread_name[core][num_threads[core]] = name;
thread_stack[num_threads] = stack; thread_stack[core][num_threads[core]] = stack;
thread_stack_size[num_threads] = stack_size; thread_stack_size[core][num_threads[core]] = stack_size;
regs = &thread_contexts[num_threads]; regs = &thread_contexts[core][num_threads[core]];
#if defined(CPU_COLDFIRE) || (CONFIG_CPU == SH7034) || defined(CPU_ARM) #if defined(CPU_COLDFIRE) || (CONFIG_CPU == SH7034) || defined(CPU_ARM)
/* Align stack to an even 32 bit boundary */ /* Align stack to an even 32 bit boundary */
regs->sp = (void*)(((unsigned int)stack + stack_size) & ~3); regs->sp = (void*)(((unsigned int)stack + stack_size) & ~3);
@ -355,65 +381,91 @@ int create_thread(void (*function)(void), void* stack, int stack_size,
regs->start = (void*)function; regs->start = (void*)function;
wake_up_thread(); wake_up_thread();
return num_threads++; /* return the current ID, e.g for remove_thread() */ return num_threads[core]++; /* return the current ID, e.g for remove_thread() */
} }
/*--------------------------------------------------------------------------- /*---------------------------------------------------------------------------
* Remove a thread from the scheduler. * Remove a thread on the current core from the scheduler.
* Parameter is the ID as returned from create_thread(). * Parameter is the ID as returned from create_thread().
*--------------------------------------------------------------------------- *---------------------------------------------------------------------------
*/ */
void remove_thread(int threadnum) void remove_thread(int threadnum)
{
remove_thread_on_core(CURRENT_CORE, threadnum);
}
/*---------------------------------------------------------------------------
* Remove a thread on the specified core from the scheduler.
* Parameters are the core and the ID as returned from create_thread().
*---------------------------------------------------------------------------
*/
void remove_thread_on_core(unsigned int core, int threadnum)
{ {
int i; int i;
if(threadnum >= num_threads) if(threadnum >= num_threads[core])
return; return;
num_threads--; num_threads[core]--;
for (i=threadnum; i<num_threads-1; i++) for (i=threadnum; i<num_threads[core]-1; i++)
{ /* move all entries which are behind */ { /* move all entries which are behind */
thread_name[i] = thread_name[i+1]; thread_name[core][i] = thread_name[core][i+1];
thread_stack[i] = thread_stack[i+1]; thread_stack[core][i] = thread_stack[core][i+1];
thread_stack_size[i] = thread_stack_size[i+1]; thread_stack_size[core][i] = thread_stack_size[core][i+1];
thread_contexts[i] = thread_contexts[i+1]; thread_contexts[core][i] = thread_contexts[core][i+1];
} }
if (current_thread == threadnum) /* deleting the current one? */ if (current_thread[core] == threadnum) /* deleting the current one? */
current_thread = num_threads; /* set beyond last, avoid store harm */ current_thread[core] = num_threads[core]; /* set beyond last, avoid store harm */
else if (current_thread > threadnum) /* within the moved positions? */ else if (current_thread[core] > threadnum) /* within the moved positions? */
current_thread--; /* adjust it, point to same context again */ current_thread[core]--; /* adjust it, point to same context again */
} }
void init_threads(void) void init_threads(void)
{ {
num_threads = 1; /* We have 1 thread to begin with */ unsigned int core = CURRENT_CORE;
current_thread = 0; /* The current thread is number 0 */
thread_name[0] = main_thread_name; num_threads[core] = 1; /* We have 1 thread to begin with */
thread_stack[0] = stackbegin; current_thread[core] = 0; /* The current thread is number 0 */
thread_stack_size[0] = (int)stackend - (int)stackbegin; thread_name[core][0] = main_thread_name;
#if CONFIG_CPU == TCC730 /* In multiple core setups, each core has a different stack. There is probably
thread_contexts[0].started = 1; a much better way to do this. */
#else if(core == CPU)
thread_contexts[0].start = 0; /* thread 0 already running */ {
thread_stack[CPU][0] = stackbegin;
thread_stack_size[CPU][0] = (int)stackend - (int)stackbegin;
} else {
#if NUM_CORES > 1 /* This code path will not be run on single core targets */
thread_stack[COP][0] = cop_stackbegin;
thread_stack_size[COP][0] = (int)cop_stackend - (int)cop_stackbegin;
#endif #endif
num_sleepers = 0; }
#if CONFIG_CPU == TCC730
thread_contexts[core][0].started = 1;
#else
thread_contexts[core][0].start = 0; /* thread 0 already running */
#endif
num_sleepers[core] = 0;
} }
int thread_stack_usage(int threadnum) int thread_stack_usage(int threadnum){
return thread_stack_usage_on_core(CURRENT_CORE, threadnum);
}
int thread_stack_usage_on_core(unsigned int core, int threadnum)
{ {
unsigned int i; unsigned int i;
unsigned int *stackptr = thread_stack[threadnum]; unsigned int *stackptr = thread_stack[core][threadnum];
if(threadnum >= num_threads) if(threadnum >= num_threads[core])
return -1; return -1;
for(i = 0;i < thread_stack_size[threadnum]/sizeof(int);i++) for(i = 0;i < thread_stack_size[core][threadnum]/sizeof(int);i++)
{ {
if(stackptr[i] != DEADBEEF) if(stackptr[i] != DEADBEEF)
break; break;
} }
return ((thread_stack_size[threadnum] - i * sizeof(int)) * 100) / return ((thread_stack_size[core][threadnum] - i * sizeof(int)) * 100) /
thread_stack_size[threadnum]; thread_stack_size[core][threadnum];
} }