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 &&
64 (int)(ticks - sleeping_queue->block_info.target) >= 0) {
66 sleeping_queue = tso->_link;
67 tso->why_blocked = NotBlocked;
68 tso->_link = END_TSO_QUEUE;
69 IF_DEBUG(scheduler,debugBelch("Waking up sleeping thread %lu\n", (unsigned long)tso->id));
70 // MainCapability: this code is !THREADED_RTS
71 pushOnRunQueue(&MainCapability,tso);
77 /* Argument 'wait' says whether to wait for I/O to become available,
78 * or whether to just check and return immediately. If there are
79 * other threads ready to run, we normally do the non-waiting variety,
80 * otherwise we wait (see Schedule.c).
82 * SMP note: must be called with sched_mutex locked.
84 * Windows: select only works on sockets, so this doesn't really work,
85 * though it makes things better than before. MsgWaitForMultipleObjects
86 * should really be used, though it only seems to work for read handles,
91 awaitEvent(rtsBool wait)
93 StgTSO *tso, *prev, *next;
98 rtsBool select_succeeded = rtsTrue;
99 rtsBool unblock_all = rtsFalse;
107 debugBelch("scheduler: checking for threads blocked on I/O");
109 debugBelch(" (waiting)");
114 /* loop until we've woken up some threads. This loop is needed
115 * because the select timing isn't accurate, we sometimes sleep
116 * for a while but not long enough to wake up a thread in
121 ticks = timestamp = getourtimeofday();
122 if (wakeUpSleepingThreads(ticks)) {
128 } else if (sleeping_queue != END_TSO_QUEUE) {
129 min = (sleeping_queue->block_info.target - ticks)
130 * RtsFlags.MiscFlags.tickInterval * 1000;
136 * Collect all of the fd's that we're interested in
141 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
144 /* On FreeBSD FD_SETSIZE is unsigned. Cast it to signed int
145 * in order to switch off the 'comparison between signed and
146 * unsigned error message
148 switch (tso->why_blocked) {
151 int fd = tso->block_info.fd;
152 if (fd >= (int)FD_SETSIZE) {
153 barf("awaitEvent: descriptor out of range");
155 maxfd = (fd > maxfd) ? fd : maxfd;
162 int fd = tso->block_info.fd;
163 if (fd >= (int)FD_SETSIZE) {
164 barf("awaitEvent: descriptor out of range");
166 maxfd = (fd > maxfd) ? fd : maxfd;
176 /* Check for any interesting events */
178 tv.tv_sec = min / 1000000;
179 tv.tv_usec = min % 1000000;
181 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
182 if (errno != EINTR) {
183 /* Handle bad file descriptors by unblocking all the
184 waiting threads. Why? Because a thread might have been
185 a bit naughty and closed a file descriptor while another
186 was blocked waiting. This is less-than-good programming
187 practice, but having the RTS as a result fall over isn't
188 acceptable, so we simply unblock all the waiting threads
189 should we see a bad file descriptor & give the threads
190 a chance to clean up their act.
192 Note: assume here that threads becoming unblocked
193 will try to read/write the file descriptor before trying
194 to issue a threadWaitRead/threadWaitWrite again (==> an
195 IOError will result for the thread that's got the bad
196 file descriptor.) Hence, there's no danger of a bad
197 file descriptor being repeatedly select()'ed on, so
200 if ( errno == EBADF ) {
201 unblock_all = rtsTrue;
205 barf("select failed");
209 /* We got a signal; could be one of ours. If so, we need
210 * to start up the signal handler straight away, otherwise
211 * we could block for a long time before the signal is
214 #if defined(RTS_USER_SIGNALS)
215 if (RtsFlags.MiscFlags.install_signal_handlers && signals_pending()) {
216 startSignalHandlers(&MainCapability);
217 return; /* still hold the lock */
221 /* we were interrupted, return to the scheduler immediately.
223 if (sched_state >= SCHED_INTERRUPTING) {
224 return; /* still hold the lock */
227 /* check for threads that need waking up
229 wakeUpSleepingThreads(getourtimeofday());
231 /* If new runnable threads have arrived, stop waiting for
234 if (!emptyRunQueue(&MainCapability)) {
235 return; /* still hold the lock */
239 /* Step through the waiting queue, unblocking every thread that now has
240 * a file descriptor in a ready state.
244 if (select_succeeded || unblock_all) {
245 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
247 switch (tso->why_blocked) {
249 ready = unblock_all || FD_ISSET(tso->block_info.fd, &rfd);
252 ready = unblock_all || FD_ISSET(tso->block_info.fd, &wfd);
259 IF_DEBUG(scheduler,debugBelch("Waking up blocked thread %lu\n", (unsigned long)tso->id));
260 tso->why_blocked = NotBlocked;
261 tso->_link = END_TSO_QUEUE;
262 pushOnRunQueue(&MainCapability,tso);
265 blocked_queue_hd = tso;
267 setTSOLink(&MainCapability, prev, tso);
273 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
275 prev->_link = END_TSO_QUEUE;
276 blocked_queue_tl = prev;
280 } while (wait && sched_state == SCHED_RUNNING
281 && emptyRunQueue(&MainCapability));
284 #endif /* THREADED_RTS */