[project @ 1999-11-03 15:04:25 by simonmar]
[ghc-hetmet.git] / ghc / rts / Select.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Select.c,v 1.4 1999/11/03 15:04:25 simonmar Exp $
3  *
4  * (c) The GHC Team 1995-1999
5  *
6  * Support for concurrent non-blocking I/O and thread waiting.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 /* we're outside the realms of POSIX here... */
11 #define NON_POSIX_SOURCE
12
13 #include "Rts.h"
14 #include "Schedule.h"
15 #include "RtsUtils.h"
16 #include "RtsFlags.h"
17 #include "Itimer.h"
18 #include "Signals.h"
19
20 # if defined(HAVE_SYS_TYPES_H)
21 #  include <sys/types.h>
22 # endif
23
24 # ifdef HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # endif
27
28 nat ticks_since_select = 0;
29
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).
34  *
35  * SMP note: must be called with sched_mutex locked.
36  */
37 void
38 awaitEvent(rtsBool wait)
39 {
40 #ifdef mingw32_TARGET_OS
41 /*
42  * Win32 doesn't support select(). ToDo: use MsgWaitForMultipleObjects()
43  * to achieve (similar) effect.
44  *
45  */
46     return;
47 #else
48
49     StgTSO *tso, *prev, *next;
50     rtsBool ready;
51     fd_set rfd,wfd;
52     int min, numFound, delta;
53     int maxfd = -1;
54    
55     struct timeval tv;
56 #ifndef linux_TARGET_OS
57     struct timeval tv_before,tv_after;
58 #endif
59
60     IF_DEBUG(scheduler,belch("Checking for threads blocked on I/O...\n"));
61
62     /* see how long it's been since we last checked the blocked queue.
63      * ToDo: make this check atomic, so we don't lose any ticks.
64      */
65     delta = ticks_since_select;
66     ticks_since_select = 0;
67     delta = delta * TICK_MILLISECS * 1000;
68
69     min = wait == rtsTrue ? 0x7fffffff : 0;
70
71     /* 
72      * Collect all of the fd's that we're interested in, and capture
73      * the minimum waiting time (in microseconds) for the delayed threads.
74      */
75     FD_ZERO(&rfd);
76     FD_ZERO(&wfd);
77
78     for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
79       next = tso->link;
80
81       switch (tso->why_blocked) {
82       case BlockedOnRead:
83         { 
84           int fd = tso->block_info.fd;
85           maxfd = (fd > maxfd) ? fd : maxfd;
86           FD_SET(fd, &rfd);
87           continue;
88         }
89
90       case BlockedOnWrite:
91         { 
92           int fd = tso->block_info.fd;
93           maxfd = (fd > maxfd) ? fd : maxfd;
94           FD_SET(fd, &wfd);
95           continue;
96         }
97
98       case BlockedOnDelay:
99         {
100           if ((int)tso->block_info.delay < min)
101             min = tso->block_info.delay;
102           continue;
103         }
104
105       default:
106         barf("AwaitEvent");
107       }
108     }
109
110     /* Release the scheduler lock while we do the poll.
111      * this means that someone might muck with the blocked_queue
112      * while we do this, but it shouldn't matter:
113      *
114      *   - another task might poll for I/O and remove one
115      *     or more threads from the blocked_queue.
116      *   - more I/O threads may be added to blocked_queue.
117      *   - more delayed threads may be added to blocked_queue. We'll
118      *     just subtract delta from their delays after the poll.
119      *
120      * I believe none of these cases lead to trouble --SDM.
121      */
122     RELEASE_LOCK(&sched_mutex);
123
124     /* Check for any interesting events */
125
126     tv.tv_sec = min / 1000000;
127     tv.tv_usec = min % 1000000;
128
129 #ifndef linux_TARGET_OS
130     gettimeofday(&tv_before, (struct timezone *) NULL);
131 #endif
132
133     while ((numFound = select(maxfd+1, &rfd, &wfd, NULL, &tv)) < 0) {
134       if (errno != EINTR) {
135         /* fflush(stdout); */
136         fprintf(stderr, "awaitEvent: select failed\n");
137         stg_exit(EXIT_FAILURE);
138       }
139       ACQUIRE_LOCK(&sched_mutex);
140       /* We got a signal; could be one of ours.  If so, we need
141        * to start up the signal handler straight away, otherwise
142        * we could block for a long time before the signal is
143        * serviced.
144        */
145       if (signals_pending()) {
146         start_signal_handlers();
147         return;
148       }
149
150       /* If new runnable threads have arrived, stop waiting for
151        * I/O and run them.
152        */
153       if (run_queue_hd != END_TSO_QUEUE) {
154         return;
155       }
156       RELEASE_LOCK(&sched_mutex);
157     }   
158
159     if (numFound != 0) { 
160       /* 
161         File descriptors ready, but we don't know how much time was spent
162         in the select(). To interpolate, we compare the time before
163         and after the select(). 
164       */
165
166 #ifdef linux_TARGET_OS
167       /* on Linux, tv is set to indicate the amount of time not
168        * slept, so we don't need to gettimeofday() to find out.
169        */
170       delta += min - (tv.tv_sec * 1000000 + tv.tv_usec);
171 #else
172       gettimeofday(&tv_after, (struct timezone *) NULL);
173       delta += (tv_after.tv_sec - tv_before.tv_sec) * 1000000 +
174                 tv_after.tv_usec - tv_before.tv_usec;
175 #endif
176     } else {
177       delta += min;
178     }
179
180     ACQUIRE_LOCK(&sched_mutex);
181
182     /*
183       Step through the waiting queue, unblocking every thread that now has
184       a file descriptor in a ready state.
185
186       For the delayed threads, decrement the number of microsecs
187       we've been blocked for. Unblock the threads that have thusly expired.
188      */
189
190     prev = NULL;
191     for(tso = blocked_queue_hd; tso != END_TSO_QUEUE; tso = next) {
192       next = tso->link;
193       switch (tso->why_blocked) {
194       case BlockedOnRead:
195         ready = FD_ISSET(tso->block_info.fd, &rfd);
196         break;
197         
198       case BlockedOnWrite:
199         ready = FD_ISSET(tso->block_info.fd, &wfd);
200         break;
201         
202       case BlockedOnDelay:
203         tso->block_info.delay -= delta;
204         ready = (tso->block_info.delay <= 0);
205         break;
206         
207       default:
208         barf("awaitEvent");
209       }
210       
211       if (ready) {
212         IF_DEBUG(scheduler,belch("Waking up thread %d\n", tso->id));
213         tso->why_blocked = NotBlocked;
214         tso->link = END_TSO_QUEUE;
215         PUSH_ON_RUN_QUEUE(tso);
216       } else {
217         if (prev == NULL)
218           blocked_queue_hd = tso;
219         else
220           prev->link = tso;
221         prev = tso;
222       }
223     }
224
225     if (prev == NULL)
226       blocked_queue_hd = blocked_queue_tl = END_TSO_QUEUE;
227     else {
228       prev->link = END_TSO_QUEUE;
229       blocked_queue_tl = prev;
230     }
231 #endif
232 }