/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2006, 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.
*
* ---
*
* Steve McClure, 1998
*/
+#include <config.h>
+
#include <stdio.h>
#if !defined(_WIN32)
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
+#include <limits.h>
#include "misc.h"
#include "empthread.h"
#include <stdarg.h>
-#ifdef _EMPTH_POSIX
+#define EMPTH_KILLED 1
+
+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;
+};
+
+/* 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(void *);
-#endif
-static void empth_restorectx();
+static void empth_status(char *format, ...) ATTRIBUTE((format (printf, 1, 2)));
+static void empth_alarm(int sig);
static void *
-empth_start(void *ctx)
+empth_start(void *arg)
{
+ empth_t *ctx = arg;
struct sigaction act;
/* actually it should inherit all this from main but... */
-#ifdef SA_SIGINFO
- act.sa_flags = SA_SIGINFO;
-#endif
+ act.sa_flags = 0;
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;
act.sa_handler = empth_alarm;
sigaction(SIGALRM, &act, NULL);
- ((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;
}
int
-empth_init(char **ctx_ptr, int flags)
+empth_init(void **ctx_ptr, int flags)
{
empth_t *ctx;
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
+ pthread_key_create(&ctx_key, NULL);
+ pthread_mutex_init(&mtx_ctxsw, NULL);
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM, &act, NULL);
udata = ctx_ptr;
- ctx = (empth_t *)malloc(sizeof(empth_t));
+ ctx = malloc(sizeof(empth_t));
if (!ctx) {
logerror("pthread init failed: not enough memory");
exit(1);
empth_status("creating new thread %s:%s", name, desc);
- ctx = (empth_t *)malloc(sizeof(empth_t));
+ ctx = malloc(sizeof(empth_t));
if (!ctx) {
- logerror("not enough memoty to create thread: %s (%s)", name,
+ logerror("not enough memory to create thread: %s (%s)", name,
desc);
return NULL;
}
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));
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;
-
+ 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
+ eno = pthread_create(&t, &attr, empth_start, ctx);
if (eno) {
logerror("can not create thread: %s (%s): %s", name, desc,
strerror(eno));
goto bad;
}
- empth_status("new thread id is %d", t);
+ empth_status("new thread id is %ld", (long)t);
return ctx;
- pthread_attr_destroy(&attr);
+
bad:
pthread_attr_destroy(&attr);
free(ctx);
}
-#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();
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);
}
void
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
+ 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) {
-#ifdef _DECTHREADS_
- pthread_yield();
-#endif
sleep(60);
}
}
empth_yield(void)
{
pthread_mutex_unlock(&mtx_ctxsw);
- sleep(10); /* take a nap pthread_yield(); */
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
}
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;
}
void
}
-void
+static void
empth_alarm(int sig)
{
struct sigaction act;
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(); */
}
void
-empth_sleep(long until)
+empth_sleep(time_t until)
{
struct timeval tv;
{
empth_sem_t *sm;
- sm = (empth_sem_t *)malloc(sizeof(empth_sem_t));
+ sm = malloc(sizeof(empth_sem_t));
if (!sm) {
logerror("out of memory at %s:%d", __FILE__, __LINE__);
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
+ pthread_mutex_init(&sm->mtx_update, NULL);
+ pthread_mutex_init(&sm->mtx_sem, NULL);
+ pthread_cond_init(&sm->cnd_sem, NULL);
return sm;
}
} else
pthread_mutex_unlock(&sm->mtx_update);
}
-
-#endif