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)
34 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
36 struct lwpProc *LwpCurrent = NULL;
38 int LwpMaxpri = 0; /* maximum priority so far */
40 static sigset_t oldmask;
42 static void lwpStackCheckInit(struct lwpProc *newp);
43 static void lwpStackCheck(struct lwpProc *newp);
44 static void lwpStackCheckUsed(struct lwpProc *newp);
46 /* check stack direction */
56 * lwpReschedule -- schedule another process. we also check for dead
57 * processes here and free them.
62 static int lcount = LCOUNT;
63 static struct lwpProc *nextp;
65 static sigset_t tmask;
67 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
68 lwpStackCheck(LwpCurrent);
71 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
73 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
74 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
78 /* destroy dead threads */
79 lwpStatus(LwpCurrent, "Cleaning dead queue");
80 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
81 if (nextp == LwpCurrent) {
82 lwpStatus(nextp, "OOOPS, we are running already dead thread");
86 lwpStatus(LwpCurrent, "Destroying done");
89 for (i = LwpMaxpri + 1; i--;) {
90 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
93 /* clean up after dead bodies */
94 lwpStatus(nextp, "got a dead body");
95 if (nextp == LwpCurrent) {
96 lwpStatus(nextp, "we are in it -- will bury later");
97 lwpAddTail(&LwpDeadQ, nextp);
106 if (LwpCurrent == 0 && nextp == 0) {
107 fprintf(stderr, "No processes to run!\n");
111 lwpStatus(LwpCurrent, "switch out");
112 /* do context switch */
113 i = LwpCurrent && lwpSave(LwpCurrent->context);
114 if (LwpCurrent != nextp && !i) {
115 /* restore previous context */
116 lwpStatus(nextp, "switch in %d", nextp->pri);
118 *LwpContextPtr = LwpCurrent->ud;
119 lwpRestore(LwpCurrent->context);
124 * lwpEntryPoint -- process entry point.
132 sigaddset(&set, SIGALRM);
133 sigprocmask(SIG_SETMASK, &set, &oldmask);
134 *LwpContextPtr = LwpCurrent->ud;
136 lwpStatus(LwpCurrent, "starting at entry point");
137 (*LwpCurrent->entry)(LwpCurrent->ud);
142 * lwpCreate -- create a process.
145 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
147 struct lwpProc *newp;
152 #endif /* UCONTEXT */
154 if (!(newp = malloc(sizeof(struct lwpProc))))
156 /* Add a 1K buffer on each side of the stack */
157 redsize = flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
159 size += LWP_EXTRASTACK;
160 size += sizeof(stkalign_t);
161 if (!(s = malloc(size)))
164 newp->name = strdup(name);
165 newp->desc = strdup(desc);
171 sp = s + size - sizeof(stkalign_t) - LWP_EXTRASTACK - redsize;
172 sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
173 newp->lowmark = sp + LWP_EXTRASTACK;
176 sp = s + LWP_EXTRASTACK + redsize;
177 sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
179 newp->himark = s + size - LWP_REDZONE;
181 if (LWP_MAX_PRIO <= priority)
182 priority = LWP_MAX_PRIO - 1;
183 if (LwpMaxpri < (newp->pri = priority))
184 LwpMaxpri = priority;
188 if (flags & LWP_STACKCHECK)
189 lwpStackCheckInit(newp);
190 lwpStatus(newp, "creating process structure sbtm: %p",
193 lwpReady(LwpCurrent);
198 lwpInitContext(newp, &usp); /* architecture-dependent: from arch.c */
200 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
201 #endif /* UCONTEXT */
207 lwpDestroy(struct lwpProc *proc)
209 if (proc->flags & LWP_STACKCHECK) {
210 lwpStackCheckUsed(proc);
213 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
229 * lwpReady -- put process on ready queue. if null, assume current.
232 lwpReady(struct lwpProc *p)
236 lwpStatus(p, "added to run queue");
237 lwpAddTail(&LwpSchedQ[p->pri], p);
244 lwpGetUD(struct lwpProc *p)
255 lwpSetUD(struct lwpProc *p, char *ud)
266 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
272 p->name = strdup(name);
273 p->desc = strdup(desc);
277 * lwpYield -- yield the processor to another thread.
282 lwpStatus(LwpCurrent, "yielding control");
283 lwpReady(LwpCurrent);
288 * cause the current process to be scheduled for deletion.
293 lwpStatus(LwpCurrent, "marking self as dead");
294 LwpCurrent->dead = 1;
299 * mark another process as dead, so it will never be rescheduled.
300 * remove any lingering FD action
303 lwpTerminate(struct lwpProc *p)
305 lwpStatus(p, "terminating process");
312 * set the thread's priority, returning the old.
313 * if the new priority is lower than the old, we reschedule.
316 lwpSetPriority(int new)
318 int old = LwpCurrent->pri;
320 if (LWP_MAX_PRIO <= new)
321 new = LWP_MAX_PRIO - 1;
324 LwpCurrent->pri = new;
325 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
332 * initialise the coroutine structures
335 lwpInitSystem(int pri, char **ctxptr, int flags)
341 LwpContextPtr = ctxptr;
344 /* *LwpContextPtr = 0; */
345 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
347 if (!(stack = malloc(64)))
349 if (LWP_MAX_PRIO <= pri)
350 pri = LWP_MAX_PRIO - 1;
353 LwpCurrent->next = 0;
354 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
355 LwpCurrent->pri = pri;
356 LwpCurrent->dead = 0;
357 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
358 LwpCurrent->name = "Main";
359 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
360 q->head = q->tail = 0;
361 LwpDeadQ.head = LwpDeadQ.tail = 0;
362 /* must be lower in priority than us for this to work right */
363 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
364 "Select (main loop) Event Handler", 0, 0, 0);
371 * Initialize the entire stack (including both redzones) with the stack
372 * check mark. Thus, we can get some indication of how much stack was
376 lwpStackCheckInit(struct lwpProc *newp)
381 int lim = newp->size / sizeof(long);
382 if (!newp || !newp->sbtm)
384 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
391 * Check if the thread has overflowed/underflowed its stack.
392 * Should that happen, abort the process, as we cannot recover.
395 lwpStackCheck(struct lwpProc *newp)
397 register int end, amt;
398 register unsigned int i;
400 register int growsDown;
403 if (CANT_HAPPEN(!newp || !newp->himark || !newp->lowmark))
405 growsDown = growsdown(&marker);
406 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
408 if (*lp == LWP_CHECKMARK)
410 /* Stack overflow. */
413 while (i < LWP_REDZONE / sizeof(long)) {
414 if (*lp++ != LWP_CHECKMARK)
418 amt = (end + 1) * sizeof(long);
420 amt = (i + 1) * sizeof(long);
422 logerror("Thread %s stack overflow %d bytes (of %u)",
424 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
427 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
429 if (*lp == LWP_CHECKMARK)
431 /* Stack underflow. */
434 while (i < LWP_REDZONE / sizeof(long)) {
435 if (*lp++ != LWP_CHECKMARK)
439 amt = (end + 1) * sizeof(long);
441 amt = (LWP_REDZONE - i + 1) * sizeof(long);
443 logerror("Thread %s stack underflow %d bytes (of %u)",
445 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
452 * Figure out how much stack was used by this thread.
455 lwpStackCheckUsed(struct lwpProc *newp)
462 if (!newp || !newp->sbtm)
464 lim = newp->size / sizeof(long);
465 if (growsdown(&marker)) {
466 /* Start at the bottom and find first non checkmark. */
467 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
468 if (*lp != LWP_CHECKMARK) {
473 /* Start at the top and find first non checkmark. */
475 lp += newp->size / sizeof(long);
477 for (i = 0; i < lim; i++, lp--) {
478 if (*lp != LWP_CHECKMARK) {
483 lwpStatus(newp, "Thread stack %lu used %lu left %lu total",
484 labs((char *)lp - (char *)newp->lowmark) - LWP_REDZONE,
485 labs((char *)newp->himark - (char *)lp) - LWP_REDZONE,
486 labs((char *)newp->himark - (char *)newp->lowmark) - LWP_REDZONE);