/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- * Ken Stevens, Steve McClure
+ * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Ken Stevens, Steve McClure, Markus Armbruster
*
- * This program is free software; you can redistribute it and/or modify
+ * Empire is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ---
*
* ---
*
* 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
+ * Markus Armbruster, 2005-2012
+ * Ron Koenderink, 2007-2009
*/
/* Required for PTHREAD_STACK_MIN on some systems, e.g. Solaris: */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
-#include <sys/time.h>
+#include <sys/select.h>
#include <unistd.h>
-
#include "misc.h"
#include "empthread.h"
+#include "file.h"
#include "prototypes.h"
-#define EMPTH_KILLED 1
-#define EMPTH_INTR 2
-
struct empth_t {
char *name; /* thread name */
void *ud; /* user data */
- int state; /* my state */
+ int wakeup;
void (*ep)(void *); /* entry point */
pthread_t id; /* thread id */
};
struct empth_rwlock_t {
+ /* Can't use pthread_rwlock_t, because it needn't prefer writers */
char *name;
- pthread_rwlock_t lock;
+ int nread; /* #active readers */
+ int nwrite; /* total #writers (active and waiting) */
+ pthread_cond_t can_read;
+ pthread_cond_t can_write;
};
/* Thread-specific data key */
ctx->ep = 0;
ctx->ud = 0;
ctx->id = pthread_self();
- ctx->state = 0;
+ ctx->wakeup = 0;
pthread_setspecific(ctx_key, ctx);
pthread_mutex_lock(&mtx_ctxsw);
- logerror("pthreads initialized");
return 0;
}
int eno;
empth_status("creating new thread %s", name);
+ ef_make_stale();
ctx = malloc(sizeof(empth_t));
if (!ctx) {
}
ctx->name = strdup(name);
ctx->ud = ud;
- ctx->state = 0;
+ ctx->wakeup = 0;
ctx->ep = entry;
eno = pthread_attr_init(&attr);
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;
+ ctx_ptr->wakeup = 0;
empth_status("context restored");
}
empth_t *
empth_self(void)
{
- return pthread_getspecific(ctx_key);
+ return udata ? pthread_getspecific(ctx_key) : NULL;
+}
+
+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_t *ctx = pthread_getspecific(ctx_key);
empth_status("empth_exit");
+ ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
free(ctx->name);
free(ctx);
void
empth_yield(void)
{
+ ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
}
-void
-empth_terminate(empth_t *a)
-{
- empth_status("killing thread %s", a->name);
- a->state = EMPTH_KILLED;
- pthread_kill(a->id, SIGALRM);
-}
-
-void
-empth_select(int fd, int flags)
+int
+empth_select(int fd, int flags, struct timeval *timeout)
{
-
fd_set readmask;
fd_set writemask;
struct timeval tv;
int n;
+ empth_t *ctx;
+ int res = 0;
- pthread_mutex_unlock(&mtx_ctxsw);
- empth_status("%s select on %d",
- 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) {
- case EMPTH_FD_READ:
- FD_SET(fd, &readmask);
- break;
- case EMPTH_FD_WRITE:
- FD_SET(fd, &writemask);
- break;
- default:
- logerror("bad flag %d passed to empth_select", flags);
- empth_exit();
- }
-
- n = select(fd + 1, &readmask, &writemask, (fd_set *) 0, &tv);
+ if (CANT_HAPPEN(fd < 0 || fd >= FD_SETSIZE)) {
+ errno = EBADF;
+ return -1;
+ }
- if (n < 0) {
- if (errno == EINTR) {
- /* go handle the signal */
- empth_status("select broken by signal");
- goto done;
- return;
- }
- /* strange but we dont get EINTR on select broken by signal */
+ ef_make_stale();
+ pthread_mutex_unlock(&mtx_ctxsw);
+ empth_status("select on %d for %d", fd, flags);
+
+again:
+ FD_ZERO(&readmask);
+ FD_ZERO(&writemask);
+ if (flags & EMPTH_FD_READ)
+ FD_SET(fd, &readmask);
+ if (flags & EMPTH_FD_WRITE)
+ FD_SET(fd, &writemask);
+
+ if (timeout)
+ tv = *timeout;
+ n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
+ if (n < 0) {
+ ctx = pthread_getspecific(ctx_key);
+ if (ctx->wakeup) {
+ empth_status("select woken up");
+ res = 0;
+ } else if (errno == EINTR) {
+ empth_status("select broken by signal");
+ goto again;
+ } else {
empth_status("select failed (%s)", strerror(errno));
- goto done;
- return;
- }
-
- if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
- empth_status("input ready");
- break;
- }
- if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
- empth_status("output ready");
- break;
+ res = -1;
}
+ } else if (n == 0) {
+ empth_status("select timed out");
+ res = 0;
+ } else if (flags == EMPTH_FD_READ && FD_ISSET(fd, &readmask)) {
+ empth_status("input ready");
+ res = 1;
+ } else if (flags == EMPTH_FD_WRITE && FD_ISSET(fd, &writemask)) {
+ empth_status("output ready");
+ res = 1;
}
- done:
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
+ return res;
}
static void
{
/*
* Nothing to do --- we handle this signal just to let
- * empth_wakeup() and empth_terminate() interrupt system calls.
+ * empth_wakeup() interrupt system calls.
*/
}
empth_wakeup(empth_t *a)
{
empth_status("waking up thread %s", a->name);
- if (a->state == 0)
- a->state = EMPTH_INTR;
+ a->wakeup = 1;
pthread_kill(a->id, SIGALRM);
}
empth_sleep(time_t until)
{
empth_t *ctx = pthread_getspecific(ctx_key);
+ time_t now;
struct timeval tv;
int res;
- empth_status("going to sleep %ld sec", until - time(0));
+ ef_make_stale();
pthread_mutex_unlock(&mtx_ctxsw);
do {
- tv.tv_sec = until - time(NULL);
+ now = time(NULL);
+ tv.tv_sec = until >= now ? until - now : 0;
tv.tv_usec = 0;
+ empth_status("going to sleep %ld sec", (long)tv.tv_sec);
res = select(0, NULL, NULL, NULL, &tv);
- } while (res < 0 && ctx->state == 0);
+ } while (res < 0 && !ctx->wakeup);
empth_status("sleep done. Waiting for lock");
pthread_mutex_lock(&mtx_ctxsw);
empth_restorectx();
sigset_t set;
int sig, err;
+ ef_make_stale();
sigemptyset(&set);
sigaddset(&set, SIGHUP);
sigaddset(&set, SIGINT);
if (!rwlock)
return NULL;
- if (pthread_rwlock_init(&rwlock->lock, NULL) != 0) {
+ if (pthread_cond_init(&rwlock->can_read, NULL) != 0
+ || pthread_cond_init(&rwlock->can_write, NULL) != 0) {
free(rwlock);
return NULL;
}
rwlock->name = strdup(name);
+ rwlock->nread = rwlock->nwrite = 0;
return rwlock;
}
void
empth_rwlock_destroy(empth_rwlock_t *rwlock)
{
- pthread_rwlock_destroy(&rwlock->lock);
+ pthread_cond_destroy(&rwlock->can_read);
+ pthread_cond_destroy(&rwlock->can_write);
free(rwlock->name);
free(rwlock);
}
void
empth_rwlock_wrlock(empth_rwlock_t *rwlock)
{
- pthread_mutex_unlock(&mtx_ctxsw);
- pthread_rwlock_wrlock(&rwlock->lock);
- pthread_mutex_lock(&mtx_ctxsw);
- empth_restorectx();
+ empth_status("wrlock %s %d %d",
+ rwlock->name, rwlock->nread, rwlock->nwrite);
+ ef_make_stale();
+ rwlock->nwrite++;
+ while (rwlock->nread != 0 || rwlock->nwrite != 1) {
+ empth_status("waiting for wrlock %s", rwlock->name);
+ pthread_cond_wait(&rwlock->can_write, &mtx_ctxsw);
+ empth_status("got wrlock %s %d %d",
+ rwlock->name, rwlock->nread, rwlock->nwrite);
+ 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();
+ empth_status("rdlock %s %d %d",
+ rwlock->name, rwlock->nread, rwlock->nwrite);
+ ef_make_stale();
+ while (rwlock->nwrite) {
+ empth_status("waiting for rdlock %s", rwlock->name);
+ pthread_cond_wait(&rwlock->can_read, &mtx_ctxsw);
+ empth_status("got rdlock %s %d %d",
+ rwlock->name, rwlock->nread, rwlock->nwrite);
+ empth_restorectx();
+ }
+ rwlock->nread++;
}
void
empth_rwlock_unlock(empth_rwlock_t *rwlock)
{
- pthread_rwlock_unlock(&rwlock->lock);
+ if (CANT_HAPPEN(!rwlock->nread && !rwlock->nwrite))
+ return;
+ if (rwlock->nread) { /* holding read lock */
+ if (!--rwlock->nread)
+ pthread_cond_signal(&rwlock->can_write);
+ } else {
+ rwlock->nwrite--;
+ pthread_cond_signal(&rwlock->can_write);
+ }
+ if (rwlock->nwrite == 0)
+ pthread_cond_broadcast(&rwlock->can_read);
}