1 /* -----------------------------------------------------------------------------
2 * $Id: Select.c,v 1.20 2002/07/09 20:44:24 sof 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;
89 rtsBool unblock_all = rtsFalse;
90 static rtsBool prev_unblocked_all = rtsFalse;
98 belch("scheduler: checking for threads blocked on I/O");
105 /* loop until we've woken up some threads. This loop is needed
106 * because the select timing isn't accurate, we sometimes sleep
107 * for a while but not long enough to wake up a thread in
112 ticks = timestamp = getourtimeofday();
113 if (wakeUpSleepingThreads(ticks)) {
119 } else if (sleeping_queue != END_TSO_QUEUE) {
120 min = (sleeping_queue->block_info.target - ticks)
121 * TICK_MILLISECS * 1000;
126 #ifndef mingw32_TARGET_OS
128 * Collect all of the fd's that we're interested in
133 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
136 switch (tso->why_blocked) {
139 int fd = tso->block_info.fd;
140 maxfd = (fd > maxfd) ? fd : maxfd;
147 int fd = tso->block_info.fd;
148 maxfd = (fd > maxfd) ? fd : maxfd;
158 /* Release the scheduler lock while we do the poll.
159 * this means that someone might muck with the blocked_queue
160 * while we do this, but it shouldn't matter:
162 * - another task might poll for I/O and remove one
163 * or more threads from the blocked_queue.
164 * - more I/O threads may be added to blocked_queue.
165 * - more delayed threads may be added to blocked_queue. We'll
166 * just subtract delta from their delays after the poll.
168 * I believe none of these cases lead to trouble --SDM.
170 RELEASE_LOCK(&sched_mutex);
172 /* Check for any interesting events */
174 tv.tv_sec = min / 1000000;
175 tv.tv_usec = min % 1000000;
177 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
178 if (errno != EINTR) {
179 /* Handle bad file descriptors by unblocking all the
180 waiting threads. Why? Because a thread might have been
181 a bit naughty and closed a file descriptor while another
182 was blocked waiting. This is less-than-good programming
183 practice, but having the RTS as a result fall over isn't
184 acceptable, so we simply unblock all the waiting threads
185 should we see a bad file descriptor & give the threads
186 a chance to clean up their act.
188 To avoid getting stuck in a loop, repeated EBADF failures
189 are 'handled' through barfing.
191 if ( errno == EBADF && !prev_unblocked_all) {
192 unblock_all = rtsTrue;
193 prev_unblocked_all = rtsTrue;
196 fprintf(stderr,"%d\n", errno);
199 barf("select failed");
202 #else /* on mingwin */
204 Sleep(0); /* don't busy wait */
205 #endif /* mingw32_TARGET_OS */
206 ACQUIRE_LOCK(&sched_mutex);
208 prev_unblocked_all = rtsFalse;
210 #ifndef mingw32_TARGET_OS
211 /* We got a signal; could be one of ours. If so, we need
212 * to start up the signal handler straight away, otherwise
213 * we could block for a long time before the signal is
216 if (signals_pending()) {
217 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
218 startSignalHandlers();
219 ACQUIRE_LOCK(&sched_mutex);
220 return; /* still hold the lock */
224 /* we were interrupted, return to the scheduler immediately.
227 return; /* still hold the lock */
230 /* check for threads that need waking up
232 wakeUpSleepingThreads(getourtimeofday());
234 /* If new runnable threads have arrived, stop waiting for
237 if (run_queue_hd != END_TSO_QUEUE) {
238 return; /* still hold the lock */
241 RELEASE_LOCK(&sched_mutex);
244 ACQUIRE_LOCK(&sched_mutex);
246 /* Step through the waiting queue, unblocking every thread that now has
247 * a file descriptor in a ready state.
251 if (select_succeeded || unblock_all) {
252 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
254 switch (tso->why_blocked) {
256 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
259 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
266 IF_DEBUG(scheduler,belch("Waking up blocked thread %d\n", tso->id));
267 tso->why_blocked = NotBlocked;
268 tso->link = END_TSO_QUEUE;
269 PUSH_ON_RUN_QUEUE(tso);
272 blocked_queue_hd = tso;
280 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
282 prev->link = END_TSO_QUEUE;
283 blocked_queue_tl = prev;
287 } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);