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_TYPES_H
21 # include <sys/types.h>
24 # ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
35 #if !defined(THREADED_RTS)
40 * The threaded RTS uses an IO-manager thread in Haskell instead (see GHC.Conc)
43 /* There's a clever trick here to avoid problems when the time wraps
44 * around. Since our maximum delay is smaller than 31 bits of ticks
45 * (it's actually 31 bits of microseconds), we can safely check
46 * whether a timer has expired even if our timer will wrap around
47 * before the target is reached, using the following formula:
49 * (int)((uint)current_time - (uint)target_time) < 0
51 * if this is true, then our time has expired.
52 * (idea due to Andy Gill).
55 wakeUpSleepingThreads(lnat ticks)
58 rtsBool flag = rtsFalse;
60 while (sleeping_queue != END_TSO_QUEUE) {
62 if (tso->what_next == ThreadRelocated) {
63 sleeping_queue = tso->_link;
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 /* Argument 'wait' says whether to wait for I/O to become available,
81 * or whether to just check and return immediately. If there are
82 * other threads ready to run, we normally do the non-waiting variety,
83 * otherwise we wait (see Schedule.c).
85 * SMP note: must be called with sched_mutex locked.
87 * Windows: select only works on sockets, so this doesn't really work,
88 * though it makes things better than before. MsgWaitForMultipleObjects
89 * should really be used, though it only seems to work for read handles,
94 awaitEvent(rtsBool wait)
96 StgTSO *tso, *prev, *next;
101 rtsBool select_succeeded = rtsTrue;
102 rtsBool unblock_all = rtsFalse;
110 debugBelch("scheduler: checking for threads blocked on I/O");
112 debugBelch(" (waiting)");
117 /* loop until we've woken up some threads. This loop is needed
118 * because the select timing isn't accurate, we sometimes sleep
119 * for a while but not long enough to wake up a thread in
124 ticks = timestamp = getourtimeofday();
125 if (wakeUpSleepingThreads(ticks)) {
131 } else if (sleeping_queue != END_TSO_QUEUE) {
132 min = (sleeping_queue->block_info.target - ticks)
133 * RtsFlags.MiscFlags.tickInterval * 1000;
139 * Collect all of the fd's that we're interested in
144 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
147 /* On FreeBSD FD_SETSIZE is unsigned. Cast it to signed int
148 * in order to switch off the 'comparison between signed and
149 * unsigned error message
151 switch (tso->why_blocked) {
154 int fd = tso->block_info.fd;
155 if (fd >= (int)FD_SETSIZE) {
156 barf("awaitEvent: descriptor out of range");
158 maxfd = (fd > maxfd) ? fd : maxfd;
165 int fd = tso->block_info.fd;
166 if (fd >= (int)FD_SETSIZE) {
167 barf("awaitEvent: descriptor out of range");
169 maxfd = (fd > maxfd) ? fd : maxfd;
179 /* Check for any interesting events */
181 tv.tv_sec = min / 1000000;
182 tv.tv_usec = min % 1000000;
184 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
185 if (errno != EINTR) {
186 /* Handle bad file descriptors by unblocking all the
187 waiting threads. Why? Because a thread might have been
188 a bit naughty and closed a file descriptor while another
189 was blocked waiting. This is less-than-good programming
190 practice, but having the RTS as a result fall over isn't
191 acceptable, so we simply unblock all the waiting threads
192 should we see a bad file descriptor & give the threads
193 a chance to clean up their act.
195 Note: assume here that threads becoming unblocked
196 will try to read/write the file descriptor before trying
197 to issue a threadWaitRead/threadWaitWrite again (==> an
198 IOError will result for the thread that's got the bad
199 file descriptor.) Hence, there's no danger of a bad
200 file descriptor being repeatedly select()'ed on, so
203 if ( errno == EBADF ) {
204 unblock_all = rtsTrue;
208 barf("select failed");
212 /* We got a signal; could be one of ours. If so, we need
213 * to start up the signal handler straight away, otherwise
214 * we could block for a long time before the signal is
217 #if defined(RTS_USER_SIGNALS)
218 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
219 startSignalHandlers(&MainCapability);
220 return; /* still hold the lock */
224 /* we were interrupted, return to the scheduler immediately.
226 if (sched_state >= SCHED_INTERRUPTING) {
227 return; /* still hold the lock */
230 /* check for threads that need waking up
232 wakeUpSleepingThreads(getourtimeofday());
234 /* If new runnable threads have arrived, stop waiting for
237 if (!emptyRunQueue(&MainCapability)) {
238 return; /* still hold the lock */
242 /* Step through the waiting queue, unblocking every thread that now has
243 * a file descriptor in a ready state.
247 if (select_succeeded || unblock_all) {
248 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
251 if (tso->what_next == ThreadRelocated) {
255 switch (tso->why_blocked) {
257 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
260 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
267 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %lu\n", (unsigned long)tso->id));
268 tso->why_blocked = NotBlocked;
269 tso->_link = END_TSO_QUEUE;
270 pushOnRunQueue(&MainCapability,tso);
273 blocked_queue_hd = tso;
275 setTSOLink(&MainCapability, prev, tso);
281 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
283 prev->_link = END_TSO_QUEUE;
284 blocked_queue_tl = prev;
288 } while (wait && sched_state == SCHED_RUNNING
289 && emptyRunQueue(&MainCapability));
292 #endif /* THREADED_RTS */