1 /* -----------------------------------------------------------------------------
2 * $Id: Select.c,v 1.5 1999/11/24 16:39:33 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>
28 nat ticks_since_select = 0;
30 /* Argument 'wait' says whether to wait for I/O to become available,
31 * or whether to just check and return immediately. If there are
32 * other threads ready to run, we normally do the non-waiting variety,
33 * otherwise we wait (see Schedule.c).
35 * SMP note: must be called with sched_mutex locked.
38 awaitEvent(rtsBool wait)
40 #ifdef mingw32_TARGET_OS
42 * Win32 doesn't support select(). ToDo: use MsgWaitForMultipleObjects()
43 * to achieve (similar) effect.
49 StgTSO *tso, *prev, *next;
57 #ifndef linux_TARGET_OS
58 struct timeval tv_before,tv_after;
61 IF_DEBUG(scheduler,belch("Checking for threads blocked on I/O...\n"));
63 /* see how long it's been since we last checked the blocked queue.
64 * ToDo: make this check atomic, so we don't lose any ticks.
66 delta = ticks_since_select;
67 ticks_since_select = 0;
68 delta = delta * TICK_MILLISECS * 1000;
70 min = wait == rtsTrue ? 0x7fffffff : 0;
73 * Collect all of the fd's that we're interested in, and capture
74 * the minimum waiting time (in microseconds) for the delayed threads.
79 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
82 switch (tso->why_blocked) {
85 int fd = tso->block_info.fd;
86 maxfd = (fd > maxfd) ? fd : maxfd;
93 int fd = tso->block_info.fd;
94 maxfd = (fd > maxfd) ? fd : maxfd;
101 if ((int)tso->block_info.delay < min)
102 min = tso->block_info.delay;
111 /* Release the scheduler lock while we do the poll.
112 * this means that someone might muck with the blocked_queue
113 * while we do this, but it shouldn't matter:
115 * - another task might poll for I/O and remove one
116 * or more threads from the blocked_queue.
117 * - more I/O threads may be added to blocked_queue.
118 * - more delayed threads may be added to blocked_queue. We'll
119 * just subtract delta from their delays after the poll.
121 * I believe none of these cases lead to trouble --SDM.
123 RELEASE_LOCK(&sched_mutex);
125 /* Check for any interesting events */
127 tv.tv_sec = min / 1000000;
128 tv.tv_usec = min % 1000000;
130 #ifndef linux_TARGET_OS
131 gettimeofday(&tv_before, (struct timezone *) NULL);
134 while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
135 if (errno != EINTR) {
136 /* fflush(stdout); */
137 fprintf(stderr, "awaitEvent: select failed\n");
138 stg_exit(EXIT_FAILURE);
140 ACQUIRE_LOCK(&sched_mutex);
141 /* We got a signal; could be one of ours. If so, we need
142 * to start up the signal handler straight away, otherwise
143 * we could block for a long time before the signal is
146 if (signals_pending()) {
147 start_signal_handlers();
151 /* If new runnable threads have arrived, stop waiting for
154 if (run_queue_hd != END_TSO_QUEUE) {
157 RELEASE_LOCK(&sched_mutex);
162 File descriptors ready, but we don't know how much time was spent
163 in the select(). To interpolate, we compare the time before
164 and after the select().
167 #ifdef linux_TARGET_OS
168 /* on Linux, tv is set to indicate the amount of time not
169 * slept, so we don't need to gettimeofday() to find out.
171 delta += min - (tv.tv_sec * 1000000 + tv.tv_usec);
173 gettimeofday(&tv_after, (struct timezone *) NULL);
174 delta += (tv_after.tv_sec - tv_before.tv_sec) * 1000000 +
175 tv_after.tv_usec - tv_before.tv_usec;
181 ACQUIRE_LOCK(&sched_mutex);
184 Step through the waiting queue, unblocking every thread that now has
185 a file descriptor in a ready state.
187 For the delayed threads, decrement the number of microsecs
188 we've been blocked for. Unblock the threads that have thusly expired.
192 for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
194 switch (tso->why_blocked) {
196 ready = FD_ISSET(tso->block_info.fd, &rfd);
200 ready = FD_ISSET(tso->block_info.fd, &wfd);
204 if (tso->block_info.delay > delta) {
205 tso->block_info.delay -= delta;
208 tso->block_info.delay = 0;
218 IF_DEBUG(scheduler,belch("Waking up thread %d\n", tso->id));
219 tso->why_blocked = NotBlocked;
220 tso->link = END_TSO_QUEUE;
221 PUSH_ON_RUN_QUEUE(tso);
224 blocked_queue_hd = tso;
232 blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
234 prev->link = END_TSO_QUEUE;
235 blocked_queue_tl = prev;