1 /* -----------------------------------------------------------------------------
2 * $Id: Select.c,v 1.19 2001/11/13 13:38:02 simonmar Exp $
4 * (c) The GHC Team 1995-1999
6 * Support for concurrent non-blocking I/O and thread waiting.
8 * ---------------------------------------------------------------------------*/
10 /* we're outside the realms of POSIX here... */
11 /* #include "PosixSource.h" */
20 # ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
24 # ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
28 # ifdef mingw32_TARGET_OS
35 /* There's a clever trick here to avoid problems when the time wraps
36 * around. Since our maximum delay is smaller than 31 bits of ticks
37 * (it's actually 31 bits of microseconds), we can safely check
38 * whether a timer has expired even if our timer will wrap around
39 * before the target is reached, using the following formula:
41 * (int)((uint)current_time - (uint)target_time) < 0
43 * if this is true, then our time has expired.
44 * (idea due to Andy Gill).
47 wakeUpSleepingThreads(nat ticks)
50 rtsBool flag = rtsFalse;
52 while (sleeping_queue != END_TSO_QUEUE &&
53 (int)(ticks - sleeping_queue->block_info.target) > 0) {
55 sleeping_queue = tso->link;
56 tso->why_blocked = NotBlocked;
57 tso->link = END_TSO_QUEUE;
58 IF_DEBUG(scheduler,belch("Waking up sleeping thread %d\n", tso->id));
59 PUSH_ON_RUN_QUEUE(tso);
65 /* Argument 'wait' says whether to wait for I/O to become available,
66 * or whether to just check and return immediately. If there are
67 * other threads ready to run, we normally do the non-waiting variety,
68 * otherwise we wait (see Schedule.c).
70 * SMP note: must be called with sched_mutex locked.
72 * Windows: select only works on sockets, so this doesn't really work,
73 * though it makes things better than before. MsgWaitForMultipleObjects
74 * should really be used, though it only seems to work for read handles,
79 awaitEvent(rtsBool wait)
81 StgTSO *tso, *prev, *next;
84 #ifndef mingw32_TARGET_OS
88 rtsBool select_succeeded = rtsTrue;
96 belch("scheduler: checking for threads blocked on I/O");
103 /* loop until we've woken up some threads. This loop is needed
104 * because the select timing isn't accurate, we sometimes sleep
105 * for a while but not long enough to wake up a thread in
110 ticks = timestamp = getourtimeofday();
111 if (wakeUpSleepingThreads(ticks)) {
117 } else if (sleeping_queue != END_TSO_QUEUE) {
118 min = (sleeping_queue->block_info.target - ticks)
119 * TICK_MILLISECS * 1000;
124 #ifndef mingw32_TARGET_OS
126 * Collect all of the fd's that we're interested in
131 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
134 switch (tso->why_blocked) {
137 int fd = tso->block_info.fd;
138 maxfd = (fd > maxfd) ? fd : maxfd;
145 int fd = tso->block_info.fd;
146 maxfd = (fd > maxfd) ? fd : maxfd;
156 /* Release the scheduler lock while we do the poll.
157 * this means that someone might muck with the blocked_queue
158 * while we do this, but it shouldn't matter:
160 * - another task might poll for I/O and remove one
161 * or more threads from the blocked_queue.
162 * - more I/O threads may be added to blocked_queue.
163 * - more delayed threads may be added to blocked_queue. We'll
164 * just subtract delta from their delays after the poll.
166 * I believe none of these cases lead to trouble --SDM.
168 RELEASE_LOCK(&sched_mutex);
170 /* Check for any interesting events */
172 tv.tv_sec = min / 1000000;
173 tv.tv_usec = min % 1000000;
175 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
176 if (errno != EINTR) {
178 printf("%d\n", errno);
181 barf("select failed");
183 #else /* on mingwin */
185 Sleep(0); /* don't busy wait */
186 #endif /* mingw32_TARGET_OS */
187 ACQUIRE_LOCK(&sched_mutex);
189 #ifndef mingw32_TARGET_OS
190 /* We got a signal; could be one of ours. If so, we need
191 * to start up the signal handler straight away, otherwise
192 * we could block for a long time before the signal is
195 if (signals_pending()) {
196 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
197 startSignalHandlers();
198 ACQUIRE_LOCK(&sched_mutex);
199 return; /* still hold the lock */
203 /* we were interrupted, return to the scheduler immediately.
206 return; /* still hold the lock */
209 /* check for threads that need waking up
211 wakeUpSleepingThreads(getourtimeofday());
213 /* If new runnable threads have arrived, stop waiting for
216 if (run_queue_hd != END_TSO_QUEUE) {
217 return; /* still hold the lock */
220 RELEASE_LOCK(&sched_mutex);
223 ACQUIRE_LOCK(&sched_mutex);
225 /* Step through the waiting queue, unblocking every thread that now has
226 * a file descriptor in a ready state.
230 if (select_succeeded) {
231 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
233 switch (tso->why_blocked) {
235 ready = FD_ISSET(tso->block_info.fd, &rfd);
238 ready = FD_ISSET(tso->block_info.fd, &wfd);
245 IF_DEBUG(scheduler,belch("Waking up blocked thread %d\n", tso->id));
246 tso->why_blocked = NotBlocked;
247 tso->link = END_TSO_QUEUE;
248 PUSH_ON_RUN_QUEUE(tso);
251 blocked_queue_hd = tso;
259 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
261 prev->link = END_TSO_QUEUE;
262 blocked_queue_tl = prev;
266 } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);