2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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>
48 #include "prototypes.h"
50 #if defined(_EMPTH_LWP)
58 struct lwpProc **wait;
59 struct lwpQueue delayq;
63 struct lwpSelect LwpSelect;
72 LwpSelect.nfile = _NFILE;
74 LwpSelect.nfile = getdtablesize();
76 FD_ZERO(&LwpSelect.readmask);
77 FD_ZERO(&LwpSelect.writemask);
78 LwpSelect.wait = (struct lwpProc **)
79 calloc(LwpSelect.nfile, sizeof(char *));
80 LwpSelect.delayq.head = 0;
81 LwpSelect.delayq.tail = 0;
82 LwpSelect.proc = proc;
90 extern struct lwpProc *LwpCurrent;
92 lwpStatus(LwpCurrent, "sleeping on fd %d", fd);
94 if (LwpSelect.wait[fd] != 0) {
96 "multiple sleeps attempted on file descriptor %d", fd);
99 if (mask & LWP_FD_READ)
100 FD_SET(fd, &LwpSelect.readmask);
101 if (mask & LWP_FD_WRITE)
102 FD_SET(fd, &LwpSelect.writemask);
106 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
107 /* select process is sleeping until first waiter arrives */
108 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
109 lwpReady(LwpSelect.proc);
111 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
112 if (fd > LwpSelect.maxfd)
113 LwpSelect.maxfd = fd;
114 LwpSelect.wait[fd] = LwpCurrent;
121 struct lwpProc *proc;
126 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
127 FD_CLR(proc->fd, &LwpSelect.readmask);
128 FD_CLR(proc->fd, &LwpSelect.writemask);
130 LwpSelect.wait[proc->fd] = 0;
139 extern struct lwpProc *LwpCurrent;
141 lwpStatus(LwpCurrent, "sleeping for %d sec", until - time(0));
142 LwpCurrent->runtime = until;
143 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
144 /* select process is sleeping until first waiter arrives */
145 lwpReady(LwpSelect.proc);
147 lwpAddTail(&LwpSelect.delayq, LwpCurrent);
153 lwpSelect(argc, argv)
157 extern struct lwpProc *LwpCurrent;
158 struct lwpProc *us = LwpCurrent;
165 struct lwpProc *proc;
167 struct lwpQueue save;
169 lwpStatus(us, "starting select loop");
176 if (LwpSelect.delayq.head)
178 /* wait for someone to lwpSleepFd or lwpSleepUntil */
180 lwpStatus(us, "no fds or sleepers, waiting");
185 if (LwpSelect.delayq.head) {
187 proc = LwpSelect.delayq.head;
188 for (; proc != 0; proc = proc->next) {
189 delta = proc->runtime - now;
190 if (delta < tv.tv_sec)
196 lwpStatus(us, "selecting; sleep %ld secs", (long)delta);
198 bcopy((s_char *)&LwpSelect.readmask, (s_char *)&readmask,
200 bcopy((s_char *)&LwpSelect.writemask, (s_char *)&writemask,
202 n = select(LwpSelect.maxfd + 1, &readmask, &writemask,
206 if (errno == EINTR) {
207 /* go handle the signal */
212 lwpStatus(us, "select failed (bad file descriptor?)");
216 if (LwpSelect.delayq.head) {
217 /* sleeping proecss activity */
219 save.tail = save.head = 0;
220 while (NULL != (proc = lwpGetFirst(&LwpSelect.delayq))) {
221 if (now >= proc->runtime) {
222 lwpStatus(proc, "sleep done");
225 lwpAddTail(&save, proc);
228 LwpSelect.delayq = save;
231 /* file descriptor activity */
232 for (fd = 0; fd <= LwpSelect.maxfd; fd++) {
233 if (LwpSelect.wait[fd] == 0)
235 if (FD_ISSET(fd, &readmask)) {
236 lwpStatus(LwpSelect.wait[fd], "input ready");
237 lwpWakeupFd(LwpSelect.wait[fd]);
240 if (FD_ISSET(fd, &writemask)) {
241 lwpStatus(LwpSelect.wait[fd], "output ready");
242 lwpWakeupFd(LwpSelect.wait[fd]);
247 lwpStatus(us, "fd dispatch completed");
248 lwpReady(LwpCurrent);