2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 = calloc(LwpSelect.nfile, sizeof(char *));
77 LwpSelect.delayq.head = 0;
78 LwpSelect.delayq.tail = 0;
79 LwpSelect.proc = proc;
83 lwpSleepFd(int fd, int mask)
85 lwpStatus(LwpCurrent, "sleeping on fd %d", fd);
87 if (LwpSelect.wait[fd] != 0) {
89 "multiple sleeps attempted on file descriptor %d", fd);
92 if (mask & LWP_FD_READ)
93 FD_SET(fd, &LwpSelect.readmask);
94 if (mask & LWP_FD_WRITE)
95 FD_SET(fd, &LwpSelect.writemask);
99 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
100 /* select process is sleeping until first waiter arrives */
101 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
102 lwpReady(LwpSelect.proc);
104 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
105 if (fd > LwpSelect.maxfd)
106 LwpSelect.maxfd = fd;
107 LwpSelect.wait[fd] = LwpCurrent;
113 lwpWakeupFd(struct lwpProc *proc)
118 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
119 FD_CLR(proc->fd, &LwpSelect.readmask);
120 FD_CLR(proc->fd, &LwpSelect.writemask);
122 LwpSelect.wait[proc->fd] = 0;
128 lwpSleepUntil(long int until)
130 lwpStatus(LwpCurrent, "sleeping for %ld sec", until - time(0));
131 LwpCurrent->runtime = until;
132 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
133 /* select process is sleeping until first waiter arrives */
134 lwpReady(LwpSelect.proc);
136 lwpAddTail(&LwpSelect.delayq, LwpCurrent);
144 struct lwpProc *us = LwpCurrent;
151 struct lwpProc *proc;
153 struct lwpQueue save;
155 lwpStatus(us, "starting select loop");
162 if (LwpSelect.delayq.head)
164 /* wait for someone to lwpSleepFd or lwpSleepUntil */
166 lwpStatus(us, "no fds or sleepers, waiting");
171 if (LwpSelect.delayq.head) {
173 proc = LwpSelect.delayq.head;
174 for (; proc != 0; proc = proc->next) {
175 delta = proc->runtime - now;
176 if (delta < tv.tv_sec)
182 lwpStatus(us, "selecting; sleep %ld secs", (long)delta);
184 memcpy(&readmask, &LwpSelect.readmask, sizeof(fd_set));
185 memcpy(&writemask, &LwpSelect.writemask, sizeof(fd_set));
186 n = select(LwpSelect.maxfd + 1, &readmask, &writemask,
190 if (errno == EINTR) {
191 /* go handle the signal */
196 lwpStatus(us, "select failed (bad file descriptor?)");
200 if (LwpSelect.delayq.head) {
201 /* sleeping proecss activity */
203 save.tail = save.head = 0;
204 while (NULL != (proc = lwpGetFirst(&LwpSelect.delayq))) {
205 if (now >= proc->runtime) {
206 lwpStatus(proc, "sleep done");
209 lwpAddTail(&save, proc);
212 LwpSelect.delayq = save;
215 /* file descriptor activity */
216 for (fd = 0; fd <= LwpSelect.maxfd; fd++) {
217 if (LwpSelect.wait[fd] == 0)
219 if (FD_ISSET(fd, &readmask)) {
220 lwpStatus(LwpSelect.wait[fd], "input ready");
221 lwpWakeupFd(LwpSelect.wait[fd]);
224 if (FD_ISSET(fd, &writemask)) {
225 lwpStatus(LwpSelect.wait[fd], "output ready");
226 lwpWakeupFd(LwpSelect.wait[fd]);
231 lwpStatus(us, "fd dispatch completed");
232 lwpReady(LwpCurrent);