]> git.pond.sub.org Git - empserver/blobdiff - src/lib/empthread/pthread.c
Fix trailing whitespace
[empserver] / src / lib / empthread / pthread.c
index cc6d41bc56b838e5b184a44e414dd0a40717f459..aa8b56949f1d11ca9984b15841fdcbf8274da93e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *  pthread.c: Interface from Empire threads to POSIX threads
- * 
+ *
  *  Known contributors to this file:
  *     Sasha Mikheev
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2005-2007
+ *     Ron Koenderink, 2007
  */
 
+/* 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>
-#if !defined(_WIN32)
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
 #include <sys/time.h>
 #include <unistd.h>
-#endif
-#include <sys/types.h>
-#include <signal.h>
-#include <errno.h>
-#include <string.h>
 
 #include "misc.h"
 #include "empthread.h"
 #include "prototypes.h"
 
-#include <stdarg.h>
-  
-#ifdef _EMPTH_POSIX
+#define EMPTH_KILLED  1
+#define EMPTH_INTR 2
+
+struct empth_t {
+    char *name;                        /* thread name */
+    void *ud;                  /* user data */
+    int state;                 /* my state */
+    void (*ep)(void *);                /* entry point */
+    pthread_t id;              /* thread id */
+};
+
+struct empth_rwlock_t {
+    char *name;
+    pthread_rwlock_t lock;
+};
+
+/* Thread-specific data key */
 static pthread_key_t ctx_key;
+
+/* Flags that were passed to empth_init() */
 static int empth_flags;
-static char **udata; /* pointer to out global context */
 
-static pthread_mutex_t mtx_ctxsw;  /* thread in critical section */
+/* Pointer to thread context variable */
+static void **udata;
 
+/*
+ * Non-preemption mutex.
+ * Empire code outside this file is only executed while holding this
+ * mutex.  This makes sure Empire code is never preempted by Empire
+ * code.
+ */
+static pthread_mutex_t mtx_ctxsw;
 
-#if 0
-static void empth_setctx _PROTO((void *));
-#endif
-static void empth_restorectx _PROTO(());
+static void empth_status(char *format, ...)
+    ATTRIBUTE((format (printf, 1, 2)));
+static void empth_alarm(int sig);
 
-void *
-empth_start(void *ctx)
+static void *
+empth_start(void *arg)
 {
-    struct sigaction act;
-    extern emp_sig_t panic();
-    extern emp_sig_t shutdwn();
-
-    
-    /* actually it should inherit all this from main but... */ 
-#ifdef SA_SIGINFO
-    act.sa_flags = SA_SIGINFO;
-#endif
-    sigemptyset (&act.sa_mask);
-    act.sa_handler = shutdwn;
-       /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
-          a Linux box running POSIX threads -- STM */
-#if !(defined(__linux__) && defined(_EMPTH_POSIX))
-    sigaction (SIGUSR1, &act, NULL);
-#endif
-    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);
+    empth_t *ctx = arg;
 
-    ((empth_t *)ctx)->id = pthread_self();
+    ctx->id = pthread_self();
     pthread_setspecific(ctx_key, ctx);
     pthread_mutex_lock(&mtx_ctxsw);
-    *udata = ((empth_t *)ctx)->ud;
-    ((empth_t *)ctx)->ep(((empth_t *)ctx)->ud);
+    *udata = ctx->ud;
+    ctx->ep(ctx->ud);
     empth_exit();
     return NULL;
