]> git.pond.sub.org Git - empserver/blobdiff - src/lib/lwp/lwp.c
Update known contributors comments
[empserver] / src / lib / lwp / lwp.c
index 9dbcf646e3ccc16faf2da8478baaaa08d5cfa4e8..8c97162637f1576c0aee6b4ff4106fc3b46b7623 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1994-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1994-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *  Copyright (C) 1991-3 Stephen Crane
  *
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  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-2006
+ *     Markus Armbruster, 2004-2009
  */
 
 #include <config.h>
 
-#include <stdio.h>
-#include <signal.h>
-#include <stdlib.h>
 #include <string.h>
 #include "lwp.h"
 #include "lwpint.h"
 #include "prototypes.h"
 
-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 */
-int LwpStackGrowsDown;
-
-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);
@@ -71,29 +67,21 @@ growsdown(void *x)
 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");
     }
@@ -110,15 +98,13 @@ lwpReschedule(void)
            } 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 (CANT_HAPPEN(!LwpCurrent && !nextp))
+       abort();
     if (LwpCurrent != nextp) {
        struct lwpProc *oldp = LwpCurrent;
        if (oldp)
@@ -126,7 +112,7 @@ lwpReschedule(void)
        LwpCurrent = nextp;
        *LwpContextPtr = nextp->ud;
        lwpSwitchContext(oldp, nextp);
-       lwpStatus(nextp, "switch in %d", nextp->pri);
+       lwpStatus(nextp, "switch in");
     }
 }
 
@@ -136,23 +122,43 @@ lwpReschedule(void)
 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;
 
@@ -160,33 +166,33 @@ lwpCreate(int priority, void (*entry)(void *), int stacksz, int flags, char *nam
        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;
     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;
-    if (lwpNewContext(newp, stacksz) < 0) {
+    if (lwpNewStack(newp, stacksz) < 0 || lwpNewContext(newp) < 0) {
+       free(newp->sbtm);
        free(newp->name);
-       free(newp->desc);
        free(newp);
        return NULL;
     }
     lwpStatus(newp, "creating process structure %p (sbtm %p)",
              newp, newp->sbtm);
-    if (flags & LWP_STACKCHECK)
-       lwpStackCheckInit(newp);
     lwpReady(newp);
     lwpReady(LwpCurrent);
     lwpReschedule();
     return newp;
 }
 
-void
+static void
 lwpDestroy(struct lwpProc *proc)
 {
     if (proc->flags & LWP_STACKCHECK) {
@@ -196,7 +202,6 @@ lwpDestroy(struct lwpProc *proc)
     lwpStatus(proc, "destroying sbtm: %p", proc->sbtm);
     free(proc->sbtm);
     free(proc->name);
-    free(proc->desc);
     free(proc);
 }
 
@@ -234,20 +239,6 @@ lwpSetUD(struct lwpProc *p, char *ud)
     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.
  */
@@ -279,8 +270,7 @@ lwpTerminate(struct lwpProc *p)
 {
     lwpStatus(p, "terminating process");
     p->dead = 1;
-    if (p->fd >= 0)
-       lwpWakeupFd(p);
+    lwpWakeup(p);
 }
 
 /*
@@ -307,7 +297,7 @@ lwpSetPriority(int new)
  * 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, marker;
@@ -316,7 +306,6 @@ lwpInitSystem(int pri, char **ctxptr, int flags)
     LwpContextPtr = ctxptr;
     if (pri < 1)
        pri = 1;
-    /* *LwpContextPtr = 0; */
     LwpStackGrowsDown = growsdown(&marker);
     if (!(LwpCurrent = calloc(1, sizeof(struct lwpProc))))
        return NULL;
@@ -326,18 +315,19 @@ lwpInitSystem(int pri, char **ctxptr, int flags)
        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;
 }
@@ -416,3 +406,27 @@ lwpStackCheckUsed(struct lwpProc *newp)
     lwpStatus(newp, "Thread stack %d used, %d left, %d total",
              used, total - used, total);
 }
+
+/* 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);
+}
+