/*
- * lwp.c -- lightweight process creation, destruction and manipulation.
- * Copyright (C) 1991-3 Stephen Crane.
+ * Empire - A multi-player, client/server Internet based war game.
+ * Copyright (C) 1994-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure
+ * Copyright (C) 1991-3 Stephen Crane
*
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
- * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
- * Imperial College of Science, Technology and Medicine, 180 Queen's
- * Gate, London SW7 2BZ, England.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * ---
+ *
+ * See files README, COPYING and CREDITS in the root of the source
+ * tree for related information and legal notices. It is expected
+ * that future projects/authors will amend these files as needed.
+ *
+ * ---
+ *
+ * lwp.c: lightweight process creation, destruction and manipulation
+ *
+ * Known contributors to this file:
+ * Markus Armbruster, 2004-2009
*/
-#include <stdio.h>
-#include <signal.h>
-#include <stdlib.h>
+#include <config.h>
+
#include <string.h>
#include "lwp.h"
#include "lwpint.h"
#include "prototypes.h"
-#if defined(_EMPTH_LWP)
-
-struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
+static struct lwpQueue LwpSchedQ[LWP_MAX_PRIO], LwpDeadQ;
struct lwpProc *LwpCurrent = NULL;
-char **LwpContextPtr;
-int LwpMaxpri = 0; /* maximum priority so far */
-
-static sigset_t oldmask;
+static void **LwpContextPtr;
+static int LwpMaxpri = 0; /* maximum priority so far */
+static int LwpStackGrowsDown;
+static void lwpDestroy(struct lwpProc *proc);
static void lwpStackCheckInit(struct lwpProc *newp);
static void lwpStackCheck(struct lwpProc *newp);
static void lwpStackCheckUsed(struct lwpProc *newp);
void
lwpReschedule(void)
{
- static int lcount = LCOUNT;
static struct lwpProc *nextp;
static int i;
- static sigset_t tmask;
if (LwpCurrent && (LwpCurrent->flags & LWP_STACKCHECK)) {
lwpStackCheck(LwpCurrent);
}
- if (!--lcount) {
- int p = lwpSetPriority(LWP_MAX_PRIO - 1);
- lcount = LCOUNT;
- sigprocmask(SIG_SETMASK, &oldmask, &tmask);
- sigprocmask(SIG_SETMASK, &tmask, &oldmask);
- LwpCurrent->pri = p;
- }
+
+ lwpSigWakeup();
+ lwpWakeupSleep();
/* destroy dead threads */
lwpStatus(LwpCurrent, "Cleaning dead queue");
while (NULL != (nextp = lwpGetFirst(&LwpDeadQ))) {
- if (nextp == LwpCurrent) {
- lwpStatus(nextp, "OOOPS, we are running already dead thread");
- exit(1);
- }
+ if (CANT_HAPPEN(nextp == LwpCurrent))
+ abort();
lwpDestroy(nextp);
lwpStatus(LwpCurrent, "Destroying done");
}
} else {
lwpDestroy(nextp);
}
- nextp = 0;
+ nextp = NULL;
}
if (nextp)
break;
}
- if (LwpCurrent == 0 && nextp == 0) {
- fprintf(stderr, "No processes to run!\n");
- exit(1);
- }
- if (LwpCurrent)
- lwpStatus(LwpCurrent, "switch out");
- /* do context switch */
- i = LwpCurrent && lwpSave(LwpCurrent->context);
- if (LwpCurrent != nextp && !i) {
- /* restore previous context */
- lwpStatus(nextp, "switch in %d", nextp->pri);
+ if (CANT_HAPPEN(!LwpCurrent && !nextp))
+ abort();
+ if (LwpCurrent != nextp) {
+ struct lwpProc *oldp = LwpCurrent;
+ if (oldp)
+ lwpStatus(oldp, "switch out");
LwpCurrent = nextp;
- *LwpContextPtr = LwpCurrent->ud;
- lwpRestore(LwpCurrent->context);
+ *LwpContextPtr = nextp->ud;
+ lwpSwitchContext(oldp, nextp);
+ lwpStatus(nextp, "switch in");
}
}
void
lwpEntryPoint(void)
{
- sigset_t set;
-
- sigemptyset(&set);
- sigaddset(&set, SIGALRM);
- sigprocmask(SIG_SETMASK, &set, &oldmask);
- *LwpContextPtr = LwpCurrent->ud;
-
lwpStatus(LwpCurrent, "starting at entry point");
(*LwpCurrent->entry)(LwpCurrent->ud);
lwpExit();
}
+static int
+lwpNewStack(struct lwpProc *newp, int stacksz)
+{
+ char *s;
+ int size, redsize;
+
+ /* Make size a multiple of sizeof(long) to keep things aligned */
+ stacksz = (stacksz + sizeof(long) - 1) & -sizeof(long);
+ /* Add a red zone on each side of the stack for LWP_STACKCHECK */
+ redsize = newp->flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
+ size = stacksz + 2 * redsize;
+
+ s = malloc(size);
+ if (!s)
+ return -1;
+
+ newp->sbtm = s;
+ newp->size = size;
+ newp->ustack = s + redsize;
+ newp->usize = stacksz;
+
+ if (newp->flags & LWP_STACKCHECK)
+ lwpStackCheckInit(newp);
+ return 0;
+}
+
/*
* lwpCreate -- create a process.
*/
struct lwpProc *
-lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *name, char *desc, int argc, char **argv, void *ud)
+lwpCreate(int priority, void (*entry)(void *), int stacksz,
+ int flags, char *name, int argc, char **argv, void *ud)
{
struct lwpProc *newp;
- char *s, *sp;
- int size, redsize, x;
-#ifdef UCONTEXT
- stack_t usp;
-#endif /* UCONTEXT */
-
- if (CANT_HAPPEN(STKALIGN == 0|| (STKALIGN & (STKALIGN - 1))))
- return NULL; /* STKALIGN not power of 2 */
+
if (!(newp = malloc(sizeof(struct lwpProc))))
- return 0;
- /* Make size a multiple of sizeof(long) to keep things aligned */
- stacksz = (stacksz + sizeof(long) - 1) & -sizeof(long);
- /* Add a red zone on each side of the stack for LWP_STACKCHECK */
- redsize = flags & LWP_STACKCHECK ? LWP_REDZONE : 0;
- size = stacksz + 2 * redsize + LWP_EXTRASTACK + STKALIGN - 1;
- if (!(s = malloc(size)))
- return 0;
+ return NULL;
newp->flags = flags;
newp->name = strdup(name);
- newp->desc = strdup(desc);
newp->entry = entry;
newp->argc = argc;
newp->argv = argv;
newp->ud = ud;
- if (growsdown(&x)) {
- /*
- * Stack layout for stack growing downward:
- * ptr block size
- * --------------------------------------
- * waste x
- * red zone LWP_REDZONE
- * sp -> extra LWP_EXTRASTACK
- * ustack -> stack stacksz
- * red zone LWP_REDZONE
- * waste STKALIGN - 1 - x
- * sp is aligned to a multiple of STKALIGN.
- */
- sp = s + redsize + stacksz;
- sp = (char *)0 + (((sp + STKALIGN - 1) - (char *)0) & -STKALIGN);
- newp->ustack = sp - stacksz;
- } else {
- /*
- * Stack layout for stack growing upward:
- * ptr block size
- * --------------------------------------
- * waste x
- * red zone LWP_REDZONE
- * sp -> stack stacksz
- * ustack -> extra LWP_EXTRASTACK
- * red zone LWP_REDZONE
- * waste STKALIGN - 1 - x
- * sp is aligned to a multiple of STKALIGN.
- */
- sp = s + redsize + LWP_EXTRASTACK;
- sp = (char *)0 + (((sp + STKALIGN - 1) - (char *)0) & -STKALIGN);
- newp->ustack = sp - LWP_EXTRASTACK;
- }
- newp->usize = stacksz + LWP_EXTRASTACK;
+ newp->dead = 0;
+ newp->runtime = (time_t)-1;
+ newp->fd = -1;
+ newp->sbtm = NULL;
if (LWP_MAX_PRIO <= priority)
priority = LWP_MAX_PRIO - 1;
if (LwpMaxpri < (newp->pri = priority))
LwpMaxpri = priority;
- newp->sbtm = s;
- newp->size = size;
- newp->dead = 0;
- if (flags & LWP_STACKCHECK)
- lwpStackCheckInit(newp);
+ if (lwpNewStack(newp, stacksz) < 0 || lwpNewContext(newp) < 0) {
+ free(newp->sbtm);
+ free(newp->name);
+ free(newp);
+ return NULL;
+ }
lwpStatus(newp, "creating process structure %p (sbtm %p)",
newp, newp->sbtm);
lwpReady(newp);
lwpReady(LwpCurrent);
-#ifdef UCONTEXT
- usp.ss_sp = s + redsize;
- usp.ss_size = stacksz;
- usp.ss_flags = 0;
- lwpInitContext(newp, &usp); /* architecture-dependent: from arch.c */
-#else /* UCONTEXT */
- lwpInitContext(newp, sp); /* architecture-dependent: from arch.c */
-#endif /* UCONTEXT */
lwpReschedule();
return newp;
}
-void
+static void
lwpDestroy(struct lwpProc *proc)
{
if (proc->flags & LWP_STACKCHECK) {
lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
free(proc->sbtm);
free(proc->name);
- free(proc->desc);
free(proc);
}
p->ud = ud;
}
-/*
- * set name & desc
- */
-void
-lwpSetDesc(struct lwpProc *p, char *name, char *desc)
-{
- if (!p)
- p = LwpCurrent;
- free(p->name);
- free(p->desc);
- p->name = strdup(name);
- p->desc = strdup(desc);
-}
-
/*
* lwpYield -- yield the processor to another thread.
*/
{
lwpStatus(p, "terminating process");
p->dead = 1;
- if (p->fd >= 0)
- lwpWakeupFd(p);
+ lwpWakeup(p);
}
/*
* initialise the coroutine structures
*/
struct lwpProc *
-lwpInitSystem(int pri, char **ctxptr, int flags)
+lwpInitSystem(int pri, void **ctxptr, int flags, sigset_t *waitset)
{
struct lwpQueue *q;
- int i, *stack;
+ int i, *stack, marker;
struct lwpProc *sel;
LwpContextPtr = ctxptr;
if (pri < 1)
pri = 1;
- /* *LwpContextPtr = 0; */
+ LwpStackGrowsDown = growsdown(&marker);
if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
- return 0;
+ return NULL;
if (!(stack = malloc(64)))
- return 0;
+ return NULL;
if (LWP_MAX_PRIO <= pri)
pri = LWP_MAX_PRIO - 1;
if (LwpMaxpri < pri)
LwpMaxpri = pri;
- LwpCurrent->next = 0;
+ LwpCurrent->next = NULL;
LwpCurrent->sbtm = stack; /* dummy stack for "main" */
LwpCurrent->pri = pri;
LwpCurrent->dead = 0;
LwpCurrent->flags = flags & ~LWP_STACKCHECK;
LwpCurrent->name = "Main";
for (i = LWP_MAX_PRIO, q = LwpSchedQ; i--; q++)
- q->head = q->tail = 0;
- LwpDeadQ.head = LwpDeadQ.tail = 0;
+ q->head = q->tail = NULL;
+ LwpDeadQ.head = LwpDeadQ.tail = NULL;
+ lwpInitSigWait(waitset);
/* must be lower in priority than us for this to work right */
- sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler",
- "Select (main loop) Event Handler", 0, 0, 0);
+ sel = lwpCreate(0, lwpSelect, 16384, flags, "EventHandler", 0,
+ NULL, NULL);
lwpInitSelect(sel);
return LwpCurrent;
}
int *btm = (int *)(newp->ustack - LWP_REDZONE);
int *top = (int *)(newp->ustack + newp->usize + LWP_REDZONE);
int n = LWP_REDZONE / sizeof(int);
- int i, lo_clean, hi_clean, marker, overflow, underflow;
+ int i, lo_clean, hi_clean, overflow, underflow;
for (i = 0; i < n && btm[i] == LWP_CHECKMARK; i++) ;
lo_clean = i;
for (i = 1; i <= n && top[-i] == LWP_CHECKMARK; i++) ;
hi_clean = i - 1;
- if (growsdown(&marker)) {
+ if (LwpStackGrowsDown) {
overflow = n - lo_clean;
underflow = n - hi_clean;
} else {
}
if (overflow)
logerror("Thread %s stack overflow %d bytes",
- newp->name, overflow * sizeof(int));
+ newp->name, overflow * (int)sizeof(int));
if (underflow)
logerror("Thread %s stack underflow %d bytes",
- newp->name, underflow * sizeof(int));
+ newp->name, underflow * (int)sizeof(int));
if (overflow || underflow)
abort();
}
int *base = (int *)newp->ustack;
int *lim = (int *)(newp->ustack + newp->usize);
int total = (lim + 1 - base) * sizeof(int);
- int marker, used, *p;
+ int used, *p;
- if (growsdown(&marker)) {
+ if (LwpStackGrowsDown) {
for (p = base; p < lim && *p == LWP_CHECKMARK; ++p) ;
used = (lim - p) * sizeof(int);
} else {
used, total - used, total);
}
-#endif
+/* lwpName
+ *
+ * Return the name of the thread.
+ */
+char *
+lwpName(struct lwpProc *proc)
+{
+ return proc->name;
+}
+
+/* lwpSetName
+ *
+ * Set the name of the thread.
+ */
+void
+lwpSetName(struct lwpProc *proc, char *name)
+{
+ if (proc->name != NULL)
+ free(proc->name);
+
+ proc->name = strdup(name);
+}
+