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 (tso->what_next == ThreadRelocated) {
67 sleeping_queue = tso->_link;
70 if (((long)ticks - (long)tso->block_info.target) < 0) {
73 sleeping_queue = tso->_link;
74 tso->why_blocked = NotBlocked;
75 tso->_link = END_TSO_QUEUE;
76 IF_DEBUG(scheduler,debugBelch("Waking up sleeping thread %lu\n", (unsigned long)tso->id));
77 // MainCapability: this code is !THREADED_RTS
78 pushOnRunQueue(&MainCapability,tso);
84 static void GNUC3_ATTRIBUTE(__noreturn__)
87 errorBelch("file descriptor %d out of range for select (0--%d).\nRecompile with -threaded to work around this.", fd, (int)FD_SETSIZE);
88 stg_exit(EXIT_FAILURE);
91 /* Argument 'wait' says whether to wait for I/O to become available,
92 * or whether to just check and return immediately. If there are
93 * other threads ready to run, we normally do the non-waiting variety,
94 * otherwise we wait (see Schedule.c).
96 * SMP note: must be called with sched_mutex locked.
98 * Windows: select only works on sockets, so this doesn't really work,
99 * though it makes things better than before. MsgWaitForMultipleObjects
100 * should really be used, though it only seems to work for read handles,
105 awaitEvent(rtsBool wait)
107 StgTSO *tso, *prev, *next;
112 rtsBool select_succeeded = rtsTrue;
113 rtsBool unblock_all = rtsFalse;
121 debugBelch("scheduler: checking for threads blocked on I/O");
123 debugBelch(" (waiting)");
128 /* loop until we've woken up some threads. This loop is needed
129 * because the select timing isn't accurate, we sometimes sleep
130 * for a while but not long enough to wake up a thread in
135 ticks = timestamp = getourtimeofday();
136 if (wakeUpSleepingThreads(ticks)) {
142 } else if (sleeping_queue != END_TSO_QUEUE) {
143 min = (sleeping_queue->block_info.target - ticks)
144 * RtsFlags.MiscFlags.tickInterval * 1000;
150 * Collect all of the fd's that we're interested in
155 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
158 /* On FreeBSD FD_SETSIZE is unsigned. Cast it to signed int
159 * in order to switch off the 'comparison between signed and
160 * unsigned error message
162 switch (tso->why_blocked) {
165 int fd = tso->block_info.fd;
166 if ((fd >= (int)FD_SETSIZE) || (fd < 0)) {
169 maxfd = (fd > maxfd) ? fd : maxfd;
176 int fd = tso->block_info.fd;
177 if ((fd >= (int)FD_SETSIZE) || (fd < 0)) {
180 maxfd = (fd > maxfd) ? fd : maxfd;
190 /* Check for any interesting events */
192 tv.tv_sec = min / 1000000;
193 tv.tv_usec = min % 1000000;
195 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
196 if (errno != EINTR) {
197 /* Handle bad file descriptors by unblocking all the
198 waiting threads. Why? Because a thread might have been
199 a bit naughty and closed a file descriptor while another
200 was blocked waiting. This is less-than-good programming
201 practice, but having the RTS as a result fall over isn't
202 acceptable, so we simply unblock all the waiting threads
203 should we see a bad file descriptor & give the threads
204 a chance to clean up their act.
206 Note: assume here that threads becoming unblocked
207 will try to read/write the file descriptor before trying
208 to issue a threadWaitRead/threadWaitWrite again (==> an
209 IOError will result for the thread that's got the bad
210 file descriptor.) Hence, there's no danger of a bad
211 file descriptor being repeatedly select()'ed on, so
214 if ( errno == EBADF ) {
215 unblock_all = rtsTrue;
219 barf("select failed");
223 /* We got a signal; could be one of ours. If so, we need
224 * to start up the signal handler straight away, otherwise
225 * we could block for a long time before the signal is
228 #if defined(RTS_USER_SIGNALS)
229 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
230 startSignalHandlers(&MainCapability);
231 return; /* still hold the lock */
235 /* we were interrupted, return to the scheduler immediately.
237 if (sched_state >= SCHED_INTERRUPTING) {
238 return; /* still hold the lock */
241 /* check for threads that need waking up
243 wakeUpSleepingThreads(getourtimeofday());
245 /* If new runnable threads have arrived, stop waiting for
248 if (!emptyRunQueue(&MainCapability)) {
249 return; /* still hold the lock */
253 /* Step through the waiting queue, unblocking every thread that now has
254 * a file descriptor in a ready state.
258 if (select_succeeded || unblock_all) {
259 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
262 if (tso->what_next == ThreadRelocated) {
266 switch (tso->why_blocked) {
268 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
271 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
278 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %lu\n", (unsigned long)tso->id));
279 tso->why_blocked = NotBlocked;
280 tso->_link = END_TSO_QUEUE;
281 pushOnRunQueue(&MainCapability,tso);
284 blocked_queue_hd = tso;
286 setTSOLink(&MainCapability, prev, tso);
292 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
294 prev->_link = END_TSO_QUEUE;
295 blocked_queue_tl = prev;
299 } while (wait && sched_state == SCHED_RUNNING
300 && emptyRunQueue(&MainCapability));
303 #endif /* THREADED_RTS */