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 extern struct lwpQueue LwpSchedQ[];
91 static int lcount = LCOUNT;
92 static struct lwpProc *nextp;
95 static sigset_t tmask;
96 #endif /* POSIXSIGNALS */
98 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
99 lwpStackCheck(LwpCurrent);
102 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
105 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
106 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
107 #else /* POSIXSIGNALS */
108 sigsetmask(sigsetmask(oldmask));
109 #endif /* POSIXSIGNALS */
113 /* destroy dead threads */
114 lwpStatus(LwpCurrent, "Cleaning dead queue");
115 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
116 if (nextp == LwpCurrent) {
117 lwpStatus(nextp, "OOOPS, we are running already dead thread");
121 lwpStatus(LwpCurrent, "Destroying done");
124 for (i = LwpMaxpri + 1; i--;) {
125 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
128 /* clean up after dead bodies */
129 lwpStatus(nextp, "got a dead body");
130 if (nextp == LwpCurrent) {
131 lwpStatus(nextp, "we are in it -- will bury later");
132 lwpAddTail(&LwpDeadQ, nextp);
135 /* fprintf(stderr, "Destroying done\n"); */
142 if (LwpCurrent == 0 && nextp == 0) {
143 fprintf(stderr, "No processes to run!\n");
147 lwpStatus(LwpCurrent, "switch out");
148 /* do context switch */
157 endpoint = &endpoint;
158 if (initcontext == NULL || endpoint < startpoint) {
159 i = lwpSave(LwpCurrent->context);
161 LwpCurrent->size = endpoint - startpoint;
162 LwpCurrent->sbtm = realloc(LwpCurrent->sbtm, LwpCurrent->size);
163 memcpy(LwpCurrent->sbtm, startpoint, LwpCurrent->size);
164 if (i = lwpSave(LwpCurrent->context)) {
165 memcpy(startpoint, LwpCurrent->sbtm, LwpCurrent->size);
171 i = lwpSave(LwpCurrent->context);
177 if (LwpCurrent != nextp && !(LwpCurrent && i)) {
178 /* restore previous context */
179 lwpStatus(nextp, "switch in", nextp->pri);
181 *LwpContextPtr = LwpCurrent->ud;
185 lwpRestore(LwpCurrent->context);
194 * lwpEntryPoint -- process entry point.
201 #endif /* POSIXSIGNALS */
208 sigaddset(&set, SIGALRM);
209 sigprocmask(SIG_SETMASK, &set, &oldmask);
210 #else /* POSIXSIGNALS */
212 #endif /* POSIXSIGNALS */
213 *LwpContextPtr = LwpCurrent->ud;
215 lwpStatus(LwpCurrent, "starting at entry point");
216 (*LwpCurrent->entry)(LwpCurrent->ud);
226 * lwpCreate -- create a process.
229 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
231 struct lwpProc *newp;
237 #endif /* UCONTEXT */
238 unsigned long stackp;
240 if (!(newp = (struct lwpProc *)malloc(sizeof(struct lwpProc))))
242 if (flags & LWP_STACKCHECK) {
243 /* Add a 1K buffer on each side of the stack */
244 size += 2 * LWP_REDZONE;
246 size += LWP_EXTRASTACK;
247 size += sizeof(stkalign_t);
248 if (!(s = (int *)malloc(size)))
251 newp->name = strdup(name);
252 newp->desc = strdup(desc);
257 if ((newp->flags & LWP_STACKCHECK) == 0) {
258 stackp = growsdown((void *)&x) ?
259 (((long)s) + size - sizeof(stkalign_t) - LWP_EXTRASTACK) :
260 (long)s + LWP_EXTRASTACK;
262 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
266 sp = (void *)(stackp & -sizeof(stkalign_t));
267 #endif /* UCONTEXT */
270 /* round address off to stkalign_t */
271 stackp = ((long)s) + size - LWP_REDZONE -
272 LWP_EXTRASTACK - sizeof(stkalign_t);
274 sp.ss_sp = (void *)(stackp & -sizeof(stkalign_t));
277 newp->lowmark = (void *)(((long)sp.ss_sp) + LWP_EXTRASTACK);
279 sp = (void *)(stackp & -sizeof(stkalign_t));
280 newp->lowmark = (void *)(((long)sp) + LWP_EXTRASTACK);
281 #endif /* UCONTEXT */
284 stackp = ((long)s) + LWP_REDZONE + LWP_EXTRASTACK;
286 sp.ss_sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
290 sp = (void *)(((long)stackp) & -sizeof(stkalign_t));
291 #endif /* UCONTEXT */
292 newp->lowmark = (void *)s;
293 newp->himark = (void *)(((long)s) + size - LWP_REDZONE);
296 if (LWP_MAX_PRIO <= priority)
297 priority = LWP_MAX_PRIO - 1;
298 if (LwpMaxpri < (newp->pri = priority))
299 LwpMaxpri = priority;
300 newp->sbtm = (void *)s;
303 if (flags & LWP_STACKCHECK)
304 lwpStackCheckInit(newp);
305 lwpStatus(newp, "creating process structure sbtm: %d",
308 lwpReady(LwpCurrent);
310 lwpInitContext(newp, &sp); /* architecture-dependent: from arch.c */
312 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
313 #endif /* UCONTEXT */
319 lwpDestroy(struct lwpProc *proc)
321 if (proc->flags & LWP_STACKCHECK) {
322 lwpStackCheckUsed(proc);
325 lwpStatus(proc, "destroying sbtm: %d", (int)proc->sbtm);
329 free((char *)proc->sbtm);
341 * lwpReady -- put process on ready queue. if null, assume current.
344 lwpReady(struct lwpProc *p)
348 lwpStatus(p, "added to run queue");
349 lwpAddTail(&LwpSchedQ[p->pri], p);
356 lwpGetUD(struct lwpProc *p)
367 lwpSetUD(struct lwpProc *p, char *ud)
378 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
384 p->name = strdup(name);
385 p->desc = strdup(desc);
389 * lwpYield -- yield the processor to another thread.
394 lwpStatus(LwpCurrent, "yielding control");
395 lwpReady(LwpCurrent);
400 * cause the current process to be scheduled for deletion.
405 lwpStatus(LwpCurrent, "marking self as dead");
406 LwpCurrent->dead = 1;
411 * mark another process as dead, so it will never be rescheduled.
412 * remove any lingering FD action
415 lwpTerminate(struct lwpProc *p)
417 lwpStatus(p, "terminating process");
424 * set the thread's priority, returning the old.
425 * if the new priority is lower than the old, we reschedule.
428 lwpSetPriority(int new)
430 int old = LwpCurrent->pri;
432 if (LWP_MAX_PRIO <= new)
433 new = LWP_MAX_PRIO - 1;
436 LwpCurrent->pri = new;
437 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
444 * initialise the coroutine structures
447 lwpInitSystem(int pri, char **ctxptr, int flags)
453 LwpContextPtr = ctxptr;
456 /* *LwpContextPtr = 0; */
458 (LwpCurrent = (struct lwpProc *)calloc(1, sizeof(struct lwpProc))))
460 if (!(stack = (int *)malloc(64)))
462 if (LWP_MAX_PRIO <= pri)
463 pri = LWP_MAX_PRIO - 1;
466 LwpCurrent->next = 0;
467 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
468 LwpCurrent->pri = pri;
469 LwpCurrent->dead = 0;
470 LwpCurrent->flags = flags;
471 LwpCurrent->name = "Main";
472 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
473 q->head = q->tail = 0;
474 LwpDeadQ.head = LwpDeadQ.tail = 0;
475 /* must be lower in priority than us for this to work right */
476 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
477 "Select (main loop) Event Handler", 0, 0, 0);
484 * Initialize the entire stack (including both redzones) with the stack
485 * check mark. Thus, we can get some indication of how much stack was
489 lwpStackCheckInit(struct lwpProc *newp)
494 int lim = newp->size / sizeof(long);
495 if (!newp || !newp->sbtm)
497 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
504 * Check if the thread has overflowed/underflowed its stack.
506 * If an problem occurs, it is not corrected.
507 * The buffer is not cleaned up, nor is the thread terminated.
508 * Cleaning up the buffer would be a mistake, and terminating
509 * the thread, well, could be done. Should more like take
510 * down the entire process.
513 lwpStackCheck(struct lwpProc *newp)
515 register int end, amt;
516 register unsigned int i;
518 register int growsDown;
521 if (!newp || !newp->himark || !newp->lowmark)
523 growsDown = growsdown(&marker);
524 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
526 if (*lp == LWP_CHECKMARK)
528 /* Stack overflow. */
531 while (i < LWP_REDZONE / sizeof(long)) {
532 if (*lp++ != LWP_CHECKMARK)
536 amt = (end + 1) * sizeof(long);
538 amt = (i + 1) * sizeof(long);
540 lwpStatus(newp, "Thread stack overflowed %d bytes (of %u)",
541 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
544 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
546 if (*lp == LWP_CHECKMARK)
548 /* Stack underflow. */
551 while (i < LWP_REDZONE / sizeof(long)) {
552 if (*lp++ != LWP_CHECKMARK)
556 amt = (end + 1) * sizeof(long);
558 amt = (LWP_REDZONE - i + 1) * sizeof(long);
560 lwpStatus(newp, "Thread stack underflow %d bytes (of %u)",
561 amt, newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));
569 * Figure out how much stack was used by this thread.
572 lwpStackCheckUsed(struct lwpProc *newp)
579 if (!newp || !newp->sbtm)
581 lim = newp->size / sizeof(long);
582 if (growsdown(&marker)) {
583 /* Start at the bottom and find first non checkmark. */
584 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
585 if (*lp != LWP_CHECKMARK) {
590 /* Start at the top and find first non checkmark. */
592 lp += newp->size / sizeof(long);
594 for (i = 0; i < lim; i++, lp--) {
595 if (*lp != LWP_CHECKMARK) {
600 lwpStatus(newp, "stack use: %u bytes (of %u total)",
601 (i * sizeof(long)) - LWP_REDZONE,
602 newp->size - 2 * LWP_REDZONE - sizeof(stkalign_t));