3 * Integrating Win32 asynchronous I/O with the GHC RTS.
12 #include "Capability.h"
13 #include "win32/AsyncIO.h"
14 #include "win32/IOManager.h"
19 * Haskell code issue asynchronous I/O requests via the
20 * async{Read,Write,DoOp}# primops. These cause addIORequest()
21 * to be invoked, which forwards the request to the underlying
22 * asynchronous I/O subsystem. Each request is tagged with a unique
25 * addIORequest() returns this ID, so that when the blocked CH
26 * thread is added onto blocked_queue, its TSO is annotated with
27 * it. Upon completion of an I/O request, the async I/O handling
28 * code makes a back-call to signal its completion; the local
29 * onIOComplete() routine. It adds the IO request ID (along with
30 * its result data) to a queue of completed requests before returning.
32 * The queue of completed IO request is read by the thread operating
33 * the RTS scheduler. It de-queues the CH threads corresponding
34 * to the request IDs, making them runnable again.
38 typedef struct CompletedReq {
44 #define MAX_REQUESTS 200
46 static CRITICAL_SECTION queue_lock;
47 static HANDLE completed_req_event;
48 static HANDLE abandon_req_wait;
49 static HANDLE wait_handles[2];
50 static CompletedReq completedTable[MAX_REQUESTS];
51 static int completed_hw;
52 static int issued_reqs;
55 onIOComplete(unsigned int reqID,
61 /* Deposit result of request in queue/table */
62 EnterCriticalSection(&queue_lock);
63 if (completed_hw == MAX_REQUESTS) {
65 fprintf(stderr, "Request table overflow (%d); dropping.\n", reqID);
69 fprintf(stderr, "onCompl: %d %d %d %d %d\n",
70 reqID, len, errCode, issued_reqs, completed_hw);
73 completedTable[completed_hw].reqID = reqID;
74 completedTable[completed_hw].len = len;
75 completedTable[completed_hw].errCode = errCode;
78 if (completed_hw == 1) {
79 /* The event is used to wake up the scheduler thread should it
80 * be blocked waiting for requests to complete. The event resets once
81 * that thread has cleared out the request queue/table.
83 SetEvent(completed_req_event);
86 LeaveCriticalSection(&queue_lock);
96 EnterCriticalSection(&queue_lock);
98 LeaveCriticalSection(&queue_lock);
100 fprintf(stderr, "addIOReq: %d %d %d\n", fd, forWriting, len); fflush(stderr);
102 return AddIORequest(fd,forWriting,isSock,len,buf,onIOComplete);
106 addDelayRequest(int msecs)
108 EnterCriticalSection(&queue_lock);
110 LeaveCriticalSection(&queue_lock);
112 fprintf(stderr, "addDelayReq: %d\n", msecs); fflush(stderr);
114 return AddDelayRequest(msecs,onIOComplete);
118 addDoProcRequest(void* proc, void* param)
120 EnterCriticalSection(&queue_lock);
122 LeaveCriticalSection(&queue_lock);
124 fprintf(stderr, "addProcReq: %p %p\n", proc, param); fflush(stderr);
126 return AddProcRequest(proc,param,onIOComplete);
133 if (!StartIOManager()) {
136 InitializeCriticalSection(&queue_lock);
137 /* Create a pair of events:
139 * - completed_req_event -- signals the deposit of request result; manual reset.
140 * - abandon_req_wait -- external OS thread tells current RTS/Scheduler
141 * thread to abandon wait for IO request completion.
144 completed_req_event = CreateEvent (NULL, TRUE, FALSE, NULL);
145 abandon_req_wait = CreateEvent (NULL, FALSE, FALSE, NULL);
146 wait_handles[0] = completed_req_event;
147 wait_handles[1] = abandon_req_wait;
149 return ( completed_req_event != INVALID_HANDLE_VALUE &&
150 abandon_req_wait != INVALID_HANDLE_VALUE );
156 CloseHandle(completed_req_event);
161 * Function: awaitRequests(wait)
163 * Check for the completion of external IO work requests. Worker
164 * threads signal completion of IO requests by depositing them
165 * in a table (completedTable). awaitRequests() matches up
166 * requests in that table with threads on the blocked_queue,
167 * making the threads whose IO requests have completed runnable
170 * awaitRequests() is called by the scheduler periodically _or_ if
171 * it is out of work, and need to wait for the completion of IO
172 * requests to make further progress. In the latter scenario,
173 * awaitRequests() will simply block waiting for worker threads
174 * to complete if the 'completedTable' is empty.
177 awaitRequests(rtsBool wait)
181 fprintf(stderr, "awaitRequests(): %d %d %d\n", issued_reqs, completed_hw, wait);
184 EnterCriticalSection(&queue_lock);
185 /* Nothing immediately available & we won't wait */
186 if ((!wait && completed_hw == 0)
188 // If we just return when wait==rtsFalse, we'll go into a busy
189 // wait loop, so I disabled this condition --SDM 18/12/2003
190 (issued_reqs == 0 && completed_hw == 0)
193 LeaveCriticalSection(&queue_lock);
196 if (completed_hw == 0) {
197 /* empty table, drop lock and wait */
198 LeaveCriticalSection(&queue_lock);
199 if ( wait && !interrupted ) {
200 DWORD dwRes = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
204 case WAIT_OBJECT_0 + 1:
208 DWORD dw = GetLastError();
209 fprintf(stderr, "awaitRequests: wait failed -- error code: %lu\n", dw); fflush(stderr);
213 fprintf(stderr, "awaitRequests: unexpected wait return code %lu\n", dwRes); fflush(stderr);
224 for (i=0; i < completed_hw; i++) {
225 /* For each of the completed requests, match up their Ids
226 * with those of the threads on the blocked_queue. If the
227 * thread that made the IO request has been subsequently
228 * killed (and removed from blocked_queue), no match will
229 * be found for that request Id.
231 * i.e., killing a Haskell thread doesn't attempt to cancel
232 * the IO request it is blocked on.
235 unsigned int rID = completedTable[i].reqID;
238 for(tso = blocked_queue_hd ; tso != END_TSO_QUEUE; prev = tso, tso = tso->link) {
240 switch(tso->why_blocked) {
243 case BlockedOnDoProc:
244 if (tso->block_info.async_result->reqID == rID) {
245 /* Found the thread blocked waiting on request; stodgily fill
246 * in its result block.
248 tso->block_info.async_result->len = completedTable[i].len;
249 tso->block_info.async_result->errCode = completedTable[i].errCode;
251 /* Drop the matched TSO from blocked_queue */
253 prev->link = tso->link;
255 blocked_queue_hd = tso->link;
257 if (blocked_queue_tl == tso) {
258 blocked_queue_tl = prev;
261 /* Terminates the run queue + this inner for-loop. */
262 tso->link = END_TSO_QUEUE;
263 tso->why_blocked = NotBlocked;
264 PUSH_ON_RUN_QUEUE(tso);
269 if (tso->why_blocked != NotBlocked) {
270 barf("awaitRequests: odd thread state");
277 ResetEvent(completed_req_event);
278 LeaveCriticalSection(&queue_lock);
284 * Function: abandonRequestWait()
286 * Wake up a thread that's blocked waiting for new IO requests
287 * to complete (via awaitRequests().)
290 abandonRequestWait( void )
292 /* the event is auto-reset, but in case there's no thread
293 * already waiting on the event, we want to return it to
294 * a non-signalled state.
296 * Careful! There is no synchronisation between
297 * abandonRequestWait and awaitRequest, which means that
298 * abandonRequestWait might be called just before a thread
299 * goes into a wait, and we miss the abandon signal. So we
300 * must SetEvent() here rather than PulseEvent() to ensure
301 * that the event isn't lost. We can re-optimise by resetting
302 * the event somewhere safe if we know the event has been
303 * properly serviced (see resetAbandon() below). --SDM 18/12/2003
305 SetEvent(abandon_req_wait);
309 resetAbandonRequestWait( void )
311 ResetEvent(abandon_req_wait);