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
43 #include "prototypes.h"
45 struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
47 struct lwpProc *LwpCurrent = NULL;
49 int LwpMaxpri = 0; /* maximum priority so far */
50 int LwpStackGrowsDown;
52 static sigset_t oldmask;
54 static void lwpStackCheckInit(struct lwpProc *newp);
55 static void lwpStackCheck(struct lwpProc *newp);
56 static void lwpStackCheckUsed(struct lwpProc *newp);
58 /* check stack direction */
68 * lwpReschedule -- schedule another process. we also check for dead
69 * processes here and free them.
74 static int lcount = LCOUNT;
75 static struct lwpProc *nextp;
77 static sigset_t tmask;
79 if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
80 lwpStackCheck(LwpCurrent);
83 int p = lwpSetPriority(LWP_MAX_PRIO - 1);
85 sigprocmask(SIG_SETMASK, &oldmask, &tmask);
86 sigprocmask(SIG_SETMASK, &tmask, &oldmask);
90 /* destroy dead threads */
91 lwpStatus(LwpCurrent, "Cleaning dead queue");
92 while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
93 if (nextp == LwpCurrent) {
94 lwpStatus(nextp, "OOOPS, we are running already dead thread");
98 lwpStatus(LwpCurrent, "Destroying done");
101 for (i = LwpMaxpri + 1; i--;) {
102 while (NULL != (nextp = lwpGetFirst(&LwpSchedQ[i]))) {
105 /* clean up after dead bodies */
106 lwpStatus(nextp, "got a dead body");
107 if (nextp == LwpCurrent) {
108 lwpStatus(nextp, "we are in it -- will bury later");
109 lwpAddTail(&LwpDeadQ, nextp);
118 if (LwpCurrent == 0 && nextp == 0) {
119 fprintf(stderr, "No processes to run!\n");
122 if (LwpCurrent != nextp) {
123 struct lwpProc *oldp = LwpCurrent;
125 lwpStatus(oldp, "switch out");
127 *LwpContextPtr = nextp->ud;
128 lwpSwitchContext(oldp, nextp);
129 lwpStatus(nextp, "switch in %d", nextp->pri);
134 * lwpEntryPoint -- process entry point.
142 sigaddset(&set, SIGALRM);
143 sigprocmask(SIG_SETMASK, &set, &oldmask);
144 *LwpContextPtr = LwpCurrent->ud;
146 lwpStatus(LwpCurrent, "starting at entry point");
147 (*LwpCurrent->entry)(LwpCurrent->ud);
152 * lwpCreate -- create a process.
155 lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
157 struct lwpProc *newp;
159 if (!(newp = malloc(sizeof(struct lwpProc))))
162 newp->name = strdup(name);
163 newp->desc = strdup(desc);
169 if (LWP_MAX_PRIO <= priority)
170 priority = LWP_MAX_PRIO - 1;
171 if (LwpMaxpri < (newp->pri = priority))
172 LwpMaxpri = priority;
173 if (lwpNewContext(newp, stacksz) < 0) {
179 lwpStatus(newp, "creating process structure %p (sbtm %p)",
181 if (flags & LWP_STACKCHECK)
182 lwpStackCheckInit(newp);
184 lwpReady(LwpCurrent);
190 lwpDestroy(struct lwpProc *proc)
192 if (proc->flags & LWP_STACKCHECK) {
193 lwpStackCheckUsed(proc);
196 lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
204 * lwpReady -- put process on ready queue. if null, assume current.
207 lwpReady(struct lwpProc *p)
211 lwpStatus(p, "added to run queue");
212 lwpAddTail(&LwpSchedQ[p->pri], p);
219 lwpGetUD(struct lwpProc *p)
230 lwpSetUD(struct lwpProc *p, char *ud)
241 lwpSetDesc(struct lwpProc *p, char *name, char *desc)
247 p->name = strdup(name);
248 p->desc = strdup(desc);
252 * lwpYield -- yield the processor to another thread.
257 lwpStatus(LwpCurrent, "yielding control");
258 lwpReady(LwpCurrent);
263 * cause the current process to be scheduled for deletion.
268 lwpStatus(LwpCurrent, "marking self as dead");
269 LwpCurrent->dead = 1;
274 * mark another process as dead, so it will never be rescheduled.
275 * remove any lingering FD action
278 lwpTerminate(struct lwpProc *p)
280 lwpStatus(p, "terminating process");
287 * set the thread's priority, returning the old.
288 * if the new priority is lower than the old, we reschedule.
291 lwpSetPriority(int new)
293 int old = LwpCurrent->pri;
295 if (LWP_MAX_PRIO <= new)
296 new = LWP_MAX_PRIO - 1;
299 LwpCurrent->pri = new;
300 lwpStatus(LwpCurrent, "resetting priority (%d -> %d)", old, new);
307 * initialise the coroutine structures
310 lwpInitSystem(int pri, char **ctxptr, int flags)
313 int i, *stack, marker;
316 LwpContextPtr = ctxptr;
319 /* *LwpContextPtr = 0; */
320 LwpStackGrowsDown = growsdown(&marker);
321 if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
323 if (!(stack = malloc(64)))
325 if (LWP_MAX_PRIO <= pri)
326 pri = LWP_MAX_PRIO - 1;
329 LwpCurrent->next = 0;
330 LwpCurrent->sbtm = stack; /* dummy stack for "main" */
331 LwpCurrent->pri = pri;
332 LwpCurrent->dead = 0;
333 LwpCurrent->flags = flags & ~LWP_STACKCHECK;
334 LwpCurrent->name = "Main";
335 for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
336 q->head = q->tail = 0;
337 LwpDeadQ.head = LwpDeadQ.tail = 0;
338 /* must be lower in priority than us for this to work right */
339 sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
340 "Select (main loop) Event Handler", 0, 0, 0);
347 * Initialize the entire stack (including both redzones) with the stack
348 * check mark. Thus, we can get some indication of how much stack was
352 lwpStackCheckInit(struct lwpProc *newp)
355 int *lim = (int *)((char *)newp->sbtm + newp->size);
357 for (p = newp->sbtm; p < lim; p++)
363 * Check if the thread has overflowed/underflowed its stack.
364 * Should that happen, abort the process, as we cannot recover.
367 lwpStackCheck(struct lwpProc *newp)
369 int *btm = (int *)(newp->ustack - LWP_REDZONE);
370 int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
371 int n = LWP_REDZONE / sizeof(int);
372 int i, lo_clean, hi_clean, overflow, underflow;
374 for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
377 for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
380 if (LwpStackGrowsDown) {
381 overflow = n - lo_clean;
382 underflow = n - hi_clean;
384 overflow = n - hi_clean;
385 underflow = n - lo_clean;
388 logerror("Thread %s stack overflow %d bytes",
389 newp->name, overflow * (int)sizeof(int));
391 logerror("Thread %s stack underflow %d bytes",
392 newp->name, underflow * (int)sizeof(int));
393 if (overflow || underflow)
399 * Figure out how much stack was used by this thread.
402 lwpStackCheckUsed(struct lwpProc *newp)
404 int *base = (int *)newp->ustack;
405 int *lim = (int *)(newp->ustack + newp->usize);
406 int total = (lim + 1 - base) * sizeof(int);
409 if (LwpStackGrowsDown) {
410 for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
411 used = (lim - p) * sizeof(int);
413 for (p = lim - 1; p >= base && *p == LWP_CHECKMARK; --p) ;
414 used = (p - base + 1) * sizeof(int);
416 lwpStatus(newp, "Thread stack %d used, %d left, %d total",
417 used, total - used, total);