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 /* Argument 'wait' says whether to wait for I/O to become available,
85 * or whether to just check and return immediately. If there are
86 * other threads ready to run, we normally do the non-waiting variety,
87 * otherwise we wait (see Schedule.c).
89 * SMP note: must be called with sched_mutex locked.
91 * Windows: select only works on sockets, so this doesn't really work,
92 * though it makes things better than before. MsgWaitForMultipleObjects
93 * should really be used, though it only seems to work for read handles,
98 awaitEvent(rtsBool wait)
100 StgTSO *tso, *prev, *next;
105 rtsBool select_succeeded = rtsTrue;
106 rtsBool unblock_all = rtsFalse;
114 debugBelch("scheduler: checking for threads blocked on I/O");
116 debugBelch(" (waiting)");
121 /* loop until we've woken up some threads. This loop is needed
122 * because the select timing isn't accurate, we sometimes sleep
123 * for a while but not long enough to wake up a thread in
128 ticks = timestamp = getourtimeofday();
129 if (wakeUpSleepingThreads(ticks)) {
135 } else if (sleeping_queue != END_TSO_QUEUE) {
136 min = (sleeping_queue->block_info.target - ticks)
137 * RtsFlags.MiscFlags.tickInterval * 1000;
143 * Collect all of the fd's that we're interested in
148 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
151 /* On FreeBSD FD_SETSIZE is unsigned. Cast it to signed int
152 * in order to switch off the 'comparison between signed and
153 * unsigned error message
155 switch (tso->why_blocked) {
158 int fd = tso->block_info.fd;
159 if (fd >= (int)FD_SETSIZE) {
160 barf("awaitEvent: descriptor out of range");
162 maxfd = (fd > maxfd) ? fd : maxfd;
169 int fd = tso->block_info.fd;
170 if (fd >= (int)FD_SETSIZE) {
171 barf("awaitEvent: descriptor out of range");
173 maxfd = (fd > maxfd) ? fd : maxfd;
183 /* Check for any interesting events */
185 tv.tv_sec = min / 1000000;
186 tv.tv_usec = min % 1000000;
188 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
189 if (errno != EINTR) {
190 /* Handle bad file descriptors by unblocking all the
191 waiting threads. Why? Because a thread might have been
192 a bit naughty and closed a file descriptor while another
193 was blocked waiting. This is less-than-good programming
194 practice, but having the RTS as a result fall over isn't
195 acceptable, so we simply unblock all the waiting threads
196 should we see a bad file descriptor & give the threads
197 a chance to clean up their act.
199 Note: assume here that threads becoming unblocked
200 will try to read/write the file descriptor before trying
201 to issue a threadWaitRead/threadWaitWrite again (==> an
202 IOError will result for the thread that's got the bad
203 file descriptor.) Hence, there's no danger of a bad
204 file descriptor being repeatedly select()'ed on, so
207 if ( errno == EBADF ) {
208 unblock_all = rtsTrue;
212 barf("select failed");
216 /* We got a signal; could be one of ours. If so, we need
217 * to start up the signal handler straight away, otherwise
218 * we could block for a long time before the signal is
221 #if defined(RTS_USER_SIGNALS)
222 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
223 startSignalHandlers(&MainCapability);
224 return; /* still hold the lock */
228 /* we were interrupted, return to the scheduler immediately.
230 if (sched_state >= SCHED_INTERRUPTING) {
231 return; /* still hold the lock */
234 /* check for threads that need waking up
236 wakeUpSleepingThreads(getourtimeofday());
238 /* If new runnable threads have arrived, stop waiting for
241 if (!emptyRunQueue(&MainCapability)) {
242 return; /* still hold the lock */
246 /* Step through the waiting queue, unblocking every thread that now has
247 * a file descriptor in a ready state.
251 if (select_succeeded || unblock_all) {
252 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
255 if (tso->what_next == ThreadRelocated) {
259 switch (tso->why_blocked) {
261 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
264 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
271 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %lu\n", (unsigned long)tso->id));
272 tso->why_blocked = NotBlocked;
273 tso->_link = END_TSO_QUEUE;
274 pushOnRunQueue(&MainCapability,tso);
277 blocked_queue_hd = tso;
279 setTSOLink(&MainCapability, prev, tso);
285 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
287 prev->_link = END_TSO_QUEUE;
288 blocked_queue_tl = prev;
292 } while (wait && sched_state == SCHED_RUNNING
293 && emptyRunQueue(&MainCapability));
296 #endif /* THREADED_RTS */