1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1995-2002
5 * Support for concurrent non-blocking I/O and thread waiting.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
16 #include "Capability.h"
18 #include "AwaitEvent.h"
20 # ifdef HAVE_SYS_SELECT_H
21 # include <sys/select.h>
24 # ifdef HAVE_SYS_TYPES_H
25 # include <sys/types.h>
28 # ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
39 #if !defined(THREADED_RTS)
44 * The threaded RTS uses an IO-manager thread in Haskell instead (see GHC.Conc)
47 /* There's a clever trick here to avoid problems when the time wraps
48 * around. Since our maximum delay is smaller than 31 bits of ticks
49 * (it's actually 31 bits of microseconds), we can safely check
50 * whether a timer has expired even if our timer will wrap around
51 * before the target is reached, using the following formula:
53 * (int)((uint)current_time - (uint)target_time) < 0
55 * if this is true, then our time has expired.
56 * (idea due to Andy Gill).
59 wakeUpSleepingThreads(lnat ticks)
62 rtsBool flag = rtsFalse;
64 while (sleeping_queue != END_TSO_QUEUE) {
66 if (((long)ticks - (long)tso->block_info.target) < 0) {
69 sleeping_queue = tso->_link;
70 tso->why_blocked = NotBlocked;
71 tso->_link = END_TSO_QUEUE;
72 IF_DEBUG(scheduler,debugBelch("Waking up sleeping thread %lu\n", (unsigned long)tso->id));
73 // MainCapability: this code is !THREADED_RTS
74 pushOnRunQueue(&MainCapability,tso);
80 static void GNUC3_ATTRIBUTE(__noreturn__)
83 errorBelch("file descriptor %d out of range for select (0--%d).\nRecompile with -threaded to work around this.", fd, (int)FD_SETSIZE);
84 stg_exit(EXIT_FAILURE);
87 /* Argument 'wait' says whether to wait for I/O to become available,
88 * or whether to just check and return immediately. If there are
89 * other threads ready to run, we normally do the non-waiting variety,
90 * otherwise we wait (see Schedule.c).
92 * SMP note: must be called with sched_mutex locked.
94 * Windows: select only works on sockets, so this doesn't really work,
95 * though it makes things better than before. MsgWaitForMultipleObjects
96 * should really be used, though it only seems to work for read handles,
101 awaitEvent(rtsBool wait)
103 StgTSO *tso, *prev, *next;
108 rtsBool select_succeeded = rtsTrue;
109 rtsBool unblock_all = rtsFalse;
117 debugBelch("scheduler: checking for threads blocked on I/O");
119 debugBelch(" (waiting)");
124 /* loop until we've woken up some threads. This loop is needed
125 * because the select timing isn't accurate, we sometimes sleep
126 * for a while but not long enough to wake up a thread in
131 ticks = timestamp = getourtimeofday();
132 if (wakeUpSleepingThreads(ticks)) {
138 } else if (sleeping_queue != END_TSO_QUEUE) {
139 min = (sleeping_queue->block_info.target - ticks)
140 * RtsFlags.MiscFlags.tickInterval * 1000;
146 * Collect all of the fd's that we're interested in
151 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
154 /* On FreeBSD FD_SETSIZE is unsigned. Cast it to signed int
155 * in order to switch off the 'comparison between signed and
156 * unsigned error message
158 switch (tso->why_blocked) {
161 int fd = tso->block_info.fd;
162 if ((fd >= (int)FD_SETSIZE) || (fd < 0)) {
165 maxfd = (fd > maxfd) ? fd : maxfd;
172 int fd = tso->block_info.fd;
173 if ((fd >= (int)FD_SETSIZE) || (fd < 0)) {
176 maxfd = (fd > maxfd) ? fd : maxfd;
186 /* Check for any interesting events */
188 tv.tv_sec = min / 1000000;
189 tv.tv_usec = min % 1000000;
191 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
192 if (errno != EINTR) {
193 /* Handle bad file descriptors by unblocking all the
194 waiting threads. Why? Because a thread might have been
195 a bit naughty and closed a file descriptor while another
196 was blocked waiting. This is less-than-good programming
197 practice, but having the RTS as a result fall over isn't
198 acceptable, so we simply unblock all the waiting threads
199 should we see a bad file descriptor & give the threads
200 a chance to clean up their act.
202 Note: assume here that threads becoming unblocked
203 will try to read/write the file descriptor before trying
204 to issue a threadWaitRead/threadWaitWrite again (==> an
205 IOError will result for the thread that's got the bad
206 file descriptor.) Hence, there's no danger of a bad
207 file descriptor being repeatedly select()'ed on, so
210 if ( errno == EBADF ) {
211 unblock_all = rtsTrue;
215 barf("select failed");
219 /* We got a signal; could be one of ours. If so, we need
220 * to start up the signal handler straight away, otherwise
221 * we could block for a long time before the signal is
224 #if defined(RTS_USER_SIGNALS)
225 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
226 startSignalHandlers(&MainCapability);
227 return; /* still hold the lock */
231 /* we were interrupted, return to the scheduler immediately.
233 if (sched_state >= SCHED_INTERRUPTING) {
234 return; /* still hold the lock */
237 /* check for threads that need waking up
239 wakeUpSleepingThreads(getourtimeofday());
241 /* If new runnable threads have arrived, stop waiting for
244 if (!emptyRunQueue(&MainCapability)) {
245 return; /* still hold the lock */
249 /* Step through the waiting queue, unblocking every thread that now has
250 * a file descriptor in a ready state.
254 if (select_succeeded || unblock_all) {
255 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
258 switch (tso->why_blocked) {
260 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
263 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
270 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %lu\n", (unsigned long)tso->id));
271 tso->why_blocked = NotBlocked;
272 tso->_link = END_TSO_QUEUE;
273 pushOnRunQueue(&MainCapability,tso);
276 blocked_queue_hd = tso;
278 setTSOLink(&MainCapability, prev, tso);
284 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
286 prev->_link = END_TSO_QUEUE;
287 blocked_queue_tl = prev;
291 } while (wait && sched_state == SCHED_RUNNING
292 && emptyRunQueue(&MainCapability));
295 #endif /* THREADED_RTS */