-}    
-    
+}
+
 static void
 empth_status(char *format, ...)
 {
-       va_list ap;
-       static  struct timeval startTime;
-       struct  timeval tv;
-       char    buf[1024];
-       int     sec, msec;
-       empth_t *a;
-       
-       va_start(ap, format);
-       if (empth_flags & EMPTH_PRINT) {
-               if (startTime.tv_sec == 0)
-                       gettimeofday(&startTime, 0);
-               gettimeofday(&tv, 0);
-               sec = tv.tv_sec - startTime.tv_sec;
-               msec = (tv.tv_usec - startTime.tv_usec) / 1000;
-               if (msec < 0) {
-                       sec++;
-                       msec += 1000;
-               }
-               vsprintf(buf, format, ap);
-               a = empth_self();
-               printf("%d:%02d.%03d %17s: %s\n", sec/60, sec%60, msec/10,
-                      a->name,
-                      buf);
-
+    va_list ap;
+    static struct timeval startTime;
+    struct timeval tv;
+    char buf[1024];
+    int sec, msec;
+    empth_t *a;
+
+    va_start(ap, format);
+    if (empth_flags & EMPTH_PRINT) {
+       if (startTime.tv_sec == 0)
+           gettimeofday(&startTime, 0);
+       gettimeofday(&tv, 0);
+       sec = tv.tv_sec - startTime.tv_sec;
+       msec = (tv.tv_usec - startTime.tv_usec) / 1000;
+       if (msec < 0) {
+           sec++;
+           msec += 1000;
        }
-       va_end(ap);
+       vsprintf(buf, format, ap);
+       a = empth_self();
+       printf("%d:%02d.%03d %17s: %s\n", sec / 60, sec % 60, msec / 10,
+              a->name, buf);
+
+    }
+    va_end(ap);
 }
 
 
 int
-empth_init (char **ctx_ptr, int flags)
+empth_init(void **ctx_ptr, int flags)
 {
     empth_t *ctx;
+    sigset_t set;
     struct sigaction act;
-    
-
-    pthread_key_create(&ctx_key, 0);    
-#ifdef _DECTHREADS_
-    pthread_mutex_init(&mtx_ctxsw, pthread_mutexattr_default);
-#else
-    pthread_mutex_init(&mtx_ctxsw, 0);
-#endif
-    
+
+    empth_flags = flags;
+    udata = ctx_ptr;
+
+    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);
+    sigemptyset(&act.sa_mask);
     act.sa_handler = empth_alarm;
     sigaction(SIGALRM, &act, NULL);
-    
-    udata = ctx_ptr;
-    ctx = (empth_t *)malloc(sizeof(empth_t));
-    if(!ctx) {
+
+    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");
        exit(1);
     }
     ctx->name = "Main";
-    ctx->desc = "empire main";
     ctx->ep = 0;
     ctx->ud = 0;
     ctx->id = pthread_self();
-    ctx->state = 0;    
+    ctx->state = 0;
     pthread_setspecific(ctx_key, ctx);
-    pthread_mutex_lock(&mtx_ctxsw);    
-    empth_flags = flags;
+    pthread_mutex_lock(&mtx_ctxsw);
     logerror("pthreads initialized");
     return 0;
 }
 
-    
-/*
- * prio can be used for setting scheeduling policy but...
- * it seems to be optional in POSIX threads and Solaris
- * for example just ignores it.
- * More then that priority is not needed even in lwp threads.
- */
+
 empth_t *
