1 /* -----------------------------------------------------------------------------
2 * $Id: Select.c,v 1.14 2000/08/25 13:12:07 simonmar 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 # if defined(HAVE_SYS_TYPES_H)
21 # include <sys/types.h>
24 # ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
31 /* keep track of the number of ticks since we last called
32 * gettimeofday(), to avoid having to call it every time we need
35 nat ticks_since_timestamp = 0;
37 /* There's a clever trick here to avoid problems when the time wraps
38 * around. Since our maximum delay is smaller than 31 bits of ticks
39 * (it's actually 31 bits of microseconds), we can safely check
40 * whether a timer has expired even if our timer will wrap around
41 * before the target is reached, using the following formula:
43 * (int)((uint)current_time - (uint)target_time) < 0
45 * if this is true, then our time has expired.
46 * (idea due to Andy Gill).
49 wakeUpSleepingThreads(nat ticks)
52 rtsBool flag = rtsFalse;
54 while (sleeping_queue != END_TSO_QUEUE &&
55 (int)(ticks - sleeping_queue->block_info.target) > 0) {
57 sleeping_queue = tso->link;
58 tso->why_blocked = NotBlocked;
59 tso->link = END_TSO_QUEUE;
60 IF_DEBUG(scheduler,belch("Waking up sleeping thread %d\n", tso->id));
61 PUSH_ON_RUN_QUEUE(tso);
67 /* Argument 'wait' says whether to wait for I/O to become available,
68 * or whether to just check and return immediately. If there are
69 * other threads ready to run, we normally do the non-waiting variety,
70 * otherwise we wait (see Schedule.c).
72 * SMP note: must be called with sched_mutex locked.
75 awaitEvent(rtsBool wait)
77 #ifdef mingw32_TARGET_OS
79 * Win32 doesn't support select(). ToDo: use MsgWaitForMultipleObjects()
80 * to achieve (similar) effect.
86 StgTSO *tso, *prev, *next;
91 rtsBool select_succeeded = rtsTrue;
99 belch("scheduler: checking for threads blocked on I/O");
106 /* loop until we've woken up some threads. This loop is needed
107 * because the select timing isn't accurate, we sometimes sleep
108 * for a while but not long enough to wake up a thread in
113 ticks = timestamp = getourtimeofday();
114 ticks_since_timestamp = 0;
115 if (wakeUpSleepingThreads(ticks)) {
121 } else if (sleeping_queue != END_TSO_QUEUE) {
122 min = (sleeping_queue->block_info.target - ticks)
123 * TICK_MILLISECS * 1000;
129 * Collect all of the fd's that we're interested in
134 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
137 switch (tso->why_blocked) {
140 int fd = tso->block_info.fd;
141 maxfd = (fd > maxfd) ? fd : maxfd;
148 int fd = tso->block_info.fd;
149 maxfd = (fd > maxfd) ? fd : maxfd;
159 /* Release the scheduler lock while we do the poll.
160 * this means that someone might muck with the blocked_queue
161 * while we do this, but it shouldn't matter:
163 * - another task might poll for I/O and remove one
164 * or more threads from the blocked_queue.
165 * - more I/O threads may be added to blocked_queue.
166 * - more delayed threads may be added to blocked_queue. We'll
167 * just subtract delta from their delays after the poll.
169 * I believe none of these cases lead to trouble --SDM.
171 RELEASE_LOCK(&sched_mutex);
173 /* Check for any interesting events */
175 tv.tv_sec = min / 1000000;
176 tv.tv_usec = min % 1000000;
178 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
180 if (errno != EINTR) {
181 /* fflush(stdout); */
183 barf("select failed");
185 ACQUIRE_LOCK(&sched_mutex);
187 /* We got a signal; could be one of ours. If so, we need
188 * to start up the signal handler straight away, otherwise
189 * we could block for a long time before the signal is
192 if (signals_pending()) {
193 RELEASE_LOCK(&sched_mutex); /* ToDo: kill */
194 start_signal_handlers();
195 ACQUIRE_LOCK(&sched_mutex);
196 return; /* still hold the lock */
199 /* we were interrupted, return to the scheduler immediately.
202 return; /* still hold the lock */
205 /* check for threads that need waking up
207 wakeUpSleepingThreads(getourtimeofday());
209 /* If new runnable threads have arrived, stop waiting for
212 if (run_queue_hd != END_TSO_QUEUE) {
213 return; /* still hold the lock */
216 RELEASE_LOCK(&sched_mutex);
219 ACQUIRE_LOCK(&sched_mutex);
221 /* Step through the waiting queue, unblocking every thread that now has
222 * a file descriptor in a ready state.
226 if (select_succeeded) {
227 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
229 switch (tso->why_blocked) {
231 ready = FD_ISSET(tso->block_info.fd, &rfd);
234 ready = FD_ISSET(tso->block_info.fd, &wfd);
241 IF_DEBUG(scheduler,belch("Waking up blocked thread %d\n", tso->id));
242 tso->why_blocked = NotBlocked;
243 tso->link = END_TSO_QUEUE;
244 PUSH_ON_RUN_QUEUE(tso);
247 blocked_queue_hd = tso;
255 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
257 prev->link = END_TSO_QUEUE;
258 blocked_queue_tl = prev;
262 } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);