1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1995-2002
5 * Support for concurrent non-blocking I/O and thread waiting.
7 * ---------------------------------------------------------------------------*/
9 /* we're outside the realms of POSIX here... */
10 /* #include "PosixSource.h" */
19 #include "Capability.h"
20 #include "posix/Select.h"
22 # ifdef HAVE_SYS_TYPES_H
23 # include <sys/types.h>
26 # ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
37 #if !defined(THREADED_RTS)
42 * The threaded RTS uses an IO-manager thread in Haskell instead (see GHC.Conc)
45 /* There's a clever trick here to avoid problems when the time wraps
46 * around. Since our maximum delay is smaller than 31 bits of ticks
47 * (it's actually 31 bits of microseconds), we can safely check
48 * whether a timer has expired even if our timer will wrap around
49 * before the target is reached, using the following formula:
51 * (int)((uint)current_time - (uint)target_time) < 0
53 * if this is true, then our time has expired.
54 * (idea due to Andy Gill).
57 wakeUpSleepingThreads(lnat ticks)
60 rtsBool flag = rtsFalse;
62 while (sleeping_queue != END_TSO_QUEUE &&
63 (int)(ticks - sleeping_queue->block_info.target) > 0) {
65 sleeping_queue = tso->link;
66 tso->why_blocked = NotBlocked;
67 tso->link = END_TSO_QUEUE;
68 IF_DEBUG(scheduler,debugBelch("Waking up sleeping thread %d\n", tso->id));
69 // MainCapability: this code is !THREADED_RTS
70 pushOnRunQueue(&MainCapability,tso);
76 /* Argument 'wait' says whether to wait for I/O to become available,
77 * or whether to just check and return immediately. If there are
78 * other threads ready to run, we normally do the non-waiting variety,
79 * otherwise we wait (see Schedule.c).
81 * SMP note: must be called with sched_mutex locked.
83 * Windows: select only works on sockets, so this doesn't really work,
84 * though it makes things better than before. MsgWaitForMultipleObjects
85 * should really be used, though it only seems to work for read handles,
90 awaitEvent(rtsBool wait)
92 StgTSO *tso, *prev, *next;
97 rtsBool select_succeeded = rtsTrue;
98 rtsBool unblock_all = rtsFalse;
106 debugBelch("scheduler: checking for threads blocked on I/O");
108 debugBelch(" (waiting)");
113 /* loop until we've woken up some threads. This loop is needed
114 * because the select timing isn't accurate, we sometimes sleep
115 * for a while but not long enough to wake up a thread in
120 ticks = timestamp = getourtimeofday();
121 if (wakeUpSleepingThreads(ticks)) {
127 } else if (sleeping_queue != END_TSO_QUEUE) {
128 min = (sleeping_queue->block_info.target - ticks)
129 * TICK_MILLISECS * 1000;
135 * Collect all of the fd's that we're interested in
140 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
143 switch (tso->why_blocked) {
146 int fd = tso->block_info.fd;
147 maxfd = (fd > maxfd) ? fd : maxfd;
154 int fd = tso->block_info.fd;
155 maxfd = (fd > maxfd) ? fd : maxfd;
165 /* Check for any interesting events */
167 tv.tv_sec = min / 1000000;
168 tv.tv_usec = min % 1000000;
170 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
171 if (errno != EINTR) {
172 /* Handle bad file descriptors by unblocking all the
173 waiting threads. Why? Because a thread might have been
174 a bit naughty and closed a file descriptor while another
175 was blocked waiting. This is less-than-good programming
176 practice, but having the RTS as a result fall over isn't
177 acceptable, so we simply unblock all the waiting threads
178 should we see a bad file descriptor & give the threads
179 a chance to clean up their act.
181 Note: assume here that threads becoming unblocked
182 will try to read/write the file descriptor before trying
183 to issue a threadWaitRead/threadWaitWrite again (==> an
184 IOError will result for the thread that's got the bad
185 file descriptor.) Hence, there's no danger of a bad
186 file descriptor being repeatedly select()'ed on, so
189 if ( errno == EBADF ) {
190 unblock_all = rtsTrue;
194 barf("select failed");
198 /* We got a signal; could be one of ours. If so, we need
199 * to start up the signal handler straight away, otherwise
200 * we could block for a long time before the signal is
203 #if defined(RTS_USER_SIGNALS)
204 if (signals_pending()) {
205 startSignalHandlers();
206 return; /* still hold the lock */
210 /* we were interrupted, return to the scheduler immediately.
213 return; /* still hold the lock */
216 /* check for threads that need waking up
218 wakeUpSleepingThreads(getourtimeofday());
220 /* If new runnable threads have arrived, stop waiting for
223 if (!emptyRunQueue(&MainCapability)) {
224 return; /* still hold the lock */
228 /* Step through the waiting queue, unblocking every thread that now has
229 * a file descriptor in a ready state.
233 if (select_succeeded || unblock_all) {
234 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
236 switch (tso->why_blocked) {
238 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
241 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
248 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %d\n", tso->id));
249 tso->why_blocked = NotBlocked;
250 tso->link = END_TSO_QUEUE;
251 pushOnRunQueue(&MainCapability,tso);
254 blocked_queue_hd = tso;
262 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
264 prev->link = END_TSO_QUEUE;
265 blocked_queue_tl = prev;
269 } while (wait && !interrupted && emptyRunQueue(&MainCapability));
272 #endif /* THREADED_RTS */