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>
40 extern struct lwpProc *initcontext;
41 extern int startpoint;
44 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
46 struct lwpProc *LwpCurrent = NULL;
48 int LwpMaxpri = 0; /* maximum priority so far */
51 static sigset_t oldmask;
52 #else /* POSIXSIGNALS */
54 #endif /* POSIXSIGNALS */
56 /* for systems without strdup */
58 extern char *strdup();
61 static void lwpStackCheckInit();
62 static int lwpStackCheck();
63 static void lwpStackCheckUsed();
65 /* check stack direction */
85 * lwpReschedule -- schedule another process. we also check for dead
86 * processes here and free them.
91 extern struct lwpQueue LwpSchedQ[];
92 static int lcount = LCOUNT;
93 static struct lwpProc *nextp;
96 static sigset_t tmask;
97 #endif /* POSIXSIGNALS */
99 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
100 lwpStackCheck(LwpCurrent);
103 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
106 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
107 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
108 #else /* POSIXSIGNALS */
109 sigsetmask(sigsetmask(oldmask));
110 #endif /* POSIXSIGNALS */
114 /* destroy dead threads */
115 lwpStatus(LwpCurrent, "Cleaning dead queue");
116 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
117 if (nextp == LwpCurrent) {
118 lwpStatus(nextp, "OOOPS, we are running already dead thread");
122 lwpStatus(LwpCurrent, "Destroying done");
125 for (i = LwpMaxpri + 1; i--;) {
126 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
129 /* clean up after dead bodies */
130 lwpStatus(nextp, "got a dead body");
131 if (nextp == LwpCurrent) {
132 lwpStatus(nextp, "we are in it -- will bury later");
133 lwpAddTail(&LwpDeadQ, nextp);
136 /* fprintf(stderr, "Destroying done\n"); */
143 if (LwpCurrent == 0 && nextp == 0) {
144 fprintf(stderr, "No processes to run!\n");
148 lwpStatus(LwpCurrent, "switch out");
149 /* do context switch */
158 endpoint = &endpoint;
159 if (initcontext == NULL || endpoint < startpoint) {
160 i = lwpSave(LwpCurrent->context);
162 LwpCurrent->size = endpoint - startpoint;
163 LwpCurrent->sbtm = realloc(LwpCurrent->sbtm, LwpCurrent->size);
164 memcpy(LwpCurrent->sbtm, startpoint, LwpCurrent->size);
165 if (i = lwpSave(LwpCurrent->context)) {
166 memcpy(startpoint, LwpCurrent->sbtm, LwpCurrent->size);
172 i = lwpSave(LwpCurrent->context);
178 if (LwpCurrent != nextp && !(LwpCurrent && i)) {
179 /* restore previous context */
180 lwpStatus(nextp, "switch in", nextp->pri);
182 *LwpContextPtr = LwpCurrent->ud;
186 lwpRestore(LwpCurrent->context);
195 * lwpEntryPoint -- process entry point.
200 extern struct lwpProc *LwpCurrent;
203 #endif /* POSIXSIGNALS */
210 sigaddset(&set, SIGALRM);
211 sigprocmask(SIG_SETMASK, &set, &oldmask);
212 #else /* POSIXSIGNALS */
214 #endif /* POSIXSIGNALS */
215 *LwpContextPtr = LwpCurrent->ud;
217 lwpStatus(LwpCurrent, "starting at entry point");
218 (*LwpCurrent->entry) (LwpCurrent->argc, LwpCurrent->argv,
229 * lwpCreate -- create a process.
232 lwpCreate(priority, entry, size, flags, name, desc, argc, argv, ud)
243 extern struct lwpProc *LwpCurrent;
244 struct lwpProc *newp;
250 #endif /* UCONTEXT */
251 unsigned long stackp;
253 if (!(newp = (struct lwpProc *)malloc(sizeof(struct lwpProc))))
255 if (flags & LWP_STACKCHECK) {
256 /* Add a 1K buffer on each side of the stack */
257 size += 2 * LWP_REDZONE;
259 size += LWP_EXTRASTACK;
260 size += sizeof(stkalign_t);
261 if (!(s = (int *)malloc(size)))
264 newp->name = strdup(name);
265 newp->desc = strdup(desc);
270 if ((newp->flags & LWP_STACKCHECK) == 0) {
271 stackp = growsdown((void *)&x) ?
272 (((long)s) + size - sizeof(stkalign_t) - LWP_EXTRASTACK) :
273 (long)s + LWP_EXTRASTACK;
275 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
279 sp = (void *)(stackp & -sizeof(stkalign_t));
280 #endif /* UCONTEXT */
283 /* round address off to stkalign_t */
284 stackp = ((long)s) + size - LWP_REDZONE -
285 LWP_EXTRASTACK - sizeof(stkalign_t);
287 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
290 newp->lowmark = (void *)(((long)sp.ss_sp) + LWP_EXTRASTACK);
292 sp = (void *)(stackp & -sizeof(stkalign_t));
293 newp->lowmark = (void *)(((long)sp) + LWP_EXTRASTACK);
294 #endif /* UCONTEXT */
297 stackp = ((long)s) + LWP_REDZONE + LWP_EXTRASTACK;
299 sp.ss_sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
303 sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
304 #endif /* UCONTEXT */
305 newp->lowmark = (void *)s;
306 newp->himark = (void *)(((long)s) + size - LWP_REDZONE);
309 if (LWP_MAX_PRIO <= priority)
310 priority = LWP_MAX_PRIO - 1;
311 if (LwpMaxpri < (newp->pri = priority))
312 LwpMaxpri = priority;
313 newp->sbtm = (void *)s;
316 if (flags & LWP_STACKCHECK)
317 lwpStackCheckInit(newp);
318 lwpStatus(newp, "creating process structure sbtm: %d",
321 lwpReady(LwpCurrent);
323 lwpInitContext(newp, &sp); /* architecture-dependent: from arch.c */
325 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
326 #endif /* UCONTEXT */
333 struct lwpProc *proc;
335 if (proc->flags & LWP_STACKCHECK) {
336 lwpStackCheckUsed(proc);
339 lwpStatus(proc, "destroying sbtm: %d", (int)proc->sbtm);
343 free((char *)proc->sbtm);
355 * lwpReady -- put process on ready queue. if null, assume current.
361 extern struct lwpProc *LwpCurrent;
362 extern struct lwpQueue LwpSchedQ[];
366 lwpStatus(p, "added to run queue");
367 lwpAddTail(&LwpSchedQ[p->pri], p);
399 lwpSetDesc(p, name, desc)
408 p->name = strdup(name);
409 p->desc = strdup(desc);
413 * lwpYield -- yield the processor to another thread.
418 lwpStatus(LwpCurrent, "yielding control");
419 lwpReady(LwpCurrent);
424 * cause the current process to be scheduled for deletion.
429 lwpStatus(LwpCurrent, "marking self as dead");
430 LwpCurrent->dead = 1;
435 * mark another process as dead, so it will never be rescheduled.
436 * remove any lingering FD action
442 lwpStatus(p, "terminating process");
449 * set the thread's priority, returning the old.
450 * if the new priority is lower than the old, we reschedule.
456 int old = LwpCurrent->pri;
458 if (LWP_MAX_PRIO <= new)
459 new = LWP_MAX_PRIO - 1;
462 LwpCurrent->pri = new;
463 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
470 * initialise the coroutine structures
473 lwpInitSystem(pri, ctxptr, flags)
478 extern struct lwpQueue LwpSchedQ[];
479 extern struct lwpProc *LwpCurrent;
484 LwpContextPtr = ctxptr;
487 /* *LwpContextPtr = 0; */
489 (LwpCurrent = (struct lwpProc *)calloc(1, sizeof(struct lwpProc))))
491 if (!(stack = (int *)malloc(64)))
493 if (LWP_MAX_PRIO <= pri)
494 pri = LWP_MAX_PRIO - 1;
497 LwpCurrent->next = 0;
498 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
499 LwpCurrent->pri = pri;
500 LwpCurrent->dead = 0;
501 LwpCurrent->flags = flags;
502 LwpCurrent->name = "Main";
503 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
504 q->head = q->tail = 0;
505 LwpDeadQ.head = LwpDeadQ.tail = 0;
506 /* must be lower in priority than us for this to work right */
507 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
508 "Select (main loop) Event Handler", 0, 0, 0);
515 * Initialize the entire stack (including both redzones) with the stack
516 * check mark. Thus, we can get some indication of how much stack was
520 lwpStackCheckInit(newp)
521 struct lwpProc *newp;
526 int lim = newp->size / sizeof(long);
527 if (!newp || !newp->sbtm)
529 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
536 * Check if the thread has overflowed/underflowed its stack.
538 * If an problem occurs, it is not corrected.
539 * The buffer is not cleaned up, nor is the thread terminated.
540 * Cleaning up the buffer would be a mistake, and terminating
541 * the thread, well, could be done. Should more like take
542 * down the entire process.
546 struct lwpProc *newp;
548 register int end, amt;
549 register unsigned int i;
551 register int growsDown;
554 if (!newp || !newp->himark || !newp->lowmark)
556 growsDown = growsdown(&marker);
557 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
559 if (*lp == LWP_CHECKMARK)
561 /* Stack overflow. */
564 while (i < LWP_REDZONE / sizeof(long)) {
565 if (*lp++ != LWP_CHECKMARK)
569 amt = (end + 1) * sizeof(long);
571 amt = (i + 1) * sizeof(long);
573 lwpStatus(newp, "Thread stack overflowed %d bytes (of %u)",
574 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
577 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
579 if (*lp == LWP_CHECKMARK)
581 /* Stack underflow. */
584 while (i < LWP_REDZONE / sizeof(long)) {
585 if (*lp++ != LWP_CHECKMARK)
589 amt = (end + 1) * sizeof(long);
591 amt = (LWP_REDZONE - i + 1) * sizeof(long);
593 lwpStatus(newp, "Thread stack underflow %d bytes (of %u)",
594 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
602 * Figure out how much stack was used by this thread.
605 lwpStackCheckUsed(newp)
606 struct lwpProc *newp;
613 if (!newp || !newp->sbtm)
615 lim = newp->size / sizeof(long);
616 if (growsdown(&marker)) {
617 /* Start at the bottom and find first non checkmark. */
618 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
619 if (*lp != LWP_CHECKMARK) {
624 /* Start at the top and find first non checkmark. */
626 lp += newp->size / sizeof(long);
628 for (i = 0; i < lim; i++, lp--) {
629 if (*lp != LWP_CHECKMARK) {
634 lwpStatus(newp, "stack use: %u bytes (of %u total)",
635 (i * sizeof(long)) - LWP_REDZONE,
636 newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));