-empth_create (int prio, void (*entry)(), int size, int flags,
-             char *name, char *desc, void *ud)
+empth_create(void (*entry)(void *), int size, int flags,
+            char *name, void *ud)
 {
     pthread_t t;
     pthread_attr_t attr;
     empth_t *ctx;
     int eno;
-    
-    empth_status("creating new thread %s:%s", name, desc);
-    
-    ctx = (empth_t *)malloc(sizeof(empth_t));
-    if(!ctx) {
-       logerror("not enough memoty to create thread: %s (%s)", name, desc);
+
+    empth_status("creating new thread %s", name);
+
+    ctx = malloc(sizeof(empth_t));
+    if (!ctx) {
+       logerror("not enough memory to create thread %s", name);
        return NULL;
     }
     ctx->name = strdup(name);
-    ctx->desc = strdup(desc);
     ctx->ud = ud;
-    ctx->state = 0;        
+    ctx->state = 0;
     ctx->ep = entry;
 
-#ifdef _DECTHREADS_
-    eno = pthread_attr_init(&attr) ? errno : 0;
-#else
     eno = pthread_attr_init(&attr);
-#endif
-    if(eno) {
-       logerror("can not create thread attribute %s (%s): %s", name, desc,
-                strerror(eno));
+    if (eno) {
+       logerror("can not create thread attribute %s: %s",
+                name, strerror(eno));
        goto bad;
     }
-
-#if defined(__linux__)
-       /* Linux doesn't let you adjust the stack */
-#elif defined(_DECTHREADS_)
-    /* DEC does not have PTHREAD_STACK_MIN constant */
-    /* Do not go below default size                 */
-    if(size > pthread_attr_getstacksize(attr))
-      pthread_attr_setstacksize(&attr, size);
-#else
-    if(size < PTHREAD_STACK_MIN)
-      size = PTHREAD_STACK_MIN + 1;
-
+    if (size < PTHREAD_STACK_MIN)
+       size = PTHREAD_STACK_MIN;
     pthread_attr_setstacksize(&attr, size);
-#endif
-    
-    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
-
-#ifdef _DECTHREADS_
-    eno = pthread_create(&t, attr, empth_start, (void *)ctx) ? errno : 0;
-#else
-    eno = pthread_create(&t, &attr, empth_start, (void *)ctx);
-#endif
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+    eno = pthread_create(&t, &attr, empth_start, ctx);
     if (eno) {
-      logerror("can not create thread: %s (%s): %s", name, desc,
-              strerror(eno));
-      goto bad;
+       logerror("can not create thread: %s: %s", name, strerror(eno));
+       goto bad;
     }
-    empth_status("new thread id is %d", t);
+    empth_status("new thread id is %ld", (long)t);
+    empth_yield();
     return ctx;
-    pthread_attr_destroy(&attr);
-bad:
+
+  bad:
     pthread_attr_destroy(&attr);
     free(ctx);
     return NULL;
 }
 
 
-#if 0
 static void
-empth_setctx(void *ct)
-{
-    empth_t *ctx_ptr;
-
-#ifdef _DECTHREADS_
-    pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
-#else
-    ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
-#endif
-    ctx_ptr->ud = ct;
-    *udata = ((empth_t *)ctx_ptr)->ud;
-    pthread_setspecific(ctx_key, (void *)ctx_ptr);
-    empth_status("context saved");
-}
-#endif
-
-static void 
 empth_restorectx(void)
 {
     empth_t *ctx_ptr;
-    
-#ifdef _DECTHREADS_
-    pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
-#else
-    ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
-#endif
-    *udata = (char *)ctx_ptr->ud;
+
+    ctx_ptr = pthread_getspecific(ctx_key);
+    *udata = ctx_ptr->ud;
     if (ctx_ptr->state == EMPTH_KILLED) {
        empth_status("i am dead");
        empth_exit();
     }
+    ctx_ptr->state = 0;
     empth_status("context restored");
 }
 
 empth_t *
 empth_self(void)
 {
-#ifdef _DECTHREADS_
-  empth_t *ctx_ptr;
-  
-  pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
-  return ctx_ptr;
-#else
-  return (empth_t *)pthread_getspecific(ctx_key);
-#endif
+    return pthread_getspecific(ctx_key);
+}
+
+char *
+empth_name(empth_t *thread)
+{
+    return thread->name;
 }
 
 void
-empth_exit (void)
+empth_set_name(empth_t *thread, char *name)
 {
-    empth_t *ctx_ptr;
+    if (thread->name)
+       free(thread->name);
+    thread->name = strdup(name);
+}
 
-    pthread_mutex_unlock(&mtx_ctxsw);
-    empth_status("empth_exit");
-#ifdef _DECTHREADS_
-    pthread_getspecific(ctx_key, (pthread_addr_t *)&ctx_ptr);
-#else
-    ctx_ptr = (empth_t *)pthread_getspecific(ctx_key);
-#endif
-       /* 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) {
-#ifdef _DECTHREADS_
-                       pthread_yield();
-#endif
-                       sleep(60);
-               }
-    }
+void
+empth_exit(void)
+{
+    empth_t *ctx = pthread_getspecific(ctx_key);
 
-    free(ctx_ptr);
+    empth_status("empth_exit");
+    pthread_mutex_unlock(&mtx_ctxsw);
+    free(ctx->name);
+    free(ctx);
     pthread_exit(0);
 }
-       
+
 void
-empth_yield (void)
+empth_yield(void)
 {
     pthread_mutex_unlock(&mtx_ctxsw);
-    sleep(10); /* take a nap  pthread_yield(); */
     pthread_mutex_lock(&mtx_ctxsw);
     empth_restorectx();
 }
@@ -336,17 +284,9 @@ empth_yield (void)
 void
 empth_terminate(empth_t *a)
 {
-    /* logerror("calling non supported function empth_terminate: %s:%d",
-            __FILE__, __LINE__); */
     empth_status("killing thread %s", a->name);
     a->state = EMPTH_KILLED;
-#ifndef _DECTHREADS_
-    /* DEC and OSX do not have pthread_kill. Not sure that cancel is correct. */
-    #if (!defined __ppc__)
-        pthread_kill(a->id, SIGALRM);
-    #endif
-#endif
-    return; 
+    pthread_kill(a->id, SIGALRM);
 }
 
 void
@@ -357,18 +297,18 @@ empth_select(int fd, int flags)
     fd_set writemask;
     struct timeval tv;
     int n;
-    
+
     pthread_mutex_unlock(&mtx_ctxsw);
     empth_status("%s select on %d",
-                flags == EMPTH_FD_READ ? "read" : "write", fd );
+                flags == EMPTH_FD_READ ? "read" : "write", fd);
     while (1) {
        tv.tv_sec = 1000000;
        tv.tv_usec = 0;
 
        FD_ZERO(&readmask);
        FD_ZERO(&writemask);
-       
-       switch(flags) {
+
+       switch (flags) {
        case EMPTH_FD_READ:
            FD_SET(fd, &readmask);
            break;
@@ -380,8 +320,8 @@ empth_select(int fd, int flags)
            empth_exit();
        }
 
-       n = select(fd + 1, &readmask, &writemask, (fd_set *)0, &tv);
-       
+       n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
+
        if (n < 0) {
            if (errno == EINTR) {
                /* go handle the signal */
@@ -391,10 +331,10 @@ empth_select(int fd, int flags)
            }
            /* strange but we dont get EINTR on select broken by signal */
            empth_status("select failed (%s)", strerror(errno));
-           goto done; 
+           goto done;
            return;
        }
-       
+
        if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
            empth_status("input ready");
            break;
@@ -404,115 +344,121 @@ empth_select(int fd, int flags)
            break;
        }
     }
