2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1994-2008, 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-2008
40 #include "prototypes.h"
42 static struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
44 struct lwpProc *LwpCurrent = NULL;
45 static void **LwpContextPtr;
46 static int LwpMaxpri = 0; /* maximum priority so far */
47 static int LwpStackGrowsDown;
49 static void lwpDestroy(struct lwpProc *proc);
50 static void lwpStackCheckInit(struct lwpProc *newp);
51 static void lwpStackCheck(struct lwpProc *newp);
52 static void lwpStackCheckUsed(struct lwpProc *newp);
54 /* check stack direction */
64 * lwpReschedule -- schedule another process. we also check for dead
65 * processes here and free them.
70 static struct lwpProc *nextp;
73 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
74 lwpStackCheck(LwpCurrent);
80 /* destroy dead threads */
81 lwpStatus(LwpCurrent, "Cleaning dead queue");
82 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
83 if (CANT_HAPPEN(nextp == LwpCurrent))
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 (CANT_HAPPEN(LwpCurrent == 0 && nextp == 0))
108 if (LwpCurrent != nextp) {
109 struct lwpProc *oldp = LwpCurrent;
111 lwpStatus(oldp, "switch out");
113 *LwpContextPtr = nextp->ud;
114 lwpSwitchContext(oldp, nextp);
115 lwpStatus(nextp, "switch in %d", nextp->pri);
120 * lwpEntryPoint -- process entry point.
125 lwpStatus(LwpCurrent, "starting at entry point");
126 (*LwpCurrent->entry)(LwpCurrent->ud);
131 lwpNewStack(struct lwpProc *newp, int stacksz)
136 /* Make size a multiple of sizeof(long) to keep things aligned */
137 stacksz = (stacksz + sizeof(long) - 1) & -sizeof(long);
138 /* Add a red zone on each side of the stack for LWP_STACKCHECK */
139 redsize = newp->flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
140 size = stacksz + 2 * redsize;
148 newp->ustack = s + redsize;
149 newp->usize = stacksz;
151 if (newp->flags & LWP_STACKCHECK)
152 lwpStackCheckInit(newp);
157 * lwpCreate -- create a process.
160 lwpCreate(int priority, void (*entry)(void *), int stacksz,
161 int flags, char *name, int argc, char **argv, void *ud)
163 struct lwpProc *newp;
165 if (!(newp = malloc(sizeof(struct lwpProc))))
168 newp->name = strdup(name);
174 newp->runtime = (time_t)-1;
177 if (LWP_MAX_PRIO <= priority)
178 priority = LWP_MAX_PRIO - 1;
179 if (LwpMaxpri < (newp->pri = priority))
180 LwpMaxpri = priority;
181 if (lwpNewStack(newp, stacksz) < 0 || lwpNewContext(newp) < 0) {
187 lwpStatus(newp, "creating process structure %p (sbtm %p)",
190 lwpReady(LwpCurrent);
196 lwpDestroy(struct lwpProc *proc)
198 if (proc->flags & LWP_STACKCHECK) {
199 lwpStackCheckUsed(proc);
202 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
209 * lwpReady -- put process on ready queue. if null, assume current.
212 lwpReady(struct lwpProc *p)
216 lwpStatus(p, "added to run queue");
217 lwpAddTail(&LwpSchedQ[p->pri], p);
224 lwpGetUD(struct lwpProc *p)
235 lwpSetUD(struct lwpProc *p, char *ud)
243 * lwpYield -- yield the processor to another thread.
248 lwpStatus(LwpCurrent, "yielding control");
249 lwpReady(LwpCurrent);
254 * cause the current process to be scheduled for deletion.
259 lwpStatus(LwpCurrent, "marking self as dead");
260 LwpCurrent->dead = 1;
265 * mark another process as dead, so it will never be rescheduled.
266 * remove any lingering FD action
269 lwpTerminate(struct lwpProc *p)
271 lwpStatus(p, "terminating process");
277 * set the thread's priority, returning the old.
278 * if the new priority is lower than the old, we reschedule.
281 lwpSetPriority(int new)
283 int old = LwpCurrent->pri;
285 if (LWP_MAX_PRIO <= new)
286 new = LWP_MAX_PRIO - 1;
289 LwpCurrent->pri = new;
290 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
297 * initialise the coroutine structures
300 lwpInitSystem(int pri, void **ctxptr, int flags, sigset_t *waitset)
303 int i, *stack, marker;
306 LwpContextPtr = ctxptr;
309 LwpStackGrowsDown = growsdown(&marker);
310 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
312 if (!(stack = malloc(64)))
314 if (LWP_MAX_PRIO <= pri)
315 pri = LWP_MAX_PRIO - 1;
318 LwpCurrent->next = 0;
319 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
320 LwpCurrent->pri = pri;
321 LwpCurrent->dead = 0;
322 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
323 LwpCurrent->name = "Main";
324 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
325 q->head = q->tail = 0;
326 LwpDeadQ.head = LwpDeadQ.tail = 0;
327 lwpInitSigWait(waitset);
328 /* must be lower in priority than us for this to work right */
329 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler", 0, 0, 0);
336 * Initialize the entire stack (including both redzones) with the stack
337 * check mark. Thus, we can get some indication of how much stack was
341 lwpStackCheckInit(struct lwpProc *newp)
344 int *lim = (int *)((char *)newp->sbtm + newp->size);
346 for (p = newp->sbtm; p < lim; p++)
352 * Check if the thread has overflowed/underflowed its stack.
353 * Should that happen, abort the process, as we cannot recover.
356 lwpStackCheck(struct lwpProc *newp)
358 int *btm = (int *)(newp->ustack - LWP_REDZONE);
359 int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
360 int n = LWP_REDZONE / sizeof(int);
361 int i, lo_clean, hi_clean, overflow, underflow;
363 for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
366 for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
369 if (LwpStackGrowsDown) {
370 overflow = n - lo_clean;
371 underflow = n - hi_clean;
373 overflow = n - hi_clean;
374 underflow = n - lo_clean;
377 logerror("Thread %s stack overflow %d bytes",
378 newp->name, overflow * (int)sizeof(int));
380 logerror("Thread %s stack underflow %d bytes",
381 newp->name, underflow * (int)sizeof(int));
382 if (overflow || underflow)
388 * Figure out how much stack was used by this thread.
391 lwpStackCheckUsed(struct lwpProc *newp)
393 int *base = (int *)newp->ustack;
394 int *lim = (int *)(newp->ustack + newp->usize);
395 int total = (lim + 1 - base) * sizeof(int);
398 if (LwpStackGrowsDown) {
399 for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
400 used = (lim - p) * sizeof(int);
402 for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
403 used = (p - base + 1) * sizeof(int);
405 lwpStatus(newp, "Thread stack %d used, %d left, %d total",
406 used, total - used, total);
411 * Return the name of the thread.
414 lwpName(struct lwpProc *proc)
421 * Set the name of the thread.
424 lwpSetName(struct lwpProc *proc, char *name)
426 if (proc->name != NULL)
429 proc->name = strdup(name);