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 if (LwpCurrent != nextp && !(LwpCurrent && lwpSave(LwpCurrent->context))) {
114 /* restore previous context */
115 lwpStatus(nextp, "switch in %d", nextp->pri);
117 *LwpContextPtr = LwpCurrent->ud;
118 lwpRestore(LwpCurrent->context);
123 * lwpEntryPoint -- process entry point.
131 sigaddset(&set, SIGALRM);
132 sigprocmask(SIG_SETMASK, &set, &oldmask);
133 *LwpContextPtr = LwpCurrent->ud;
135 lwpStatus(LwpCurrent, "starting at entry point");
136 (*LwpCurrent->entry)(LwpCurrent->ud);
141 * lwpCreate -- create a process.
144 lwpCreate(int priority, void (*entry)(void *), int size, int flags, char *name, char *desc, int argc, char **argv, void *ud)
146 struct lwpProc *newp;
151 #endif /* UCONTEXT */
153 if (!(newp = malloc(sizeof(struct lwpProc))))
155 /* Add a 1K buffer on each side of the stack */
156 redsize = flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
158 size += LWP_EXTRASTACK;
159 size += sizeof(stkalign_t);
160 if (!(s = malloc(size)))
163 newp->name = strdup(name);
164 newp->desc = strdup(desc);
170 sp = s + size - sizeof(stkalign_t) - LWP_EXTRASTACK - redsize;
171 sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
172 newp->lowmark = sp + LWP_EXTRASTACK;
175 sp = s + LWP_EXTRASTACK + redsize;
176 sp = (char *)0 + ((sp - (char *)0) & -sizeof(stkalign_t));
178 newp->himark = s + size - LWP_REDZONE;
180 if (LWP_MAX_PRIO <= priority)
181 priority = LWP_MAX_PRIO - 1;
182 if (LwpMaxpri < (newp->pri = priority))
183 LwpMaxpri = priority;
187 if (flags & LWP_STACKCHECK)
188 lwpStackCheckInit(newp);
189 lwpStatus(newp, "creating process structure sbtm: %p",
192 lwpReady(LwpCurrent);
197 lwpInitContext(newp, &usp); /* architecture-dependent: from arch.c */
199 lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
200 #endif /* UCONTEXT */
206 lwpDestroy(struct lwpProc *proc)
208 if (proc->flags & LWP_STACKCHECK) {
209 lwpStackCheckUsed(proc);
212 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
228 * lwpReady -- put process on ready queue. if null, assume current.
231 lwpReady(struct lwpProc *p)
235 lwpStatus(p, "added to run queue");
236 lwpAddTail(&LwpSchedQ[p->pri], p);
243 lwpGetUD(struct lwpProc *p)
254 lwpSetUD(struct lwpProc *p, char *ud)
265 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
271 p->name = strdup(name);
272 p->desc = strdup(desc);
276 * lwpYield -- yield the processor to another thread.
281 lwpStatus(LwpCurrent, "yielding control");
282 lwpReady(LwpCurrent);
287 * cause the current process to be scheduled for deletion.
292 lwpStatus(LwpCurrent, "marking self as dead");
293 LwpCurrent->dead = 1;
298 * mark another process as dead, so it will never be rescheduled.
299 * remove any lingering FD action
302 lwpTerminate(struct lwpProc *p)
304 lwpStatus(p, "terminating process");
311 * set the thread's priority, returning the old.
312 * if the new priority is lower than the old, we reschedule.
315 lwpSetPriority(int new)
317 int old = LwpCurrent->pri;
319 if (LWP_MAX_PRIO <= new)
320 new = LWP_MAX_PRIO - 1;
323 LwpCurrent->pri = new;
324 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
331 * initialise the coroutine structures
334 lwpInitSystem(int pri, char **ctxptr, int flags)
340 LwpContextPtr = ctxptr;
343 /* *LwpContextPtr = 0; */
344 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
346 if (!(stack = malloc(64)))
348 if (LWP_MAX_PRIO <= pri)
349 pri = LWP_MAX_PRIO - 1;
352 LwpCurrent->next = 0;
353 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
354 LwpCurrent->pri = pri;
355 LwpCurrent->dead = 0;
356 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
357 LwpCurrent->name = "Main";
358 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
359 q->head = q->tail = 0;
360 LwpDeadQ.head = LwpDeadQ.tail = 0;
361 /* must be lower in priority than us for this to work right */
362 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
363 "Select (main loop) Event Handler", 0, 0, 0);
370 * Initialize the entire stack (including both redzones) with the stack
371 * check mark. Thus, we can get some indication of how much stack was
375 lwpStackCheckInit(struct lwpProc *newp)
380 int lim = newp->size / sizeof(long);
381 if (!newp || !newp->sbtm)
383 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
390 * Check if the thread has overflowed/underflowed its stack.
391 * Should that happen, abort the process, as we cannot recover.
394 lwpStackCheck(struct lwpProc *newp)
396 register int end, amt;
397 register unsigned int i;
399 register int growsDown;
402 if (CANT_HAPPEN(!newp || !newp->himark || !newp->lowmark))
404 growsDown = growsdown(&marker);
405 for (lp = newp->himark, i = 0; i < LWP_REDZONE / sizeof(long);
407 if (*lp == LWP_CHECKMARK)
409 /* Stack overflow. */
412 while (i < LWP_REDZONE / sizeof(long)) {
413 if (*lp++ != LWP_CHECKMARK)
417 amt = (end + 1) * sizeof(long);
419 amt = (i + 1) * sizeof(long);
421 logerror("Thread %s stack overflow %d bytes (of %u)",
423 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
426 for (lp = newp->lowmark, i = 0; i < LWP_REDZONE / sizeof(long);
428 if (*lp == LWP_CHECKMARK)
430 /* Stack underflow. */
433 while (i < LWP_REDZONE / sizeof(long)) {
434 if (*lp++ != LWP_CHECKMARK)
438 amt = (end + 1) * sizeof(long);
440 amt = (LWP_REDZONE - i + 1) * sizeof(long);
442 logerror("Thread %s stack underflow %d bytes (of %u)",
444 newp->size - 2 * LWP_REDZONE - (int)sizeof(stkalign_t));
451 * Figure out how much stack was used by this thread.
454 lwpStackCheckUsed(struct lwpProc *newp)
461 if (!newp || !newp->sbtm)
463 lim = newp->size / sizeof(long);
464 if (growsdown(&marker)) {
465 /* Start at the bottom and find first non checkmark. */
466 for (lp = newp->sbtm, i = 0; i < lim; i++, lp++) {
467 if (*lp != LWP_CHECKMARK) {
472 /* Start at the top and find first non checkmark. */
474 lp += newp->size / sizeof(long);
476 for (i = 0; i < lim; i++, lp--) {
477 if (*lp != LWP_CHECKMARK) {
482 lwpStatus(newp, "Thread stack %lu used %lu left %lu total",
483 labs((char *)lp - (char *)newp->lowmark) - LWP_REDZONE,
484 labs((char *)newp->himark - (char *)lp) - LWP_REDZONE,
485 labs((char *)newp->himark - (char *)newp->lowmark) - LWP_REDZONE);