2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1994-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
5 * Copyright (C) 1991-3 Stephen Crane
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * See files README, COPYING and CREDITS in the root of the source
24 * tree for related information and legal notices. It is expected
25 * that future projects/authors will amend these files as needed.
29 * sel.c: arrange to block on read/write file descriptors using lwp
31 * Known contributors to this file:
33 * Markus Armbruster, 2007
46 #include "prototypes.h"
48 /* Largest fd in LwpReadfds, LwpWritefds */
51 /* Number of file descriptors in LwpReadfds, LwpWritefds */
54 /* File descriptors waited for in lwpSleepFd() */
55 static fd_set LwpReadfds, LwpWritefds;
57 /* Map file descriptor to thread sleeping in lwpSleepFd() */
58 static struct lwpProc **LwpFdwait;
60 /* Threads sleeping in lwpSleepUntil(), in no particular order */
61 static struct lwpQueue LwpDelayq;
63 /* The thread executing lwpSelect() */
64 static struct lwpProc *LwpSelProc;
67 lwpInitSelect(struct lwpProc *proc)
72 FD_ZERO(&LwpWritefds);
73 LwpFdwait = calloc(FD_SETSIZE, sizeof(struct lwpProc *));
80 lwpSleepFd(int fd, int mask)
82 lwpStatus(LwpCurrent, "sleeping on fd %d for %d", fd, mask);
84 if (CANT_HAPPEN(fd > FD_SETSIZE))
86 if (LwpFdwait[fd] != 0) {
88 "multiple sleeps attempted on file descriptor %d", fd);
91 if (mask & LWP_FD_READ)
92 FD_SET(fd, &LwpReadfds);
93 if (mask & LWP_FD_WRITE)
94 FD_SET(fd, &LwpWritefds);
98 if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
99 /* select process is sleeping until first waiter arrives */
100 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
101 lwpReady(LwpSelProc);
103 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
106 LwpFdwait[fd] = LwpCurrent;
112 lwpWakeupFd(struct lwpProc *proc)
117 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
118 FD_CLR(proc->fd, &LwpReadfds);
119 FD_CLR(proc->fd, &LwpWritefds);
121 LwpFdwait[proc->fd] = 0;
130 struct lwpQueue save;
131 struct lwpProc *proc;
133 if (LwpDelayq.head) {
135 save.tail = save.head = 0;
136 while (NULL != (proc = lwpGetFirst(&LwpDelayq))) {
137 if (now >= proc->runtime) {
138 lwpStatus(proc, "sleep done");
141 lwpAddTail(&save, proc);
149 lwpWakeup(struct lwpProc *proc)
153 else if (proc->runtime != (time_t)-1) {
160 lwpSleepUntil(time_t until)
164 lwpStatus(LwpCurrent, "sleeping for %ld sec",
165 (long)(until - time(NULL)));
166 LwpCurrent->runtime = until;
167 if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
168 /* select process is sleeping until first waiter arrives */
169 lwpReady(LwpSelProc);
171 lwpAddTail(&LwpDelayq, LwpCurrent);
173 res = LwpCurrent->runtime ? 0 : -1;
174 LwpCurrent->runtime = (time_t)-1;
182 struct lwpProc *us = LwpCurrent;
189 struct lwpProc *proc;
192 lwpStatus(us, "starting select loop");
201 /* wait for someone to lwpSleepFd or lwpSleepUntil */
203 lwpStatus(us, "no fds or sleepers, waiting");
208 if (LwpDelayq.head) {
210 proc = LwpDelayq.head;
211 for (; proc != 0; proc = proc->next) {
212 delta = proc->runtime - now;
213 if (delta < tv.tv_sec)
219 lwpStatus(us, "selecting; sleep %ld secs", tv.tv_sec);
221 memcpy(&readmask, &LwpReadfds, sizeof(fd_set));
222 memcpy(&writemask, &LwpWritefds, sizeof(fd_set));
223 n = select(LwpMaxfd + 1, &readmask, &writemask, NULL, &tv);
225 if (errno != EINTR) {
226 logerror("select failed (%s)", strerror(errno));
229 /* go handle the signal */
237 /* file descriptor activity */
238 for (fd = 0; fd <= LwpMaxfd; fd++) {
239 if (LwpFdwait[fd] == 0)
241 if (FD_ISSET(fd, &readmask)) {
242 lwpStatus(LwpFdwait[fd], "input ready");
243 lwpWakeupFd(LwpFdwait[fd]);
246 if (FD_ISSET(fd, &writemask)) {
247 lwpStatus(LwpFdwait[fd], "output ready");
248 lwpWakeupFd(LwpFdwait[fd]);
253 lwpStatus(us, "fd dispatch completed");
254 lwpReady(LwpCurrent);