]> git.pond.sub.org Git - empserver/blobdiff - src/lib/empthread/pthread.c
Update copyright notice.
[empserver] / src / lib / empthread / pthread.c
index 0f7614c60d9e066c15df91331d2bcb9cabd89452..f532f763b99e80774bd8a6e38f9b9a4a043bf15c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  Known contributors to this file:
  *     Sasha Mikheev
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2005-2006
  */
 
+/* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
+#define _XOPEN_SOURCE 500
+
 #include <config.h>
 
+#include <errno.h>
+#include <limits.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
 #if !defined(_WIN32)
 #include <sys/time.h>
 #include <unistd.h>
 #endif
-#include <sys/types.h>
-#include <signal.h>
-#include <errno.h>
-#include <string.h>
-#include <limits.h>
 
 #include "misc.h"
 #include "empthread.h"
 #include "prototypes.h"
 
-#include <stdarg.h>
-
-#ifdef _EMPTH_POSIX
-
 #define EMPTH_KILLED  1
 
 struct empth_t {
@@ -89,31 +92,14 @@ static void **udata;
  */
 static pthread_mutex_t mtx_ctxsw;
 
-static void empth_status(char *format, ...) ATTRIBUTE((format (printf, 1, 2)));
+static void empth_status(char *format, ...)
+    ATTRIBUTE((format (printf, 1, 2)));
 static void empth_alarm(int sig);
 
 static void *
 empth_start(void *arg)
 {
     empth_t *ctx = arg;
-    struct sigaction act;
-
-    /* actually it should inherit all this from main but... */
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-    act.sa_handler = shutdwn;
-    sigaction(SIGTERM, &act, NULL);
-    sigaction(SIGINT, &act, NULL);
-    act.sa_handler = panic;
-    sigaction(SIGBUS, &act, NULL);
-    sigaction(SIGSEGV, &act, NULL);
-    sigaction(SIGILL, &act, NULL);
-    sigaction(SIGFPE, &act, NULL);
-    act.sa_handler = SIG_IGN;
-    sigaction(SIGPIPE, &act, NULL);
-
-    act.sa_handler = empth_alarm;
-    sigaction(SIGALRM, &act, NULL);
 
     ctx->id = pthread_self();
     pthread_setspecific(ctx_key, ctx);
@@ -159,18 +145,26 @@ int
 empth_init(void **ctx_ptr, int flags)
 {
     empth_t *ctx;
+    sigset_t set;
     struct sigaction act;
 
+    empth_flags = flags;
+    udata = ctx_ptr;
 
-    pthread_key_create(&ctx_key, NULL);
-    pthread_mutex_init(&mtx_ctxsw, NULL);
-
+    empth_init_signals();
+    sigemptyset(&set);
+    sigaddset(&set, SIGHUP);
+    sigaddset(&set, SIGINT);
+    sigaddset(&set, SIGTERM);
+    pthread_sigmask(SIG_BLOCK, &set, NULL);
     act.sa_flags = 0;
     sigemptyset(&act.sa_mask);
     act.sa_handler = empth_alarm;
     sigaction(SIGALRM, &act, NULL);
 
-    udata = ctx_ptr;
+    pthread_key_create(&ctx_key, NULL);
+    pthread_mutex_init(&mtx_ctxsw, NULL);
+
     ctx = malloc(sizeof(empth_t));
     if (!ctx) {
        logerror("pthread init failed: not enough memory");
@@ -184,7 +178,6 @@ empth_init(void **ctx_ptr, int flags)
     ctx->state = 0;
     pthread_setspecific(ctx_key, ctx);
     pthread_mutex_lock(&mtx_ctxsw);
-    empth_flags = flags;
     logerror("pthreads initialized");
     return 0;
 }
@@ -209,8 +202,8 @@ empth_create(int prio, void (*entry)(void *), int size, int flags,
 
     ctx = malloc(sizeof(empth_t));
     if (!ctx) {
-       logerror("not enough memory to create thread: %s (%s)", name,
-                desc);
+       logerror("not enough memory to create thread: %s (%s)",
+                name, desc);
        return NULL;
     }
     ctx->name = strdup(name);
@@ -221,8 +214,8 @@ empth_create(int prio, void (*entry)(void *), int size, int flags,
 
     eno = pthread_attr_init(&attr);
     if (eno) {
-       logerror("can not create thread attribute %s (%s): %s", name, desc,
-                strerror(eno));
+       logerror("can not create thread attribute %s (%s): %s",
+                name, desc, strerror(eno));
        goto bad;
     }
     if (size < PTHREAD_STACK_MIN)
@@ -232,11 +225,12 @@ empth_create(int prio, void (*entry)(void *), int size, int flags,
 
     eno = pthread_create(&t, &attr, empth_start, ctx);
     if (eno) {
-       logerror("can not create thread: %s (%s): %s", name, desc,
-                strerror(eno));
+       logerror("can not create thread: %s (%s): %s",
+                name, desc, strerror(eno));
        goto bad;
     }
     empth_status("new thread id is %ld", (long)t);
+    empth_yield();
     return ctx;
 
   bad:
@@ -269,20 +263,9 @@ empth_self(void)
 void
 empth_exit(void)
 {
-    empth_t *ctx_ptr;
-
-    pthread_mutex_unlock(&mtx_ctxsw);
     empth_status("empth_exit");
-    ctx_ptr = pthread_getspecific(ctx_key);
-    /* We want to leave the main thread around forever, until it's time
-       for it to die for real (in a shutdown) */
-    if (!strcmp(ctx_ptr->name, "Main")) {
-       while (1) {
-           sleep(60);
-       }
-    }
-
-    free(ctx_ptr);
+    pthread_mutex_unlock(&mtx_ctxsw);
+    free(pthread_getspecific(ctx_key));
     pthread_exit(0);
 }
 
@@ -361,21 +344,16 @@ empth_select(int fd, int flags)
   done:
     pthread_mutex_lock(&mtx_ctxsw);
     empth_restorectx();
-
 }
 
-
 static void
 empth_alarm(int sig)
 {
-    struct sigaction act;
+    /*
+     * Nothing to do --- we handle this signal just to let
+     * empth_wakeup() interrupt system calls.
+     */
     empth_status("got alarm signal");
-#ifdef SA_RESTART
-    act.sa_flags &= ~SA_RESTART;
-#endif
-    sigemptyset(&act.sa_mask);
-    act.sa_handler = empth_alarm;
-    sigaction(SIGALRM, &act, NULL);
 }
 
 void
@@ -383,7 +361,6 @@ empth_wakeup(empth_t *a)
 {
     empth_status("waking up thread %s", a->name);
     pthread_kill(a->id, SIGALRM);
-    empth_status("waiting for it to run");
 }
 
 void
@@ -403,6 +380,30 @@ empth_sleep(time_t until)
     empth_restorectx();
 }
 
+int
+empth_wait_for_signal(void)
+{
+    sigset_t set;
+    int sig, err;
+
+    sigemptyset(&set);
+    sigaddset(&set, SIGHUP);
+    sigaddset(&set, SIGINT);
+    sigaddset(&set, SIGTERM);
+    pthread_mutex_unlock(&mtx_ctxsw);
+    for (;;) {
+       empth_status("waiting for signals");
+       err = sigwait(&set, &sig);
+       if (CANT_HAPPEN(err)) {
+           sleep(60);
+           continue;
+       }
+       empth_status("got awaited signal %d", sig);
+       pthread_mutex_lock(&mtx_ctxsw);
+       empth_restorectx();
+       return sig;
+    }
+}
 
 empth_sem_t *
 empth_sem_create(char *name, int cnt)
@@ -454,5 +455,3 @@ empth_sem_wait(empth_sem_t *sm)
     } else
        pthread_mutex_unlock(&sm->mtx_update);
 }
-
-#endif