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 static void lwpStackCheckInit(struct lwpProc *newp);
48 static void lwpStackCheck(struct lwpProc *newp);
49 static void lwpStackCheckUsed(struct lwpProc *newp);
51 /* check stack direction */
70 * lwpReschedule -- schedule another process. we also check for dead
71 * processes here and free them.
76 static int lcount = LCOUNT;
77 static struct lwpProc *nextp;
79 static sigset_t tmask;
81 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
82 lwpStackCheck(LwpCurrent);
85 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
87 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
88 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
92 /* destroy dead threads */
93 lwpStatus(LwpCurrent, "Cleaning dead queue");
94 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
95 if (nextp == LwpCurrent) {
96 lwpStatus(nextp, "OOOPS, we are running already dead thread");
100 lwpStatus(LwpCurrent, "Destroying done");
103 for (i = LwpMaxpri + 1; i--;) {
104 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
107 /* clean up after dead bodies */
108 lwpStatus(nextp, "got a dead body");
109 if (nextp == LwpCurrent) {
110 lwpStatus(nextp, "we are in it -- will bury later");
111 lwpAddTail(&LwpDeadQ, nextp);
114 /* fprintf(stderr, "Destroying done\n"); */
121 if (LwpCurrent == 0 && nextp == 0) {
122 fprintf(stderr, "No processes to run!\n");
126 lwpStatus(LwpCurrent, "switch out");
127 /* do context switch */
132 i = lwpSave(LwpCurrent->context);
137 if (LwpCurrent != nextp && !(LwpCurrent && i)) {
138 /* restore previous context */
139 lwpStatus(nextp, "switch in %d", nextp->pri);
141 *LwpContextPtr = LwpCurrent->ud;
145 lwpRestore(LwpCurrent->context);
154 * lwpEntryPoint -- process entry point.
165 sigaddset(&set, SIGALRM);
166 sigprocmask(SIG_SETMASK, &set, &oldmask);
167 *LwpContextPtr = LwpCurrent->ud;
169 lwpStatus(LwpCurrent, "starting at entry point");
170 (*LwpCurrent->entry)(LwpCurrent->ud);
180 * lwpCreate -- create a process.
183 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
185 struct lwpProc *newp;
190 #endif /* UCONTEXT */
192 if (!(newp = malloc(sizeof(struct lwpProc))))
194 /* Add a 1K buffer on each side of the stack */
195 redsize = flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
197 size += LWP_EXTRASTACK;
198 size += sizeof(stkalign_t);
199 if (!(s = malloc(size)))
202 newp->name = strdup(name);
203 newp->desc = strdup(desc);
209 sp = s + size - sizeof(stkalign_t) - LWP_EXTRASTACK - redsize;
210 sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
211 newp->lowmark = sp + LWP_EXTRASTACK;
214 sp = s + LWP_EXTRASTACK + redsize;
215 sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
217 newp->himark = s + size - LWP_REDZONE;
219 if (LWP_MAX_PRIO <= priority)
220 priority = LWP_MAX_PRIO - 1;
221 if (LwpMaxpri < (newp->pri = priority))
222 LwpMaxpri = priority;
226 if (flags & LWP_STACKCHECK)
227 lwpStackCheckInit(newp);
228 lwpStatus(newp, "creating process structure sbtm: %p",
231 lwpReady(LwpCurrent);
236 lwpInitContext(newp, &usp); /* architecture-dependent: from arch.c */
238 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
239 #endif /* UCONTEXT */
245 lwpDestroy(struct lwpProc *proc)
247 if (proc->flags & LWP_STACKCHECK) {
248 lwpStackCheckUsed(proc);
251 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
267 * lwpReady -- put process on ready queue. if null, assume current.
270 lwpReady(struct lwpProc *p)
274 lwpStatus(p, "added to run queue");
275 lwpAddTail(&LwpSchedQ[p->pri], p);
282 lwpGetUD(struct lwpProc *p)
293 lwpSetUD(struct lwpProc *p, char *ud)
304 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
310 p->name = strdup(name);
311 p->desc = strdup(desc);
315 * lwpYield -- yield the processor to another thread.
320 lwpStatus(LwpCurrent, "yielding control");
321 lwpReady(LwpCurrent);
326 * cause the current process to be scheduled for deletion.
331 lwpStatus(LwpCurrent, "marking self as dead");
332 LwpCurrent->dead = 1;
337 * mark another process as dead, so it will never be rescheduled.
338 * remove any lingering FD action
341 lwpTerminate(struct lwpProc *p)
343 lwpStatus(p, "terminating process");
350 * set the thread's priority, returning the old.
351 * if the new priority is lower than the old, we reschedule.
354 lwpSetPriority(int new)
356 int old = LwpCurrent->pri;
358 if (LWP_MAX_PRIO <= new)
359 new = LWP_MAX_PRIO - 1;
362 LwpCurrent->pri = new;
363 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
370 * initialise the coroutine structures
373 lwpInitSystem(int pri, char **ctxptr, int flags)
379 LwpContextPtr = ctxptr;
382 /* *LwpContextPtr = 0; */
383 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
385 if (!(stack = malloc(64)))
387 if (LWP_MAX_PRIO <= pri)
388 pri = LWP_MAX_PRIO - 1;
391 LwpCurrent->next = 0;
392 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
393 LwpCurrent->pri = pri;
394 LwpCurrent->dead = 0;
395 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
396 LwpCurrent->name = "Main";
397 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
398 q->head = q->tail = 0;
399 LwpDeadQ.head = LwpDeadQ.tail = 0;
400 /* must be lower in priority than us for this to work right */
401 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
402 "Select (main loop) Event Handler", 0, 0, 0);
409 * Initialize the entire stack (including both redzones) with the stack
410 * check mark. Thus, we can get some indication of how much stack was
414 lwpStackCheckInit(struct lwpProc *newp)
419 int lim = newp->size / sizeof(long);
420 if (!newp || !newp->sbtm)
422 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
429 * Check if the thread has overflowed/underflowed its stack.
430 * Should that happen, abort the process, as we cannot recover.
433 lwpStackCheck(struct lwpProc *newp)
435 register int end, amt;
436 register unsigned int i;
438 register int growsDown;
441 if (CANT_HAPPEN(!newp || !newp->himark || !newp->lowmark))
443 growsDown = growsdown(&marker);
444 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
446 if (*lp == LWP_CHECKMARK)
448 /* Stack overflow. */
451 while (i < LWP_REDZONE / sizeof(long)) {
452 if (*lp++ != LWP_CHECKMARK)
456 amt = (end + 1) * sizeof(long);
458 amt = (i + 1) * sizeof(long);
460 logerror("Thread %s stack overflow %d bytes (of %u)",
462 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
465 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
467 if (*lp == LWP_CHECKMARK)
469 /* Stack underflow. */
472 while (i < LWP_REDZONE / sizeof(long)) {
473 if (*lp++ != LWP_CHECKMARK)
477 amt = (end + 1) * sizeof(long);
479 amt = (LWP_REDZONE - i + 1) * sizeof(long);
481 logerror("Thread %s stack underflow %d bytes (of %u)",
483 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
490 * Figure out how much stack was used by this thread.
493 lwpStackCheckUsed(struct lwpProc *newp)
500 if (!newp || !newp->sbtm)
502 lim = newp->size / sizeof(long);
503 if (growsdown(&marker)) {
504 /* Start at the bottom and find first non checkmark. */
505 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
506 if (*lp != LWP_CHECKMARK) {
511 /* Start at the top and find first non checkmark. */
513 lp += newp->size / sizeof(long);
515 for (i = 0; i < lim; i++, lp--) {
516 if (*lp != LWP_CHECKMARK) {
521 lwpStatus(newp, "Thread stack %lu used %lu left %lu total",
522 labs((char *)lp - (char *)newp->lowmark) - LWP_REDZONE,
523 labs((char *)newp->himark - (char *)lp) - LWP_REDZONE,
524 labs((char *)newp->himark - (char *)newp->lowmark) - LWP_REDZONE);