blob: d507163f6f3447369e2428728c61c7797ce1a09c [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
24
25#define MAX_THREADS 8
26
27#define CTX_SIZE 512
28#define STK_SIZE 8*1024
29
30#define STATE_EMPTY 0
31#define STATE_RUNNABLE 1
32#define STATE_STOPPED 2
33#define STATE_TERMINATED 2
34
35#define MASTER_THREAD 0
36
37#define RC_FAILURE (-1)
38#define RC_SUCCESS (0)
39
wdenkb02744a2003-04-05 00:53:31 +000040typedef vu_char *jmp_ctx;
41unsigned long setctxsp (vu_char *sp);
42int ppc_setjmp(jmp_ctx env);
43void ppc_longjmp(jmp_ctx env, int val);
44#define setjmp ppc_setjmp
45#define longjmp ppc_longjmp
46
wdenk8dba0502003-03-31 16:34:49 +000047struct lthread {
48 int state;
49 int retval;
50 char stack[STK_SIZE];
51 uchar context[CTX_SIZE];
52 int (*func) (void *);
53 void *arg;
54};
55static volatile struct lthread lthreads[MAX_THREADS];
56static volatile int current_tid = MASTER_THREAD;
57
58
59static uchar dbg = 0;
60
wdenkb02744a2003-04-05 00:53:31 +000061#define PDEBUG(fmt, args...) { \
62 if(dbg != 0) { \
wdenk874ac262003-07-24 23:38:38 +000063 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
64 printf(fmt, ##args); \
65 printf("\n"); \
wdenkb02744a2003-04-05 00:53:31 +000066 } \
67}
wdenk8dba0502003-03-31 16:34:49 +000068
69static int testthread (void *);
70static void sched_init (void);
71static int thread_create (int (*func) (void *), void *arg);
72static int thread_start (int id);
73static void thread_yield (void);
74static int thread_delete (int id);
75static int thread_join (int *ret);
wdenkb02744a2003-04-05 00:53:31 +000076
77#if 0 /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000078static int thread_stop (int id);
wdenkb02744a2003-04-05 00:53:31 +000079#endif /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000080
81/* An example of schedular test */
82
83#define NUMTHREADS 7
wdenk874ac262003-07-24 23:38:38 +000084int sched (int ac, char *av[])
wdenk8dba0502003-03-31 16:34:49 +000085{
86 int i, j;
87 int tid[NUMTHREADS];
88 int names[NUMTHREADS];
89
wdenk874ac262003-07-24 23:38:38 +000090 app_startup(av);
91
wdenk8dba0502003-03-31 16:34:49 +000092 sched_init ();
93
94 for (i = 0; i < NUMTHREADS; i++) {
95 names[i] = i;
96 j = thread_create (testthread, (void *) &names[i]);
97 if (j == RC_FAILURE)
wdenk874ac262003-07-24 23:38:38 +000098 printf ("schedtest: Failed to create thread %d\n", i);
wdenk8dba0502003-03-31 16:34:49 +000099 if (j > 0) {
wdenk874ac262003-07-24 23:38:38 +0000100 printf ("schedtest: Created thread with id %d, name %d\n",
wdenkb02744a2003-04-05 00:53:31 +0000101 j, i);
wdenk8dba0502003-03-31 16:34:49 +0000102 tid[i] = j;
103 }
104 }
wdenk874ac262003-07-24 23:38:38 +0000105 printf ("schedtest: Threads created\n");
wdenk8dba0502003-03-31 16:34:49 +0000106
wdenk874ac262003-07-24 23:38:38 +0000107 printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
wdenk8dba0502003-03-31 16:34:49 +0000108 for (i = 0; i < NUMTHREADS; i++) {
wdenk874ac262003-07-24 23:38:38 +0000109 printf ("schedtest: Setting thread %d runnable\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000110 thread_start (tid[i]);
111 thread_yield ();
112 }
wdenk874ac262003-07-24 23:38:38 +0000113 printf ("schedtest: Started %d threads\n", NUMTHREADS);
wdenk8dba0502003-03-31 16:34:49 +0000114
115 while (1) {
wdenk874ac262003-07-24 23:38:38 +0000116 printf ("schedtest: Waiting for threads to complete\n");
117 if (tstc () && getc () == 0x3) {
118 printf ("schedtest: Aborting threads...\n");
wdenk8dba0502003-03-31 16:34:49 +0000119 for (i = 0; i < NUMTHREADS; i++) {
wdenk874ac262003-07-24 23:38:38 +0000120 printf ("schedtest: Deleting thread %d\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000121 thread_delete (tid[i]);
122 }
123 return RC_SUCCESS;
124 }
125 j = -1;
126 i = thread_join (&j);
127 if (i == RC_FAILURE) {
wdenk874ac262003-07-24 23:38:38 +0000128 printf ("schedtest: No threads pending, "
wdenkb02744a2003-04-05 00:53:31 +0000129 "exiting schedular test\n");
wdenk8dba0502003-03-31 16:34:49 +0000130 return RC_SUCCESS;
131 }
wdenk874ac262003-07-24 23:38:38 +0000132 printf ("schedtest: thread is %d returned %d\n", i, j);
wdenk8dba0502003-03-31 16:34:49 +0000133 thread_yield ();
134 }
135
136 return RC_SUCCESS;
137}
138
139static int testthread (void *name)
140{
141 int i;
142
wdenk874ac262003-07-24 23:38:38 +0000143 printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
144 *(int *) name, (unsigned)&i);
wdenk8dba0502003-03-31 16:34:49 +0000145
wdenk874ac262003-07-24 23:38:38 +0000146 printf ("Thread %02d, i=%d\n", *(int *) name, i);
wdenk8dba0502003-03-31 16:34:49 +0000147
148 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
wdenk874ac262003-07-24 23:38:38 +0000149 if (tstc () && getc () == 0x3) {
150 printf ("testthread: myname %d terminating.\n",
wdenkb02744a2003-04-05 00:53:31 +0000151 *(int *) name);
wdenk8dba0502003-03-31 16:34:49 +0000152 return *(int *) name + 1;
153 }
154
155 if (i % 100 == 0)
156 thread_yield ();
157 }
158
wdenk874ac262003-07-24 23:38:38 +0000159 printf ("testthread: returning %d, i=0x%x\n",
wdenkb02744a2003-04-05 00:53:31 +0000160 *(int *) name + 1, i);
wdenk8dba0502003-03-31 16:34:49 +0000161
162 return *(int *) name + 1;
163}
164
165
166static void sched_init (void)
167{
168 int i;
169
170 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
171 lthreads[i].state = STATE_EMPTY;
172
173 current_tid = MASTER_THREAD;
174 lthreads[current_tid].state = STATE_RUNNABLE;
wdenkb02744a2003-04-05 00:53:31 +0000175 PDEBUG ("sched_init: master context = 0x%08x",
wdenk874ac262003-07-24 23:38:38 +0000176 (unsigned)lthreads[current_tid].context);
wdenk8dba0502003-03-31 16:34:49 +0000177 return;
178}
179
180static void thread_yield (void)
181{
182 static int i;
183
wdenkb02744a2003-04-05 00:53:31 +0000184 PDEBUG ("thread_yield: current tid=%d", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000185
Wolfgang Denka1be4762008-05-20 16:00:29 +0200186#define SWITCH(new) \
wdenk8dba0502003-03-31 16:34:49 +0000187 if(lthreads[new].state == STATE_RUNNABLE) { \
wdenkb02744a2003-04-05 00:53:31 +0000188 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
wdenk874ac262003-07-24 23:38:38 +0000189 new, \
190 (unsigned)lthreads[current_tid].context); \
wdenk8dba0502003-03-31 16:34:49 +0000191 if(setjmp(lthreads[current_tid].context) == 0) { \
192 current_tid = new; \
wdenkb02744a2003-04-05 00:53:31 +0000193 PDEBUG("thread_yield: tid %d returns 0", \
Wolfgang Denka1be4762008-05-20 16:00:29 +0200194 new); \
wdenk8dba0502003-03-31 16:34:49 +0000195 longjmp(lthreads[new].context, 1); \
196 } else { \
wdenkb02744a2003-04-05 00:53:31 +0000197 PDEBUG("thread_yield: tid %d returns 1", \
Wolfgang Denka1be4762008-05-20 16:00:29 +0200198 new); \
wdenk8dba0502003-03-31 16:34:49 +0000199 return; \
200 } \
201 }
202
203 for (i = current_tid + 1; i < MAX_THREADS; i++) {
204 SWITCH (i);
205 }
206
207 if (current_tid != 0) {
208 for (i = 0; i <= current_tid; i++) {
209 SWITCH (i);
210 }
211 }
212
wdenkb02744a2003-04-05 00:53:31 +0000213 PDEBUG ("thread_yield: returning from thread_yield");
wdenk8dba0502003-03-31 16:34:49 +0000214 return;
215}
216
217static int thread_create (int (*func) (void *), void *arg)
218{
219 int i;
220
221 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
222 if (lthreads[i].state == STATE_EMPTY) {
223 lthreads[i].state = STATE_STOPPED;
224 lthreads[i].func = func;
225 lthreads[i].arg = arg;
wdenkb02744a2003-04-05 00:53:31 +0000226 PDEBUG ("thread_create: returns new tid %d", i);
wdenk8dba0502003-03-31 16:34:49 +0000227 return i;
228 }
229 }
230
wdenkb02744a2003-04-05 00:53:31 +0000231 PDEBUG ("thread_create: returns failure");
wdenk8dba0502003-03-31 16:34:49 +0000232 return RC_FAILURE;
233}
234
235static int thread_delete (int id)
236{
237 if (id <= MASTER_THREAD || id > MAX_THREADS)
238 return RC_FAILURE;
239
240 if (current_tid == id)
241 return RC_FAILURE;
242
243 lthreads[id].state = STATE_EMPTY;
244 return RC_SUCCESS;
245}
246
247static void thread_launcher (void)
248{
wdenkb02744a2003-04-05 00:53:31 +0000249 PDEBUG ("thread_launcher: invoking func=0x%08x",
wdenk874ac262003-07-24 23:38:38 +0000250 (unsigned)lthreads[current_tid].func);
wdenk8dba0502003-03-31 16:34:49 +0000251
252 lthreads[current_tid].retval =
wdenkb02744a2003-04-05 00:53:31 +0000253 lthreads[current_tid].func (lthreads[current_tid].arg);
wdenk8dba0502003-03-31 16:34:49 +0000254
wdenkb02744a2003-04-05 00:53:31 +0000255 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000256
257 lthreads[current_tid].state = STATE_TERMINATED;
258 thread_yield ();
wdenk874ac262003-07-24 23:38:38 +0000259 printf ("thread_launcher: should NEVER get here!\n");
wdenk8dba0502003-03-31 16:34:49 +0000260
261 return;
262}
263
264static int thread_start (int id)
265{
wdenkb02744a2003-04-05 00:53:31 +0000266 PDEBUG ("thread_start: id=%d", id);
wdenk8dba0502003-03-31 16:34:49 +0000267 if (id <= MASTER_THREAD || id > MAX_THREADS) {
268 return RC_FAILURE;
269 }
270
271 if (lthreads[id].state != STATE_STOPPED)
272 return RC_FAILURE;
273
274 if (setjmp (lthreads[current_tid].context) == 0) {
275 lthreads[id].state = STATE_RUNNABLE;
276 current_tid = id;
wdenk874ac262003-07-24 23:38:38 +0000277 PDEBUG ("thread_start: to be stack=0%08x",
278 (unsigned)lthreads[id].stack);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200279 setctxsp ((vu_char *)&lthreads[id].stack[STK_SIZE]);
wdenk8dba0502003-03-31 16:34:49 +0000280 thread_launcher ();
281 }
282
wdenkb02744a2003-04-05 00:53:31 +0000283 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
wdenk8dba0502003-03-31 16:34:49 +0000284
285 return RC_SUCCESS;
286}
287
wdenk874ac262003-07-24 23:38:38 +0000288#if 0 /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000289static int thread_stop (int id)
290{
291 if (id <= MASTER_THREAD || id >= MAX_THREADS)
292 return RC_FAILURE;
293
294 if (current_tid == id)
295 return RC_FAILURE;
296
297 lthreads[id].state = STATE_STOPPED;
298 return RC_SUCCESS;
299}
wdenk874ac262003-07-24 23:38:38 +0000300#endif /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000301
302static int thread_join (int *ret)
303{
304 int i, j = 0;
305
wdenkb02744a2003-04-05 00:53:31 +0000306 PDEBUG ("thread_join: *ret = %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000307
308 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
wdenkb02744a2003-04-05 00:53:31 +0000309 PDEBUG ("thread_join: invalid tid %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000310 return RC_FAILURE;
311 }
312
313 if (*ret == -1) {
wdenkb02744a2003-04-05 00:53:31 +0000314 PDEBUG ("Checking for tid = -1");
wdenk8dba0502003-03-31 16:34:49 +0000315 while (1) {
wdenkb02744a2003-04-05 00:53:31 +0000316 /* PDEBUG("thread_join: start while-loopn"); */
wdenk8dba0502003-03-31 16:34:49 +0000317 j = 0;
318 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
319 if (lthreads[i].state == STATE_TERMINATED) {
320 *ret = lthreads[i].retval;
321 lthreads[i].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000322 /* PDEBUG("thread_join: returning retval %d of tid %d",
323 ret, i); */
wdenk8dba0502003-03-31 16:34:49 +0000324 return RC_SUCCESS;
325 }
326
327 if (lthreads[i].state != STATE_EMPTY) {
wdenkb02744a2003-04-05 00:53:31 +0000328 PDEBUG ("thread_join: %d used slots tid %d state=%d",
329 j, i, lthreads[i].state);
wdenk8dba0502003-03-31 16:34:49 +0000330 j++;
331 }
332 }
333 if (j == 0) {
wdenkb02744a2003-04-05 00:53:31 +0000334 PDEBUG ("thread_join: all slots empty!");
wdenk8dba0502003-03-31 16:34:49 +0000335 return RC_FAILURE;
336 }
wdenkb02744a2003-04-05 00:53:31 +0000337 /* PDEBUG("thread_join: yielding"); */
wdenk8dba0502003-03-31 16:34:49 +0000338 thread_yield ();
wdenkb02744a2003-04-05 00:53:31 +0000339 /* PDEBUG("thread_join: back from yield"); */
wdenk8dba0502003-03-31 16:34:49 +0000340 }
341 }
342
343 if (lthreads[*ret].state == STATE_TERMINATED) {
344 i = *ret;
345 *ret = lthreads[*ret].retval;
346 lthreads[*ret].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000347 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
wdenk8dba0502003-03-31 16:34:49 +0000348 return RC_SUCCESS;
349 }
350
wdenkb02744a2003-04-05 00:53:31 +0000351 PDEBUG ("thread_join: thread %d is not terminated!", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000352 return RC_FAILURE;
353}