blob: f2c496127692b14f1bfae337667e6476ab3624aa [file] [log] [blame]
wdenk8dba0502003-03-31 16:34:49 +00001/*
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02002 * SPDX-License-Identifier: GPL-2.0+
wdenk8dba0502003-03-31 16:34:49 +00003 */
4
5#include <common.h>
wdenk874ac262003-07-24 23:38:38 +00006#include <exports.h>
wdenk8dba0502003-03-31 16:34:49 +00007
8/*
9 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
10 *
11 * A very simple thread/schedular model:
12 * - only one master thread, and no parent child relation maintained
13 * - parent thread cannot be stopped or deleted
14 * - no permissions or credentials
15 * - no elaborate safety checks
16 * - cooperative multi threading
17 * - Simple round-robin scheduleing with no priorities
18 * - no metering/statistics collection
19 *
20 * Basic idea of implementing this is to allow more than one tests to
21 * execute "simultaneously".
22 *
23 * This may be modified such thread_yield may be called in syscalls, and
24 * timer interrupts.
25 */
26
27
28#define MAX_THREADS 8
29
30#define CTX_SIZE 512
31#define STK_SIZE 8*1024
32
33#define STATE_EMPTY 0
34#define STATE_RUNNABLE 1
35#define STATE_STOPPED 2
36#define STATE_TERMINATED 2
37
38#define MASTER_THREAD 0
39
40#define RC_FAILURE (-1)
41#define RC_SUCCESS (0)
42
wdenkb02744a2003-04-05 00:53:31 +000043typedef vu_char *jmp_ctx;
44unsigned long setctxsp (vu_char *sp);
45int ppc_setjmp(jmp_ctx env);
46void ppc_longjmp(jmp_ctx env, int val);
47#define setjmp ppc_setjmp
48#define longjmp ppc_longjmp
49
wdenk8dba0502003-03-31 16:34:49 +000050struct lthread {
51 int state;
52 int retval;
53 char stack[STK_SIZE];
54 uchar context[CTX_SIZE];
55 int (*func) (void *);
56 void *arg;
57};
58static volatile struct lthread lthreads[MAX_THREADS];
59static volatile int current_tid = MASTER_THREAD;
60
61
62static uchar dbg = 0;
63
wdenkb02744a2003-04-05 00:53:31 +000064#define PDEBUG(fmt, args...) { \
65 if(dbg != 0) { \
wdenk874ac262003-07-24 23:38:38 +000066 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
67 printf(fmt, ##args); \
68 printf("\n"); \
wdenkb02744a2003-04-05 00:53:31 +000069 } \
70}
wdenk8dba0502003-03-31 16:34:49 +000071
72static int testthread (void *);
73static void sched_init (void);
74static int thread_create (int (*func) (void *), void *arg);
75static int thread_start (int id);
76static void thread_yield (void);
77static int thread_delete (int id);
78static int thread_join (int *ret);
wdenkb02744a2003-04-05 00:53:31 +000079
80#if 0 /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000081static int thread_stop (int id);
wdenkb02744a2003-04-05 00:53:31 +000082#endif /* not used yet */
wdenk8dba0502003-03-31 16:34:49 +000083
84/* An example of schedular test */
85
86#define NUMTHREADS 7
wdenk874ac262003-07-24 23:38:38 +000087int sched (int ac, char *av[])
wdenk8dba0502003-03-31 16:34:49 +000088{
89 int i, j;
90 int tid[NUMTHREADS];
91 int names[NUMTHREADS];
92
wdenk874ac262003-07-24 23:38:38 +000093 app_startup(av);
94
wdenk8dba0502003-03-31 16:34:49 +000095 sched_init ();
96
97 for (i = 0; i < NUMTHREADS; i++) {
98 names[i] = i;
99 j = thread_create (testthread, (void *) &names[i]);
100 if (j == RC_FAILURE)
wdenk874ac262003-07-24 23:38:38 +0000101 printf ("schedtest: Failed to create thread %d\n", i);
wdenk8dba0502003-03-31 16:34:49 +0000102 if (j > 0) {
wdenk874ac262003-07-24 23:38:38 +0000103 printf ("schedtest: Created thread with id %d, name %d\n",
wdenkb02744a2003-04-05 00:53:31 +0000104 j, i);
wdenk8dba0502003-03-31 16:34:49 +0000105 tid[i] = j;
106 }
107 }
wdenk874ac262003-07-24 23:38:38 +0000108 printf ("schedtest: Threads created\n");
wdenk8dba0502003-03-31 16:34:49 +0000109
wdenk874ac262003-07-24 23:38:38 +0000110 printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
wdenk8dba0502003-03-31 16:34:49 +0000111 for (i = 0; i < NUMTHREADS; i++) {
wdenk874ac262003-07-24 23:38:38 +0000112 printf ("schedtest: Setting thread %d runnable\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000113 thread_start (tid[i]);
114 thread_yield ();
115 }
wdenk874ac262003-07-24 23:38:38 +0000116 printf ("schedtest: Started %d threads\n", NUMTHREADS);
wdenk8dba0502003-03-31 16:34:49 +0000117
118 while (1) {
wdenk874ac262003-07-24 23:38:38 +0000119 printf ("schedtest: Waiting for threads to complete\n");
120 if (tstc () && getc () == 0x3) {
121 printf ("schedtest: Aborting threads...\n");
wdenk8dba0502003-03-31 16:34:49 +0000122 for (i = 0; i < NUMTHREADS; i++) {
wdenk874ac262003-07-24 23:38:38 +0000123 printf ("schedtest: Deleting thread %d\n", tid[i]);
wdenk8dba0502003-03-31 16:34:49 +0000124 thread_delete (tid[i]);
125 }
126 return RC_SUCCESS;
127 }
128 j = -1;
129 i = thread_join (&j);
130 if (i == RC_FAILURE) {
wdenk874ac262003-07-24 23:38:38 +0000131 printf ("schedtest: No threads pending, "
wdenkb02744a2003-04-05 00:53:31 +0000132 "exiting schedular test\n");
wdenk8dba0502003-03-31 16:34:49 +0000133 return RC_SUCCESS;
134 }
wdenk874ac262003-07-24 23:38:38 +0000135 printf ("schedtest: thread is %d returned %d\n", i, j);
wdenk8dba0502003-03-31 16:34:49 +0000136 thread_yield ();
137 }
138
139 return RC_SUCCESS;
140}
141
142static int testthread (void *name)
143{
144 int i;
145
wdenk874ac262003-07-24 23:38:38 +0000146 printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
147 *(int *) name, (unsigned)&i);
wdenk8dba0502003-03-31 16:34:49 +0000148
wdenk874ac262003-07-24 23:38:38 +0000149 printf ("Thread %02d, i=%d\n", *(int *) name, i);
wdenk8dba0502003-03-31 16:34:49 +0000150
151 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
wdenk874ac262003-07-24 23:38:38 +0000152 if (tstc () && getc () == 0x3) {
153 printf ("testthread: myname %d terminating.\n",
wdenkb02744a2003-04-05 00:53:31 +0000154 *(int *) name);
wdenk8dba0502003-03-31 16:34:49 +0000155 return *(int *) name + 1;
156 }
157
158 if (i % 100 == 0)
159 thread_yield ();
160 }
161
wdenk874ac262003-07-24 23:38:38 +0000162 printf ("testthread: returning %d, i=0x%x\n",
wdenkb02744a2003-04-05 00:53:31 +0000163 *(int *) name + 1, i);
wdenk8dba0502003-03-31 16:34:49 +0000164
165 return *(int *) name + 1;
166}
167
168
169static void sched_init (void)
170{
171 int i;
172
173 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
174 lthreads[i].state = STATE_EMPTY;
175
176 current_tid = MASTER_THREAD;
177 lthreads[current_tid].state = STATE_RUNNABLE;
wdenkb02744a2003-04-05 00:53:31 +0000178 PDEBUG ("sched_init: master context = 0x%08x",
wdenk874ac262003-07-24 23:38:38 +0000179 (unsigned)lthreads[current_tid].context);
wdenk8dba0502003-03-31 16:34:49 +0000180 return;
181}
182
183static void thread_yield (void)
184{
185 static int i;
186
wdenkb02744a2003-04-05 00:53:31 +0000187 PDEBUG ("thread_yield: current tid=%d", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000188
Wolfgang Denka1be4762008-05-20 16:00:29 +0200189#define SWITCH(new) \
wdenk8dba0502003-03-31 16:34:49 +0000190 if(lthreads[new].state == STATE_RUNNABLE) { \
wdenkb02744a2003-04-05 00:53:31 +0000191 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
wdenk874ac262003-07-24 23:38:38 +0000192 new, \
193 (unsigned)lthreads[current_tid].context); \
wdenk8dba0502003-03-31 16:34:49 +0000194 if(setjmp(lthreads[current_tid].context) == 0) { \
195 current_tid = new; \
wdenkb02744a2003-04-05 00:53:31 +0000196 PDEBUG("thread_yield: tid %d returns 0", \
Wolfgang Denka1be4762008-05-20 16:00:29 +0200197 new); \
wdenk8dba0502003-03-31 16:34:49 +0000198 longjmp(lthreads[new].context, 1); \
199 } else { \
wdenkb02744a2003-04-05 00:53:31 +0000200 PDEBUG("thread_yield: tid %d returns 1", \
Wolfgang Denka1be4762008-05-20 16:00:29 +0200201 new); \
wdenk8dba0502003-03-31 16:34:49 +0000202 return; \
203 } \
204 }
205
206 for (i = current_tid + 1; i < MAX_THREADS; i++) {
207 SWITCH (i);
208 }
209
210 if (current_tid != 0) {
211 for (i = 0; i <= current_tid; i++) {
212 SWITCH (i);
213 }
214 }
215
wdenkb02744a2003-04-05 00:53:31 +0000216 PDEBUG ("thread_yield: returning from thread_yield");
wdenk8dba0502003-03-31 16:34:49 +0000217 return;
218}
219
220static int thread_create (int (*func) (void *), void *arg)
221{
222 int i;
223
224 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
225 if (lthreads[i].state == STATE_EMPTY) {
226 lthreads[i].state = STATE_STOPPED;
227 lthreads[i].func = func;
228 lthreads[i].arg = arg;
wdenkb02744a2003-04-05 00:53:31 +0000229 PDEBUG ("thread_create: returns new tid %d", i);
wdenk8dba0502003-03-31 16:34:49 +0000230 return i;
231 }
232 }
233
wdenkb02744a2003-04-05 00:53:31 +0000234 PDEBUG ("thread_create: returns failure");
wdenk8dba0502003-03-31 16:34:49 +0000235 return RC_FAILURE;
236}
237
238static int thread_delete (int id)
239{
240 if (id <= MASTER_THREAD || id > MAX_THREADS)
241 return RC_FAILURE;
242
243 if (current_tid == id)
244 return RC_FAILURE;
245
246 lthreads[id].state = STATE_EMPTY;
247 return RC_SUCCESS;
248}
249
250static void thread_launcher (void)
251{
wdenkb02744a2003-04-05 00:53:31 +0000252 PDEBUG ("thread_launcher: invoking func=0x%08x",
wdenk874ac262003-07-24 23:38:38 +0000253 (unsigned)lthreads[current_tid].func);
wdenk8dba0502003-03-31 16:34:49 +0000254
255 lthreads[current_tid].retval =
wdenkb02744a2003-04-05 00:53:31 +0000256 lthreads[current_tid].func (lthreads[current_tid].arg);
wdenk8dba0502003-03-31 16:34:49 +0000257
wdenkb02744a2003-04-05 00:53:31 +0000258 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
wdenk8dba0502003-03-31 16:34:49 +0000259
260 lthreads[current_tid].state = STATE_TERMINATED;
261 thread_yield ();
wdenk874ac262003-07-24 23:38:38 +0000262 printf ("thread_launcher: should NEVER get here!\n");
wdenk8dba0502003-03-31 16:34:49 +0000263
264 return;
265}
266
267static int thread_start (int id)
268{
wdenkb02744a2003-04-05 00:53:31 +0000269 PDEBUG ("thread_start: id=%d", id);
wdenk8dba0502003-03-31 16:34:49 +0000270 if (id <= MASTER_THREAD || id > MAX_THREADS) {
271 return RC_FAILURE;
272 }
273
274 if (lthreads[id].state != STATE_STOPPED)
275 return RC_FAILURE;
276
277 if (setjmp (lthreads[current_tid].context) == 0) {
278 lthreads[id].state = STATE_RUNNABLE;
279 current_tid = id;
wdenk874ac262003-07-24 23:38:38 +0000280 PDEBUG ("thread_start: to be stack=0%08x",
281 (unsigned)lthreads[id].stack);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200282 setctxsp ((vu_char *)&lthreads[id].stack[STK_SIZE]);
wdenk8dba0502003-03-31 16:34:49 +0000283 thread_launcher ();
284 }
285
wdenkb02744a2003-04-05 00:53:31 +0000286 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
wdenk8dba0502003-03-31 16:34:49 +0000287
288 return RC_SUCCESS;
289}
290
wdenk874ac262003-07-24 23:38:38 +0000291#if 0 /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000292static int thread_stop (int id)
293{
294 if (id <= MASTER_THREAD || id >= MAX_THREADS)
295 return RC_FAILURE;
296
297 if (current_tid == id)
298 return RC_FAILURE;
299
300 lthreads[id].state = STATE_STOPPED;
301 return RC_SUCCESS;
302}
wdenk874ac262003-07-24 23:38:38 +0000303#endif /* not used so far */
wdenk8dba0502003-03-31 16:34:49 +0000304
305static int thread_join (int *ret)
306{
307 int i, j = 0;
308
wdenkb02744a2003-04-05 00:53:31 +0000309 PDEBUG ("thread_join: *ret = %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000310
311 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
wdenkb02744a2003-04-05 00:53:31 +0000312 PDEBUG ("thread_join: invalid tid %d", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000313 return RC_FAILURE;
314 }
315
316 if (*ret == -1) {
wdenkb02744a2003-04-05 00:53:31 +0000317 PDEBUG ("Checking for tid = -1");
wdenk8dba0502003-03-31 16:34:49 +0000318 while (1) {
wdenkb02744a2003-04-05 00:53:31 +0000319 /* PDEBUG("thread_join: start while-loopn"); */
wdenk8dba0502003-03-31 16:34:49 +0000320 j = 0;
321 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++) {
322 if (lthreads[i].state == STATE_TERMINATED) {
323 *ret = lthreads[i].retval;
324 lthreads[i].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000325 /* PDEBUG("thread_join: returning retval %d of tid %d",
326 ret, i); */
wdenk8dba0502003-03-31 16:34:49 +0000327 return RC_SUCCESS;
328 }
329
330 if (lthreads[i].state != STATE_EMPTY) {
wdenkb02744a2003-04-05 00:53:31 +0000331 PDEBUG ("thread_join: %d used slots tid %d state=%d",
332 j, i, lthreads[i].state);
wdenk8dba0502003-03-31 16:34:49 +0000333 j++;
334 }
335 }
336 if (j == 0) {
wdenkb02744a2003-04-05 00:53:31 +0000337 PDEBUG ("thread_join: all slots empty!");
wdenk8dba0502003-03-31 16:34:49 +0000338 return RC_FAILURE;
339 }
wdenkb02744a2003-04-05 00:53:31 +0000340 /* PDEBUG("thread_join: yielding"); */
wdenk8dba0502003-03-31 16:34:49 +0000341 thread_yield ();
wdenkb02744a2003-04-05 00:53:31 +0000342 /* PDEBUG("thread_join: back from yield"); */
wdenk8dba0502003-03-31 16:34:49 +0000343 }
344 }
345
346 if (lthreads[*ret].state == STATE_TERMINATED) {
347 i = *ret;
348 *ret = lthreads[*ret].retval;
349 lthreads[*ret].state = STATE_EMPTY;
wdenkb02744a2003-04-05 00:53:31 +0000350 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
wdenk8dba0502003-03-31 16:34:49 +0000351 return RC_SUCCESS;
352 }
353
wdenkb02744a2003-04-05 00:53:31 +0000354 PDEBUG ("thread_join: thread %d is not terminated!", *ret);
wdenk8dba0502003-03-31 16:34:49 +0000355 return RC_FAILURE;
356}