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(struct lwpProc *newp);
62 static int lwpStackCheck(struct lwpProc *newp);
63 static void lwpStackCheckUsed(struct lwpProc *newp);
65 /* check stack direction */
84 * lwpReschedule -- schedule another process. we also check for dead
85 * processes here and free them.
90 static int lcount = LCOUNT;
91 static struct lwpProc *nextp;
94 static sigset_t tmask;
95 #endif /* POSIXSIGNALS */
97 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
98 lwpStackCheck(LwpCurrent);
101 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
104 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
105 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
106 #else /* POSIXSIGNALS */
107 sigsetmask(sigsetmask(oldmask));
108 #endif /* POSIXSIGNALS */
112 /* destroy dead threads */
113 lwpStatus(LwpCurrent, "Cleaning dead queue");
114 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
115 if (nextp == LwpCurrent) {
116 lwpStatus(nextp, "OOOPS, we are running already dead thread");
120 lwpStatus(LwpCurrent, "Destroying done");
123 for (i = LwpMaxpri + 1; i--;) {
124 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
127 /* clean up after dead bodies */
128 lwpStatus(nextp, "got a dead body");
129 if (nextp == LwpCurrent) {
130 lwpStatus(nextp, "we are in it -- will bury later");
131 lwpAddTail(&LwpDeadQ, nextp);
134 /* fprintf(stderr, "Destroying done\n"); */
141 if (LwpCurrent == 0 && nextp == 0) {
142 fprintf(stderr, "No processes to run!\n");
146 lwpStatus(LwpCurrent, "switch out");
147 /* do context switch */
156 endpoint = &endpoint;
157 if (initcontext == NULL || endpoint < startpoint) {
158 i = lwpSave(LwpCurrent->context);
160 LwpCurrent->size = endpoint - startpoint;
161 LwpCurrent->sbtm = realloc(LwpCurrent->sbtm, LwpCurrent->size);
162 memcpy(LwpCurrent->sbtm, startpoint, LwpCurrent->size);
163 if (i = lwpSave(LwpCurrent->context)) {
164 memcpy(startpoint, LwpCurrent->sbtm, LwpCurrent->size);
170 i = lwpSave(LwpCurrent->context);
176 if (LwpCurrent != nextp && !(LwpCurrent && i)) {
177 /* restore previous context */
178 lwpStatus(nextp, "switch in", nextp->pri);
180 *LwpContextPtr = LwpCurrent->ud;
184 lwpRestore(LwpCurrent->context);
193 * lwpEntryPoint -- process entry point.
200 #endif /* POSIXSIGNALS */
207 sigaddset(&set, SIGALRM);
208 sigprocmask(SIG_SETMASK, &set, &oldmask);
209 #else /* POSIXSIGNALS */
211 #endif /* POSIXSIGNALS */
212 *LwpContextPtr = LwpCurrent->ud;
214 lwpStatus(LwpCurrent, "starting at entry point");
215 (*LwpCurrent->entry)(LwpCurrent->ud);
225 * lwpCreate -- create a process.
228 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
230 struct lwpProc *newp;
236 #endif /* UCONTEXT */
237 unsigned long stackp;
239 if (!(newp = (struct lwpProc *)malloc(sizeof(struct lwpProc))))
241 if (flags & LWP_STACKCHECK) {
242 /* Add a 1K buffer on each side of the stack */
243 size += 2 * LWP_REDZONE;
245 size += LWP_EXTRASTACK;
246 size += sizeof(stkalign_t);
247 if (!(s = (int *)malloc(size)))
250 newp->name = strdup(name);
251 newp->desc = strdup(desc);
256 if ((newp->flags & LWP_STACKCHECK) == 0) {
257 stackp = growsdown((void *)&x) ?
258 (((long)s) + size - sizeof(stkalign_t) - LWP_EXTRASTACK) :
259 (long)s + LWP_EXTRASTACK;
261 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
265 sp = (void *)(stackp & -sizeof(stkalign_t));
266 #endif /* UCONTEXT */
269 /* round address off to stkalign_t */
270 stackp = ((long)s) + size - LWP_REDZONE -
271 LWP_EXTRASTACK - sizeof(stkalign_t);
273 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
276 newp->lowmark = (void *)(((long)sp.ss_sp) + LWP_EXTRASTACK);
278 sp = (void *)(stackp & -sizeof(stkalign_t));
279 newp->lowmark = (void *)(((long)sp) + LWP_EXTRASTACK);
280 #endif /* UCONTEXT */
283 stackp = ((long)s) + LWP_REDZONE + LWP_EXTRASTACK;
285 sp.ss_sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
289 sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
290 #endif /* UCONTEXT */
291 newp->lowmark = (void *)s;
292 newp->himark = (void *)(((long)s) + size - LWP_REDZONE);
295 if (LWP_MAX_PRIO <= priority)
296 priority = LWP_MAX_PRIO - 1;
297 if (LwpMaxpri < (newp->pri = priority))
298 LwpMaxpri = priority;
299 newp->sbtm = (void *)s;
302 if (flags & LWP_STACKCHECK)
303 lwpStackCheckInit(newp);
304 lwpStatus(newp, "creating process structure sbtm: %d",
307 lwpReady(LwpCurrent);
309 lwpInitContext(newp, &sp); /* architecture-dependent: from arch.c */
311 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
312 #endif /* UCONTEXT */
318 lwpDestroy(struct lwpProc *proc)
320 if (proc->flags & LWP_STACKCHECK) {
321 lwpStackCheckUsed(proc);
324 lwpStatus(proc, "destroying sbtm: %d", (int)proc->sbtm);
328 free((char *)proc->sbtm);
340 * lwpReady -- put process on ready queue. if null, assume current.
343 lwpReady(struct lwpProc *p)
347 lwpStatus(p, "added to run queue");
348 lwpAddTail(&LwpSchedQ[p->pri], p);
355 lwpGetUD(struct lwpProc *p)
366 lwpSetUD(struct lwpProc *p, char *ud)
377 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
383 p->name = strdup(name);
384 p->desc = strdup(desc);
388 * lwpYield -- yield the processor to another thread.
393 lwpStatus(LwpCurrent, "yielding control");
394 lwpReady(LwpCurrent);
399 * cause the current process to be scheduled for deletion.
404 lwpStatus(LwpCurrent, "marking self as dead");
405 LwpCurrent->dead = 1;
410 * mark another process as dead, so it will never be rescheduled.
411 * remove any lingering FD action
414 lwpTerminate(struct lwpProc *p)
416 lwpStatus(p, "terminating process");
423 * set the thread's priority, returning the old.
424 * if the new priority is lower than the old, we reschedule.
427 lwpSetPriority(int new)
429 int old = LwpCurrent->pri;
431 if (LWP_MAX_PRIO <= new)
432 new = LWP_MAX_PRIO - 1;
435 LwpCurrent->pri = new;
436 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
443 * initialise the coroutine structures
446 lwpInitSystem(int pri, char **ctxptr, int flags)
452 LwpContextPtr = ctxptr;
455 /* *LwpContextPtr = 0; */
457 (LwpCurrent = (struct lwpProc *)calloc(1, sizeof(struct lwpProc))))
459 if (!(stack = (int *)malloc(64)))
461 if (LWP_MAX_PRIO <= pri)
462 pri = LWP_MAX_PRIO - 1;
465 LwpCurrent->next = 0;
466 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
467 LwpCurrent->pri = pri;
468 LwpCurrent->dead = 0;
469 LwpCurrent->flags = flags;
470 LwpCurrent->name = "Main";
471 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
472 q->head = q->tail = 0;
473 LwpDeadQ.head = LwpDeadQ.tail = 0;
474 /* must be lower in priority than us for this to work right */
475 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
476 "Select (main loop) Event Handler", 0, 0, 0);
483 * Initialize the entire stack (including both redzones) with the stack
484 * check mark. Thus, we can get some indication of how much stack was
488 lwpStackCheckInit(struct lwpProc *newp)
493 int lim = newp->size / sizeof(long);
494 if (!newp || !newp->sbtm)
496 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
503 * Check if the thread has overflowed/underflowed its stack.
505 * If an problem occurs, it is not corrected.
506 * The buffer is not cleaned up, nor is the thread terminated.
507 * Cleaning up the buffer would be a mistake, and terminating
508 * the thread, well, could be done. Should more like take
509 * down the entire process.
512 lwpStackCheck(struct lwpProc *newp)
514 register int end, amt;
515 register unsigned int i;
517 register int growsDown;
520 if (!newp || !newp->himark || !newp->lowmark)
522 growsDown = growsdown(&marker);
523 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
525 if (*lp == LWP_CHECKMARK)
527 /* Stack overflow. */
530 while (i < LWP_REDZONE / sizeof(long)) {
531 if (*lp++ != LWP_CHECKMARK)
535 amt = (end + 1) * sizeof(long);
537 amt = (i + 1) * sizeof(long);
539 lwpStatus(newp, "Thread stack overflowed %d bytes (of %u)",
540 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
543 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
545 if (*lp == LWP_CHECKMARK)
547 /* Stack underflow. */
550 while (i < LWP_REDZONE / sizeof(long)) {
551 if (*lp++ != LWP_CHECKMARK)
555 amt = (end + 1) * sizeof(long);
557 amt = (LWP_REDZONE - i + 1) * sizeof(long);
559 lwpStatus(newp, "Thread stack underflow %d bytes (of %u)",
560 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
568 * Figure out how much stack was used by this thread.
571 lwpStackCheckUsed(struct lwpProc *newp)
578 if (!newp || !newp->sbtm)
580 lim = newp->size / sizeof(long);
581 if (growsdown(&marker)) {
582 /* Start at the bottom and find first non checkmark. */
583 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
584 if (*lp != LWP_CHECKMARK) {
589 /* Start at the top and find first non checkmark. */
591 lp += newp->size / sizeof(long);
593 for (i = 0; i < lim; i++, lp--) {
594 if (*lp != LWP_CHECKMARK) {
599 lwpStatus(newp, "stack use: %u bytes (of %u total)",
600 (i * sizeof(long)) - LWP_REDZONE,
601 newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));