-    
-done:
+
+  done:
     pthread_mutex_lock(&mtx_ctxsw);
     empth_restorectx();
-       
 }
 
-
-emp_sig_t
+static void
 empth_alarm(int sig)
 {
-    struct sigaction act;
-    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);
+    /*
+     * Nothing to do --- we handle this signal just to let
+     * empth_wakeup() and empth_terminate() interrupt system calls.
+     */
 }
 
 void
 empth_wakeup(empth_t *a)
 {
     empth_status("waking up thread %s", a->name);
-#ifndef _DECTHREADS_
-    #if (!defined __ppc__)
-        pthread_kill(a->id, SIGALRM);
-    #endif
-#endif
-    empth_status("waiting for it to run");
-    /* empth_yield(); */
+    if (a->state == 0)
+       a->state = EMPTH_INTR;
+    pthread_kill(a->id, SIGALRM);
 }
 
-void
-empth_sleep(long until)
+int
+empth_sleep(time_t until)
 {
+    empth_t *ctx = pthread_getspecific(ctx_key);
     struct timeval tv;
+    int res;
 
     empth_status("going to sleep %ld sec", until - time(0));
     pthread_mutex_unlock(&mtx_ctxsw);
-    tv.tv_sec = until - time(NULL);
-    tv.tv_usec = 0;
     do {
- select (0, NULL, NULL, NULL, &tv);
-    } while ((tv.tv_sec = until - time(NULL)) > 0);
+       tv.tv_sec = until - time(NULL);
+       tv.tv_usec = 0;
+       res = select(0, NULL, NULL, NULL, &tv);
+    } while (res < 0 && ctx->state == 0);
     empth_status("sleep done. Waiting for lock");
     pthread_mutex_lock(&mtx_ctxsw);
     empth_restorectx();
