2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1994-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
5 * Copyright (C) 1991-3 Stephen Crane
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * See files README, COPYING and CREDITS in the root of the source
24 * tree for related information and legal notices. It is expected
25 * that future projects/authors will amend these files as needed.
29 * lwp.c: lightweight process creation, destruction and manipulation
31 * Known contributors to this file:
32 * Markus Armbruster, 2004-2006
42 #include "prototypes.h"
44 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
46 struct lwpProc *LwpCurrent = NULL;
48 int LwpMaxpri = 0; /* maximum priority so far */
49 int LwpStackGrowsDown;
51 static void lwpDestroy(struct lwpProc *proc);
52 static void lwpStackCheckInit(struct lwpProc *newp);
53 static void lwpStackCheck(struct lwpProc *newp);
54 static void lwpStackCheckUsed(struct lwpProc *newp);
56 /* check stack direction */
66 * lwpReschedule -- schedule another process. we also check for dead
67 * processes here and free them.
72 static struct lwpProc *nextp;
75 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
76 lwpStackCheck(LwpCurrent);
81 /* destroy dead threads */
82 lwpStatus(LwpCurrent, "Cleaning dead queue");
83 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
84 if (CANT_HAPPEN(nextp == LwpCurrent))
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 (CANT_HAPPEN(LwpCurrent == 0 && nextp == 0))
109 if (LwpCurrent != nextp) {
110 struct lwpProc *oldp = LwpCurrent;
112 lwpStatus(oldp, "switch out");
114 *LwpContextPtr = nextp->ud;
115 lwpSwitchContext(oldp, nextp);
116 lwpStatus(nextp, "switch in %d", nextp->pri);
121 * lwpEntryPoint -- process entry point.
126 lwpStatus(LwpCurrent, "starting at entry point");
127 (*LwpCurrent->entry)(LwpCurrent->ud);
132 * lwpCreate -- create a process.
135 lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
137 struct lwpProc *newp;
139 if (!(newp = malloc(sizeof(struct lwpProc))))
142 newp->name = strdup(name);
143 newp->desc = strdup(desc);
149 if (LWP_MAX_PRIO <= priority)
150 priority = LWP_MAX_PRIO - 1;
151 if (LwpMaxpri < (newp->pri = priority))
152 LwpMaxpri = priority;
153 if (lwpNewContext(newp, stacksz) < 0) {
159 lwpStatus(newp, "creating process structure %p (sbtm %p)",
161 if (flags & LWP_STACKCHECK)
162 lwpStackCheckInit(newp);
164 lwpReady(LwpCurrent);
170 lwpDestroy(struct lwpProc *proc)
172 if (proc->flags & LWP_STACKCHECK) {
173 lwpStackCheckUsed(proc);
176 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
184 * lwpReady -- put process on ready queue. if null, assume current.
187 lwpReady(struct lwpProc *p)
191 lwpStatus(p, "added to run queue");
192 lwpAddTail(&LwpSchedQ[p->pri], p);
199 lwpGetUD(struct lwpProc *p)
210 lwpSetUD(struct lwpProc *p, char *ud)
221 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
227 p->name = strdup(name);
228 p->desc = strdup(desc);
232 * lwpYield -- yield the processor to another thread.
237 lwpStatus(LwpCurrent, "yielding control");
238 lwpReady(LwpCurrent);
243 * cause the current process to be scheduled for deletion.
248 lwpStatus(LwpCurrent, "marking self as dead");
249 LwpCurrent->dead = 1;
254 * mark another process as dead, so it will never be rescheduled.
255 * remove any lingering FD action
258 lwpTerminate(struct lwpProc *p)
260 lwpStatus(p, "terminating process");
267 * set the thread's priority, returning the old.
268 * if the new priority is lower than the old, we reschedule.
271 lwpSetPriority(int new)
273 int old = LwpCurrent->pri;
275 if (LWP_MAX_PRIO <= new)
276 new = LWP_MAX_PRIO - 1;
279 LwpCurrent->pri = new;
280 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
287 * initialise the coroutine structures
290 lwpInitSystem(int pri, char **ctxptr, int flags, sigset_t *waitset)
293 int i, *stack, marker;
296 LwpContextPtr = ctxptr;
299 /* *LwpContextPtr = 0; */
300 LwpStackGrowsDown = growsdown(&marker);
301 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
303 if (!(stack = malloc(64)))
305 if (LWP_MAX_PRIO <= pri)
306 pri = LWP_MAX_PRIO - 1;
309 LwpCurrent->next = 0;
310 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
311 LwpCurrent->pri = pri;
312 LwpCurrent->dead = 0;
313 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
314 LwpCurrent->name = "Main";
315 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
316 q->head = q->tail = 0;
317 LwpDeadQ.head = LwpDeadQ.tail = 0;
318 lwpInitSigWait(waitset);
319 /* must be lower in priority than us for this to work right */
320 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
321 "Select (main loop) Event Handler", 0, 0, 0);
328 * Initialize the entire stack (including both redzones) with the stack
329 * check mark. Thus, we can get some indication of how much stack was
333 lwpStackCheckInit(struct lwpProc *newp)
336 int *lim = (int *)((char *)newp->sbtm + newp->size);
338 for (p = newp->sbtm; p < lim; p++)
344 * Check if the thread has overflowed/underflowed its stack.
345 * Should that happen, abort the process, as we cannot recover.
348 lwpStackCheck(struct lwpProc *newp)
350 int *btm = (int *)(newp->ustack - LWP_REDZONE);
351 int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
352 int n = LWP_REDZONE / sizeof(int);
353 int i, lo_clean, hi_clean, overflow, underflow;
355 for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
358 for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
361 if (LwpStackGrowsDown) {
362 overflow = n - lo_clean;
363 underflow = n - hi_clean;
365 overflow = n - hi_clean;
366 underflow = n - lo_clean;
369 logerror("Thread %s stack overflow %d bytes",
370 newp->name, overflow * (int)sizeof(int));
372 logerror("Thread %s stack underflow %d bytes",
373 newp->name, underflow * (int)sizeof(int));
374 if (overflow || underflow)
380 * Figure out how much stack was used by this thread.
383 lwpStackCheckUsed(struct lwpProc *newp)
385 int *base = (int *)newp->ustack;
386 int *lim = (int *)(newp->ustack + newp->usize);
387 int total = (lim + 1 - base) * sizeof(int);
390 if (LwpStackGrowsDown) {
391 for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
392 used = (lim - p) * sizeof(int);
394 for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
395 used = (p - base + 1) * sizeof(int);
397 lwpStatus(newp, "Thread stack %d used, %d left, %d total",
398 used, total - used, total);