summaryrefslogtreecommitdiff
path: root/examples/sched.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/sched.c')
-rw-r--r--examples/sched.c112
1 files changed, 58 insertions, 54 deletions
diff --git a/examples/sched.c b/examples/sched.c
index 4f6f98e78f..ceb5c1f08e 100644
--- a/examples/sched.c
+++ b/examples/sched.c
@@ -3,12 +3,12 @@
* 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.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
@@ -17,7 +17,6 @@
#include <common.h>
#include <syscall.h>
-#include <setjmp.h>
/*
* Author: Arun Dharankar <ADharankar@ATTBI.Com>
@@ -54,6 +53,13 @@
#define RC_FAILURE (-1)
#define RC_SUCCESS (0)
+typedef vu_char *jmp_ctx;
+unsigned long setctxsp (vu_char *sp);
+int ppc_setjmp(jmp_ctx env);
+void ppc_longjmp(jmp_ctx env, int val);
+#define setjmp ppc_setjmp
+#define longjmp ppc_longjmp
+
struct lthread {
int state;
int retval;
@@ -68,13 +74,13 @@ static volatile int current_tid = MASTER_THREAD;
static uchar dbg = 0;
-#define DEBUG(fmt, args...) { \
- if(dbg != 0) { \
- mon_printf("[%s %d %s]: ", __FILE__, __LINE__, __FUNCTION__); \
- mon_printf(fmt, ##args); \
- mon_printf("\n"); \
- } \
- }
+#define PDEBUG(fmt, args...) { \
+ if(dbg != 0) { \
+ mon_printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
+ mon_printf(fmt, ##args); \
+ mon_printf("\n"); \
+ } \
+}
static int testthread (void *);
static void sched_init (void);
@@ -83,14 +89,15 @@ static int thread_start (int id);
static void thread_yield (void);
static int thread_delete (int id);
static int thread_join (int *ret);
-#if 0 /* not used yet */
+
+#if 0 /* not used yet */
static int thread_stop (int id);
-#endif /* not used yet */
+#endif /* not used yet */
/* An example of schedular test */
#define NUMTHREADS 7
-int sched (bd_t *bd, int ac, char *av[])
+int sched (bd_t * bd, int ac, char *av[])
{
int i, j;
int tid[NUMTHREADS];
@@ -102,11 +109,10 @@ int sched (bd_t *bd, int ac, char *av[])
names[i] = i;
j = thread_create (testthread, (void *) &names[i]);
if (j == RC_FAILURE)
- mon_printf ("schedtest: Failed to create thread %d\n",
- i);
+ mon_printf ("schedtest: Failed to create thread %d\n", i);
if (j > 0) {
mon_printf ("schedtest: Created thread with id %d, name %d\n",
- j, i);
+ j, i);
tid[i] = j;
}
}
@@ -125,8 +131,7 @@ int sched (bd_t *bd, int ac, char *av[])
if (mon_tstc () && mon_getc () == 0x3) {
mon_printf ("schedtest: Aborting threads...\n");
for (i = 0; i < NUMTHREADS; i++) {
- mon_printf ("schedtest: Deleting thread %d\n",
- tid[i]);
+ mon_printf ("schedtest: Deleting thread %d\n", tid[i]);
thread_delete (tid[i]);
}
return RC_SUCCESS;
@@ -135,11 +140,10 @@ int sched (bd_t *bd, int ac, char *av[])
i = thread_join (&j);
if (i == RC_FAILURE) {
mon_printf ("schedtest: No threads pending, "
- "exiting schedular test\n");
+ "exiting schedular test\n");
return RC_SUCCESS;
}
- mon_printf ("schedtest: thread is %d returned %d\n", i,
- j);
+ mon_printf ("schedtest: thread is %d returned %d\n", i, j);
thread_yield ();
}
@@ -151,14 +155,14 @@ static int testthread (void *name)
int i;
mon_printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
- *(int *) name, &i);
+ *(int *) name, &i);
mon_printf ("Thread %02d, i=%d\n", *(int *) name);
for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
if (mon_tstc () && mon_getc () == 0x3) {
mon_printf ("testthread: myname %d terminating.\n",
- *(int *) name);
+ *(int *) name);
return *(int *) name + 1;
}
@@ -167,7 +171,7 @@ static int testthread (void *name)
}
mon_printf ("testthread: returning %d, i=0x%x\n",
- *(int *) name + 1, i);
+ *(int *) name + 1, i);
return *(int *) name + 1;
}
@@ -182,8 +186,8 @@ static void sched_init (void)
current_tid = MASTER_THREAD;
lthreads[current_tid].state = STATE_RUNNABLE;
- DEBUG ("sched_init: master context = 0x%08x",
- lthreads[current_tid].context);
+ PDEBUG ("sched_init: master context = 0x%08x",
+ lthreads[current_tid].context);
return;
}
@@ -191,19 +195,19 @@ static void thread_yield (void)
{
static int i;
- DEBUG ("thread_yield: current tid=%d", current_tid);
+ PDEBUG ("thread_yield: current tid=%d", current_tid);
#define SWITCH(new) \
if(lthreads[new].state == STATE_RUNNABLE) { \
- DEBUG("thread_yield: %d match, ctx=0x%08x", \
+ PDEBUG("thread_yield: %d match, ctx=0x%08x", \
new, lthreads[current_tid].context); \
if(setjmp(lthreads[current_tid].context) == 0) { \
current_tid = new; \
- DEBUG("thread_yield: tid %d returns 0", \
+ PDEBUG("thread_yield: tid %d returns 0", \
new); \
longjmp(lthreads[new].context, 1); \
} else { \
- DEBUG("thread_yield: tid %d returns 1", \
+ PDEBUG("thread_yield: tid %d returns 1", \
new); \
return; \
} \
@@ -219,7 +223,7 @@ static void thread_yield (void)
}
}
- DEBUG ("thread_yield: returning from thread_yield");
+ PDEBUG ("thread_yield: returning from thread_yield");
return;
}
@@ -232,12 +236,12 @@ static int thread_create (int (*func) (void *), void *arg)
lthreads[i].state = STATE_STOPPED;
lthreads[i].func = func;
lthreads[i].arg = arg;
- DEBUG ("thread_create: returns new tid %d", i);
+ PDEBUG ("thread_create: returns new tid %d", i);
return i;
}
}
- DEBUG ("thread_create: returns failure");
+ PDEBUG ("thread_create: returns failure");
return RC_FAILURE;
}
@@ -255,13 +259,13 @@ static int thread_delete (int id)
static void thread_launcher (void)
{
- DEBUG ("thread_launcher: invoking func=0x%08x",
+ PDEBUG ("thread_launcher: invoking func=0x%08x",
lthreads[current_tid].func);
lthreads[current_tid].retval =
- lthreads[current_tid].func(lthreads[current_tid].arg);
+ lthreads[current_tid].func (lthreads[current_tid].arg);
- DEBUG ("thread_launcher: tid %d terminated", current_tid);
+ PDEBUG ("thread_launcher: tid %d terminated", current_tid);
lthreads[current_tid].state = STATE_TERMINATED;
thread_yield ();
@@ -272,7 +276,7 @@ static void thread_launcher (void)
static int thread_start (int id)
{
- DEBUG ("thread_start: id=%d", id);
+ PDEBUG ("thread_start: id=%d", id);
if (id <= MASTER_THREAD || id > MAX_THREADS) {
return RC_FAILURE;
}
@@ -283,17 +287,17 @@ static int thread_start (int id)
if (setjmp (lthreads[current_tid].context) == 0) {
lthreads[id].state = STATE_RUNNABLE;
current_tid = id;
- DEBUG ("thread_start: to be stack=0%08x", lthreads[id].stack);
+ PDEBUG ("thread_start: to be stack=0%08x", lthreads[id].stack);
setctxsp (&lthreads[id].stack[STK_SIZE]);
thread_launcher ();
}
- DEBUG ("thread_start: Thread id=%d started, parent returns", id);
+ PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
return RC_SUCCESS;
}
-#if 0 /* not used so far */
+#if 0 /* not used so far */
static int thread_stop (int id)
{
if (id <= MASTER_THREAD || id >= MAX_THREADS)
@@ -305,46 +309,46 @@ static int thread_stop (int id)
lthreads[id].state = STATE_STOPPED;
return RC_SUCCESS;
}
-#endif /* not used so far */
+#endif /* not used so far */
static int thread_join (int *ret)
{
int i, j = 0;
- DEBUG ("thread_join: *ret = %d", *ret);
+ PDEBUG ("thread_join: *ret = %d", *ret);
if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
- DEBUG ("thread_join: invalid tid %d", *ret);
+ PDEBUG ("thread_join: invalid tid %d", *ret);
return RC_FAILURE;
}
if (*ret == -1) {
- DEBUG ("Checking for tid = -1");
+ PDEBUG ("Checking for tid = -1");
while (1) {
- /* DEBUG("thread_join: start while-loopn"); */
+ /* PDEBUG("thread_join: start while-loopn"); */
j = 0;
for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
if (lthreads[i].state == STATE_TERMINATED) {
*ret = lthreads[i].retval;
lthreads[i].state = STATE_EMPTY;
- /* DEBUG("thread_join: returning retval %d of tid %d",
- ret, i); */
+ /* PDEBUG("thread_join: returning retval %d of tid %d",
+ ret, i); */
return RC_SUCCESS;
}
if (lthreads[i].state != STATE_EMPTY) {
- DEBUG ("thread_join: %d used slots tid %d state=%d",
- j, i, lthreads[i].state);
+ PDEBUG ("thread_join: %d used slots tid %d state=%d",
+ j, i, lthreads[i].state);
j++;
}
}
if (j == 0) {
- DEBUG ("thread_join: all slots empty!");
+ PDEBUG ("thread_join: all slots empty!");
return RC_FAILURE;
}
- /* DEBUG("thread_join: yielding"); */
+ /* PDEBUG("thread_join: yielding"); */
thread_yield ();
- /* DEBUG("thread_join: back from yield"); */
+ /* PDEBUG("thread_join: back from yield"); */
}
}
@@ -352,10 +356,10 @@ static int thread_join (int *ret)
i = *ret;
*ret = lthreads[*ret].retval;
lthreads[*ret].state = STATE_EMPTY;
- DEBUG ("thread_join: returing %d for tid %d", *ret, i);
+ PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
return RC_SUCCESS;
}
- DEBUG ("thread_join: thread %d is not terminated!", *ret);
+ PDEBUG ("thread_join: thread %d is not terminated!", *ret);
return RC_FAILURE;
}