2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1994-2008, 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;
59 * Threads sleeping until a wakeup time, in lwpSleepUntil() or
60 * lwpSleepFd(), in no particular order
62 static struct lwpQueue LwpDelayq;
64 /* The thread executing lwpSelect() */
65 static struct lwpProc *LwpSelProc;
68 lwpInitSelect(struct lwpProc *proc)
73 FD_ZERO(&LwpWritefds);
74 LwpFdwait = calloc(FD_SETSIZE, sizeof(struct lwpProc *));
81 lwpSleepFd(int fd, int mask, struct timeval *timeout)
83 lwpStatus(LwpCurrent, "sleeping on fd %d for %d", fd, mask);
85 if (CANT_HAPPEN(fd > FD_SETSIZE)) {
89 if (LwpFdwait[fd] != 0) {
91 "multiple sleeps attempted on file descriptor %d", fd);
95 if (mask & LWP_FD_READ)
96 FD_SET(fd, &LwpReadfds);
97 if (mask & LWP_FD_WRITE)
98 FD_SET(fd, &LwpWritefds);
101 if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
102 /* select process is sleeping until first waiter arrives */
103 lwpStatus(LwpCurrent, "going to resched fd %d", fd);
104 lwpReady(LwpSelProc);
106 lwpStatus(LwpCurrent, "going to wait on fd %d", fd);
109 LwpCurrent->runtime = time(NULL) + timeout->tv_sec +
110 (timeout->tv_usec > 0);
111 lwpAddTail(&LwpDelayq, LwpCurrent);
113 LwpCurrent->runtime = (time_t)-1;
117 LwpFdwait[fd] = LwpCurrent;
119 LwpCurrent->fd_ready = 0;
121 return LwpCurrent->fd_ready != 0;
125 * Wake up PROC if it is sleeping in lwpSleepFd().
126 * Must be followed by lwpWakeupSleep() before the next lwpReschedule().
129 lwpWakeupFd(struct lwpProc *proc)
131 if (CANT_HAPPEN(proc->fd < 0 || proc->fd > LwpMaxfd))
134 lwpStatus(proc, "awakening; was sleeping on fd %d", proc->fd);
135 if (proc->runtime != (time_t)-1 && proc->runtime != 0) {
136 /* is in LwpDelayq; leave the job to lwpWakeupSleep() */
137 proc->runtime = (time_t)-1;
140 FD_CLR(proc->fd, &LwpReadfds);
141 FD_CLR(proc->fd, &LwpWritefds);
143 LwpFdwait[proc->fd] = 0;
149 * Wake up threads in LwpDelayq whose time has come.
155 struct lwpQueue save;
156 struct lwpProc *proc;
158 if (LwpDelayq.head) {
160 save.tail = save.head = 0;
161 while (NULL != (proc = lwpGetFirst(&LwpDelayq))) {
162 if (now >= proc->runtime) {
163 lwpStatus(proc, "sleep done");
164 if (proc->runtime != 0)
165 proc->runtime = (time_t)-1;
171 lwpAddTail(&save, proc);
179 lwpWakeup(struct lwpProc *proc)
183 else if (proc->runtime != (time_t)-1)
189 lwpSleepUntil(time_t until)
193 lwpStatus(LwpCurrent, "sleeping for %ld sec",
194 (long)(until - time(NULL)));
195 LwpCurrent->runtime = until;
196 if (LwpMaxfd == 0 && LwpDelayq.head == 0) {
197 /* select process is sleeping until first waiter arrives */
198 lwpReady(LwpSelProc);
200 lwpAddTail(&LwpDelayq, LwpCurrent);
202 res = LwpCurrent->runtime ? 0 : -1;
203 LwpCurrent->runtime = (time_t)-1;
211 struct lwpProc *us = LwpCurrent;
218 struct lwpProc *proc;
221 lwpStatus(us, "starting select loop");
230 /* wait for someone to lwpSleepFd or lwpSleepUntil */
232 lwpStatus(us, "no fds or sleepers, waiting");
237 if (LwpDelayq.head) {
239 proc = LwpDelayq.head;
240 for (; proc != 0; proc = proc->next) {
241 delta = proc->runtime - now;
242 if (delta < tv.tv_sec)
248 lwpStatus(us, "selecting; sleep %ld secs", tv.tv_sec);
250 memcpy(&readmask, &LwpReadfds, sizeof(fd_set));
251 memcpy(&writemask, &LwpWritefds, sizeof(fd_set));
252 n = select(LwpMaxfd + 1, &readmask, &writemask, NULL, &tv);
254 if (errno != EINTR) {
255 logerror("select failed (%s)", strerror(errno));
258 /* go handle the signal */
265 /* file descriptor activity */
266 for (fd = 0; fd <= LwpMaxfd; fd++) {
267 if (LwpFdwait[fd] == 0)
269 if (FD_ISSET(fd, &readmask)) {
270 lwpStatus(LwpFdwait[fd], "input ready");
271 LwpFdwait[fd]->fd_ready = 1;
272 lwpWakeupFd(LwpFdwait[fd]);
275 if (FD_ISSET(fd, &writemask)) {
276 lwpStatus(LwpFdwait[fd], "output ready");
277 LwpFdwait[fd]->fd_ready = 1;
278 lwpWakeupFd(LwpFdwait[fd]);
284 lwpStatus(us, "fd dispatch completed");
285 lwpReady(LwpCurrent);