2 * lwp.c -- lightweight process creation, destruction and manipulation.
3 * Copyright (C) 1991-3 Stephen Crane.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
20 * Imperial College of Science, Technology and Medicine, 180 Queen's
21 * Gate, London SW7 2BZ, England.
30 #include "prototypes.h"
32 #if defined(_EMPTH_LWP)
34 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
36 struct lwpProc *LwpCurrent = NULL;
38 int LwpMaxpri = 0; /* maximum priority so far */
40 static sigset_t oldmask;
42 static void lwpStackCheckInit(struct lwpProc *newp);
43 static void lwpStackCheck(struct lwpProc *newp);
44 static void lwpStackCheckUsed(struct lwpProc *newp);
46 /* check stack direction */
56 * lwpReschedule -- schedule another process. we also check for dead
57 * processes here and free them.
62 static int lcount = LCOUNT;
63 static struct lwpProc *nextp;
65 static sigset_t tmask;
67 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
68 lwpStackCheck(LwpCurrent);
71 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
73 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
74 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
78 /* destroy dead threads */
79 lwpStatus(LwpCurrent, "Cleaning dead queue");
80 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
81 if (nextp == LwpCurrent) {
82 lwpStatus(nextp, "OOOPS, we are running already dead thread");
86 lwpStatus(LwpCurrent, "Destroying done");
89 for (i = LwpMaxpri + 1; i--;) {
90 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
93 /* clean up after dead bodies */
94 lwpStatus(nextp, "got a dead body");
95 if (nextp == LwpCurrent) {
96 lwpStatus(nextp, "we are in it -- will bury later");
97 lwpAddTail(&LwpDeadQ, nextp);
106 if (LwpCurrent == 0 && nextp == 0) {
107 fprintf(stderr, "No processes to run!\n");
111 lwpStatus(LwpCurrent, "switch out");
112 /* do context switch */
113 i = LwpCurrent && lwpSave(LwpCurrent->context);
114 if (LwpCurrent != nextp && !i) {
115 /* restore previous context */
116 lwpStatus(nextp, "switch in %d", nextp->pri);
118 *LwpContextPtr = LwpCurrent->ud;
119 lwpRestore(LwpCurrent->context);
124 * lwpEntryPoint -- process entry point.
132 sigaddset(&set, SIGALRM);
133 sigprocmask(SIG_SETMASK, &set, &oldmask);
134 *LwpContextPtr = LwpCurrent->ud;
136 lwpStatus(LwpCurrent, "starting at entry point");
137 (*LwpCurrent->entry)(LwpCurrent->ud);
142 * lwpCreate -- create a process.
145 lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
147 struct lwpProc *newp;
149 int size, redsize, x;
152 #endif /* UCONTEXT */
154 if (CANT_HAPPEN(STKALIGN == 0|| (STKALIGN & (STKALIGN - 1))))
155 return NULL; /* STKALIGN not power of 2 */
156 if (!(newp = malloc(sizeof(struct lwpProc))))
158 /* Make size a multiple of sizeof(long) to things aligned */
159 stacksz = (stacksz + sizeof(long) - 1) & -sizeof(long);
160 /* Add a red zone on each side of the stack for LWP_STACKCHECK */
161 redsize = flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
162 size = stacksz + 2 * redsize + LWP_EXTRASTACK + STKALIGN - 1;
163 if (!(s = malloc(size)))
166 newp->name = strdup(name);
167 newp->desc = strdup(desc);
174 * Stack layout for stack growing downward:
176 * --------------------------------------
178 * lowmark -> red zone LWP_REDZONE
179 * sp -> extra LWP_EXTRASTACK
181 * himark -> red zone LWP_EXTRASTACK
182 * waste STKALIGN - 1 - x
183 * sp is aligned to a multiple of STKALIGN.
185 sp = s + redsize + stacksz;
186 sp = (char *)0 + (((sp + STKALIGN - 1) - (char *)0) & -STKALIGN);
187 newp->lowmark = sp + LWP_EXTRASTACK;
188 newp->himark = sp - stacksz - redsize;
191 * Stack layout for stack growing upward:
193 * --------------------------------------
195 * himark -> red zone LWP_REDZONE
196 * extra LWP_EXTRASTACK
197 * sp -> stack stacksz
198 * lowmark -> red zone LWP_EXTRASTACK
199 * waste STKALIGN - 1 - x
200 * sp is aligned to a multiple of STKALIGN.
202 sp = s + redsize + LWP_EXTRASTACK;
203 sp = (char *)0 + (((sp + STKALIGN - 1) - (char *)0) & -STKALIGN);
204 newp->lowmark = sp - LWP_EXTRASTACK - redsize;
205 newp->himark = sp + size;
207 if (LWP_MAX_PRIO <= priority)
208 priority = LWP_MAX_PRIO - 1;
209 if (LwpMaxpri < (newp->pri = priority))
210 LwpMaxpri = priority;
214 if (flags & LWP_STACKCHECK)
215 lwpStackCheckInit(newp);
216 lwpStatus(newp, "creating process structure sbtm: %p",
219 lwpReady(LwpCurrent);
221 usp.ss_sp = s + redsize;
222 usp.ss_size = stacksz;
224 lwpInitContext(newp, &usp); /* architecture-dependent: from arch.c */
226 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
227 #endif /* UCONTEXT */
233 lwpDestroy(struct lwpProc *proc)
235 if (proc->flags & LWP_STACKCHECK) {
236 lwpStackCheckUsed(proc);
239 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
255 * lwpReady -- put process on ready queue. if null, assume current.
258 lwpReady(struct lwpProc *p)
262 lwpStatus(p, "added to run queue");
263 lwpAddTail(&LwpSchedQ[p->pri], p);
270 lwpGetUD(struct lwpProc *p)
281 lwpSetUD(struct lwpProc *p, char *ud)
292 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
298 p->name = strdup(name);
299 p->desc = strdup(desc);
303 * lwpYield -- yield the processor to another thread.
308 lwpStatus(LwpCurrent, "yielding control");
309 lwpReady(LwpCurrent);
314 * cause the current process to be scheduled for deletion.
319 lwpStatus(LwpCurrent, "marking self as dead");
320 LwpCurrent->dead = 1;
325 * mark another process as dead, so it will never be rescheduled.
326 * remove any lingering FD action
329 lwpTerminate(struct lwpProc *p)
331 lwpStatus(p, "terminating process");
338 * set the thread's priority, returning the old.
339 * if the new priority is lower than the old, we reschedule.
342 lwpSetPriority(int new)
344 int old = LwpCurrent->pri;
346 if (LWP_MAX_PRIO <= new)
347 new = LWP_MAX_PRIO - 1;
350 LwpCurrent->pri = new;
351 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
358 * initialise the coroutine structures
361 lwpInitSystem(int pri, char **ctxptr, int flags)
367 LwpContextPtr = ctxptr;
370 /* *LwpContextPtr = 0; */
371 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
373 if (!(stack = malloc(64)))
375 if (LWP_MAX_PRIO <= pri)
376 pri = LWP_MAX_PRIO - 1;
379 LwpCurrent->next = 0;
380 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
381 LwpCurrent->pri = pri;
382 LwpCurrent->dead = 0;
383 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
384 LwpCurrent->name = "Main";
385 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
386 q->head = q->tail = 0;
387 LwpDeadQ.head = LwpDeadQ.tail = 0;
388 /* must be lower in priority than us for this to work right */
389 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
390 "Select (main loop) Event Handler", 0, 0, 0);
397 * Initialize the entire stack (including both redzones) with the stack
398 * check mark. Thus, we can get some indication of how much stack was
402 lwpStackCheckInit(struct lwpProc *newp)
407 int lim = newp->size / sizeof(long);
408 if (!newp || !newp->sbtm)
410 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
417 * Check if the thread has overflowed/underflowed its stack.
418 * Should that happen, abort the process, as we cannot recover.
421 lwpStackCheck(struct lwpProc *newp)
423 register int end, amt;
424 register unsigned int i;
426 register int growsDown;
429 if (CANT_HAPPEN(!newp || !newp->himark || !newp->lowmark))
431 growsDown = growsdown(&marker);
432 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
434 if (*lp == LWP_CHECKMARK)
436 /* Stack overflow. */
439 while (i < LWP_REDZONE / sizeof(long)) {
440 if (*lp++ != LWP_CHECKMARK)
444 amt = (end + 1) * sizeof(long);
446 amt = (i + 1) * sizeof(long);
448 logerror("Thread %s stack overflow %d bytes (of %u)",
450 newp->size - 2 * LWP_REDZONE - (int)STKALIGN);
453 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
455 if (*lp == LWP_CHECKMARK)
457 /* Stack underflow. */
460 while (i < LWP_REDZONE / sizeof(long)) {
461 if (*lp++ != LWP_CHECKMARK)
465 amt = (end + 1) * sizeof(long);
467 amt = (LWP_REDZONE - i + 1) * sizeof(long);
469 logerror("Thread %s stack underflow %d bytes (of %u)",
471 newp->size - 2 * LWP_REDZONE - (int)STKALIGN);
478 * Figure out how much stack was used by this thread.
481 lwpStackCheckUsed(struct lwpProc *newp)
488 if (!newp || !newp->sbtm)
490 lim = newp->size / sizeof(long);
491 if (growsdown(&marker)) {
492 /* Start at the bottom and find first non checkmark. */
493 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
494 if (*lp != LWP_CHECKMARK) {
499 /* Start at the top and find first non checkmark. */
501 lp += newp->size / sizeof(long);
503 for (i = 0; i < lim; i++, lp--) {
504 if (*lp != LWP_CHECKMARK) {
509 lwpStatus(newp, "Thread stack %lu used %lu left %lu total",
510 labs((char *)lp - (char *)newp->lowmark) - LWP_REDZONE,
511 labs((char *)newp->himark - (char *)lp) - LWP_REDZONE,
512 labs((char *)newp->himark - (char *)newp->lowmark) - LWP_REDZONE);