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:
38 #include <sys/types.h>
52 #if defined(_EMPTH_LWP)
60 struct lwpProc **wait;
61 struct lwpQueue delayq;
65 struct lwpSelect LwpSelect;
68 lwpInitSelect(struct lwpProc *proc)
73 LwpSelect.nfile = _NFILE;
75 LwpSelect.nfile = getdtablesize();
77 FD_ZERO(&LwpSelect.readmask);
78 FD_ZERO(&LwpSelect.writemask);
79 LwpSelect.wait = calloc(LwpSelect.nfile, sizeof(char *));
80 LwpSelect.delayq.head = 0;
81 LwpSelect.delayq.tail = 0;
82 LwpSelect.proc = proc;
86 lwpSleepFd(int fd, int mask)
88 lwpStatus(LwpCurrent, "sleeping on fd %d for %d", fd, mask);
90 if (LwpSelect.wait[fd] != 0) {
92 "multiple sleeps attempted on file descriptor %d", fd);
95 if (mask & LWP_FD_READ)
96 FD_SET(fd, &LwpSelect.readmask);
97 if (mask & LWP_FD_WRITE)
98 FD_SET(fd, &LwpSelect.writemask);
102 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
103 /* select process is sleeping until first waiter arrives */
104 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
105 lwpReady(LwpSelect.proc);
107 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
108 if (fd > LwpSelect.maxfd)
109 LwpSelect.maxfd = fd;
110 LwpSelect.wait[fd] = LwpCurrent;
116 lwpWakeupFd(struct lwpProc *proc)
121 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
122 FD_CLR(proc->fd, &LwpSelect.readmask);
123 FD_CLR(proc->fd, &LwpSelect.writemask);
125 LwpSelect.wait[proc->fd] = 0;
131 lwpSleepUntil(long int until)
133 lwpStatus(LwpCurrent, "sleeping for %ld sec", until - time(0));
134 LwpCurrent->runtime = until;
135 if (LwpSelect.maxfd == 0 && LwpSelect.delayq.head == 0) {
136 /* select process is sleeping until first waiter arrives */
137 lwpReady(LwpSelect.proc);
139 lwpAddTail(&LwpSelect.delayq, LwpCurrent);
147 struct lwpProc *us = LwpCurrent;
154 struct lwpProc *proc;
156 struct lwpQueue save;
158 lwpStatus(us, "starting select loop");
165 if (LwpSelect.delayq.head)
167 /* wait for someone to lwpSleepFd or lwpSleepUntil */
169 lwpStatus(us, "no fds or sleepers, waiting");
174 if (LwpSelect.delayq.head) {
176 proc = LwpSelect.delayq.head;
177 for (; proc != 0; proc = proc->next) {
178 delta = proc->runtime - now;
179 if (delta < tv.tv_sec)
185 lwpStatus(us, "selecting; sleep %ld secs", tv.tv_sec);
187 memcpy(&readmask, &LwpSelect.readmask, sizeof(fd_set));
188 memcpy(&writemask, &LwpSelect.writemask, sizeof(fd_set));
189 n = select(LwpSelect.maxfd + 1, &readmask, &writemask,
193 if (errno == EINTR) {
194 /* go handle the signal */
199 lwpStatus(us, "select failed (bad file descriptor?)");
203 if (LwpSelect.delayq.head) {
204 /* sleeping proecss activity */
206 save.tail = save.head = 0;
207 while (NULL != (proc = lwpGetFirst(&LwpSelect.delayq))) {
208 if (now >= proc->runtime) {
209 lwpStatus(proc, "sleep done");
212 lwpAddTail(&save, proc);
215 LwpSelect.delayq = save;
218 /* file descriptor activity */
219 for (fd = 0; fd <= LwpSelect.maxfd; fd++) {
220 if (LwpSelect.wait[fd] == 0)
222 if (FD_ISSET(fd, &readmask)) {
223 lwpStatus(LwpSelect.wait[fd], "input ready");
224 lwpWakeupFd(LwpSelect.wait[fd]);
227 if (FD_ISSET(fd, &writemask)) {
228 lwpStatus(LwpSelect.wait[fd], "output ready");
229 lwpWakeupFd(LwpSelect.wait[fd]);
234 lwpStatus(us, "fd dispatch completed");
235 lwpReady(LwpCurrent);