fix up Win32 build
[ghc-hetmet.git] / ghc / rts / win32 / AsyncIO.c
1 /* AsyncIO.c
2  *
3  * Integrating Win32 asynchronous I/O with the GHC RTS.
4  *
5  * (c) sof, 2002-2003.
6  */
7 #include "Rts.h"
8 #include "RtsUtils.h"
9 #include <windows.h>
10 #include <stdio.h>
11 #include "Schedule.h"
12 #include "RtsFlags.h"
13 #include "Capability.h"
14 #include "win32/AsyncIO.h"
15 #include "win32/IOManager.h"
16
17 /*
18  * Overview:
19  *
20  * Haskell code issue asynchronous I/O requests via the 
21  * async{Read,Write,DoOp}# primops. These cause addIORequest()
22  * to be invoked, which forwards the request to the underlying
23  * asynchronous I/O subsystem. Each request is tagged with a unique
24  * ID.
25  *
26  * addIORequest() returns this ID, so that when the blocked CH
27  * thread is added onto blocked_queue, its TSO is annotated with
28  * it. Upon completion of an I/O request, the async I/O handling
29  * code makes a back-call to signal its completion; the local
30  * onIOComplete() routine. It adds the IO request ID (along with
31  * its result data) to a queue of completed requests before returning. 
32  *
33  * The queue of completed IO request is read by the thread operating
34  * the RTS scheduler. It de-queues the CH threads corresponding
35  * to the request IDs, making them runnable again.
36  *
37  */
38
39 typedef struct CompletedReq {
40     unsigned int   reqID;
41     int            len;
42     int            errCode;
43 } CompletedReq;
44
45 #define MAX_REQUESTS 200
46
47 static CRITICAL_SECTION queue_lock;
48 static HANDLE           completed_req_event;
49 static HANDLE           abandon_req_wait;
50 static HANDLE           wait_handles[2];
51 static CompletedReq     completedTable[MAX_REQUESTS];
52 static int              completed_hw;
53 static HANDLE           completed_table_sema;
54 static int              issued_reqs;
55
56 static void
57 onIOComplete(unsigned int reqID,
58              int   fd STG_UNUSED,
59              int   len,
60              void* buf STG_UNUSED,
61              int   errCode)
62 {
63     DWORD dwRes;
64     /* Deposit result of request in queue/table..when there's room. */
65     dwRes = WaitForSingleObject(completed_table_sema, INFINITE);
66     switch (dwRes) {
67     case WAIT_OBJECT_0:
68         break;
69     default:
70         /* Not likely */
71         fprintf(stderr, "onIOComplete: failed to grab table semaphore, dropping request 0x%x\n", reqID);
72         fflush(stderr);
73         return;
74     }
75     EnterCriticalSection(&queue_lock);
76     if (completed_hw == MAX_REQUESTS) {
77         /* Shouldn't happen */
78         fprintf(stderr, "onIOComplete: ERROR -- Request table overflow (%d); dropping.\n", reqID);
79         fflush(stderr);
80     } else {
81 #if 0
82         fprintf(stderr, "onCompl: %d %d %d %d %d\n", 
83                 reqID, len, errCode, issued_reqs, completed_hw); 
84         fflush(stderr);
85 #endif
86         completedTable[completed_hw].reqID   = reqID;
87         completedTable[completed_hw].len     = len;
88         completedTable[completed_hw].errCode = errCode;
89         completed_hw++;
90         issued_reqs--;
91         if (completed_hw == 1) {
92             /* The event is used to wake up the scheduler thread should it
93              * be blocked waiting for requests to complete. The event resets once
94              * that thread has cleared out the request queue/table.
95              */
96             SetEvent(completed_req_event);
97         }
98     }
99     LeaveCriticalSection(&queue_lock);
100 }
101
102 unsigned int
103 addIORequest(int   fd,
104              int   forWriting,
105              int   isSock,
106              int   len,
107              char* buf)
108 {
109     EnterCriticalSection(&queue_lock);
110     issued_reqs++;
111     LeaveCriticalSection(&queue_lock);
112 #if 0
113     fprintf(stderr, "addIOReq: %d %d %d\n", fd, forWriting, len); fflush(stderr);
114 #endif
115     return AddIORequest(fd,forWriting,isSock,len,buf,onIOComplete);
116 }
117
118 unsigned int
119 addDelayRequest(int msecs)
120 {
121     EnterCriticalSection(&queue_lock);
122     issued_reqs++;
123     LeaveCriticalSection(&queue_lock);
124 #if 0
125     fprintf(stderr, "addDelayReq: %d\n", msecs); fflush(stderr);
126 #endif
127     return AddDelayRequest(msecs,onIOComplete);
128 }
129
130 unsigned int
131 addDoProcRequest(void* proc, void* param)
132 {
133     EnterCriticalSection(&queue_lock);
134     issued_reqs++;
135     LeaveCriticalSection(&queue_lock);
136 #if 0
137     fprintf(stderr, "addProcReq: %p %p\n", proc, param); fflush(stderr);
138 #endif
139     return AddProcRequest(proc,param,onIOComplete);
140 }
141
142
143 int
144 startupAsyncIO()
145 {
146     if (!StartIOManager()) {
147         return 0;
148     }
149     InitializeCriticalSection(&queue_lock);
150     /* Create a pair of events:
151      *
152      *    - completed_req_event  -- signals the deposit of request result; manual reset.
153      *    - abandon_req_wait     -- external OS thread tells current RTS/Scheduler
154      *                              thread to abandon wait for IO request completion.
155      *                              Auto reset.
156      */
157     completed_req_event = CreateEvent (NULL, TRUE,  FALSE, NULL);
158     abandon_req_wait    = CreateEvent (NULL, FALSE, FALSE, NULL);
159     wait_handles[0] = completed_req_event;
160     wait_handles[1] = abandon_req_wait;
161     completed_hw = 0;
162     if ( !(completed_table_sema = CreateSemaphore (NULL, MAX_REQUESTS, MAX_REQUESTS, NULL)) ) {
163         DWORD rc = GetLastError();
164         fprintf(stderr, "startupAsyncIO: CreateSemaphore failed 0x%x\n", rc);
165         fflush(stderr);
166     }
167
168     return ( completed_req_event  != INVALID_HANDLE_VALUE &&
169              abandon_req_wait     != INVALID_HANDLE_VALUE &&
170              completed_table_sema != NULL );
171 }
172
173 void
174 shutdownAsyncIO()
175 {
176     CloseHandle(completed_req_event);
177     ShutdownIOManager();
178 }
179
180 /*
181  * Function: awaitRequests(wait)
182  *
183  * Check for the completion of external IO work requests. Worker
184  * threads signal completion of IO requests by depositing them
185  * in a table (completedTable). awaitRequests() matches up 
186  * requests in that table with threads on the blocked_queue, 
187  * making the threads whose IO requests have completed runnable
188  * again.
189  * 
190  * awaitRequests() is called by the scheduler periodically _or_ if
191  * it is out of work, and need to wait for the completion of IO
192  * requests to make further progress. In the latter scenario, 
193  * awaitRequests() will simply block waiting for worker threads 
194  * to complete if the 'completedTable' is empty.
195  */
196 int
197 awaitRequests(rtsBool wait)
198 {
199 #ifndef THREADED_RTS
200   // none of this is actually used in the threaded RTS
201
202 start:
203 #if 0
204     fprintf(stderr, "awaitRequests(): %d %d %d\n", issued_reqs, completed_hw, wait);
205     fflush(stderr);
206 #endif
207     EnterCriticalSection(&queue_lock);
208     /* Nothing immediately available & we won't wait */
209     if ((!wait && completed_hw == 0)
210 #if 0
211         // If we just return when wait==rtsFalse, we'll go into a busy
212         // wait loop, so I disabled this condition --SDM 18/12/2003
213         (issued_reqs == 0 && completed_hw == 0)
214 #endif
215         ) {
216         LeaveCriticalSection(&queue_lock);
217         return 0;
218     }
219     if (completed_hw == 0) {
220         /* empty table, drop lock and wait */
221         LeaveCriticalSection(&queue_lock);
222         if ( wait && sched_state == SCHED_RUNNING ) {
223             DWORD dwRes = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
224             switch (dwRes) {
225             case WAIT_OBJECT_0:
226                 /* a request was completed */
227                 break;
228             case WAIT_OBJECT_0 + 1:
229             case WAIT_TIMEOUT:
230                 /* timeout (unlikely) or told to abandon waiting */
231                 return 0;
232             case WAIT_FAILED: {
233                 DWORD dw = GetLastError();
234                 fprintf(stderr, "awaitRequests: wait failed -- error code: %lu\n", dw); fflush(stderr);
235                 return 0;
236             }
237             default:
238                 fprintf(stderr, "awaitRequests: unexpected wait return code %lu\n", dwRes); fflush(stderr);
239                 return 0;
240             }
241         } else {
242             return 0;
243         }
244         goto start;
245     } else {
246         int i;
247         StgTSO *tso, *prev;
248         
249         for (i=0; i < completed_hw; i++) {
250             /* For each of the completed requests, match up their Ids
251              * with those of the threads on the blocked_queue. If the
252              * thread that made the IO request has been subsequently
253              * killed (and removed from blocked_queue), no match will
254              * be found for that request Id. 
255              *
256              * i.e., killing a Haskell thread doesn't attempt to cancel
257              * the IO request it is blocked on.
258              *
259              */
260             unsigned int rID = completedTable[i].reqID;
261             
262             prev = NULL;
263             for(tso = blocked_queue_hd ; tso != END_TSO_QUEUE; prev = tso, tso = tso->link) {
264         
265                 switch(tso->why_blocked) {
266                 case BlockedOnRead:
267                 case BlockedOnWrite:
268                 case BlockedOnDoProc:
269                     if (tso->block_info.async_result->reqID == rID) {
270                         /* Found the thread blocked waiting on request; stodgily fill 
271                          * in its result block. 
272                          */
273                         tso->block_info.async_result->len = completedTable[i].len;
274                         tso->block_info.async_result->errCode = completedTable[i].errCode;
275                         
276                         /* Drop the matched TSO from blocked_queue */
277                         if (prev) {
278                             prev->link = tso->link;
279                         } else {
280                             blocked_queue_hd = tso->link;
281                         }
282                         if (blocked_queue_tl == tso) {
283                             blocked_queue_tl = prev ? prev : END_TSO_QUEUE;
284                         }
285                     
286                         /* Terminates the run queue + this inner for-loop. */
287                         tso->link = END_TSO_QUEUE;
288                         tso->why_blocked = NotBlocked;
289                         pushOnRunQueue(&MainCapability, tso);
290                         break;
291                     }
292                     break;
293                 default:
294                     if (tso->why_blocked != NotBlocked) {
295                         barf("awaitRequests: odd thread state");
296                     }
297                     break;
298                 }
299             }
300             /* Signal that there's completed table slots available */
301             if ( !ReleaseSemaphore(completed_table_sema, 1, NULL) ) {
302                 DWORD dw = GetLastError();
303                 fprintf(stderr, "awaitRequests: failed to signal semaphore (error code=0x%x)\n", dw);
304                 fflush(stderr);
305             }
306         }
307         completed_hw = 0;
308         ResetEvent(completed_req_event);
309         LeaveCriticalSection(&queue_lock);
310         return 1;
311     }
312 #endif /* !THREADED_RTS */
313 }
314
315 /*
316  * Function: abandonRequestWait()
317  *
318  * Wake up a thread that's blocked waiting for new IO requests
319  * to complete (via awaitRequests().)
320  */
321 void
322 abandonRequestWait( void )
323 {
324     /* the event is auto-reset, but in case there's no thread
325      * already waiting on the event, we want to return it to
326      * a non-signalled state.
327      *
328      * Careful!  There is no synchronisation between
329      * abandonRequestWait and awaitRequest, which means that
330      * abandonRequestWait might be called just before a thread
331      * goes into a wait, and we miss the abandon signal.  So we
332      * must SetEvent() here rather than PulseEvent() to ensure
333      * that the event isn't lost.  We can re-optimise by resetting
334      * the event somewhere safe if we know the event has been
335      * properly serviced (see resetAbandon() below).  --SDM 18/12/2003
336      */
337     SetEvent(abandon_req_wait);
338 }
339
340 void
341 resetAbandonRequestWait( void )
342 {
343     ResetEvent(abandon_req_wait);
344 }
345