2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
25 * A very simple thread/schedular model:
26 * - only one master thread, and no parent child relation maintained
27 * - parent thread cannot be stopped or deleted
28 * - no permissions or credentials
29 * - no elaborate safety checks
30 * - cooperative multi threading
31 * - Simple round-robin scheduleing with no priorities
32 * - no metering/statistics collection
34 * Basic idea of implementing this is to allow more than one tests to
35 * execute "simultaneously".
37 * This may be modified such thread_yield may be called in syscalls, and
45 #define STK_SIZE 8*1024
48 #define STATE_RUNNABLE 1
49 #define STATE_STOPPED 2
50 #define STATE_TERMINATED 2
52 #define MASTER_THREAD 0
54 #define RC_FAILURE (-1)
55 #define RC_SUCCESS (0)
61 uchar context[CTX_SIZE];
65 static volatile struct lthread lthreads[MAX_THREADS];
66 static volatile int current_tid = MASTER_THREAD;
71 #define DEBUG(fmt, args...) { \
73 mon_printf("[%s %d %s]: ", __FILE__, __LINE__, __FUNCTION__); \
74 mon_printf(fmt, ##args); \
79 static int testthread (void *);
80 static void sched_init (void);
81 static int thread_create (int (*func) (void *), void *arg);
82 static int thread_start (int id);
83 static void thread_yield (void);
84 static int thread_delete (int id);
85 static int thread_join (int *ret);
86 #if 0 /* not used yet */
87 static int thread_stop (int id);
88 #endif /* not used yet */
90 /* An example of schedular test */
93 int sched (bd_t *bd, int ac, char *av[])
97 int names[NUMTHREADS];
101 for (i = 0; i < NUMTHREADS; i++) {
103 j = thread_create (testthread, (void *) &names[i]);
105 mon_printf ("schedtest: Failed to create thread %d\n",
108 mon_printf ("schedtest: Created thread with id %d, name %d\n",
113 mon_printf ("schedtest: Threads created\n");
115 mon_printf ("sched_test: function=0x%08x\n", testthread);
116 for (i = 0; i < NUMTHREADS; i++) {
117 mon_printf ("schedtest: Setting thread %d runnable\n", tid[i]);
118 thread_start (tid[i]);
121 mon_printf ("schedtest: Started %d threads\n", NUMTHREADS);
124 mon_printf ("schedtest: Waiting for threads to complete\n");
125 if (mon_tstc () && mon_getc () == 0x3) {
126 mon_printf ("schedtest: Aborting threads...\n");
127 for (i = 0; i < NUMTHREADS; i++) {
128 mon_printf ("schedtest: Deleting thread %d\n",
130 thread_delete (tid[i]);
135 i = thread_join (&j);
136 if (i == RC_FAILURE) {
137 mon_printf ("schedtest: No threads pending, "
138 "exiting schedular test\n");
141 mon_printf ("schedtest: thread is %d returned %d\n", i,
149 static int testthread (void *name)
153 mon_printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
156 mon_printf ("Thread %02d, i=%d\n", *(int *) name);
158 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
159 if (mon_tstc () && mon_getc () == 0x3) {
160 mon_printf ("testthread: myname %d terminating.\n",
162 return *(int *) name + 1;
169 mon_printf ("testthread: returning %d, i=0x%x\n",
170 *(int *) name + 1, i);
172 return *(int *) name + 1;
176 static void sched_init (void)
180 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
181 lthreads[i].state = STATE_EMPTY;
183 current_tid = MASTER_THREAD;
184 lthreads[current_tid].state = STATE_RUNNABLE;
185 DEBUG ("sched_init: master context = 0x%08x",
186 lthreads[current_tid].context);
190 static void thread_yield (void)
194 DEBUG ("thread_yield: current tid=%d", current_tid);
196 #define SWITCH(new) \
197 if(lthreads[new].state == STATE_RUNNABLE) { \
198 DEBUG("thread_yield: %d match, ctx=0x%08x", \
199 new, lthreads[current_tid].context); \
200 if(setjmp(lthreads[current_tid].context) == 0) { \
202 DEBUG("thread_yield: tid %d returns 0", \
204 longjmp(lthreads[new].context, 1); \
206 DEBUG("thread_yield: tid %d returns 1", \
212 for (i = current_tid + 1; i < MAX_THREADS; i++) {
216 if (current_tid != 0) {
217 for (i = 0; i <= current_tid; i++) {
222 DEBUG ("thread_yield: returning from thread_yield");
226 static int thread_create (int (*func) (void *), void *arg)
230 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
231 if (lthreads[i].state == STATE_EMPTY) {
232 lthreads[i].state = STATE_STOPPED;
233 lthreads[i].func = func;
234 lthreads[i].arg = arg;
235 DEBUG ("thread_create: returns new tid %d", i);
240 DEBUG ("thread_create: returns failure");
244 static int thread_delete (int id)
246 if (id <= MASTER_THREAD || id > MAX_THREADS)
249 if (current_tid == id)
252 lthreads[id].state = STATE_EMPTY;
256 static void thread_launcher (void)
258 DEBUG ("thread_launcher: invoking func=0x%08x",
259 lthreads[current_tid].func);
261 lthreads[current_tid].retval =
262 lthreads[current_tid].func(lthreads[current_tid].arg);
264 DEBUG ("thread_launcher: tid %d terminated", current_tid);
266 lthreads[current_tid].state = STATE_TERMINATED;
268 mon_printf ("thread_launcher: should NEVER get here!\n");
273 static int thread_start (int id)
275 DEBUG ("thread_start: id=%d", id);
276 if (id <= MASTER_THREAD || id > MAX_THREADS) {
280 if (lthreads[id].state != STATE_STOPPED)
283 if (setjmp (lthreads[current_tid].context) == 0) {
284 lthreads[id].state = STATE_RUNNABLE;
286 DEBUG ("thread_start: to be stack=0%08x", lthreads[id].stack);
287 setctxsp (<hreads[id].stack[STK_SIZE]);
291 DEBUG ("thread_start: Thread id=%d started, parent returns", id);
296 #if 0 /* not used so far */
297 static int thread_stop (int id)
299 if (id <= MASTER_THREAD || id >= MAX_THREADS)
302 if (current_tid == id)
305 lthreads[id].state = STATE_STOPPED;
308 #endif /* not used so far */
310 static int thread_join (int *ret)
314 DEBUG ("thread_join: *ret = %d", *ret);
316 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
317 DEBUG ("thread_join: invalid tid %d", *ret);
322 DEBUG ("Checking for tid = -1");
324 /* DEBUG("thread_join: start while-loopn"); */
326 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
327 if (lthreads[i].state == STATE_TERMINATED) {
328 *ret = lthreads[i].retval;
329 lthreads[i].state = STATE_EMPTY;
330 /* DEBUG("thread_join: returning retval %d of tid %d",
335 if (lthreads[i].state != STATE_EMPTY) {
336 DEBUG ("thread_join: %d used slots tid %d state=%d",
337 j, i, lthreads[i].state);
342 DEBUG ("thread_join: all slots empty!");
345 /* DEBUG("thread_join: yielding"); */
347 /* DEBUG("thread_join: back from yield"); */
351 if (lthreads[*ret].state == STATE_TERMINATED) {
353 *ret = lthreads[*ret].retval;
354 lthreads[*ret].state = STATE_EMPTY;
355 DEBUG ("thread_join: returing %d for tid %d", *ret, i);
359 DEBUG ("thread_join: thread %d is not terminated!", *ret);