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 */
39 int LwpStackGrowsDown;
41 static sigset_t oldmask;
43 static void lwpStackCheckInit(struct lwpProc *newp);
44 static void lwpStackCheck(struct lwpProc *newp);
45 static void lwpStackCheckUsed(struct lwpProc *newp);
47 /* check stack direction */
57 * lwpReschedule -- schedule another process. we also check for dead
58 * processes here and free them.
63 static int lcount = LCOUNT;
64 static struct lwpProc *nextp;
66 static sigset_t tmask;
68 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
69 lwpStackCheck(LwpCurrent);
72 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
74 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
75 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
79 /* destroy dead threads */
80 lwpStatus(LwpCurrent, "Cleaning dead queue");
81 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
82 if (nextp == LwpCurrent) {
83 lwpStatus(nextp, "OOOPS, we are running already dead thread");
87 lwpStatus(LwpCurrent, "Destroying done");
90 for (i = LwpMaxpri + 1; i--;) {
91 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
94 /* clean up after dead bodies */
95 lwpStatus(nextp, "got a dead body");
96 if (nextp == LwpCurrent) {
97 lwpStatus(nextp, "we are in it -- will bury later");
98 lwpAddTail(&LwpDeadQ, nextp);
107 if (LwpCurrent == 0 && nextp == 0) {
108 fprintf(stderr, "No processes to run!\n");
111 if (LwpCurrent != nextp) {
112 struct lwpProc *oldp = LwpCurrent;
114 lwpStatus(oldp, "switch out");
116 *LwpContextPtr = nextp->ud;
117 lwpSwitchContext(oldp, nextp);
118 lwpStatus(nextp, "switch in %d", nextp->pri);
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 stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
146 struct lwpProc *newp;
148 if (!(newp = malloc(sizeof(struct lwpProc))))
151 newp->name = strdup(name);
152 newp->desc = strdup(desc);
158 if (LWP_MAX_PRIO <= priority)
159 priority = LWP_MAX_PRIO - 1;
160 if (LwpMaxpri < (newp->pri = priority))
161 LwpMaxpri = priority;
162 lwpNewContext(newp, stacksz);
163 lwpStatus(newp, "creating process structure %p (sbtm %p)",
165 if (flags & LWP_STACKCHECK)
166 lwpStackCheckInit(newp);
168 lwpReady(LwpCurrent);
174 lwpDestroy(struct lwpProc *proc)
176 if (proc->flags & LWP_STACKCHECK) {
177 lwpStackCheckUsed(proc);
180 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
188 * lwpReady -- put process on ready queue. if null, assume current.
191 lwpReady(struct lwpProc *p)
195 lwpStatus(p, "added to run queue");
196 lwpAddTail(&LwpSchedQ[p->pri], p);
203 lwpGetUD(struct lwpProc *p)
214 lwpSetUD(struct lwpProc *p, char *ud)
225 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
231 p->name = strdup(name);
232 p->desc = strdup(desc);
236 * lwpYield -- yield the processor to another thread.
241 lwpStatus(LwpCurrent, "yielding control");
242 lwpReady(LwpCurrent);
247 * cause the current process to be scheduled for deletion.
252 lwpStatus(LwpCurrent, "marking self as dead");
253 LwpCurrent->dead = 1;
258 * mark another process as dead, so it will never be rescheduled.
259 * remove any lingering FD action
262 lwpTerminate(struct lwpProc *p)
264 lwpStatus(p, "terminating process");
271 * set the thread's priority, returning the old.
272 * if the new priority is lower than the old, we reschedule.
275 lwpSetPriority(int new)
277 int old = LwpCurrent->pri;
279 if (LWP_MAX_PRIO <= new)
280 new = LWP_MAX_PRIO - 1;
283 LwpCurrent->pri = new;
284 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
291 * initialise the coroutine structures
294 lwpInitSystem(int pri, char **ctxptr, int flags)
297 int i, *stack, marker;
300 LwpContextPtr = ctxptr;
303 /* *LwpContextPtr = 0; */
304 LwpStackGrowsDown = growsdown(&marker);
305 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
307 if (!(stack = malloc(64)))
309 if (LWP_MAX_PRIO <= pri)
310 pri = LWP_MAX_PRIO - 1;
313 LwpCurrent->next = 0;
314 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
315 LwpCurrent->pri = pri;
316 LwpCurrent->dead = 0;
317 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
318 LwpCurrent->name = "Main";
319 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
320 q->head = q->tail = 0;
321 LwpDeadQ.head = LwpDeadQ.tail = 0;
322 /* must be lower in priority than us for this to work right */
323 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
324 "Select (main loop) Event Handler", 0, 0, 0);
331 * Initialize the entire stack (including both redzones) with the stack
332 * check mark. Thus, we can get some indication of how much stack was
336 lwpStackCheckInit(struct lwpProc *newp)
339 int *lim = (int *)((char *)newp->sbtm + newp->size);
341 for (p = newp->sbtm; p < lim; p++)
347 * Check if the thread has overflowed/underflowed its stack.
348 * Should that happen, abort the process, as we cannot recover.
351 lwpStackCheck(struct lwpProc *newp)
353 int *btm = (int *)(newp->ustack - LWP_REDZONE);
354 int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
355 int n = LWP_REDZONE / sizeof(int);
356 int i, lo_clean, hi_clean, overflow, underflow;
358 for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
361 for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
364 if (LwpStackGrowsDown) {
365 overflow = n - lo_clean;
366 underflow = n - hi_clean;
368 overflow = n - hi_clean;
369 underflow = n - lo_clean;
372 logerror("Thread %s stack overflow %d bytes",
373 newp->name, overflow * sizeof(int));
375 logerror("Thread %s stack underflow %d bytes",
376 newp->name, underflow * sizeof(int));
377 if (overflow || underflow)
383 * Figure out how much stack was used by this thread.
386 lwpStackCheckUsed(struct lwpProc *newp)
388 int *base = (int *)newp->ustack;
389 int *lim = (int *)(newp->ustack + newp->usize);
390 int total = (lim + 1 - base) * sizeof(int);
393 if (LwpStackGrowsDown) {
394 for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
395 used = (lim - p) * sizeof(int);
397 for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
398 used = (p - base + 1) * sizeof(int);
400 lwpStatus(newp, "Thread stack %d used, %d left, %d total",
401 used, total - used, total);