+    return res;
 }
 
+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)
+empth_rwlock_t *
+empth_rwlock_create(char *name)
 {
-    empth_sem_t *sm;
+    empth_rwlock_t *rwlock;
+
+    rwlock = malloc(sizeof(*rwlock));
+    if (!rwlock)
+       return NULL;
 
-    sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
-    if(!sm) {
-       logerror("out of memory at %s:%d", __FILE__, __LINE__);
+    if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
+       free(rwlock);
        return NULL;
     }
-    strncpy(sm->name, name, sizeof(sm->name)-1);
-    sm->count = cnt;
-#ifdef _DECTHREADS_
-    pthread_mutex_init(&sm->mtx_update, pthread_mutexattr_default);
-    pthread_mutex_init(&sm->mtx_sem, pthread_mutexattr_default);
-    pthread_cond_init(&sm->cnd_sem, pthread_condattr_default);
-#else
-    pthread_mutex_init(&sm->mtx_update, 0);
-    pthread_mutex_init(&sm->mtx_sem, 0);
-    pthread_cond_init(&sm->cnd_sem, 0);
-#endif
-    return sm;
+
+    rwlock->name = strdup(name);
+    return rwlock;
 }
 
 void
-empth_sem_signal(empth_sem_t *sm)
+empth_rwlock_destroy(empth_rwlock_t *rwlock)
 {
-    empth_status("signal on semaphore %s:%d", sm->name, sm->count);
-    pthread_mutex_lock(&sm->mtx_update);
-    if(sm->count++ < 0) {
-       pthread_mutex_unlock(&sm->mtx_update);
-       pthread_mutex_lock(&sm->mtx_sem);
-       pthread_cond_signal(&sm->cnd_sem);
-       pthread_mutex_unlock(&sm->mtx_sem);
-    }
-    else
-       pthread_mutex_unlock(&sm->mtx_update);
+    pthread_rwlock_destroy(&rwlock->lock);
+    free(rwlock->name);
+    free(rwlock);
 }
 
 void
-empth_sem_wait (empth_sem_t *sm)
+empth_rwlock_wrlock(empth_rwlock_t *rwlock)
 {
-    empth_status("wait on semaphore %s:%d", sm->name, sm->count);
-    pthread_mutex_lock(&sm->mtx_update);
-    if(--sm->count < 0) {
-       pthread_mutex_unlock(&sm->mtx_update);
-       empth_status("blocking");
-       pthread_mutex_unlock(&mtx_ctxsw);
-       pthread_mutex_lock(&sm->mtx_sem);
-       pthread_cond_wait(&sm->cnd_sem, &sm->mtx_sem);
-       empth_status("waking up");
-       pthread_mutex_unlock(&sm->mtx_sem);
-       pthread_mutex_lock(&mtx_ctxsw);
-       empth_restorectx();
-    }
-    else
-       pthread_mutex_unlock(&sm->mtx_update);
+    pthread_mutex_unlock(&mtx_ctxsw);
+    pthread_rwlock_wrlock(&rwlock->lock);
+    pthread_mutex_lock(&mtx_ctxsw);
+    empth_restorectx();
+}
+
+void
+empth_rwlock_rdlock(empth_rwlock_t *rwlock)
+{
+    pthread_mutex_unlock(&mtx_ctxsw);
+    pthread_rwlock_rdlock(&rwlock->lock);
+    pthread_mutex_lock(&mtx_ctxsw);
+    empth_restorectx();
 }
 
-#endif
+void
+empth_rwlock_unlock(empth_rwlock_t *rwlock)
+{
+    pthread_rwlock_unlock(&rwlock->lock);
+}