/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1994-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
+ * Copyright (C) 1991-3 Stephen Crane
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* ---
*
- * 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.
*
* ---
*
* sel.c: arrange to block on read/write file descriptors using lwp
- *
+ *
* Known contributors to this file:
* Dave Pare, 1994
+ * Markus Armbruster, 2007
*/
-#include <stdlib.h>
+#include <config.h>
+
#include <errno.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <sys/file.h>
#include <sys/time.h>
-
-#ifdef hpux
-#include <stdio.h>
-#endif
-
-#include "bit.h"
+#include <time.h>
+#include <unistd.h>
#include "lwp.h"
#include "lwpint.h"
#include "prototypes.h"
-#if defined(_EMPTH_LWP)
+/* Largest fd in LwpReadfds, LwpWritefds */
+static int LwpMaxfd;
+
+/* Number of file descriptors in LwpReadfds, LwpWritefds */
+static int LwpNfds;
-struct lwpSelect {
- int maxfd;
- int nfds;
- int nfile;
- fd_set readmask;
- fd_set writemask;
- struct lwpProc **wait;
- struct lwpQueue delayq;
- struct lwpProc *proc;
-};
+/* File descriptors waited for in lwpSleepFd() */
+static fd_set LwpReadfds, LwpWritefds;
-struct lwpSelect LwpSelect;
+/* Map file descriptor to thread sleeping in lwpSleepFd() */
+static struct lwpProc **LwpFdwait;
-void lwpInitSelect(proc)
- struct lwpProc *proc;
+/*
+ * Threads sleeping until a wakeup time, in lwpSleepUntil() or
+ * lwpSleepFd(), in no particular order
+ */
+static struct lwpQueue LwpDelayq;
+
+/* The thread executing lwpSelect() */
+static struct lwpProc *LwpSelProc;
+
+void
+lwpInitSelect(struct lwpProc *proc)
{
- LwpSelect.maxfd = 0;
- LwpSelect.nfds = 0;
-#ifdef hpux
- LwpSelect.nfile = _NFILE;
-#else
- LwpSelect.nfile = getdtablesize();
-#endif
- FD_ZERO(&LwpSelect.readmask);
- FD_ZERO(&LwpSelect.writemask);
- LwpSelect.wait = (struct lwpProc **)
- calloc(LwpSelect.nfile, sizeof(char *));
- LwpSelect.delayq.head = 0;
- LwpSelect.delayq.tail = 0;
- LwpSelect.proc = proc;
+ LwpMaxfd = 0;
+ LwpNfds = 0;
+ FD_ZERO(&LwpReadfds);
+ FD_ZERO(&LwpWritefds);
+ LwpFdwait = calloc(FD_SETSIZE, sizeof(struct lwpProc *));
+ LwpDelayq.head = 0;
+ LwpDelayq.tail = 0;
+ LwpSelProc = proc;
}
-void lwpSleepFd(fd, mask)
- int fd;
- int mask;
+int
+lwpSleepFd(int fd, int mask, struct timeval *timeout)
{
- extern struct lwpProc *LwpCurrent;
+ lwpStatus(LwpCurrent, "sleeping on fd %d for %d", fd, mask);
- lwpStatus(LwpCurrent, "sleeping on fd %d", fd);
+ if (CANT_HAPPEN(fd > FD_SETSIZE)) {
+ errno = EBADF;
+ return -1;
+ }
+ if (LwpFdwait[fd] != 0) {
+ lwpStatus(LwpCurrent,
+ "multiple sleeps attempted on file descriptor %d", fd);
+ errno = EBADF;
+ return -1;
+ }
+ if (mask & LWP_FD_READ)
+ FD_SET(fd, &LwpReadfds);
+ if (mask & LWP_FD_WRITE)
+ FD_SET(fd, &LwpWritefds);
+ LwpNfds++;
- if (LwpSelect.wait[fd] != 0) {
- lwpStatus(LwpCurrent,
- "multiple sleeps attempted on file descriptor %d", fd);
- return;
- }
- if (mask & LWP_FD_READ)
- FD_SET(fd, &LwpSelect.readmask);
- if (mask & LWP_FD_WRITE)
- FD_SET(fd, &LwpSelect.writemask);
+ if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
+ /* select process is sleeping until first waiter arrives */
+ lwpStatus(LwpCurrent, "going to resched fd %d", fd);
+ lwpReady(LwpSelProc);
+ }
+ lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
- LwpSelect.nfds++;
+ if (timeout) {
+ LwpCurrent->runtime = time(NULL) + timeout->tv_sec +
+ (timeout->tv_usec > 0);
+ lwpAddTail(&LwpDelayq, LwpCurrent);
+ } else
+ LwpCurrent->runtime = (time_t)-1;
- if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
- /* select process is sleeping until first waiter arrives */
- lwpStatus(LwpCurrent, "going to resched fd %d", fd);
- lwpReady(LwpSelect.proc);
- }
- lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
- if (fd > LwpSelect.maxfd)
- LwpSelect.maxfd = fd;
- LwpSelect.wait[fd] = LwpCurrent;
- LwpCurrent->fd = fd;
- lwpReschedule();
+ if (fd > LwpMaxfd)
+ LwpMaxfd = fd;
+ LwpFdwait[fd] = LwpCurrent;
+ LwpCurrent->fd = fd;
+ LwpCurrent->fd_ready = 0;
+ lwpReschedule();
+ return LwpCurrent->fd_ready != 0;
}
-void lwpWakeupFd(proc)
- struct lwpProc *proc;
+/*
+ * Wake up PROC if it is sleeping in lwpSleepFd().
+ * Must be followed by lwpWakeupSleep() before the next lwpReschedule().
+ */
+static void
+lwpWakeupFd(struct lwpProc *proc)
{
- if (proc->fd < 0)
- return;
-
- lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
- FD_CLR(proc->fd, &LwpSelect.readmask);
- FD_CLR(proc->fd, &LwpSelect.writemask);
- LwpSelect.nfds--;
- LwpSelect.wait[proc->fd] = 0;
- proc->fd = -1;
- lwpReady(proc);
+ if (CANT_HAPPEN(proc->fd < 0 || proc->fd > LwpMaxfd))
+ return;
+
+ lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
+ if (proc->runtime != (time_t)-1) {
+ /* is in LwpDelayq; leave the job to lwpWakeupSleep() */
+ proc->runtime = 0;
+ return;
+ }
+ FD_CLR(proc->fd, &LwpReadfds);
+ FD_CLR(proc->fd, &LwpWritefds);
+ LwpNfds--;
+ LwpFdwait[proc->fd] = 0;
+ proc->fd = -1;
+ lwpReady(proc);
}
-void lwpSleepUntil(until)
- long until;
+/*
+ * Wake up threads in LwpDelayq whose time has come.
+ */
+void
+lwpWakeupSleep(void)
{
- extern struct lwpProc *LwpCurrent;
+ time_t now;
+ struct lwpQueue save;
+ struct lwpProc *proc;
- lwpStatus(LwpCurrent, "sleeping for %d sec", until - time(0));
- LwpCurrent->runtime = until;
- if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
- /* select process is sleeping until first waiter arrives */
- lwpReady(LwpSelect.proc);
+ if (LwpDelayq.head) {
+ now = time(NULL);
+ save.tail = save.head = 0;
+ while (NULL != (proc = lwpGetFirst(&LwpDelayq))) {
+ if (now >= proc->runtime) {
+ lwpStatus(proc, "sleep done");
+ proc->runtime = (time_t)-1;
+ if (proc->fd >= 0)
+ lwpWakeupFd(proc);
+ else
+ lwpReady(proc);
+ } else {
+ lwpAddTail(&save, proc);
+ }
}
- lwpAddTail(&LwpSelect.delayq, LwpCurrent);
- lwpReschedule();
+ LwpDelayq = save;
+ }
+}
+
+void
+lwpWakeup(struct lwpProc *proc)
+{
+ if (proc->fd >= 0)
+ lwpWakeupFd(proc);
+ else if (proc->runtime != (time_t)-1)
+ proc->runtime = 0;
+ lwpWakeupSleep();
+}
+
+int
+lwpSleepUntil(time_t until)
+{
+ int res;
+
+ lwpStatus(LwpCurrent, "sleeping for %ld sec",
+ (long)(until - time(NULL)));
+ LwpCurrent->runtime = until;
+ if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
+ /* select process is sleeping until first waiter arrives */
+ lwpReady(LwpSelProc);
+ }
+ lwpAddTail(&LwpDelayq, LwpCurrent);
+ lwpReschedule();
+ res = LwpCurrent->runtime ? 0 : -1;
+ LwpCurrent->runtime = (time_t)-1;
+ return res;
}
/*ARGSUSED*/
void
-lwpSelect(argc, argv)
- int argc;
- char **argv;
+lwpSelect(void *arg)
{
- extern struct lwpProc *LwpCurrent;
- struct lwpProc *us = LwpCurrent;
- fd_set readmask;
- fd_set writemask;
- int n;
- int fd;
- time_t now;
- time_t delta;
- struct lwpProc *proc;
- struct timeval tv;
- struct lwpQueue save;
-
- lwpStatus(us, "starting select loop");
- FD_ZERO(&readmask);
- FD_ZERO(&writemask);
+ struct lwpProc *us = LwpCurrent;
+ fd_set readmask;
+ fd_set writemask;
+ int n;
+ int fd;
+ time_t now;
+ time_t delta;
+ struct lwpProc *proc;
+ struct timeval tv;
+
+ lwpStatus(us, "starting select loop");
+ FD_ZERO(&readmask);
+ FD_ZERO(&writemask);
+ while (1) {
while (1) {
- while (1) {
- if (LwpSelect.nfds)
- break;
- if (LwpSelect.delayq.head)
- break;
- /* wait for someone to lwpSleepFd or lwpSleepUntil */
- LwpSelect.maxfd = 0;
- lwpStatus(us, "no fds or sleepers, waiting");
- lwpReschedule();
- }
- tv.tv_sec = 1000000;
- tv.tv_usec = 0;
- if (LwpSelect.delayq.head) {
- time(&now);
- proc = LwpSelect.delayq.head;
- for ( ; proc != 0; proc = proc->next) {
- delta = proc->runtime - now;
- if (delta < tv.tv_sec)
- tv.tv_sec = delta;
- }
- if (tv.tv_sec < 0)
- tv.tv_sec = 0;
- }
- lwpStatus(us, "selecting; sleep %ld secs", (long)delta);
-
- bcopy((s_char *)&LwpSelect.readmask, (s_char *)&readmask, sizeof(fd_set));
- bcopy((s_char *)&LwpSelect.writemask, (s_char *)&writemask, sizeof(fd_set));
- n = select(LwpSelect.maxfd + 1, &readmask, &writemask,
- (fd_set *)0, &tv);
-
- if (n < 0) {
- if (errno == EINTR) {
- /* go handle the signal */
- lwpReady(us);
- lwpReschedule();
- continue;
- }
- lwpStatus(us,
- "select failed (bad file descriptor?)");
- exit(-1);
- }
-
- if (LwpSelect.delayq.head) {
- /* sleeping proecss activity */
- time(&now);
- save.tail = save.head = 0;
- while (NULL != (proc = lwpGetFirst(&LwpSelect.delayq))) {
- if (now >= proc->runtime) {
- lwpStatus(proc, "sleep done");
- lwpReady(proc);
- } else {
- lwpAddTail(&save, proc);
- }
- }
- LwpSelect.delayq = save;
+ if (LwpNfds)
+ break;
+ if (LwpDelayq.head)
+ break;
+ /* wait for someone to lwpSleepFd or lwpSleepUntil */
+ LwpMaxfd = 0;
+ lwpStatus(us, "no fds or sleepers, waiting");
+ lwpReschedule();
+ }
+ tv.tv_sec = 1000000;
+ tv.tv_usec = 0;
+ if (LwpDelayq.head) {
+ time(&now);
+ proc = LwpDelayq.head;
+ for (; proc != 0; proc = proc->next) {
+ delta = proc->runtime - now;
+ if (delta < tv.tv_sec)
+ tv.tv_sec = delta;
+ }
+ if (tv.tv_sec < 0)
+ tv.tv_sec = 0;
+ }
+ lwpStatus(us, "selecting; sleep %ld secs", tv.tv_sec);
+
+ memcpy(&readmask, &LwpReadfds, sizeof(fd_set));
+ memcpy(&writemask, &LwpWritefds, sizeof(fd_set));
+ n = select(LwpMaxfd + 1, &readmask, &writemask, NULL, &tv);
+ if (n < 0) {
+ if (errno != EINTR) {
+ logerror("select failed (%s)", strerror(errno));
+ exit(1);
+ }
+ /* go handle the signal */
+ lwpReady(us);
+ lwpReschedule();
+ continue;
+ }
+
+ if (n > 0) {
+ /* file descriptor activity */
+ for (fd = 0; fd <= LwpMaxfd; fd++) {
+ if (LwpFdwait[fd] == 0)
+ continue;
+ if (FD_ISSET(fd, &readmask)) {
+ lwpStatus(LwpFdwait[fd], "input ready");
+ LwpFdwait[fd]->fd_ready = 1;
+ lwpWakeupFd(LwpFdwait[fd]);
+ continue;
}
- if (n > 0) {
- /* file descriptor activity */
- for(fd = 0; fd <= LwpSelect.maxfd; fd++) {
- if (LwpSelect.wait[fd] == 0)
- continue;
- if (FD_ISSET(fd, &readmask)) {
- lwpStatus(LwpSelect.wait[fd],
- "input ready");
- lwpWakeupFd(LwpSelect.wait[fd]);
- continue;
- }
- if (FD_ISSET(fd, &writemask)) {
- lwpStatus(LwpSelect.wait[fd],
- "output ready");
- lwpWakeupFd(LwpSelect.wait[fd]);
- continue;
- }
- }
+ if (FD_ISSET(fd, &writemask)) {
+ lwpStatus(LwpFdwait[fd], "output ready");
+ LwpFdwait[fd]->fd_ready = 1;
+ lwpWakeupFd(LwpFdwait[fd]);
+ continue;
}
- lwpStatus(us, "fd dispatch completed");
- lwpReady(LwpCurrent);
- lwpReschedule();
+ }
}
- /*NOTREACHED*/
+ lwpWakeupSleep();
+ lwpStatus(us, "fd dispatch completed");
+ lwpReady(LwpCurrent);
+ lwpReschedule();
+ }
+ /*NOTREACHED*/
}
-#endif