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"
21 # ifdef HAVE_SYS_TYPES_H
22 # include <sys/types.h>
25 # ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
39 #if !defined(RTS_SUPPORTS_THREADS)
41 * The threaded RTS uses an IO-manager thread in Haskell instead (see GHC.Conc)
44 /* There's a clever trick here to avoid problems when the time wraps
45 * around. Since our maximum delay is smaller than 31 bits of ticks
46 * (it's actually 31 bits of microseconds), we can safely check
47 * whether a timer has expired even if our timer will wrap around
48 * before the target is reached, using the following formula:
50 * (int)((uint)current_time - (uint)target_time) < 0
52 * if this is true, then our time has expired.
53 * (idea due to Andy Gill).
56 wakeUpSleepingThreads(lnat ticks)
59 rtsBool flag = rtsFalse;
61 while (sleeping_queue != END_TSO_QUEUE &&
62 (int)(ticks - sleeping_queue->block_info.target) > 0) {
64 sleeping_queue = tso->link;
65 tso->why_blocked = NotBlocked;
66 tso->link = END_TSO_QUEUE;
67 IF_DEBUG(scheduler,debugBelch("Waking up sleeping thread %d\n", tso->id));
68 PUSH_ON_RUN_QUEUE(tso);
74 /* Argument 'wait' says whether to wait for I/O to become available,
75 * or whether to just check and return immediately. If there are
76 * other threads ready to run, we normally do the non-waiting variety,
77 * otherwise we wait (see Schedule.c).
79 * SMP note: must be called with sched_mutex locked.
81 * Windows: select only works on sockets, so this doesn't really work,
82 * though it makes things better than before. MsgWaitForMultipleObjects
83 * should really be used, though it only seems to work for read handles,
88 awaitEvent(rtsBool wait)
90 StgTSO *tso, *prev, *next;
95 rtsBool select_succeeded = rtsTrue;
96 rtsBool unblock_all = rtsFalse;
104 debugBelch("scheduler: checking for threads blocked on I/O");
106 debugBelch(" (waiting)");
111 /* loop until we've woken up some threads. This loop is needed
112 * because the select timing isn't accurate, we sometimes sleep
113 * for a while but not long enough to wake up a thread in
118 ticks = timestamp = getourtimeofday();
119 if (wakeUpSleepingThreads(ticks)) {
125 } else if (sleeping_queue != END_TSO_QUEUE) {
126 min = (sleeping_queue->block_info.target - ticks)
127 * TICK_MILLISECS * 1000;
133 * Collect all of the fd's that we're interested in
138 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
141 switch (tso->why_blocked) {
144 int fd = tso->block_info.fd;
145 maxfd = (fd > maxfd) ? fd : maxfd;
152 int fd = tso->block_info.fd;
153 maxfd = (fd > maxfd) ? fd : maxfd;
163 /* Check for any interesting events */
165 tv.tv_sec = min / 1000000;
166 tv.tv_usec = min % 1000000;
168 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
169 if (errno != EINTR) {
170 /* Handle bad file descriptors by unblocking all the
171 waiting threads. Why? Because a thread might have been
172 a bit naughty and closed a file descriptor while another
173 was blocked waiting. This is less-than-good programming
174 practice, but having the RTS as a result fall over isn't
175 acceptable, so we simply unblock all the waiting threads
176 should we see a bad file descriptor & give the threads
177 a chance to clean up their act.
179 Note: assume here that threads becoming unblocked
180 will try to read/write the file descriptor before trying
181 to issue a threadWaitRead/threadWaitWrite again (==> an
182 IOError will result for the thread that's got the bad
183 file descriptor.) Hence, there's no danger of a bad
184 file descriptor being repeatedly select()'ed on, so
187 if ( errno == EBADF ) {
188 unblock_all = rtsTrue;
192 barf("select failed");
196 /* We got a signal; could be one of ours. If so, we need
197 * to start up the signal handler straight away, otherwise
198 * we could block for a long time before the signal is
201 #if defined(RTS_USER_SIGNALS)
202 if (signals_pending()) {
203 startSignalHandlers();
204 return; /* still hold the lock */
208 /* we were interrupted, return to the scheduler immediately.
211 return; /* still hold the lock */
214 /* check for threads that need waking up
216 wakeUpSleepingThreads(getourtimeofday());
218 /* If new runnable threads have arrived, stop waiting for
221 if (run_queue_hd != END_TSO_QUEUE) {
222 return; /* still hold the lock */
226 /* Step through the waiting queue, unblocking every thread that now has
227 * a file descriptor in a ready state.
231 if (select_succeeded || unblock_all) {
232 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
234 switch (tso->why_blocked) {
236 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
239 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
246 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %d\n", tso->id));
247 tso->why_blocked = NotBlocked;
248 tso->link = END_TSO_QUEUE;
249 PUSH_ON_RUN_QUEUE(tso);
252 blocked_queue_hd = tso;
260 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
262 prev->link = END_TSO_QUEUE;
263 blocked_queue_tl = prev;
267 } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);
270 #endif /* RTS_SUPPORTS_THREADS */