2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * sel.c: arrange to block on read/write file descriptors using lwp
30 * Known contributors to this file:
36 #include <sys/types.h>
47 #include "prototypes.h"
49 #if defined(_EMPTH_LWP)
57 struct lwpProc **wait;
58 struct lwpQueue delayq;
62 struct lwpSelect LwpSelect;
65 lwpInitSelect(struct lwpProc *proc)
70 LwpSelect.nfile = _NFILE;
72 LwpSelect.nfile = getdtablesize();
74 FD_ZERO(&LwpSelect.readmask);
75 FD_ZERO(&LwpSelect.writemask);
76 LwpSelect.wait = (struct lwpProc **)
77 calloc(LwpSelect.nfile, sizeof(char *));
78 LwpSelect.delayq.head = 0;
79 LwpSelect.delayq.tail = 0;
80 LwpSelect.proc = proc;
84 lwpSleepFd(int fd, int mask)
86 lwpStatus(LwpCurrent, "sleeping on fd %d", fd);
88 if (LwpSelect.wait[fd] != 0) {
90 "multiple sleeps attempted on file descriptor %d", fd);
93 if (mask & LWP_FD_READ)
94 FD_SET(fd, &LwpSelect.readmask);
95 if (mask & LWP_FD_WRITE)
96 FD_SET(fd, &LwpSelect.writemask);
100 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
101 /* select process is sleeping until first waiter arrives */
102 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
103 lwpReady(LwpSelect.proc);
105 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
106 if (fd > LwpSelect.maxfd)
107 LwpSelect.maxfd = fd;
108 LwpSelect.wait[fd] = LwpCurrent;
114 lwpWakeupFd(struct lwpProc *proc)
119 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
120 FD_CLR(proc->fd, &LwpSelect.readmask);
121 FD_CLR(proc->fd, &LwpSelect.writemask);
123 LwpSelect.wait[proc->fd] = 0;
129 lwpSleepUntil(long int until)
131 lwpStatus(LwpCurrent, "sleeping for %ld sec", until - time(0));
132 LwpCurrent->runtime = until;
133 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
134 /* select process is sleeping until first waiter arrives */
135 lwpReady(LwpSelect.proc);
137 lwpAddTail(&LwpSelect.delayq, LwpCurrent);
145 struct lwpProc *us = LwpCurrent;
152 struct lwpProc *proc;
154 struct lwpQueue save;
156 lwpStatus(us, "starting select loop");
163 if (LwpSelect.delayq.head)
165 /* wait for someone to lwpSleepFd or lwpSleepUntil */
167 lwpStatus(us, "no fds or sleepers, waiting");
172 if (LwpSelect.delayq.head) {
174 proc = LwpSelect.delayq.head;
175 for (; proc != 0; proc = proc->next) {
176 delta = proc->runtime - now;
177 if (delta < tv.tv_sec)
183 lwpStatus(us, "selecting; sleep %ld secs", (long)delta);
185 memcpy(&readmask, &LwpSelect.readmask, sizeof(fd_set));
186 memcpy(&writemask, &LwpSelect.writemask, sizeof(fd_set));
187 n = select(LwpSelect.maxfd + 1, &readmask, &writemask,
191 if (errno == EINTR) {
192 /* go handle the signal */
197 lwpStatus(us, "select failed (bad file descriptor?)");
201 if (LwpSelect.delayq.head) {
202 /* sleeping proecss activity */
204 save.tail = save.head = 0;
205 while (NULL != (proc = lwpGetFirst(&LwpSelect.delayq))) {
206 if (now >= proc->runtime) {
207 lwpStatus(proc, "sleep done");
210 lwpAddTail(&save, proc);
213 LwpSelect.delayq = save;
216 /* file descriptor activity */
217 for (fd = 0; fd <= LwpSelect.maxfd; fd++) {
218 if (LwpSelect.wait[fd] == 0)
220 if (FD_ISSET(fd, &readmask)) {
221 lwpStatus(LwpSelect.wait[fd], "input ready");
222 lwpWakeupFd(LwpSelect.wait[fd]);
225 if (FD_ISSET(fd, &writemask)) {
226 lwpStatus(LwpSelect.wait[fd], "output ready");
227 lwpWakeupFd(LwpSelect.wait[fd]);
232 lwpStatus(us, "fd dispatch completed");
233 lwpReady(LwpCurrent);