1 // SPDX-License-Identifier: GPL-2.0+
7 * Author: Arun Dharankar <ADharankar@ATTBI.Com>
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
18 * Basic idea of implementing this is to allow more than one tests to
19 * execute "simultaneously".
21 * This may be modified such thread_yield may be called in syscalls, and
29 #define STK_SIZE 8*1024
32 #define STATE_RUNNABLE 1
33 #define STATE_STOPPED 2
34 #define STATE_TERMINATED 2
36 #define MASTER_THREAD 0
38 #define RC_FAILURE (-1)
39 #define RC_SUCCESS (0)
41 typedef vu_char *jmp_ctx;
42 unsigned long setctxsp (vu_char *sp);
43 int ppc_setjmp(jmp_ctx env);
44 void ppc_longjmp(jmp_ctx env, int val);
45 #define setjmp ppc_setjmp
46 #define longjmp ppc_longjmp
52 uchar context[CTX_SIZE];
56 static volatile struct lthread lthreads[MAX_THREADS];
57 static volatile int current_tid = MASTER_THREAD;
62 #define PDEBUG(fmt, args...) { \
64 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);\
65 printf(fmt, ##args); \
70 static int testthread (void *);
71 static void sched_init (void);
72 static int thread_create (int (*func) (void *), void *arg);
73 static int thread_start (int id);
74 static void thread_yield (void);
75 static int thread_delete (int id);
76 static int thread_join (int *ret);
78 #if 0 /* not used yet */
79 static int thread_stop (int id);
80 #endif /* not used yet */
82 /* An example of schedular test */
85 int sched (int ac, char *av[])
89 int names[NUMTHREADS];
95 for (i = 0; i < NUMTHREADS; i++) {
97 j = thread_create (testthread, (void *) &names[i]);
99 printf ("schedtest: Failed to create thread %d\n", i);
101 printf ("schedtest: Created thread with id %d, name %d\n",
106 printf ("schedtest: Threads created\n");
108 printf ("sched_test: function=0x%08x\n", (unsigned)testthread);
109 for (i = 0; i < NUMTHREADS; i++) {
110 printf ("schedtest: Setting thread %d runnable\n", tid[i]);
111 thread_start (tid[i]);
114 printf ("schedtest: Started %d threads\n", NUMTHREADS);
117 printf ("schedtest: Waiting for threads to complete\n");
118 if (tstc () && getc () == 0x3) {
119 printf ("schedtest: Aborting threads...\n");
120 for (i = 0; i < NUMTHREADS; i++) {
121 printf ("schedtest: Deleting thread %d\n", tid[i]);
122 thread_delete (tid[i]);
127 i = thread_join (&j);
128 if (i == RC_FAILURE) {
129 printf ("schedtest: No threads pending, "
130 "exiting schedular test\n");
133 printf ("schedtest: thread is %d returned %d\n", i, j);
140 static int testthread (void *name)
144 printf ("testthread: Begin executing thread, myname %d, &i=0x%08x\n",
145 *(int *) name, (unsigned)&i);
147 printf ("Thread %02d, i=%d\n", *(int *) name, i);
149 for (i = 0; i < 0xffff * (*(int *) name + 1); i++) {
150 if (tstc () && getc () == 0x3) {
151 printf ("testthread: myname %d terminating.\n",
153 return *(int *) name + 1;
160 printf ("testthread: returning %d, i=0x%x\n",
161 *(int *) name + 1, i);
163 return *(int *) name + 1;
167 static void sched_init (void)
171 for (i = MASTER_THREAD + 1; i < MAX_THREADS; i++)
172 lthreads[i].state = STATE_EMPTY;
174 current_tid = MASTER_THREAD;
175 lthreads[current_tid].state = STATE_RUNNABLE;
176 PDEBUG ("sched_init: master context = 0x%08x",
177 (unsigned)lthreads[current_tid].context);
181 static void thread_yield (void)
185 PDEBUG ("thread_yield: current tid=%d", current_tid);
187 #define SWITCH(new) \
188 if(lthreads[new].state == STATE_RUNNABLE) { \
189 PDEBUG("thread_yield: %d match, ctx=0x%08x", \
191 (unsigned)lthreads[current_tid].context); \
192 if(setjmp(lthreads[current_tid].context) == 0) { \
194 PDEBUG("thread_yield: tid %d returns 0", \
196 longjmp(lthreads[new].context, 1); \
198 PDEBUG("thread_yield: tid %d returns 1", \
204 for (i = current_tid + 1; i < MAX_THREADS; i++) {
208 if (current_tid != 0) {
209 for (i = 0; i <= current_tid; i++) {
214 PDEBUG ("thread_yield: returning from thread_yield");
218 static int thread_create (int (*func) (void *), void *arg)
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;
227 PDEBUG ("thread_create: returns new tid %d", i);
232 PDEBUG ("thread_create: returns failure");
236 static int thread_delete (int id)
238 if (id <= MASTER_THREAD || id > MAX_THREADS)
241 if (current_tid == id)
244 lthreads[id].state = STATE_EMPTY;
248 static void thread_launcher (void)
250 PDEBUG ("thread_launcher: invoking func=0x%08x",
251 (unsigned)lthreads[current_tid].func);
253 lthreads[current_tid].retval =
254 lthreads[current_tid].func (lthreads[current_tid].arg);
256 PDEBUG ("thread_launcher: tid %d terminated", current_tid);
258 lthreads[current_tid].state = STATE_TERMINATED;
260 printf ("thread_launcher: should NEVER get here!\n");
265 static int thread_start (int id)
267 PDEBUG ("thread_start: id=%d", id);
268 if (id <= MASTER_THREAD || id > MAX_THREADS) {
272 if (lthreads[id].state != STATE_STOPPED)
275 if (setjmp (lthreads[current_tid].context) == 0) {
276 lthreads[id].state = STATE_RUNNABLE;
278 PDEBUG ("thread_start: to be stack=0%08x",
279 (unsigned)lthreads[id].stack);
280 setctxsp ((vu_char *)<hreads[id].stack[STK_SIZE]);
284 PDEBUG ("thread_start: Thread id=%d started, parent returns", id);
289 #if 0 /* not used so far */
290 static int thread_stop (int id)
292 if (id <= MASTER_THREAD || id >= MAX_THREADS)
295 if (current_tid == id)
298 lthreads[id].state = STATE_STOPPED;
301 #endif /* not used so far */
303 static int thread_join (int *ret)
307 PDEBUG ("thread_join: *ret = %d", *ret);
309 if (!(*ret == -1 || *ret > MASTER_THREAD || *ret < MAX_THREADS)) {
310 PDEBUG ("thread_join: invalid tid %d", *ret);
315 PDEBUG ("Checking for tid = -1");
317 /* PDEBUG("thread_join: start while-loopn"); */
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;
323 /* PDEBUG("thread_join: returning retval %d of tid %d",
328 if (lthreads[i].state != STATE_EMPTY) {
329 PDEBUG ("thread_join: %d used slots tid %d state=%d",
330 j, i, lthreads[i].state);
335 PDEBUG ("thread_join: all slots empty!");
338 /* PDEBUG("thread_join: yielding"); */
340 /* PDEBUG("thread_join: back from yield"); */
344 if (lthreads[*ret].state == STATE_TERMINATED) {
346 *ret = lthreads[*ret].retval;
347 lthreads[*ret].state = STATE_EMPTY;
348 PDEBUG ("thread_join: returing %d for tid %d", *ret, i);
352 PDEBUG ("thread_join: thread %d is not terminated!", *ret);