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 */
46 static sigset_t oldmask;
47 #else /* POSIXSIGNALS */
49 #endif /* POSIXSIGNALS */
51 /* for systems without strdup */
53 extern char *strdup();
56 static void lwpStackCheckInit(struct lwpProc *newp);
57 static int lwpStackCheck(struct lwpProc *newp);
58 static void lwpStackCheckUsed(struct lwpProc *newp);
60 /* check stack direction */
79 * lwpReschedule -- schedule another process. we also check for dead
80 * processes here and free them.
85 static int lcount = LCOUNT;
86 static struct lwpProc *nextp;
89 static sigset_t tmask;
90 #endif /* POSIXSIGNALS */
92 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
93 lwpStackCheck(LwpCurrent);
96 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
99 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
100 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
101 #else /* POSIXSIGNALS */
102 sigsetmask(sigsetmask(oldmask));
103 #endif /* POSIXSIGNALS */
107 /* destroy dead threads */
108 lwpStatus(LwpCurrent, "Cleaning dead queue");
109 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
110 if (nextp == LwpCurrent) {
111 lwpStatus(nextp, "OOOPS, we are running already dead thread");
115 lwpStatus(LwpCurrent, "Destroying done");
118 for (i = LwpMaxpri + 1; i--;) {
119 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
122 /* clean up after dead bodies */
123 lwpStatus(nextp, "got a dead body");
124 if (nextp == LwpCurrent) {
125 lwpStatus(nextp, "we are in it -- will bury later");
126 lwpAddTail(&LwpDeadQ, nextp);
129 /* fprintf(stderr, "Destroying done\n"); */
136 if (LwpCurrent == 0 && nextp == 0) {
137 fprintf(stderr, "No processes to run!\n");
141 lwpStatus(LwpCurrent, "switch out");
142 /* do context switch */
151 endpoint = &endpoint;
152 if (initcontext == NULL || endpoint < startpoint) {
153 i = lwpSave(LwpCurrent->context);
155 LwpCurrent->size = endpoint - startpoint;
156 LwpCurrent->sbtm = realloc(LwpCurrent->sbtm, LwpCurrent->size);
157 memcpy(LwpCurrent->sbtm, startpoint, LwpCurrent->size);
158 if (i = lwpSave(LwpCurrent->context)) {
159 memcpy(startpoint, LwpCurrent->sbtm, LwpCurrent->size);
165 i = lwpSave(LwpCurrent->context);
171 if (LwpCurrent != nextp && !(LwpCurrent && i)) {
172 /* restore previous context */
173 lwpStatus(nextp, "switch in", nextp->pri);
175 *LwpContextPtr = LwpCurrent->ud;
179 lwpRestore(LwpCurrent->context);
188 * lwpEntryPoint -- process entry point.
195 #endif /* POSIXSIGNALS */
202 sigaddset(&set, SIGALRM);
203 sigprocmask(SIG_SETMASK, &set, &oldmask);
204 #else /* POSIXSIGNALS */
206 #endif /* POSIXSIGNALS */
207 *LwpContextPtr = LwpCurrent->ud;
209 lwpStatus(LwpCurrent, "starting at entry point");
210 (*LwpCurrent->entry)(LwpCurrent->ud);
220 * lwpCreate -- create a process.
223 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
225 struct lwpProc *newp;
231 #endif /* UCONTEXT */
232 unsigned long stackp;
234 if (!(newp = (struct lwpProc *)malloc(sizeof(struct lwpProc))))
236 if (flags & LWP_STACKCHECK) {
237 /* Add a 1K buffer on each side of the stack */
238 size += 2 * LWP_REDZONE;
240 size += LWP_EXTRASTACK;
241 size += sizeof(stkalign_t);
242 if (!(s = (int *)malloc(size)))
245 newp->name = strdup(name);
246 newp->desc = strdup(desc);
251 if ((newp->flags & LWP_STACKCHECK) == 0) {
252 stackp = growsdown((void *)&x) ?
253 (((long)s) + size - sizeof(stkalign_t) - LWP_EXTRASTACK) :
254 (long)s + LWP_EXTRASTACK;
256 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
260 sp = (void *)(stackp & -sizeof(stkalign_t));
261 #endif /* UCONTEXT */
264 /* round address off to stkalign_t */
265 stackp = ((long)s) + size - LWP_REDZONE -
266 LWP_EXTRASTACK - sizeof(stkalign_t);
268 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
271 newp->lowmark = (void *)(((long)sp.ss_sp) + LWP_EXTRASTACK);
273 sp = (void *)(stackp & -sizeof(stkalign_t));
274 newp->lowmark = (void *)(((long)sp) + LWP_EXTRASTACK);
275 #endif /* UCONTEXT */
278 stackp = ((long)s) + LWP_REDZONE + LWP_EXTRASTACK;
280 sp.ss_sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
284 sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
285 #endif /* UCONTEXT */
286 newp->lowmark = (void *)s;
287 newp->himark = (void *)(((long)s) + size - LWP_REDZONE);
290 if (LWP_MAX_PRIO <= priority)
291 priority = LWP_MAX_PRIO - 1;
292 if (LwpMaxpri < (newp->pri = priority))
293 LwpMaxpri = priority;
294 newp->sbtm = (void *)s;
297 if (flags & LWP_STACKCHECK)
298 lwpStackCheckInit(newp);
299 lwpStatus(newp, "creating process structure sbtm: %d",
302 lwpReady(LwpCurrent);
304 lwpInitContext(newp, &sp); /* architecture-dependent: from arch.c */
306 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
307 #endif /* UCONTEXT */
313 lwpDestroy(struct lwpProc *proc)
315 if (proc->flags & LWP_STACKCHECK) {
316 lwpStackCheckUsed(proc);
319 lwpStatus(proc, "destroying sbtm: %d", (int)proc->sbtm);
323 free((char *)proc->sbtm);
335 * lwpReady -- put process on ready queue. if null, assume current.
338 lwpReady(struct lwpProc *p)
342 lwpStatus(p, "added to run queue");
343 lwpAddTail(&LwpSchedQ[p->pri], p);
350 lwpGetUD(struct lwpProc *p)
361 lwpSetUD(struct lwpProc *p, char *ud)
372 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
378 p->name = strdup(name);
379 p->desc = strdup(desc);
383 * lwpYield -- yield the processor to another thread.
388 lwpStatus(LwpCurrent, "yielding control");
389 lwpReady(LwpCurrent);
394 * cause the current process to be scheduled for deletion.
399 lwpStatus(LwpCurrent, "marking self as dead");
400 LwpCurrent->dead = 1;
405 * mark another process as dead, so it will never be rescheduled.
406 * remove any lingering FD action
409 lwpTerminate(struct lwpProc *p)
411 lwpStatus(p, "terminating process");
418 * set the thread's priority, returning the old.
419 * if the new priority is lower than the old, we reschedule.
422 lwpSetPriority(int new)
424 int old = LwpCurrent->pri;
426 if (LWP_MAX_PRIO <= new)
427 new = LWP_MAX_PRIO - 1;
430 LwpCurrent->pri = new;
431 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
438 * initialise the coroutine structures
441 lwpInitSystem(int pri, char **ctxptr, int flags)
447 LwpContextPtr = ctxptr;
450 /* *LwpContextPtr = 0; */
451 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
453 if (!(stack = (int *)malloc(64)))
455 if (LWP_MAX_PRIO <= pri)
456 pri = LWP_MAX_PRIO - 1;
459 LwpCurrent->next = 0;
460 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
461 LwpCurrent->pri = pri;
462 LwpCurrent->dead = 0;
463 LwpCurrent->flags = flags;
464 LwpCurrent->name = "Main";
465 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
466 q->head = q->tail = 0;
467 LwpDeadQ.head = LwpDeadQ.tail = 0;
468 /* must be lower in priority than us for this to work right */
469 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
470 "Select (main loop) Event Handler", 0, 0, 0);
477 * Initialize the entire stack (including both redzones) with the stack
478 * check mark. Thus, we can get some indication of how much stack was
482 lwpStackCheckInit(struct lwpProc *newp)
487 int lim = newp->size / sizeof(long);
488 if (!newp || !newp->sbtm)
490 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
497 * Check if the thread has overflowed/underflowed its stack.
499 * If an problem occurs, it is not corrected.
500 * The buffer is not cleaned up, nor is the thread terminated.
501 * Cleaning up the buffer would be a mistake, and terminating
502 * the thread, well, could be done. Should more like take
503 * down the entire process.
506 lwpStackCheck(struct lwpProc *newp)
508 register int end, amt;
509 register unsigned int i;
511 register int growsDown;
514 if (!newp || !newp->himark || !newp->lowmark)
516 growsDown = growsdown(&marker);
517 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
519 if (*lp == LWP_CHECKMARK)
521 /* Stack overflow. */
524 while (i < LWP_REDZONE / sizeof(long)) {
525 if (*lp++ != LWP_CHECKMARK)
529 amt = (end + 1) * sizeof(long);
531 amt = (i + 1) * sizeof(long);
533 lwpStatus(newp, "Thread stack overflowed %d bytes (of %u)",
534 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
537 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
539 if (*lp == LWP_CHECKMARK)
541 /* Stack underflow. */
544 while (i < LWP_REDZONE / sizeof(long)) {
545 if (*lp++ != LWP_CHECKMARK)
549 amt = (end + 1) * sizeof(long);
551 amt = (LWP_REDZONE - i + 1) * sizeof(long);
553 lwpStatus(newp, "Thread stack underflow %d bytes (of %u)",
554 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
562 * Figure out how much stack was used by this thread.
565 lwpStackCheckUsed(struct lwpProc *newp)
572 if (!newp || !newp->sbtm)
574 lim = newp->size / sizeof(long);
575 if (growsdown(&marker)) {
576 /* Start at the bottom and find first non checkmark. */
577 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
578 if (*lp != LWP_CHECKMARK) {
583 /* Start at the top and find first non checkmark. */
585 lp += newp->size / sizeof(long);
587 for (i = 0; i < lim; i++, lp--) {
588 if (*lp != LWP_CHECKMARK) {
593 lwpStatus(newp, "stack use: %u bytes (of %u total)",
594 (i * sizeof(long)) - LWP_REDZONE,
595 newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));