2 * SPDX-License-Identifier: GPL-2.0+
9 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
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
20 * Basic idea of implementing this is to allow more than one tests to
21 * execute "simultaneously".
23 * This may be modified such thread_yield may be called in syscalls, and
31 #define STK_SIZE 8*1024
34 #define STATE_RUNNABLE 1
35 #define STATE_STOPPED 2
36 #define STATE_TERMINATED 2
38 #define MASTER_THREAD 0
40 #define RC_FAILURE (-1)
41 #define RC_SUCCESS (0)
43 typedef vu_char *jmp_ctx;
44 unsigned long setctxsp (vu_char *sp);
45 int ppc_setjmp(jmp_ctx env);
46 void ppc_longjmp(jmp_ctx env, int val);
47 #define setjmp ppc_setjmp
48 #define longjmp ppc_longjmp
54 uchar context[CTX_SIZE];
58 static volatile struct lthread lthreads[MAX_THREADS];
59 static volatile int current_tid = MASTER_THREAD;
64 #define PDEBUG(fmt, args...) { \
66 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
67 printf(fmt, ##args); \
72 static int testthread (void *);
73 static void sched_init (void);
74 static int thread_create (int (*func) (void *), void *arg);
75 static int thread_start (int id);
76 static void thread_yield (void);
77 static int thread_delete (int id);
78 static int thread_join (int *ret);
80 #if 0 /* not used yet */
81 static int thread_stop (int id);
82 #endif /* not used yet */
84 /* An example of schedular test */
87 int sched (int ac, char *av[])
91 int names[NUMTHREADS];
97 for (i = 0; i < NUMTHREADS; i++) {
99 j = thread_create (testthread, (void *) &names[i]);
101 printf ("schedtest: Failed to create thread %d\n", i);
103 printf ("schedtest: Created thread with id %d, name %d\n",
108 printf ("schedtest: Threads created\n");
110 printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
111 for (i = 0; i < NUMTHREADS; i++) {
112 printf ("schedtest: Setting thread %d runnable\n", tid[i]);
113 thread_start (tid[i]);
116 printf ("schedtest: Started %d threads\n", NUMTHREADS);
119 printf ("schedtest: Waiting for threads to complete\n");
120 if (tstc () && getc () == 0x3) {
121 printf ("schedtest: Aborting threads...\n");
122 for (i = 0; i < NUMTHREADS; i++) {
123 printf ("schedtest: Deleting thread %d\n", tid[i]);
124 thread_delete (tid[i]);
129 i = thread_join (&j);
130 if (i == RC_FAILURE) {
131 printf ("schedtest: No threads pending, "
132 "exiting schedular test\n");
135 printf ("schedtest: thread is %d returned %d\n", i, j);
142 static int testthread (void *name)
146 printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
147 *(int *) name, (unsigned)&i);
149 printf ("Thread %02d, i=%d\n", *(int *) name, i);
151 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
152 if (tstc () && getc () == 0x3) {
153 printf ("testthread: myname %d terminating.\n",
155 return *(int *) name + 1;
162 printf ("testthread: returning %d, i=0x%x\n",
163 *(int *) name + 1, i);
165 return *(int *) name + 1;
169 static void sched_init (void)
173 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
174 lthreads[i].state = STATE_EMPTY;
176 current_tid = MASTER_THREAD;
177 lthreads[current_tid].state = STATE_RUNNABLE;
178 PDEBUG ("sched_init: master context = 0x%08x",
179 (unsigned)lthreads[current_tid].context);
183 static void thread_yield (void)
187 PDEBUG ("thread_yield: current tid=%d", current_tid);
189 #define SWITCH(new) \
190 if(lthreads[new].state == STATE_RUNNABLE) { \
191 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
193 (unsigned)lthreads[current_tid].context); \
194 if(setjmp(lthreads[current_tid].context) == 0) { \
196 PDEBUG("thread_yield: tid %d returns 0", \
198 longjmp(lthreads[new].context, 1); \
200 PDEBUG("thread_yield: tid %d returns 1", \
206 for (i = current_tid + 1; i < MAX_THREADS; i++) {
210 if (current_tid != 0) {
211 for (i = 0; i <= current_tid; i++) {
216 PDEBUG ("thread_yield: returning from thread_yield");
220 static int thread_create (int (*func) (void *), void *arg)
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;
229 PDEBUG ("thread_create: returns new tid %d", i);
234 PDEBUG ("thread_create: returns failure");
238 static int thread_delete (int id)
240 if (id <= MASTER_THREAD || id > MAX_THREADS)
243 if (current_tid == id)
246 lthreads[id].state = STATE_EMPTY;
250 static void thread_launcher (void)
252 PDEBUG ("thread_launcher: invoking func=0x%08x",
253 (unsigned)lthreads[current_tid].func);
255 lthreads[current_tid].retval =
256 lthreads[current_tid].func (lthreads[current_tid].arg);
258 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
260 lthreads[current_tid].state = STATE_TERMINATED;
262 printf ("thread_launcher: should NEVER get here!\n");
267 static int thread_start (int id)
269 PDEBUG ("thread_start: id=%d", id);
270 if (id <= MASTER_THREAD || id > MAX_THREADS) {
274 if (lthreads[id].state != STATE_STOPPED)
277 if (setjmp (lthreads[current_tid].context) == 0) {
278 lthreads[id].state = STATE_RUNNABLE;
280 PDEBUG ("thread_start: to be stack=0%08x",
281 (unsigned)lthreads[id].stack);
282 setctxsp ((vu_char *)<hreads[id].stack[STK_SIZE]);
286 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
291 #if 0 /* not used so far */
292 static int thread_stop (int id)
294 if (id <= MASTER_THREAD || id >= MAX_THREADS)
297 if (current_tid == id)
300 lthreads[id].state = STATE_STOPPED;
303 #endif /* not used so far */
305 static int thread_join (int *ret)
309 PDEBUG ("thread_join: *ret = %d", *ret);
311 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
312 PDEBUG ("thread_join: invalid tid %d", *ret);
317 PDEBUG ("Checking for tid = -1");
319 /* PDEBUG("thread_join: start while-loopn"); */
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;
325 /* PDEBUG("thread_join: returning retval %d of tid %d",
330 if (lthreads[i].state != STATE_EMPTY) {
331 PDEBUG ("thread_join: %d used slots tid %d state=%d",
332 j, i, lthreads[i].state);
337 PDEBUG ("thread_join: all slots empty!");
340 /* PDEBUG("thread_join: yielding"); */
342 /* PDEBUG("thread_join: back from yield"); */
346 if (lthreads[*ret].state == STATE_TERMINATED) {
348 *ret = lthreads[*ret].retval;
349 lthreads[*ret].state = STATE_EMPTY;
350 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
354 PDEBUG ("thread_join: thread %d is not terminated!", *ret);