]> git.pond.sub.org Git - empserver/blobdiff - src/lib/empthread/pthread.c
Update copyright notice
[empserver] / src / lib / empthread / pthread.c
index 9c1163ff5fc888b793f06c64f55df88c2d7c015f..de3090b45468dffe0484725fda51699ee22c6d92 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, 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,
@@ -14,8 +14,7 @@
  *  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/>.
  *
  *  ---
  *
@@ -30,7 +29,7 @@
  *  Known contributors to this file:
  *     Sasha Mikheev
  *     Steve McClure, 1998
- *     Markus Armbruster, 2005-2009
+ *     Markus Armbruster, 2005-2012
  *     Ron Koenderink, 2007-2009
  */
 
 #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"
 
 struct empth_t {
@@ -63,8 +62,12 @@ struct empth_t {
 };
 
 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 */
@@ -169,7 +172,6 @@ empth_init(void **ctx_ptr, int flags)
     ctx->wakeup = 0;
     pthread_setspecific(ctx_key, ctx);
     pthread_mutex_lock(&mtx_ctxsw);
-    logerror("pthreads initialized");
     return 0;
 }
 
@@ -184,6 +186,7 @@ empth_create(void (*entry)(void *), int size, int flags,
     int eno;
 
     empth_status("creating new thread %s", name);
+    ef_make_stale();
 
     ctx = malloc(sizeof(empth_t));
     if (!ctx) {
@@ -236,7 +239,7 @@ empth_restorectx(void)
 empth_t *
 empth_self(void)
 {
-    return pthread_getspecific(ctx_key);
+    return udata ? pthread_getspecific(ctx_key) : NULL;
 }
 
 char *
@@ -259,6 +262,7 @@ empth_exit(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);
@@ -268,6 +272,7 @@ empth_exit(void)
 void
 empth_yield(void)
 {
+    ef_make_stale();
     pthread_mutex_unlock(&mtx_ctxsw);
     pthread_mutex_lock(&mtx_ctxsw);
     empth_restorectx();
@@ -280,11 +285,19 @@ empth_select(int fd, int flags, struct timeval *timeout)
     fd_set writemask;
     struct timeval tv;
     int n;
+    empth_t *ctx;
     int res = 0;
 
+    if (CANT_HAPPEN(fd < 0 || fd >= FD_SETSIZE)) {
+       errno = EBADF;
+       return -1;
+    }
+
+    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)
@@ -292,18 +305,21 @@ empth_select(int fd, int flags, struct timeval *timeout)
     if (flags & EMPTH_FD_WRITE)
        FD_SET(fd, &writemask);
 
-    if (timeout) {
+    if (timeout)
        tv = *timeout;
-       timeout = &tv;
-    }
-    n = select(fd + 1, &readmask, &writemask, NULL, timeout);
-
+    n = select(fd + 1, &readmask, &writemask, NULL, timeout ? &tv : NULL);
     if (n < 0) {
-       if (errno == EINTR) /* go handle the signal */
+       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");
-        else
+           goto again;
+       } else {
            empth_status("select failed (%s)", strerror(errno));
-       res = -1;
+           res = -1;
+       }
     } else if (n == 0) {
        empth_status("select timed out");
        res = 0;
@@ -341,14 +357,17 @@ int
 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->wakeup);
     empth_status("sleep done. Waiting for lock");
@@ -363,6 +382,7 @@ empth_wait_for_signal(void)
     sigset_t set;
     int sig, err;
 
+    ef_make_stale();
     sigemptyset(&set);
     sigaddset(&set, SIGHUP);
     sigaddset(&set, SIGINT);
@@ -391,19 +411,22 @@ empth_rwlock_create(char *name)
     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);
 }
@@ -411,23 +434,47 @@ empth_rwlock_destroy(empth_rwlock_t *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);
 }