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)
35 #include <bounds/fix-args.h>
36 #include <bounds/unchecked.h>
39 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
41 struct lwpProc *LwpCurrent = NULL;
43 int LwpMaxpri = 0; /* maximum priority so far */
45 static sigset_t oldmask;
47 /* for systems without strdup */
49 extern char *strdup();
52 static void lwpStackCheckInit(struct lwpProc *newp);
53 static void lwpStackCheck(struct lwpProc *newp);
54 static void lwpStackCheckUsed(struct lwpProc *newp);
56 /* check stack direction */
75 * lwpReschedule -- schedule another process. we also check for dead
76 * processes here and free them.
81 static int lcount = LCOUNT;
82 static struct lwpProc *nextp;
84 static sigset_t tmask;
86 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
87 lwpStackCheck(LwpCurrent);
90 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
92 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
93 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
97 /* destroy dead threads */
98 lwpStatus(LwpCurrent, "Cleaning dead queue");
99 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
100 if (nextp == LwpCurrent) {
101 lwpStatus(nextp, "OOOPS, we are running already dead thread");
105 lwpStatus(LwpCurrent, "Destroying done");
108 for (i = LwpMaxpri + 1; i--;) {
109 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
112 /* clean up after dead bodies */
113 lwpStatus(nextp, "got a dead body");
114 if (nextp == LwpCurrent) {
115 lwpStatus(nextp, "we are in it -- will bury later");
116 lwpAddTail(&LwpDeadQ, nextp);
119 /* fprintf(stderr, "Destroying done\n"); */
126 if (LwpCurrent == 0 && nextp == 0) {
127 fprintf(stderr, "No processes to run!\n");
131 lwpStatus(LwpCurrent, "switch out");
132 /* do context switch */
141 endpoint = &endpoint;
142 if (initcontext == NULL || endpoint < startpoint) {
143 i = lwpSave(LwpCurrent->context);
145 LwpCurrent->size = endpoint - startpoint;
146 LwpCurrent->sbtm = realloc(LwpCurrent->sbtm, LwpCurrent->size);
147 memcpy(LwpCurrent->sbtm, startpoint, LwpCurrent->size);
148 if (i = lwpSave(LwpCurrent->context)) {
149 memcpy(startpoint, LwpCurrent->sbtm, LwpCurrent->size);
155 i = lwpSave(LwpCurrent->context);
161 if (LwpCurrent != nextp && !(LwpCurrent && i)) {
162 /* restore previous context */
163 lwpStatus(nextp, "switch in %d", nextp->pri);
165 *LwpContextPtr = LwpCurrent->ud;
169 lwpRestore(LwpCurrent->context);
178 * lwpEntryPoint -- process entry point.
189 sigaddset(&set, SIGALRM);
190 sigprocmask(SIG_SETMASK, &set, &oldmask);
191 *LwpContextPtr = LwpCurrent->ud;
193 lwpStatus(LwpCurrent, "starting at entry point");
194 (*LwpCurrent->entry)(LwpCurrent->ud);
204 * lwpCreate -- create a process.
207 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
209 struct lwpProc *newp;
215 #endif /* UCONTEXT */
216 unsigned long stackp;
218 if (!(newp = malloc(sizeof(struct lwpProc))))
220 if (flags & LWP_STACKCHECK) {
221 /* Add a 1K buffer on each side of the stack */
222 size += 2 * LWP_REDZONE;
224 size += LWP_EXTRASTACK;
225 size += sizeof(stkalign_t);
226 if (!(s = malloc(size)))
229 newp->name = strdup(name);
230 newp->desc = strdup(desc);
235 if ((newp->flags & LWP_STACKCHECK) == 0) {
236 stackp = growsdown((void *)&x)
237 ? (unsigned long)s + size - sizeof(stkalign_t) - LWP_EXTRASTACK
238 : (unsigned long)s + LWP_EXTRASTACK;
240 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
244 sp = (void *)(stackp & -sizeof(stkalign_t));
245 #endif /* UCONTEXT */
248 /* round address off to stkalign_t */
249 stackp = ((long)s) + size - LWP_REDZONE -
250 LWP_EXTRASTACK - sizeof(stkalign_t);
252 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
255 newp->lowmark = (void *)(((long)sp.ss_sp) + LWP_EXTRASTACK);
257 sp = (void *)(stackp & -sizeof(stkalign_t));
258 newp->lowmark = (void *)(((long)sp) + LWP_EXTRASTACK);
259 #endif /* UCONTEXT */
262 stackp = ((long)s) + LWP_REDZONE + LWP_EXTRASTACK;
264 sp.ss_sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
268 sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
269 #endif /* UCONTEXT */
270 newp->lowmark = (void *)s;
271 newp->himark = (void *)(((long)s) + size - LWP_REDZONE);
274 if (LWP_MAX_PRIO <= priority)
275 priority = LWP_MAX_PRIO - 1;
276 if (LwpMaxpri < (newp->pri = priority))
277 LwpMaxpri = priority;
278 newp->sbtm = (void *)s;
281 if (flags & LWP_STACKCHECK)
282 lwpStackCheckInit(newp);
283 lwpStatus(newp, "creating process structure sbtm: %p",
286 lwpReady(LwpCurrent);
288 lwpInitContext(newp, &sp); /* architecture-dependent: from arch.c */
290 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
291 #endif /* UCONTEXT */
297 lwpDestroy(struct lwpProc *proc)
299 if (proc->flags & LWP_STACKCHECK) {
300 lwpStackCheckUsed(proc);
303 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
319 * lwpReady -- put process on ready queue. if null, assume current.
322 lwpReady(struct lwpProc *p)
326 lwpStatus(p, "added to run queue");
327 lwpAddTail(&LwpSchedQ[p->pri], p);
334 lwpGetUD(struct lwpProc *p)
345 lwpSetUD(struct lwpProc *p, char *ud)
356 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
362 p->name = strdup(name);
363 p->desc = strdup(desc);
367 * lwpYield -- yield the processor to another thread.
372 lwpStatus(LwpCurrent, "yielding control");
373 lwpReady(LwpCurrent);
378 * cause the current process to be scheduled for deletion.
383 lwpStatus(LwpCurrent, "marking self as dead");
384 LwpCurrent->dead = 1;
389 * mark another process as dead, so it will never be rescheduled.
390 * remove any lingering FD action
393 lwpTerminate(struct lwpProc *p)
395 lwpStatus(p, "terminating process");
402 * set the thread's priority, returning the old.
403 * if the new priority is lower than the old, we reschedule.
406 lwpSetPriority(int new)
408 int old = LwpCurrent->pri;
410 if (LWP_MAX_PRIO <= new)
411 new = LWP_MAX_PRIO - 1;
414 LwpCurrent->pri = new;
415 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
422 * initialise the coroutine structures
425 lwpInitSystem(int pri, char **ctxptr, int flags)
431 LwpContextPtr = ctxptr;
434 /* *LwpContextPtr = 0; */
435 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
437 if (!(stack = malloc(64)))
439 if (LWP_MAX_PRIO <= pri)
440 pri = LWP_MAX_PRIO - 1;
443 LwpCurrent->next = 0;
444 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
445 LwpCurrent->pri = pri;
446 LwpCurrent->dead = 0;
447 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
448 LwpCurrent->name = "Main";
449 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
450 q->head = q->tail = 0;
451 LwpDeadQ.head = LwpDeadQ.tail = 0;
452 /* must be lower in priority than us for this to work right */
453 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
454 "Select (main loop) Event Handler", 0, 0, 0);
461 * Initialize the entire stack (including both redzones) with the stack
462 * check mark. Thus, we can get some indication of how much stack was
466 lwpStackCheckInit(struct lwpProc *newp)
471 int lim = newp->size / sizeof(long);
472 if (!newp || !newp->sbtm)
474 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
481 * Check if the thread has overflowed/underflowed its stack.
482 * Should that happen, abort the process, as we cannot recover.
485 lwpStackCheck(struct lwpProc *newp)
487 register int end, amt;
488 register unsigned int i;
490 register int growsDown;
493 if (CANT_HAPPEN(!newp || !newp->himark || !newp->lowmark))
495 growsDown = growsdown(&marker);
496 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
498 if (*lp == LWP_CHECKMARK)
500 /* Stack overflow. */
503 while (i < LWP_REDZONE / sizeof(long)) {
504 if (*lp++ != LWP_CHECKMARK)
508 amt = (end + 1) * sizeof(long);
510 amt = (i + 1) * sizeof(long);
512 logerror("Thread %s stack overflow %d bytes (of %u)",
514 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
517 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
519 if (*lp == LWP_CHECKMARK)
521 /* Stack underflow. */
524 while (i < LWP_REDZONE / sizeof(long)) {
525 if (*lp++ != LWP_CHECKMARK)
529 amt = (end + 1) * sizeof(long);
531 amt = (LWP_REDZONE - i + 1) * sizeof(long);
533 logerror("Thread %s stack underflow %d bytes (of %u)",
535 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
542 * Figure out how much stack was used by this thread.
545 lwpStackCheckUsed(struct lwpProc *newp)
552 if (!newp || !newp->sbtm)
554 lim = newp->size / sizeof(long);
555 if (growsdown(&marker)) {
556 /* Start at the bottom and find first non checkmark. */
557 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
558 if (*lp != LWP_CHECKMARK) {
563 /* Start at the top and find first non checkmark. */
565 lp += newp->size / sizeof(long);
567 for (i = 0; i < lim; i++, lp--) {
568 if (*lp != LWP_CHECKMARK) {
573 lwpStatus(newp, "Thread stack %lu used %lu left %lu total",
574 labs((char *)lp - (char *)newp->lowmark) - LWP_REDZONE,
575 labs((char *)newp->himark - (char *)lp) - LWP_REDZONE,
576 labs((char *)newp->himark - (char *)newp->lowmark) - LWP_REDZONE);