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" */
20 #include "Capability.h"
21 #include "posix/Select.h"
23 # ifdef HAVE_SYS_TYPES_H
24 # include <sys/types.h>
27 # ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
38 #if !defined(THREADED_RTS)
43 * The threaded RTS uses an IO-manager thread in Haskell instead (see GHC.Conc)
46 /* There's a clever trick here to avoid problems when the time wraps
47 * around. Since our maximum delay is smaller than 31 bits of ticks
48 * (it's actually 31 bits of microseconds), we can safely check
49 * whether a timer has expired even if our timer will wrap around
50 * before the target is reached, using the following formula:
52 * (int)((uint)current_time - (uint)target_time) < 0
54 * if this is true, then our time has expired.
55 * (idea due to Andy Gill).
58 wakeUpSleepingThreads(lnat ticks)
61 rtsBool flag = rtsFalse;
63 while (sleeping_queue != END_TSO_QUEUE) {
65 if (tso->what_next == ThreadRelocated) {
66 sleeping_queue = tso->_link;
69 if (((long)ticks - (long)tso->block_info.target) < 0) {
72 sleeping_queue = tso->_link;
73 tso->why_blocked = NotBlocked;
74 tso->_link = END_TSO_QUEUE;
75 IF_DEBUG(scheduler,debugBelch("Waking up sleeping thread %lu\n", (unsigned long)tso->id));
76 // MainCapability: this code is !THREADED_RTS
77 pushOnRunQueue(&MainCapability,tso);
83 /* Argument 'wait' says whether to wait for I/O to become available,
84 * or whether to just check and return immediately. If there are
85 * other threads ready to run, we normally do the non-waiting variety,
86 * otherwise we wait (see Schedule.c).
88 * SMP note: must be called with sched_mutex locked.
90 * Windows: select only works on sockets, so this doesn't really work,
91 * though it makes things better than before. MsgWaitForMultipleObjects
92 * should really be used, though it only seems to work for read handles,
97 awaitEvent(rtsBool wait)
99 StgTSO *tso, *prev, *next;
104 rtsBool select_succeeded = rtsTrue;
105 rtsBool unblock_all = rtsFalse;
113 debugBelch("scheduler: checking for threads blocked on I/O");
115 debugBelch(" (waiting)");
120 /* loop until we've woken up some threads. This loop is needed
121 * because the select timing isn't accurate, we sometimes sleep
122 * for a while but not long enough to wake up a thread in
127 ticks = timestamp = getourtimeofday();
128 if (wakeUpSleepingThreads(ticks)) {
134 } else if (sleeping_queue != END_TSO_QUEUE) {
135 min = (sleeping_queue->block_info.target - ticks)
136 * RtsFlags.MiscFlags.tickInterval * 1000;
142 * Collect all of the fd's that we're interested in
147 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
150 /* On FreeBSD FD_SETSIZE is unsigned. Cast it to signed int
151 * in order to switch off the 'comparison between signed and
152 * unsigned error message
154 switch (tso->why_blocked) {
157 int fd = tso->block_info.fd;
158 if (fd >= (int)FD_SETSIZE) {
159 barf("awaitEvent: descriptor out of range");
161 maxfd = (fd > maxfd) ? fd : maxfd;
168 int fd = tso->block_info.fd;
169 if (fd >= (int)FD_SETSIZE) {
170 barf("awaitEvent: descriptor out of range");
172 maxfd = (fd > maxfd) ? fd : maxfd;
182 /* Check for any interesting events */
184 tv.tv_sec = min / 1000000;
185 tv.tv_usec = min % 1000000;
187 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
188 if (errno != EINTR) {
189 /* Handle bad file descriptors by unblocking all the
190 waiting threads. Why? Because a thread might have been
191 a bit naughty and closed a file descriptor while another
192 was blocked waiting. This is less-than-good programming
193 practice, but having the RTS as a result fall over isn't
194 acceptable, so we simply unblock all the waiting threads
195 should we see a bad file descriptor & give the threads
196 a chance to clean up their act.
198 Note: assume here that threads becoming unblocked
199 will try to read/write the file descriptor before trying
200 to issue a threadWaitRead/threadWaitWrite again (==> an
201 IOError will result for the thread that's got the bad
202 file descriptor.) Hence, there's no danger of a bad
203 file descriptor being repeatedly select()'ed on, so
206 if ( errno == EBADF ) {
207 unblock_all = rtsTrue;
211 barf("select failed");
215 /* We got a signal; could be one of ours. If so, we need
216 * to start up the signal handler straight away, otherwise
217 * we could block for a long time before the signal is
220 #if defined(RTS_USER_SIGNALS)
221 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
222 startSignalHandlers(&MainCapability);
223 return; /* still hold the lock */
227 /* we were interrupted, return to the scheduler immediately.
229 if (sched_state >= SCHED_INTERRUPTING) {
230 return; /* still hold the lock */
233 /* check for threads that need waking up
235 wakeUpSleepingThreads(getourtimeofday());
237 /* If new runnable threads have arrived, stop waiting for
240 if (!emptyRunQueue(&MainCapability)) {
241 return; /* still hold the lock */
245 /* Step through the waiting queue, unblocking every thread that now has
246 * a file descriptor in a ready state.
250 if (select_succeeded || unblock_all) {
251 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
254 if (tso->what_next == ThreadRelocated) {
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 */