/*
* 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
*
* ---
*
- * 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 {
*/
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);
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");
ctx->state = 0;
pthread_setspecific(ctx_key, ctx);
pthread_mutex_lock(&mtx_ctxsw);
- empth_flags = flags;
logerror("pthreads initialized");
return 0;
}
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);
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)
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:
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);
}
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
{
empth_status("waking up thread %s", a->name);
pthread_kill(a->id, SIGALRM);
- empth_status("waiting for it to run");
}
void
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)
} else
pthread_mutex_unlock(&sm->mtx_update);
}
-
-#endif