1 /* -----------------------------------------------------------------------------
2 * $Id: Select.c,v 1.16 2001/02/28 14:23:55 sewardj Exp $
4 * (c) The GHC Team 1995-1999
6 * Support for concurrent non-blocking I/O and thread waiting.
8 * ---------------------------------------------------------------------------*/
10 /* we're outside the realms of POSIX here... */
11 #define NON_POSIX_SOURCE
20 # ifdef HAVE_SYS_TYPES_H
21 # include <sys/types.h>
24 # ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
28 # ifdef mingw32_TARGET_OS
35 /* keep track of the number of ticks since we last called
36 * gettimeofday(), to avoid having to call it every time we need
39 nat ticks_since_timestamp = 0;
41 /* There's a clever trick here to avoid problems when the time wraps
42 * around. Since our maximum delay is smaller than 31 bits of ticks
43 * (it's actually 31 bits of microseconds), we can safely check
44 * whether a timer has expired even if our timer will wrap around
45 * before the target is reached, using the following formula:
47 * (int)((uint)current_time - (uint)target_time) < 0
49 * if this is true, then our time has expired.
50 * (idea due to Andy Gill).
53 wakeUpSleepingThreads(nat ticks)
56 rtsBool flag = rtsFalse;
58 while (sleeping_queue != END_TSO_QUEUE &&
59 (int)(ticks - sleeping_queue->block_info.target) > 0) {
61 sleeping_queue = tso->link;
62 tso->why_blocked = NotBlocked;
63 tso->link = END_TSO_QUEUE;
64 IF_DEBUG(scheduler,belch("Waking up sleeping thread %d\n", tso->id));
65 PUSH_ON_RUN_QUEUE(tso);
71 /* Argument 'wait' says whether to wait for I/O to become available,
72 * or whether to just check and return immediately. If there are
73 * other threads ready to run, we normally do the non-waiting variety,
74 * otherwise we wait (see Schedule.c).
76 * SMP note: must be called with sched_mutex locked.
78 * Windows: select only works on sockets, so this doesn't really work,
79 * though it makes things better than before. MsgWaitForMultipleObjects
80 * should really be used, though it only seems to work for read handles,
85 awaitEvent(rtsBool wait)
87 StgTSO *tso, *prev, *next;
90 #ifndef mingw32_TARGET_OS
94 rtsBool select_succeeded = rtsTrue;
102 belch("scheduler: checking for threads blocked on I/O");
109 /* loop until we've woken up some threads. This loop is needed
110 * because the select timing isn't accurate, we sometimes sleep
111 * for a while but not long enough to wake up a thread in
116 ticks = timestamp = getourtimeofday();
117 ticks_since_timestamp = 0;
118 if (wakeUpSleepingThreads(ticks)) {
124 } else if (sleeping_queue != END_TSO_QUEUE) {
125 min = (sleeping_queue->block_info.target - ticks)
126 * TICK_MILLISECS * 1000;
131 #ifndef mingw32_TARGET_OS
133 * Collect all of the fd's that we're interested in
138 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
141 switch (tso->why_blocked) {
144 int fd = tso->block_info.fd;
145 maxfd = (fd > maxfd) ? fd : maxfd;
152 int fd = tso->block_info.fd;
153 maxfd = (fd > maxfd) ? fd : maxfd;
163 /* Release the scheduler lock while we do the poll.
164 * this means that someone might muck with the blocked_queue
165 * while we do this, but it shouldn't matter:
167 * - another task might poll for I/O and remove one
168 * or more threads from the blocked_queue.
169 * - more I/O threads may be added to blocked_queue.
170 * - more delayed threads may be added to blocked_queue. We'll
171 * just subtract delta from their delays after the poll.
173 * I believe none of these cases lead to trouble --SDM.
175 RELEASE_LOCK(&sched_mutex);
177 /* Check for any interesting events */
179 tv.tv_sec = min / 1000000;
180 tv.tv_usec = min % 1000000;
182 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
183 if (errno != EINTR) {
185 printf("%d\n", errno);
188 barf("select failed");
190 #else /* on mingwin */
192 Sleep(0); /* don't busy wait */
193 #endif /* mingw32_TARGET_OS */
194 ACQUIRE_LOCK(&sched_mutex);
196 #ifndef mingw32_TARGET_OS
197 /* We got a signal; could be one of ours. If so, we need
198 * to start up the signal handler straight away, otherwise
199 * we could block for a long time before the signal is
202 if (signals_pending()) {
203 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
204 start_signal_handlers();
205 ACQUIRE_LOCK(&sched_mutex);
206 return; /* still hold the lock */
210 /* we were interrupted, return to the scheduler immediately.
213 return; /* still hold the lock */
216 /* check for threads that need waking up
218 wakeUpSleepingThreads(getourtimeofday());
220 /* If new runnable threads have arrived, stop waiting for
223 if (run_queue_hd != END_TSO_QUEUE) {
224 return; /* still hold the lock */
227 RELEASE_LOCK(&sched_mutex);
230 ACQUIRE_LOCK(&sched_mutex);
232 /* Step through the waiting queue, unblocking every thread that now has
233 * a file descriptor in a ready state.
237 if (select_succeeded) {
238 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
240 switch (tso->why_blocked) {
242 ready = FD_ISSET(tso->block_info.fd, &rfd);
245 ready = FD_ISSET(tso->block_info.fd, &wfd);
252 IF_DEBUG(scheduler,belch("Waking up blocked thread %d\n", tso->id));
253 tso->why_blocked = NotBlocked;
254 tso->link = END_TSO_QUEUE;
255 PUSH_ON_RUN_QUEUE(tso);
258 blocked_queue_hd = tso;
266 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
268 prev->link = END_TSO_QUEUE;
269 blocked_queue_tl = prev;
273 } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);