blob: 57ae67e1a5bc39eeb748c226157f95a20ac6470b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk8dba0502003-03-31 16:34:49 +00002
wdenk874ac262003-07-24 23:38:38 +00003#include <exports.h>
wdenk8dba0502003-03-31 16:34:49 +00004
5/*
6 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
7 *
8 * A very simple thread/schedular model:
9 * - only one master thread, and no parent child relation maintained
10 * - parent thread cannot be stopped or deleted
11 * - no permissions or credentials
12 * - no elaborate safety checks
13 * - cooperative multi threading
14 * - Simple round-robin scheduleing with no priorities
15 * - no metering/statistics collection
16 *
17 * Basic idea of implementing this is to allow more than one tests to
18 * execute "simultaneously".
19 *
20 * This may be modified such thread_yield may be called in syscalls, and
21 * timer interrupts.
22 */
23
wdenk8dba0502003-03-31 16:34:49 +000024#define MAX_THREADS 8
25
26#define CTX_SIZE 512
27#define STK_SIZE 8*1024
28
29#define STATE_EMPTY 0
30#define STATE_RUNNABLE 1
31#define STATE_STOPPED 2
32#define STATE_TERMINATED 2
33
34#define MASTER_THREAD 0
35
36#define RC_FAILURE (-1)
37#define RC_SUCCESS (0)
38
wdenkb02744a2003-04-05 00:53:31 +000039typedef vu_char *jmp_ctx;
40unsigned long setctxsp (vu_char *sp);
41int ppc_setjmp(jmp_ctx env);
42void ppc_longjmp(jmp_ctx env, int val);
43#define setjmp ppc_setjmp
44#define longjmp ppc_longjmp
45
wdenk8dba0502003-03-31 16:34:49 +000046struct lthread {
47 int state;
48 int retval;
49 char stack[STK_SIZE];
50 uchar context[CTX_SIZE];
51 int (*func) (void *);
52 void *arg;
53};
54static volatile struct lthread lthreads[MAX_THREADS];
55static volatile int current_tid = MASTER_THREAD;
56
wdenk8dba0502003-03-31 16:34:49 +000057static uchar dbg = 0;
58
wdenkb02744a2003-04-05 00:53:31 +000059#define PDEBUG(fmt, args...) { \
60 if(dbg != 0) { \
wdenk874ac262003-07-24 23:38:38 +000061 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
62 printf(fmt, ##args); \
63 printf("\n"); \
wdenkb02744a2003-04-05 00:53:31 +000064 } \
65}
wdenk8dba0502003-03-31 16:34:49 +000066
67static int testthread (void *);
68static void sched_init (void);
69static int thread_create (int (*func) (void *), void *arg);
70static int thread_start (int id);
71static void thread_yield (void);
72static int thread_delete (int id);
73static int thread_join (int *ret);
wdenkb02744a2003-04-05 00:53:31 +000074
75#if 0 /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000076static int thread_stop (int id);
wdenkb02744a2003-04-05 00:53:31 +000077#endif /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000078
79/* An example of schedular test */
80
81#define NUMTHREADS 7
wdenk874ac262003-07-24 23:38:38 +000082int sched (int ac, char *av[])
wdenk8dba0502003-03-31 16:34:49 +000083{
84 int i, j;
85 int tid[NUMTHREADS];
86 int names[NUMTHREADS];
87
wdenk874ac262003-07-24 23:38:38 +000088 app_startup(av);
89
wdenk8dba0502003-03-31 16:34:49 +000090 sched_init ();
91
92 for (i = 0; i < NUMTHREADS; i++) {
93 names[i] = i;
94 j = thread_create (testthread, (void *) &names[i]);
95 if (j == RC_FAILURE)
wdenk874ac262003-07-24 23:38:38 +000096 printf ("schedtest: Failed to create thread %d\n", i);
wdenk8dba0502003-03-31 16:34:49 +000097 if (j > 0) {
wdenk874ac262003-07-24 23:38:38 +000098 printf ("schedtest: Created thread with id %d, name %d\n",
wdenkb02744a2003-04-05 00:53:31 +000099 j, i);
wdenk8dba0502003-03-31 16:34:49 +0000100 tid[i] = j;
101 }
102 }
wdenk874ac262003-07-24 23:38:38 +0000103 printf ("schedtest: Threads created\n");
wdenk8dba0502003-03-31 16:34:49 +0000104
wdenk874ac262003-07-24 23:38:38 +0000105 printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
wdenk8dba0502003-03-31 16:34:49 +0000106 for (i = 0; i < NUMTHREADS; i++) {
wdenk874ac262003-07-24 23:38:38 +0000107 printf ("schedtest: Setting thread %d runnable\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000108 thread_start (tid[i]);
109 thread_yield ();
110 }
wdenk874ac262003-07-24 23:38:38 +0000111 printf ("schedtest: Started %d threads\n", NUMTHREADS);
wdenk8dba0502003-03-31 16:34:49 +0000112
113 while (1) {
wdenk874ac262003-07-24 23:38:38 +0000114 printf ("schedtest: Waiting for threads to complete\n");
115 if (tstc () && getc () == 0x3) {
116 printf ("schedtest: Aborting threads...\n");
wdenk8dba0502003-03-31 16:34:49 +0000117 for (i = 0; i < NUMTHREADS; i++) {
wdenk874ac262003-07-24 23:38:38 +0000118 printf ("schedtest: Deleting thread %d\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000119 thread_delete (tid[i]);
120 }
121 return RC_SUCCESS;
122 }
123 j = -1;
124 i = thread_join (&j);
125 if (i == RC_FAILURE) {
wdenk874ac262003-07-24 23:38:38 +0000126 printf ("schedtest: No threads pending, "
wdenkb02744a2003-04-05 00:53:31 +0000127 "exiting schedular test\n");
wdenk8dba0502003-03-31 16:34:49 +0000128 return RC_SUCCESS;
129 }
wdenk874ac262003-07-24 23:38:38 +0000130 printf ("schedtest: thread is %d returned %d\n", i, j);
wdenk8dba0502003-03-31 16:34:49 +0000131 thread_yield ();
132 }
133
134 return RC_SUCCESS;
135}
136
137static int testthread (void *name)
138{
139 int i;
140
wdenk874ac262003-07-24 23:38:38 +0000141 printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
142 *(int *) name, (unsigned)&i);
wdenk8dba0502003-03-31 16:34:49 +0000143
wdenk874ac262003-07-24 23:38:38 +0000144 printf ("Thread %02d, i=%d\n", *(int *) name, i);
wdenk8dba0502003-03-31 16:34:49 +0000145
146 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
wdenk874ac262003-07-24 23:38:38 +0000147 if (tstc () && getc () == 0x3) {
148 printf ("testthread: myname %d terminating.\n",
wdenkb02744a2003-04-05 00:53:31 +0000149 *(int *) name);
wdenk8dba0502003-03-31 16:34:49 +0000150 return *(int *) name + 1;
151 }
152
153 if (i % 100 == 0)
154 thread_yield ();
155 }
156
wdenk874ac262003-07-24 23:38:38 +0000157 printf ("testthread: returning %d, i=0x%x\n",
wdenkb02744a2003-04-05 00:53:31 +0000158 *(int *) name + 1, i);
wdenk8dba0502003-03-31 16:34:49 +0000159
160 return *(int *) name + 1;
161}
162
wdenk8dba0502003-03-31 16:34:49 +0000163static void sched_init (void)
164{
165 int i;
166
167 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
168 lthreads[i].state = STATE_EMPTY;
169
170 current_tid = MASTER_THREAD;
171 lthreads[current_tid].state = STATE_RUNNABLE;
wdenkb02744a2003-04-05 00:53:31 +0000172 PDEBUG ("sched_init: master context = 0x%08x",
wdenk874ac262003-07-24 23:38:38 +0000173 (unsigned)lthreads[current_tid].context);
wdenk8dba0502003-03-31 16:34:49 +0000174 return;
175}
176
177static void thread_yield (void)
178{
179 static int i;
180
wdenkb02744a2003-04-05 00:53:31 +0000181 PDEBUG ("thread_yield: current tid=%d", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000182
Wolfgang Denka1be4762008-05-20 16:00:29 +0200183#define SWITCH(new) \
wdenk8dba0502003-03-31 16:34:49 +0000184 if(lthreads[new].state == STATE_RUNNABLE) { \
wdenkb02744a2003-04-05 00:53:31 +0000185 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
wdenk874ac262003-07-24 23:38:38 +0000186 new, \
187 (unsigned)lthreads[current_tid].context); \
wdenk8dba0502003-03-31 16:34:49 +0000188 if(setjmp(lthreads[current_tid].context) == 0) { \
189 current_tid = new; \
wdenkb02744a2003-04-05 00:53:31 +0000190 PDEBUG("thread_yield: tid %d returns 0", \
Wolfgang Denka1be4762008-05-20 16:00:29 +0200191 new); \
wdenk8dba0502003-03-31 16:34:49 +0000192 longjmp(lthreads[new].context, 1); \
193 } else { \
wdenkb02744a2003-04-05 00:53:31 +0000194 PDEBUG("thread_yield: tid %d returns 1", \
Wolfgang Denka1be4762008-05-20 16:00:29 +0200195 new); \
wdenk8dba0502003-03-31 16:34:49 +0000196 return; \
197 } \
198 }
199
200 for (i = current_tid + 1; i < MAX_THREADS; i++) {
201 SWITCH (i);
202 }
203
204 if (current_tid != 0) {
205 for (i = 0; i <= current_tid; i++) {
206 SWITCH (i);
207 }
208 }
209
wdenkb02744a2003-04-05 00:53:31 +0000210 PDEBUG ("thread_yield: returning from thread_yield");
wdenk8dba0502003-03-31 16:34:49 +0000211 return;
212}
213
214static int thread_create (int (*func) (void *), void *arg)
215{
216 int i;
217
218 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
219 if (lthreads[i].state == STATE_EMPTY) {
220 lthreads[i].state = STATE_STOPPED;
221 lthreads[i].func = func;
222 lthreads[i].arg = arg;
wdenkb02744a2003-04-05 00:53:31 +0000223 PDEBUG ("thread_create: returns new tid %d", i);
wdenk8dba0502003-03-31 16:34:49 +0000224 return i;
225 }
226 }
227
wdenkb02744a2003-04-05 00:53:31 +0000228 PDEBUG ("thread_create: returns failure");
wdenk8dba0502003-03-31 16:34:49 +0000229 return RC_FAILURE;
230}
231
232static int thread_delete (int id)
233{
234 if (id <= MASTER_THREAD || id > MAX_THREADS)
235 return RC_FAILURE;
236
237 if (current_tid == id)
238 return RC_FAILURE;
239
240 lthreads[id].state = STATE_EMPTY;
241 return RC_SUCCESS;
242}
243
244static void thread_launcher (void)
245{
wdenkb02744a2003-04-05 00:53:31 +0000246 PDEBUG ("thread_launcher: invoking func=0x%08x",
wdenk874ac262003-07-24 23:38:38 +0000247 (unsigned)lthreads[current_tid].func);
wdenk8dba0502003-03-31 16:34:49 +0000248
249 lthreads[current_tid].retval =
wdenkb02744a2003-04-05 00:53:31 +0000250 lthreads[current_tid].func (lthreads[current_tid].arg);
wdenk8dba0502003-03-31 16:34:49 +0000251
wdenkb02744a2003-04-05 00:53:31 +0000252 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000253
254 lthreads[current_tid].state = STATE_TERMINATED;
255 thread_yield ();
wdenk874ac262003-07-24 23:38:38 +0000256 printf ("thread_launcher: should NEVER get here!\n");
wdenk8dba0502003-03-31 16:34:49 +0000257
258 return;
259}
260
261static int thread_start (int id)
262{
wdenkb02744a2003-04-05 00:53:31 +0000263 PDEBUG ("thread_start: id=%d", id);
wdenk8dba0502003-03-31 16:34:49 +0000264 if (id <= MASTER_THREAD || id > MAX_THREADS) {
265 return RC_FAILURE;
266 }
267
268 if (lthreads[id].state != STATE_STOPPED)
269 return RC_FAILURE;
270
271 if (setjmp (lthreads[current_tid].context) == 0) {
272 lthreads[id].state = STATE_RUNNABLE;
273 current_tid = id;
wdenk874ac262003-07-24 23:38:38 +0000274 PDEBUG ("thread_start: to be stack=0%08x",
275 (unsigned)lthreads[id].stack);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200276 setctxsp ((vu_char *)&lthreads[id].stack[STK_SIZE]);
wdenk8dba0502003-03-31 16:34:49 +0000277 thread_launcher ();
278 }
279
wdenkb02744a2003-04-05 00:53:31 +0000280 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
wdenk8dba0502003-03-31 16:34:49 +0000281
282 return RC_SUCCESS;
283}
284
wdenk874ac262003-07-24 23:38:38 +0000285#if 0 /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000286static int thread_stop (int id)
287{
288 if (id <= MASTER_THREAD || id >= MAX_THREADS)
289 return RC_FAILURE;
290
291 if (current_tid == id)
292 return RC_FAILURE;
293
294 lthreads[id].state = STATE_STOPPED;
295 return RC_SUCCESS;
296}
wdenk874ac262003-07-24 23:38:38 +0000297#endif /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000298
299static int thread_join (int *ret)
300{
301 int i, j = 0;
302
wdenkb02744a2003-04-05 00:53:31 +0000303 PDEBUG ("thread_join: *ret = %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000304
305 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
wdenkb02744a2003-04-05 00:53:31 +0000306 PDEBUG ("thread_join: invalid tid %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000307 return RC_FAILURE;
308 }
309
310 if (*ret == -1) {
wdenkb02744a2003-04-05 00:53:31 +0000311 PDEBUG ("Checking for tid = -1");
wdenk8dba0502003-03-31 16:34:49 +0000312 while (1) {
wdenkb02744a2003-04-05 00:53:31 +0000313 /* PDEBUG("thread_join: start while-loopn"); */
wdenk8dba0502003-03-31 16:34:49 +0000314 j = 0;
315 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
316 if (lthreads[i].state == STATE_TERMINATED) {
317 *ret = lthreads[i].retval;
318 lthreads[i].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000319 /* PDEBUG("thread_join: returning retval %d of tid %d",
320 ret, i); */
wdenk8dba0502003-03-31 16:34:49 +0000321 return RC_SUCCESS;
322 }
323
324 if (lthreads[i].state != STATE_EMPTY) {
wdenkb02744a2003-04-05 00:53:31 +0000325 PDEBUG ("thread_join: %d used slots tid %d state=%d",
326 j, i, lthreads[i].state);
wdenk8dba0502003-03-31 16:34:49 +0000327 j++;
328 }
329 }
330 if (j == 0) {
wdenkb02744a2003-04-05 00:53:31 +0000331 PDEBUG ("thread_join: all slots empty!");
wdenk8dba0502003-03-31 16:34:49 +0000332 return RC_FAILURE;
333 }
wdenkb02744a2003-04-05 00:53:31 +0000334 /* PDEBUG("thread_join: yielding"); */
wdenk8dba0502003-03-31 16:34:49 +0000335 thread_yield ();
wdenkb02744a2003-04-05 00:53:31 +0000336 /* PDEBUG("thread_join: back from yield"); */
wdenk8dba0502003-03-31 16:34:49 +0000337 }
338 }
339
340 if (lthreads[*ret].state == STATE_TERMINATED) {
341 i = *ret;
342 *ret = lthreads[*ret].retval;
343 lthreads[*ret].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000344 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
wdenk8dba0502003-03-31 16:34:49 +0000345 return RC_SUCCESS;
346 }
347
wdenkb02744a2003-04-05 00:53:31 +0000348 PDEBUG ("thread_join: thread %d is not terminated!", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000349 return RC_FAILURE;
350}