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);
79 /* destroy dead threads */
80 lwpStatus(LwpCurrent, "Cleaning dead queue");
81 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
82 if (CANT_HAPPEN(nextp == LwpCurrent))
85 lwpStatus(LwpCurrent, "Destroying done");
88 for (i = LwpMaxpri + 1; i--;) {
89 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
92 /* clean up after dead bodies */
93 lwpStatus(nextp, "got a dead body");
94 if (nextp == LwpCurrent) {
95 lwpStatus(nextp, "we are in it -- will bury later");
96 lwpAddTail(&LwpDeadQ, nextp);
105 if (CANT_HAPPEN(LwpCurrent == 0 && nextp == 0))
107 if (LwpCurrent != nextp) {
108 struct lwpProc *oldp = LwpCurrent;
110 lwpStatus(oldp, "switch out");
112 *LwpContextPtr = nextp->ud;
113 lwpSwitchContext(oldp, nextp);
114 lwpStatus(nextp, "switch in %d", nextp->pri);
119 * lwpEntryPoint -- process entry point.
124 lwpStatus(LwpCurrent, "starting at entry point");
125 (*LwpCurrent->entry)(LwpCurrent->ud);
130 * lwpCreate -- create a process.
133 lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
135 struct lwpProc *newp;
137 if (!(newp = malloc(sizeof(struct lwpProc))))
140 newp->name = strdup(name);
141 newp->desc = strdup(desc);
147 if (LWP_MAX_PRIO <= priority)
148 priority = LWP_MAX_PRIO - 1;
149 if (LwpMaxpri < (newp->pri = priority))
150 LwpMaxpri = priority;
151 if (lwpNewContext(newp, stacksz) < 0) {
157 lwpStatus(newp, "creating process structure %p (sbtm %p)",
159 if (flags & LWP_STACKCHECK)
160 lwpStackCheckInit(newp);
162 lwpReady(LwpCurrent);
168 lwpDestroy(struct lwpProc *proc)
170 if (proc->flags & LWP_STACKCHECK) {
171 lwpStackCheckUsed(proc);
174 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
182 * lwpReady -- put process on ready queue. if null, assume current.
185 lwpReady(struct lwpProc *p)
189 lwpStatus(p, "added to run queue");
190 lwpAddTail(&LwpSchedQ[p->pri], p);
197 lwpGetUD(struct lwpProc *p)
208 lwpSetUD(struct lwpProc *p, char *ud)
219 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
225 p->name = strdup(name);
226 p->desc = strdup(desc);
230 * lwpYield -- yield the processor to another thread.
235 lwpStatus(LwpCurrent, "yielding control");
236 lwpReady(LwpCurrent);
241 * cause the current process to be scheduled for deletion.
246 lwpStatus(LwpCurrent, "marking self as dead");
247 LwpCurrent->dead = 1;
252 * mark another process as dead, so it will never be rescheduled.
253 * remove any lingering FD action
256 lwpTerminate(struct lwpProc *p)
258 lwpStatus(p, "terminating process");
265 * set the thread's priority, returning the old.
266 * if the new priority is lower than the old, we reschedule.
269 lwpSetPriority(int new)
271 int old = LwpCurrent->pri;
273 if (LWP_MAX_PRIO <= new)
274 new = LWP_MAX_PRIO - 1;
277 LwpCurrent->pri = new;
278 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
285 * initialise the coroutine structures
288 lwpInitSystem(int pri, char **ctxptr, int flags)
291 int i, *stack, marker;
294 LwpContextPtr = ctxptr;
297 /* *LwpContextPtr = 0; */
298 LwpStackGrowsDown = growsdown(&marker);
299 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
301 if (!(stack = malloc(64)))
303 if (LWP_MAX_PRIO <= pri)
304 pri = LWP_MAX_PRIO - 1;
307 LwpCurrent->next = 0;
308 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
309 LwpCurrent->pri = pri;
310 LwpCurrent->dead = 0;
311 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
312 LwpCurrent->name = "Main";
313 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
314 q->head = q->tail = 0;
315 LwpDeadQ.head = LwpDeadQ.tail = 0;
316 /* must be lower in priority than us for this to work right */
317 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
318 "Select (main loop) Event Handler", 0, 0, 0);
325 * Initialize the entire stack (including both redzones) with the stack
326 * check mark. Thus, we can get some indication of how much stack was
330 lwpStackCheckInit(struct lwpProc *newp)
333 int *lim = (int *)((char *)newp->sbtm + newp->size);
335 for (p = newp->sbtm; p < lim; p++)
341 * Check if the thread has overflowed/underflowed its stack.
342 * Should that happen, abort the process, as we cannot recover.
345 lwpStackCheck(struct lwpProc *newp)
347 int *btm = (int *)(newp->ustack - LWP_REDZONE);
348 int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
349 int n = LWP_REDZONE / sizeof(int);
350 int i, lo_clean, hi_clean, overflow, underflow;
352 for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
355 for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
358 if (LwpStackGrowsDown) {
359 overflow = n - lo_clean;
360 underflow = n - hi_clean;
362 overflow = n - hi_clean;
363 underflow = n - lo_clean;
366 logerror("Thread %s stack overflow %d bytes",
367 newp->name, overflow * (int)sizeof(int));
369 logerror("Thread %s stack underflow %d bytes",
370 newp->name, underflow * (int)sizeof(int));
371 if (overflow || underflow)
377 * Figure out how much stack was used by this thread.
380 lwpStackCheckUsed(struct lwpProc *newp)
382 int *base = (int *)newp->ustack;
383 int *lim = (int *)(newp->ustack + newp->usize);
384 int total = (lim + 1 - base) * sizeof(int);
387 if (LwpStackGrowsDown) {
388 for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
389 used = (lim - p) * sizeof(int);
391 for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
392 used = (p - base + 1) * sizeof(int);
394 lwpStatus(newp, "Thread stack %d used, %d left, %d total",
395 used, total - used, total);