blob: ceb5c1f08ecb23b08ea384b2bc24019ce964bbd5 [file] [log] [blame]
wdenk8dba0502003-03-31 16:34:49 +00001/*
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.
wdenkb02744a2003-04-05 00:53:31 +00006 *
wdenk8dba0502003-03-31 16:34:49 +00007 * 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.
wdenkb02744a2003-04-05 00:53:31 +000011 *
wdenk8dba0502003-03-31 16:34:49 +000012 * 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,
15 * MA 02111-1307 USA
16 */
17
18#include <common.h>
19#include <syscall.h>
wdenk8dba0502003-03-31 16:34:49 +000020
21/*
22 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
23 *
24 * A very simple thread/schedular model:
25 * - only one master thread, and no parent child relation maintained
26 * - parent thread cannot be stopped or deleted
27 * - no permissions or credentials
28 * - no elaborate safety checks
29 * - cooperative multi threading
30 * - Simple round-robin scheduleing with no priorities
31 * - no metering/statistics collection
32 *
33 * Basic idea of implementing this is to allow more than one tests to
34 * execute "simultaneously".
35 *
36 * This may be modified such thread_yield may be called in syscalls, and
37 * timer interrupts.
38 */
39
40
41#define MAX_THREADS 8
42
43#define CTX_SIZE 512
44#define STK_SIZE 8*1024
45
46#define STATE_EMPTY 0
47#define STATE_RUNNABLE 1
48#define STATE_STOPPED 2
49#define STATE_TERMINATED 2
50
51#define MASTER_THREAD 0
52
53#define RC_FAILURE (-1)
54#define RC_SUCCESS (0)
55
wdenkb02744a2003-04-05 00:53:31 +000056typedef vu_char *jmp_ctx;
57unsigned long setctxsp (vu_char *sp);
58int ppc_setjmp(jmp_ctx env);
59void ppc_longjmp(jmp_ctx env, int val);
60#define setjmp ppc_setjmp
61#define longjmp ppc_longjmp
62
wdenk8dba0502003-03-31 16:34:49 +000063struct lthread {
64 int state;
65 int retval;
66 char stack[STK_SIZE];
67 uchar context[CTX_SIZE];
68 int (*func) (void *);
69 void *arg;
70};
71static volatile struct lthread lthreads[MAX_THREADS];
72static volatile int current_tid = MASTER_THREAD;
73
74
75static uchar dbg = 0;
76
wdenkb02744a2003-04-05 00:53:31 +000077#define PDEBUG(fmt, args...) { \
78 if(dbg != 0) { \
79 mon_printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
80 mon_printf(fmt, ##args); \
81 mon_printf("\n"); \
82 } \
83}
wdenk8dba0502003-03-31 16:34:49 +000084
85static int testthread (void *);
86static void sched_init (void);
87static int thread_create (int (*func) (void *), void *arg);
88static int thread_start (int id);
89static void thread_yield (void);
90static int thread_delete (int id);
91static int thread_join (int *ret);
wdenkb02744a2003-04-05 00:53:31 +000092
93#if 0 /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000094static int thread_stop (int id);
wdenkb02744a2003-04-05 00:53:31 +000095#endif /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000096
97/* An example of schedular test */
98
99#define NUMTHREADS 7
wdenkb02744a2003-04-05 00:53:31 +0000100int sched (bd_t * bd, int ac, char *av[])
wdenk8dba0502003-03-31 16:34:49 +0000101{
102 int i, j;
103 int tid[NUMTHREADS];
104 int names[NUMTHREADS];
105
106 sched_init ();
107
108 for (i = 0; i < NUMTHREADS; i++) {
109 names[i] = i;
110 j = thread_create (testthread, (void *) &names[i]);
111 if (j == RC_FAILURE)
wdenkb02744a2003-04-05 00:53:31 +0000112 mon_printf ("schedtest: Failed to create thread %d\n", i);
wdenk8dba0502003-03-31 16:34:49 +0000113 if (j > 0) {
114 mon_printf ("schedtest: Created thread with id %d, name %d\n",
wdenkb02744a2003-04-05 00:53:31 +0000115 j, i);
wdenk8dba0502003-03-31 16:34:49 +0000116 tid[i] = j;
117 }
118 }
119 mon_printf ("schedtest: Threads created\n");
120
121 mon_printf ("sched_test: function=0x%08x\n", testthread);
122 for (i = 0; i < NUMTHREADS; i++) {
123 mon_printf ("schedtest: Setting thread %d runnable\n", tid[i]);
124 thread_start (tid[i]);
125 thread_yield ();
126 }
127 mon_printf ("schedtest: Started %d threads\n", NUMTHREADS);
128
129 while (1) {
130 mon_printf ("schedtest: Waiting for threads to complete\n");
131 if (mon_tstc () && mon_getc () == 0x3) {
132 mon_printf ("schedtest: Aborting threads...\n");
133 for (i = 0; i < NUMTHREADS; i++) {
wdenkb02744a2003-04-05 00:53:31 +0000134 mon_printf ("schedtest: Deleting thread %d\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000135 thread_delete (tid[i]);
136 }
137 return RC_SUCCESS;
138 }
139 j = -1;
140 i = thread_join (&j);
141 if (i == RC_FAILURE) {
142 mon_printf ("schedtest: No threads pending, "
wdenkb02744a2003-04-05 00:53:31 +0000143 "exiting schedular test\n");
wdenk8dba0502003-03-31 16:34:49 +0000144 return RC_SUCCESS;
145 }
wdenkb02744a2003-04-05 00:53:31 +0000146 mon_printf ("schedtest: thread is %d returned %d\n", i, j);
wdenk8dba0502003-03-31 16:34:49 +0000147 thread_yield ();
148 }
149
150 return RC_SUCCESS;
151}
152
153static int testthread (void *name)
154{
155 int i;
156
157 mon_printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
wdenkb02744a2003-04-05 00:53:31 +0000158 *(int *) name, &i);
wdenk8dba0502003-03-31 16:34:49 +0000159
160 mon_printf ("Thread %02d, i=%d\n", *(int *) name);
161
162 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
163 if (mon_tstc () && mon_getc () == 0x3) {
164 mon_printf ("testthread: myname %d terminating.\n",
wdenkb02744a2003-04-05 00:53:31 +0000165 *(int *) name);
wdenk8dba0502003-03-31 16:34:49 +0000166 return *(int *) name + 1;
167 }
168
169 if (i % 100 == 0)
170 thread_yield ();
171 }
172
173 mon_printf ("testthread: returning %d, i=0x%x\n",
wdenkb02744a2003-04-05 00:53:31 +0000174 *(int *) name + 1, i);
wdenk8dba0502003-03-31 16:34:49 +0000175
176 return *(int *) name + 1;
177}
178
179
180static void sched_init (void)
181{
182 int i;
183
184 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
185 lthreads[i].state = STATE_EMPTY;
186
187 current_tid = MASTER_THREAD;
188 lthreads[current_tid].state = STATE_RUNNABLE;
wdenkb02744a2003-04-05 00:53:31 +0000189 PDEBUG ("sched_init: master context = 0x%08x",
190 lthreads[current_tid].context);
wdenk8dba0502003-03-31 16:34:49 +0000191 return;
192}
193
194static void thread_yield (void)
195{
196 static int i;
197
wdenkb02744a2003-04-05 00:53:31 +0000198 PDEBUG ("thread_yield: current tid=%d", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000199
200#define SWITCH(new) \
201 if(lthreads[new].state == STATE_RUNNABLE) { \
wdenkb02744a2003-04-05 00:53:31 +0000202 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
wdenk8dba0502003-03-31 16:34:49 +0000203 new, lthreads[current_tid].context); \
204 if(setjmp(lthreads[current_tid].context) == 0) { \
205 current_tid = new; \
wdenkb02744a2003-04-05 00:53:31 +0000206 PDEBUG("thread_yield: tid %d returns 0", \
wdenk8dba0502003-03-31 16:34:49 +0000207 new); \
208 longjmp(lthreads[new].context, 1); \
209 } else { \
wdenkb02744a2003-04-05 00:53:31 +0000210 PDEBUG("thread_yield: tid %d returns 1", \
wdenk8dba0502003-03-31 16:34:49 +0000211 new); \
212 return; \
213 } \
214 }
215
216 for (i = current_tid + 1; i < MAX_THREADS; i++) {
217 SWITCH (i);
218 }
219
220 if (current_tid != 0) {
221 for (i = 0; i <= current_tid; i++) {
222 SWITCH (i);
223 }
224 }
225
wdenkb02744a2003-04-05 00:53:31 +0000226 PDEBUG ("thread_yield: returning from thread_yield");
wdenk8dba0502003-03-31 16:34:49 +0000227 return;
228}
229
230static int thread_create (int (*func) (void *), void *arg)
231{
232 int i;
233
234 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
235 if (lthreads[i].state == STATE_EMPTY) {
236 lthreads[i].state = STATE_STOPPED;
237 lthreads[i].func = func;
238 lthreads[i].arg = arg;
wdenkb02744a2003-04-05 00:53:31 +0000239 PDEBUG ("thread_create: returns new tid %d", i);
wdenk8dba0502003-03-31 16:34:49 +0000240 return i;
241 }
242 }
243
wdenkb02744a2003-04-05 00:53:31 +0000244 PDEBUG ("thread_create: returns failure");
wdenk8dba0502003-03-31 16:34:49 +0000245 return RC_FAILURE;
246}
247
248static int thread_delete (int id)
249{
250 if (id <= MASTER_THREAD || id > MAX_THREADS)
251 return RC_FAILURE;
252
253 if (current_tid == id)
254 return RC_FAILURE;
255
256 lthreads[id].state = STATE_EMPTY;
257 return RC_SUCCESS;
258}
259
260static void thread_launcher (void)
261{
wdenkb02744a2003-04-05 00:53:31 +0000262 PDEBUG ("thread_launcher: invoking func=0x%08x",
wdenk8dba0502003-03-31 16:34:49 +0000263 lthreads[current_tid].func);
264
265 lthreads[current_tid].retval =
wdenkb02744a2003-04-05 00:53:31 +0000266 lthreads[current_tid].func (lthreads[current_tid].arg);
wdenk8dba0502003-03-31 16:34:49 +0000267
wdenkb02744a2003-04-05 00:53:31 +0000268 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000269
270 lthreads[current_tid].state = STATE_TERMINATED;
271 thread_yield ();
272 mon_printf ("thread_launcher: should NEVER get here!\n");
273
274 return;
275}
276
277static int thread_start (int id)
278{
wdenkb02744a2003-04-05 00:53:31 +0000279 PDEBUG ("thread_start: id=%d", id);
wdenk8dba0502003-03-31 16:34:49 +0000280 if (id <= MASTER_THREAD || id > MAX_THREADS) {
281 return RC_FAILURE;
282 }
283
284 if (lthreads[id].state != STATE_STOPPED)
285 return RC_FAILURE;
286
287 if (setjmp (lthreads[current_tid].context) == 0) {
288 lthreads[id].state = STATE_RUNNABLE;
289 current_tid = id;
wdenkb02744a2003-04-05 00:53:31 +0000290 PDEBUG ("thread_start: to be stack=0%08x", lthreads[id].stack);
wdenk8dba0502003-03-31 16:34:49 +0000291 setctxsp (&lthreads[id].stack[STK_SIZE]);
292 thread_launcher ();
293 }
294
wdenkb02744a2003-04-05 00:53:31 +0000295 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
wdenk8dba0502003-03-31 16:34:49 +0000296
297 return RC_SUCCESS;
298}
299
wdenkb02744a2003-04-05 00:53:31 +0000300#if 0 /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000301static int thread_stop (int id)
302{
303 if (id <= MASTER_THREAD || id >= MAX_THREADS)
304 return RC_FAILURE;
305
306 if (current_tid == id)
307 return RC_FAILURE;
308
309 lthreads[id].state = STATE_STOPPED;
310 return RC_SUCCESS;
311}
wdenkb02744a2003-04-05 00:53:31 +0000312#endif /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000313
314static int thread_join (int *ret)
315{
316 int i, j = 0;
317
wdenkb02744a2003-04-05 00:53:31 +0000318 PDEBUG ("thread_join: *ret = %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000319
320 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
wdenkb02744a2003-04-05 00:53:31 +0000321 PDEBUG ("thread_join: invalid tid %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000322 return RC_FAILURE;
323 }
324
325 if (*ret == -1) {
wdenkb02744a2003-04-05 00:53:31 +0000326 PDEBUG ("Checking for tid = -1");
wdenk8dba0502003-03-31 16:34:49 +0000327 while (1) {
wdenkb02744a2003-04-05 00:53:31 +0000328 /* PDEBUG("thread_join: start while-loopn"); */
wdenk8dba0502003-03-31 16:34:49 +0000329 j = 0;
330 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
331 if (lthreads[i].state == STATE_TERMINATED) {
332 *ret = lthreads[i].retval;
333 lthreads[i].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000334 /* PDEBUG("thread_join: returning retval %d of tid %d",
335 ret, i); */
wdenk8dba0502003-03-31 16:34:49 +0000336 return RC_SUCCESS;
337 }
338
339 if (lthreads[i].state != STATE_EMPTY) {
wdenkb02744a2003-04-05 00:53:31 +0000340 PDEBUG ("thread_join: %d used slots tid %d state=%d",
341 j, i, lthreads[i].state);
wdenk8dba0502003-03-31 16:34:49 +0000342 j++;
343 }
344 }
345 if (j == 0) {
wdenkb02744a2003-04-05 00:53:31 +0000346 PDEBUG ("thread_join: all slots empty!");
wdenk8dba0502003-03-31 16:34:49 +0000347 return RC_FAILURE;
348 }
wdenkb02744a2003-04-05 00:53:31 +0000349 /* PDEBUG("thread_join: yielding"); */
wdenk8dba0502003-03-31 16:34:49 +0000350 thread_yield ();
wdenkb02744a2003-04-05 00:53:31 +0000351 /* PDEBUG("thread_join: back from yield"); */
wdenk8dba0502003-03-31 16:34:49 +0000352 }
353 }
354
355 if (lthreads[*ret].state == STATE_TERMINATED) {
356 i = *ret;
357 *ret = lthreads[*ret].retval;
358 lthreads[*ret].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000359 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
wdenk8dba0502003-03-31 16:34:49 +0000360 return RC_SUCCESS;
361 }
362
wdenkb02744a2003-04-05 00:53:31 +0000363 PDEBUG ("thread_join: thread %d is not terminated!", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000364 return RC_FAILURE;
365}