blob: 1c529607132291effc308c24a55061c409181194 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk85ec0bc2003-03-31 16:34:49 +00002
3#include <common.h>
wdenk27b207f2003-07-24 23:38:38 +00004#include <exports.h>
wdenk85ec0bc2003-03-31 16:34:49 +00005
6/*
7 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
8 *
9 * A very simple thread/schedular model:
10 * - only one master thread, and no parent child relation maintained
11 * - parent thread cannot be stopped or deleted
12 * - no permissions or credentials
13 * - no elaborate safety checks
14 * - cooperative multi threading
15 * - Simple round-robin scheduleing with no priorities
16 * - no metering/statistics collection
17 *
18 * Basic idea of implementing this is to allow more than one tests to
19 * execute "simultaneously".
20 *
21 * This may be modified such thread_yield may be called in syscalls, and
22 * timer interrupts.
23 */
24
25
26#define MAX_THREADS 8
27
28#define CTX_SIZE 512
29#define STK_SIZE 8*1024
30
31#define STATE_EMPTY 0
32#define STATE_RUNNABLE 1
33#define STATE_STOPPED 2
34#define STATE_TERMINATED 2
35
36#define MASTER_THREAD 0
37
38#define RC_FAILURE (-1)
39#define RC_SUCCESS (0)
40
wdenk3e386912003-04-05 00:53:31 +000041typedef vu_char *jmp_ctx;
42unsigned long setctxsp (vu_char *sp);
43int ppc_setjmp(jmp_ctx env);
44void ppc_longjmp(jmp_ctx env, int val);
45#define setjmp ppc_setjmp
46#define longjmp ppc_longjmp
47
wdenk85ec0bc2003-03-31 16:34:49 +000048struct lthread {
49 int state;
50 int retval;
51 char stack[STK_SIZE];
52 uchar context[CTX_SIZE];
53 int (*func) (void *);
54 void *arg;
55};
56static volatile struct lthread lthreads[MAX_THREADS];
57static volatile int current_tid = MASTER_THREAD;
58
59
60static uchar dbg = 0;
61
wdenk3e386912003-04-05 00:53:31 +000062#define PDEBUG(fmt, args...) { \
63 if(dbg != 0) { \
wdenk27b207f2003-07-24 23:38:38 +000064 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
65 printf(fmt, ##args); \
66 printf("\n"); \
wdenk3e386912003-04-05 00:53:31 +000067 } \
68}
wdenk85ec0bc2003-03-31 16:34:49 +000069
70static int testthread (void *);
71static void sched_init (void);
72static int thread_create (int (*func) (void *), void *arg);
73static int thread_start (int id);
74static void thread_yield (void);
75static int thread_delete (int id);
76static int thread_join (int *ret);
wdenk3e386912003-04-05 00:53:31 +000077
78#if 0 /* not used yet */
wdenk85ec0bc2003-03-31 16:34:49 +000079static int thread_stop (int id);
wdenk3e386912003-04-05 00:53:31 +000080#endif /* not used yet */
wdenk85ec0bc2003-03-31 16:34:49 +000081
82/* An example of schedular test */
83
84#define NUMTHREADS 7
wdenk27b207f2003-07-24 23:38:38 +000085int sched (int ac, char *av[])
wdenk85ec0bc2003-03-31 16:34:49 +000086{
87 int i, j;
88 int tid[NUMTHREADS];
89 int names[NUMTHREADS];
90
wdenk27b207f2003-07-24 23:38:38 +000091 app_startup(av);
92
wdenk85ec0bc2003-03-31 16:34:49 +000093 sched_init ();
94
95 for (i = 0; i < NUMTHREADS; i++) {
96 names[i] = i;
97 j = thread_create (testthread, (void *) &names[i]);
98 if (j == RC_FAILURE)
wdenk27b207f2003-07-24 23:38:38 +000099 printf ("schedtest: Failed to create thread %d\n", i);
wdenk85ec0bc2003-03-31 16:34:49 +0000100 if (j > 0) {
wdenk27b207f2003-07-24 23:38:38 +0000101 printf ("schedtest: Created thread with id %d, name %d\n",
wdenk3e386912003-04-05 00:53:31 +0000102 j, i);
wdenk85ec0bc2003-03-31 16:34:49 +0000103 tid[i] = j;
104 }
105 }
wdenk27b207f2003-07-24 23:38:38 +0000106 printf ("schedtest: Threads created\n");
wdenk85ec0bc2003-03-31 16:34:49 +0000107
wdenk27b207f2003-07-24 23:38:38 +0000108 printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
wdenk85ec0bc2003-03-31 16:34:49 +0000109 for (i = 0; i < NUMTHREADS; i++) {
wdenk27b207f2003-07-24 23:38:38 +0000110 printf ("schedtest: Setting thread %d runnable\n", tid[i]);
wdenk85ec0bc2003-03-31 16:34:49 +0000111 thread_start (tid[i]);
112 thread_yield ();
113 }
wdenk27b207f2003-07-24 23:38:38 +0000114 printf ("schedtest: Started %d threads\n", NUMTHREADS);
wdenk85ec0bc2003-03-31 16:34:49 +0000115
116 while (1) {
wdenk27b207f2003-07-24 23:38:38 +0000117 printf ("schedtest: Waiting for threads to complete\n");
118 if (tstc () && getc () == 0x3) {
119 printf ("schedtest: Aborting threads...\n");
wdenk85ec0bc2003-03-31 16:34:49 +0000120 for (i = 0; i < NUMTHREADS; i++) {
wdenk27b207f2003-07-24 23:38:38 +0000121 printf ("schedtest: Deleting thread %d\n", tid[i]);
wdenk85ec0bc2003-03-31 16:34:49 +0000122 thread_delete (tid[i]);
123 }
124 return RC_SUCCESS;
125 }
126 j = -1;
127 i = thread_join (&j);
128 if (i == RC_FAILURE) {
wdenk27b207f2003-07-24 23:38:38 +0000129 printf ("schedtest: No threads pending, "
wdenk3e386912003-04-05 00:53:31 +0000130 "exiting schedular test\n");
wdenk85ec0bc2003-03-31 16:34:49 +0000131 return RC_SUCCESS;
132 }
wdenk27b207f2003-07-24 23:38:38 +0000133 printf ("schedtest: thread is %d returned %d\n", i, j);
wdenk85ec0bc2003-03-31 16:34:49 +0000134 thread_yield ();
135 }
136
137 return RC_SUCCESS;
138}
139
140static int testthread (void *name)
141{
142 int i;
143
wdenk27b207f2003-07-24 23:38:38 +0000144 printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
145 *(int *) name, (unsigned)&i);
wdenk85ec0bc2003-03-31 16:34:49 +0000146
wdenk27b207f2003-07-24 23:38:38 +0000147 printf ("Thread %02d, i=%d\n", *(int *) name, i);
wdenk85ec0bc2003-03-31 16:34:49 +0000148
149 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
wdenk27b207f2003-07-24 23:38:38 +0000150 if (tstc () && getc () == 0x3) {
151 printf ("testthread: myname %d terminating.\n",
wdenk3e386912003-04-05 00:53:31 +0000152 *(int *) name);
wdenk85ec0bc2003-03-31 16:34:49 +0000153 return *(int *) name + 1;
154 }
155
156 if (i % 100 == 0)
157 thread_yield ();
158 }
159
wdenk27b207f2003-07-24 23:38:38 +0000160 printf ("testthread: returning %d, i=0x%x\n",
wdenk3e386912003-04-05 00:53:31 +0000161 *(int *) name + 1, i);
wdenk85ec0bc2003-03-31 16:34:49 +0000162
163 return *(int *) name + 1;
164}
165
166
167static void sched_init (void)
168{
169 int i;
170
171 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
172 lthreads[i].state = STATE_EMPTY;
173
174 current_tid = MASTER_THREAD;
175 lthreads[current_tid].state = STATE_RUNNABLE;
wdenk3e386912003-04-05 00:53:31 +0000176 PDEBUG ("sched_init: master context = 0x%08x",
wdenk27b207f2003-07-24 23:38:38 +0000177 (unsigned)lthreads[current_tid].context);
wdenk85ec0bc2003-03-31 16:34:49 +0000178 return;
179}
180
181static void thread_yield (void)
182{
183 static int i;
184
wdenk3e386912003-04-05 00:53:31 +0000185 PDEBUG ("thread_yield: current tid=%d", current_tid);
wdenk85ec0bc2003-03-31 16:34:49 +0000186
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200187#define SWITCH(new) \
wdenk85ec0bc2003-03-31 16:34:49 +0000188 if(lthreads[new].state == STATE_RUNNABLE) { \
wdenk3e386912003-04-05 00:53:31 +0000189 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
wdenk27b207f2003-07-24 23:38:38 +0000190 new, \
191 (unsigned)lthreads[current_tid].context); \
wdenk85ec0bc2003-03-31 16:34:49 +0000192 if(setjmp(lthreads[current_tid].context) == 0) { \
193 current_tid = new; \
wdenk3e386912003-04-05 00:53:31 +0000194 PDEBUG("thread_yield: tid %d returns 0", \
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200195 new); \
wdenk85ec0bc2003-03-31 16:34:49 +0000196 longjmp(lthreads[new].context, 1); \
197 } else { \
wdenk3e386912003-04-05 00:53:31 +0000198 PDEBUG("thread_yield: tid %d returns 1", \
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200199 new); \
wdenk85ec0bc2003-03-31 16:34:49 +0000200 return; \
201 } \
202 }
203
204 for (i = current_tid + 1; i < MAX_THREADS; i++) {
205 SWITCH (i);
206 }
207
208 if (current_tid != 0) {
209 for (i = 0; i <= current_tid; i++) {
210 SWITCH (i);
211 }
212 }
213
wdenk3e386912003-04-05 00:53:31 +0000214 PDEBUG ("thread_yield: returning from thread_yield");
wdenk85ec0bc2003-03-31 16:34:49 +0000215 return;
216}
217
218static int thread_create (int (*func) (void *), void *arg)
219{
220 int i;
221
222 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
223 if (lthreads[i].state == STATE_EMPTY) {
224 lthreads[i].state = STATE_STOPPED;
225 lthreads[i].func = func;
226 lthreads[i].arg = arg;
wdenk3e386912003-04-05 00:53:31 +0000227 PDEBUG ("thread_create: returns new tid %d", i);
wdenk85ec0bc2003-03-31 16:34:49 +0000228 return i;
229 }
230 }
231
wdenk3e386912003-04-05 00:53:31 +0000232 PDEBUG ("thread_create: returns failure");
wdenk85ec0bc2003-03-31 16:34:49 +0000233 return RC_FAILURE;
234}
235
236static int thread_delete (int id)
237{
238 if (id <= MASTER_THREAD || id > MAX_THREADS)
239 return RC_FAILURE;
240
241 if (current_tid == id)
242 return RC_FAILURE;
243
244 lthreads[id].state = STATE_EMPTY;
245 return RC_SUCCESS;
246}
247
248static void thread_launcher (void)
249{
wdenk3e386912003-04-05 00:53:31 +0000250 PDEBUG ("thread_launcher: invoking func=0x%08x",
wdenk27b207f2003-07-24 23:38:38 +0000251 (unsigned)lthreads[current_tid].func);
wdenk85ec0bc2003-03-31 16:34:49 +0000252
253 lthreads[current_tid].retval =
wdenk3e386912003-04-05 00:53:31 +0000254 lthreads[current_tid].func (lthreads[current_tid].arg);
wdenk85ec0bc2003-03-31 16:34:49 +0000255
wdenk3e386912003-04-05 00:53:31 +0000256 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
wdenk85ec0bc2003-03-31 16:34:49 +0000257
258 lthreads[current_tid].state = STATE_TERMINATED;
259 thread_yield ();
wdenk27b207f2003-07-24 23:38:38 +0000260 printf ("thread_launcher: should NEVER get here!\n");
wdenk85ec0bc2003-03-31 16:34:49 +0000261
262 return;
263}
264
265static int thread_start (int id)
266{
wdenk3e386912003-04-05 00:53:31 +0000267 PDEBUG ("thread_start: id=%d", id);
wdenk85ec0bc2003-03-31 16:34:49 +0000268 if (id <= MASTER_THREAD || id > MAX_THREADS) {
269 return RC_FAILURE;
270 }
271
272 if (lthreads[id].state != STATE_STOPPED)
273 return RC_FAILURE;
274
275 if (setjmp (lthreads[current_tid].context) == 0) {
276 lthreads[id].state = STATE_RUNNABLE;
277 current_tid = id;
wdenk27b207f2003-07-24 23:38:38 +0000278 PDEBUG ("thread_start: to be stack=0%08x",
279 (unsigned)lthreads[id].stack);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200280 setctxsp ((vu_char *)&lthreads[id].stack[STK_SIZE]);
wdenk85ec0bc2003-03-31 16:34:49 +0000281 thread_launcher ();
282 }
283
wdenk3e386912003-04-05 00:53:31 +0000284 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
wdenk85ec0bc2003-03-31 16:34:49 +0000285
286 return RC_SUCCESS;
287}
288
wdenk27b207f2003-07-24 23:38:38 +0000289#if 0 /* not used so far */
wdenk85ec0bc2003-03-31 16:34:49 +0000290static int thread_stop (int id)
291{
292 if (id <= MASTER_THREAD || id >= MAX_THREADS)
293 return RC_FAILURE;
294
295 if (current_tid == id)
296 return RC_FAILURE;
297
298 lthreads[id].state = STATE_STOPPED;
299 return RC_SUCCESS;
300}
wdenk27b207f2003-07-24 23:38:38 +0000301#endif /* not used so far */
wdenk85ec0bc2003-03-31 16:34:49 +0000302
303static int thread_join (int *ret)
304{
305 int i, j = 0;
306
wdenk3e386912003-04-05 00:53:31 +0000307 PDEBUG ("thread_join: *ret = %d", *ret);
wdenk85ec0bc2003-03-31 16:34:49 +0000308
309 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
wdenk3e386912003-04-05 00:53:31 +0000310 PDEBUG ("thread_join: invalid tid %d", *ret);
wdenk85ec0bc2003-03-31 16:34:49 +0000311 return RC_FAILURE;
312 }
313
314 if (*ret == -1) {
wdenk3e386912003-04-05 00:53:31 +0000315 PDEBUG ("Checking for tid = -1");
wdenk85ec0bc2003-03-31 16:34:49 +0000316 while (1) {
wdenk3e386912003-04-05 00:53:31 +0000317 /* PDEBUG("thread_join: start while-loopn"); */
wdenk85ec0bc2003-03-31 16:34:49 +0000318 j = 0;
319 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
320 if (lthreads[i].state == STATE_TERMINATED) {
321 *ret = lthreads[i].retval;
322 lthreads[i].state = STATE_EMPTY;
wdenk3e386912003-04-05 00:53:31 +0000323 /* PDEBUG("thread_join: returning retval %d of tid %d",
324 ret, i); */
wdenk85ec0bc2003-03-31 16:34:49 +0000325 return RC_SUCCESS;
326 }
327
328 if (lthreads[i].state != STATE_EMPTY) {
wdenk3e386912003-04-05 00:53:31 +0000329 PDEBUG ("thread_join: %d used slots tid %d state=%d",
330 j, i, lthreads[i].state);
wdenk85ec0bc2003-03-31 16:34:49 +0000331 j++;
332 }
333 }
334 if (j == 0) {
wdenk3e386912003-04-05 00:53:31 +0000335 PDEBUG ("thread_join: all slots empty!");
wdenk85ec0bc2003-03-31 16:34:49 +0000336 return RC_FAILURE;
337 }
wdenk3e386912003-04-05 00:53:31 +0000338 /* PDEBUG("thread_join: yielding"); */
wdenk85ec0bc2003-03-31 16:34:49 +0000339 thread_yield ();
wdenk3e386912003-04-05 00:53:31 +0000340 /* PDEBUG("thread_join: back from yield"); */
wdenk85ec0bc2003-03-31 16:34:49 +0000341 }
342 }
343
344 if (lthreads[*ret].state == STATE_TERMINATED) {
345 i = *ret;
346 *ret = lthreads[*ret].retval;
347 lthreads[*ret].state = STATE_EMPTY;
wdenk3e386912003-04-05 00:53:31 +0000348 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
wdenk85ec0bc2003-03-31 16:34:49 +0000349 return RC_SUCCESS;
350 }
351
wdenk3e386912003-04-05 00:53:31 +0000352 PDEBUG ("thread_join: thread %d is not terminated!", *ret);
wdenk85ec0bc2003-03-31 16:34:49 +0000353 return RC_FAILURE;
354}