AsyncIO.c 6.36 KB
Newer Older
sof's avatar
sof committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* AsyncIO.c
 *
 * Integrating Win32 asynchronous I/O with the GHC RTS.
 *
 * (c) sof, 2002-2003.
 */
#include "Rts.h"
#include <windows.h>
#include <stdio.h>
#include "Schedule.h"
#include "win32/AsyncIO.h"
#include "win32/IOManager.h"

/*
 * Overview:
 *
 * Haskell code issue asynchronous I/O requests via the 
 * asyncRead# and asyncWrite# primops. These cause addIORequest()
 * to be invoked, which forwards the request to the underlying
 * asynchronous I/O subsystem. Each request is tagged with a unique
 * ID.
 *
 * addIORequest() returns this ID, so that when the blocked CH
 * thread is added onto blocked_queue, its TSO is annotated with
 * it. Upon completion of an I/O request, the async I/O handling
 * code makes a back-call to signal its completion; the local
 * onIOComplete() routine. It adds the IO request ID (along with
 * its result data) to a queue of completed requests before returning. 
 *
 * The queue of completed IO request is read by the thread operating
 * the RTS scheduler. It de-queues the CH threads corresponding
 * to the request IDs, making them runnable again.
 *
 */

typedef struct CompletedReq {
  unsigned int   reqID;
  int            len;
  int            errCode;
} CompletedReq;

#define MAX_REQUESTS 200

static CRITICAL_SECTION queue_lock;
static HANDLE           completed_req_event;
sof's avatar
sof committed
46
47
static HANDLE           abandon_req_wait;
static HANDLE           wait_handles[2];
sof's avatar
sof committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
static CompletedReq     completedTable[MAX_REQUESTS];
static int              completed_hw;
static int              issued_reqs;

static void
onIOComplete(unsigned int reqID,
	     void* param STG_UNUSED,
	     int   fd STG_UNUSED,
	     int   len,
	     char* buf STG_UNUSED,
	     int   errCode)
{
  /* Deposit result of request in queue/table */
  EnterCriticalSection(&queue_lock);
  if (completed_hw == MAX_REQUESTS) {
    /* Not likely */
    fprintf(stderr, "Request table overflow (%d); dropping.\n", reqID);
    fflush(stderr);
  } else {
#if 0
    fprintf(stderr, "onCompl: %d %d %d %d %d\n", reqID, len, errCode, issued_reqs, completed_hw); fflush(stderr);
#endif
    completedTable[completed_hw].reqID   = reqID;
    completedTable[completed_hw].len     = len;
    completedTable[completed_hw].errCode = errCode;
    completed_hw++;
    issued_reqs--;
    if (completed_hw == 1) {
      /* The event is used to wake up the scheduler thread should it
       * be blocked waiting for requests to complete. It reset once
       * that thread has cleared out the request queue/table.
       */
      SetEvent(completed_req_event);
    }
  }
  LeaveCriticalSection(&queue_lock);
}

unsigned int
addIORequest(int   fd,
	     int   forWriting,
	     int   isSock,
	     int   len,
	     char* buf)
{
  EnterCriticalSection(&queue_lock);
  issued_reqs++;
  LeaveCriticalSection(&queue_lock);
#if 0
  fprintf(stderr, "addIOReq: %d %d %d\n", fd, forWriting, len); fflush(stderr);
#endif
  return AddIORequest(fd,forWriting,isSock,len,buf,0,onIOComplete);
}

sof's avatar
sof committed
102
103
104
105
106
107
108
109
110
111
112
113
unsigned int
addDelayRequest(int   msecs)
{
  EnterCriticalSection(&queue_lock);
  issued_reqs++;
  LeaveCriticalSection(&queue_lock);
#if 0
  fprintf(stderr, "addDelayReq: %d %d %d\n", msecs); fflush(stderr);
#endif
  return AddDelayRequest(msecs,0,onIOComplete);
}

