]> git.pond.sub.org Git - empserver/blobdiff - src/lib/empthread/pthread.c
Fix trailing whitespace
[empserver] / src / lib / empthread / pthread.c
index 045fbb2e8de781f996f52f0d0064304f92a9c493..aa8b56949f1d11ca9984b15841fdcbf8274da93e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, 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
  *  ---
  *
  *  pthread.c: Interface from Empire threads to POSIX threads
- * 
+ *
  *  Known contributors to this file:
  *     Sasha Mikheev
  *     Steve McClure, 1998
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2007
+ *     Ron Koenderink, 2007
  */
 
 /* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
 
 #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 <limits.h>
 
 #include "misc.h"
 #include "empthread.h"
 #include "prototypes.h"
 
-#include <stdarg.h>
-
 #define EMPTH_KILLED  1
+#define EMPTH_INTR 2
 
 struct empth_t {
     char *name;                        /* thread name */
-    char *desc;                        /* description */
     void *ud;                  /* user data */
     int state;                 /* my state */
     void (*ep)(void *);                /* entry point */
     pthread_t id;              /* thread id */
 };
 
-struct empth_sem_t {
-    pthread_mutex_t mtx_update;        /* use it to update count */
-    int count;
-    char name[80];
-    pthread_mutex_t mtx_sem;
-    pthread_cond_t cnd_sem;
+struct empth_rwlock_t {
+    char *name;
+    pthread_rwlock_t lock;
 };
 
 /* Thread-specific data key */
@@ -144,12 +141,18 @@ int
 empth_init(void **ctx_ptr, int flags)
 {
     empth_t *ctx;
+    sigset_t set;
     struct sigaction act;
 
     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);
     act.sa_handler = empth_alarm;
@@ -164,7 +167,6 @@ empth_init(void **ctx_ptr, int flags)
        exit(1);
     }
     ctx->name = "Main";
-    ctx->desc = "empire main";
     ctx->ep = 0;
     ctx->ud = 0;
     ctx->id = pthread_self();
@@ -176,39 +178,31 @@ empth_init(void **ctx_ptr, int flags)
 }
 
 
-/*
- * 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)(void *), 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);
+    empth_status("creating new thread %s", name);
 
     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", name);
        return NULL;
     }
     ctx->name = strdup(name);
-    ctx->desc = strdup(desc);
     ctx->ud = ud;
     ctx->state = 0;
     ctx->ep = entry;
 
     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",
+                name, strerror(eno));
        goto bad;
     }
     if (size < PTHREAD_STACK_MIN)
@@ -218,8 +212,7 @@ 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", name, strerror(eno));
        goto bad;
     }
     empth_status("new thread id is %ld", (long)t);
@@ -244,6 +237,7 @@ empth_restorectx(void)
        empth_status("i am dead");
        empth_exit();
     }
+    ctx_ptr->state = 0;
     empth_status("context restored");
 }
 
@@ -253,23 +247,29 @@ empth_self(void)
     return pthread_getspecific(ctx_key);
 }
 
+char *
+empth_name(empth_t *thread)
+{
+    return thread->name;
+}
+
+void
+empth_set_name(empth_t *thread, char *name)
+{
+    if (thread->name)
+       free(thread->name);
+    thread->name = strdup(name);
+}
+
 void
 empth_exit(void)
 {
-    empth_t *ctx_ptr;
+    empth_t *ctx = pthread_getspecific(ctx_key);
 
-    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(ctx->name);
+    free(ctx);
     pthread_exit(0);
 }
 
@@ -348,92 +348,117 @@ empth_select(int fd, int flags)
   done:
     pthread_mutex_lock(&mtx_ctxsw);
     empth_restorectx();
-
 }
 
-
 static void
 empth_alarm(int sig)
 {
     /*
      * Nothing to do --- we handle this signal just to let
-     * empth_wakeup() interrupt system calls.
+     * empth_wakeup() and empth_terminate() interrupt system calls.
      */
-    empth_status("got alarm signal");
 }
 
 void
 empth_wakeup(empth_t *a)
 {
     empth_status("waking up thread %s", a->name);
+    if (a->state == 0)
+       a->state = EMPTH_INTR;
     pthread_kill(a->id, SIGALRM);
 }
 
-void
+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;
 
-empth_sem_t *
-empth_sem_create(char *name, int cnt)
+    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_rwlock_t *
+empth_rwlock_create(char *name)
 {
-    empth_sem_t *sm;
+    empth_rwlock_t *rwlock;
+
+    rwlock = malloc(sizeof(*rwlock));
+    if (!rwlock)
+       return NULL;
 
-    sm = 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;
-    pthread_mutex_init(&sm->mtx_update, NULL);
-    pthread_mutex_init(&sm->mtx_sem, NULL);
-    pthread_cond_init(&sm->cnd_sem, NULL);
-    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();
+}
+
+void
+empth_rwlock_unlock(empth_rwlock_t *rwlock)
+{
+    pthread_rwlock_unlock(&rwlock->lock);
 }