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
44 #include "prototypes.h"
46 /* Largest fd in LwpReadfds, LwpWritefds */
49 /* Number of file descriptors in LwpReadfds, LwpWritefds */
52 /* File descriptors waited for in lwpSleepFd() */
53 static fd_set LwpReadfds, LwpWritefds;
55 /* Map file descriptor to thread sleeping in lwpSleepFd() */
56 static struct lwpProc **LwpFdwait;
58 /* Threads sleeping in lwpSleepUntil(), in no particular order */
59 static struct lwpQueue LwpDelayq;
61 /* The thread executing lwpSelect() */
62 static struct lwpProc *LwpSelProc;
65 lwpInitSelect(struct lwpProc *proc)
70 FD_ZERO(&LwpWritefds);
71 LwpFdwait = calloc(FD_SETSIZE, sizeof(struct lwpProc *));
78 lwpSleepFd(int fd, int mask)
80 lwpStatus(LwpCurrent, "sleeping on fd %d for %d", fd, mask);
82 if (CANT_HAPPEN(fd > FD_SETSIZE))
84 if (LwpFdwait[fd] != 0) {
86 "multiple sleeps attempted on file descriptor %d", fd);
89 if (mask & LWP_FD_READ)
90 FD_SET(fd, &LwpReadfds);
91 if (mask & LWP_FD_WRITE)
92 FD_SET(fd, &LwpWritefds);
96 if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
97 /* select process is sleeping until first waiter arrives */
98 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
101 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
104 LwpFdwait[fd] = LwpCurrent;
110 lwpWakeupFd(struct lwpProc *proc)
115 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
116 FD_CLR(proc->fd, &LwpReadfds);
117 FD_CLR(proc->fd, &LwpWritefds);
119 LwpFdwait[proc->fd] = 0;
128 struct lwpQueue save;
129 struct lwpProc *proc;
131 if (LwpDelayq.head) {
133 save.tail = save.head = 0;
134 while (NULL != (proc = lwpGetFirst(&LwpDelayq))) {
135 if (now >= proc->runtime) {
136 lwpStatus(proc, "sleep done");
139 lwpAddTail(&save, proc);
147 lwpWakeup(struct lwpProc *proc)
151 else if (proc->runtime != (time_t)-1) {
158 lwpSleepUntil(time_t until)
162 lwpStatus(LwpCurrent, "sleeping for %ld sec",
163 (long)(until - time(NULL)));
164 LwpCurrent->runtime = until;
165 if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
166 /* select process is sleeping until first waiter arrives */
167 lwpReady(LwpSelProc);
169 lwpAddTail(&LwpDelayq, LwpCurrent);
171 res = LwpCurrent->runtime ? 0 : -1;
172 LwpCurrent->runtime = (time_t)-1;
180 struct lwpProc *us = LwpCurrent;
187 struct lwpProc *proc;
190 lwpStatus(us, "starting select loop");
199 /* wait for someone to lwpSleepFd or lwpSleepUntil */
201 lwpStatus(us, "no fds or sleepers, waiting");
206 if (LwpDelayq.head) {
208 proc = LwpDelayq.head;
209 for (; proc != 0; proc = proc->next) {
210 delta = proc->runtime - now;
211 if (delta < tv.tv_sec)
217 lwpStatus(us, "selecting; sleep %ld secs", tv.tv_sec);
219 memcpy(&readmask, &LwpReadfds, sizeof(fd_set));
220 memcpy(&writemask, &LwpWritefds, sizeof(fd_set));
221 n = select(LwpMaxfd + 1, &readmask, &writemask, NULL, &tv);
223 if (errno != EINTR) {
224 logerror("select failed (%s)", strerror(errno));
227 /* go handle the signal */
235 /* file descriptor activity */
236 for (fd = 0; fd <= LwpMaxfd; fd++) {
237 if (LwpFdwait[fd] == 0)
239 if (FD_ISSET(fd, &readmask)) {
240 lwpStatus(LwpFdwait[fd], "input ready");
241 lwpWakeupFd(LwpFdwait[fd]);
244 if (FD_ISSET(fd, &writemask)) {
245 lwpStatus(LwpFdwait[fd], "output ready");
246 lwpWakeupFd(LwpFdwait[fd]);
251 lwpStatus(us, "fd dispatch completed");
252 lwpReady(LwpCurrent);