sof's avatar
sof committed
114
115
116
117
118
119
120
int
startupAsyncIO()
{
  if (!StartIOManager()) {
    return 0;
  }
  InitializeCriticalSection(&queue_lock);
sof's avatar
sof committed
121
122
123
124
125
126
127
128
129
130
131
  /* Create a pair of events:
   *
   *    - completed_req_event  -- signals the deposit of request result; manual reset.
   *    - abandon_req_wait     -- external OS thread tells current RTS/Scheduler
   *                              thread to abandon wait for IO request completion.
   *                              Auto reset.
   */
  completed_req_event = CreateEvent (NULL, TRUE,  FALSE, NULL);
  abandon_req_wait    = CreateEvent (NULL, FALSE, FALSE, NULL);
  wait_handles[0] = completed_req_event;
  wait_handles[1] = abandon_req_wait;
sof's avatar
sof committed
132
  completed_hw = 0;
sof's avatar
sof committed
133
134
  return ( completed_req_event != INVALID_HANDLE_VALUE &&
	   abandon_req_wait    != INVALID_HANDLE_VALUE );
sof's avatar
sof committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
}

void
shutdownAsyncIO()
{
  CloseHandle(completed_req_event);
  ShutdownIOManager();
}

int
awaitRequests(rtsBool wait)
{
start:
#if 0
  fprintf(stderr, "awaitRequests: %d %d %d\n", issued_reqs, completed_hw, wait); fflush(stderr);
#endif
  EnterCriticalSection(&queue_lock);
  /* Nothing immediately available & we won't wait */
  if ((!wait && completed_hw == 0) || 
      (issued_reqs == 0 && completed_hw == 0)) {
    LeaveCriticalSection(&queue_lock);
    return 0;
  }
  if (completed_hw == 0) {
    /* empty table, drop lock and wait */
    LeaveCriticalSection(&queue_lock);
    if (wait) {
sof's avatar
sof committed
162
163
164
165
166
167
168
169
170
171
172
      DWORD dwRes = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
      switch (dwRes) {
      case WAIT_OBJECT_0:
	break;
      case WAIT_OBJECT_0 + 1:
      case WAIT_TIMEOUT:
	return 0;
      default:
	fprintf(stderr, "awaitRequests: unexpected wait return code %lu\n", dwRes); fflush(stderr);
	return 0;
      }
sof's avatar
sof committed
173
174
175
176
177
178
179
180
181
182
183
184
185
    } else {
      return 0; /* cannot happen */
    }
    goto start;
  } else {
    int i;
    StgTSO *tso, *prev;
    
    for (i=0; i < completed_hw; i++) {
      unsigned int rID = completedTable[i].reqID;
      prev = NULL;
      for(tso = blocked_queue_hd ; tso != END_TSO_QUEUE; tso = tso->link) {
	switch(tso->why_blocked) {
sof's avatar
sof committed
186
	case BlockedOnDelay:
sof's avatar
sof committed
187
188
189
190
191
192
	case BlockedOnRead:
	case BlockedOnWrite:
	  if (tso->block_info.async_result->reqID == rID) {
	    /* Found the thread blocked waiting on request; stodgily fill 
	     * in its result block. 
	     */
sof's avatar
sof committed
193
194
195
196
	    if (tso->why_blocked != BlockedOnDelay) {
	      tso->block_info.async_result->len = completedTable[i].len;
	      tso->block_info.async_result->errCode = completedTable[i].errCode;
	    }
sof's avatar
sof committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

	    /* Drop the matched TSO from blocked_queue */
	    if (prev) {
	      prev->link = tso->link;
	    } else {
	      blocked_queue_hd = tso->link;
	    }
	    if (blocked_queue_tl == tso) {
	      blocked_queue_tl = prev;
	    }
	    /* Terminates the run queue + this inner for-loop. */
	    tso->link = END_TSO_QUEUE;
	    tso->why_blocked = NotBlocked;
	    PUSH_ON_RUN_QUEUE(tso);
	    break;
	  }
	  break;
	default:
	  break;
	}
	prev = tso;
      }
    }
    completed_hw = 0;
    ResetEvent(completed_req_event);
    LeaveCriticalSection(&queue_lock);
    return 1;
  }
}
sof's avatar
sof committed
226
227
228
229
230
231
232
233
234
235

void
abandonRequestWait()
{
  /* the event is auto-reset, but in case there's no thread
   * already waiting on the event, we want to return it to
   * a non-signalled state.
   */
  PulseEvent(abandon_req_wait);
}