2 * winhandl.c: Module to give Windows front ends the general
3 * ability to deal with consoles, pipes, serial ports, or any other
4 * type of data stream accessed through a Windows API HANDLE rather
5 * than a WinSock SOCKET.
7 * We do this by spawning a subthread to continuously try to read
8 * from the handle. Every time a read successfully returns some
9 * data, the subthread sets an event object which is picked up by
10 * the main thread, and the main thread then sets an event in
11 * return to instruct the subthread to resume reading.
13 * Output works precisely the other way round, in a second
14 * subthread. The output subthread should not be attempting to
15 * write all the time, because it hasn't always got data _to_
16 * write; so the output thread waits for an event object notifying
17 * it to _attempt_ a write, and then it sets an event in return
25 /* ----------------------------------------------------------------------
26 * Generic definitions.
30 * Maximum amount of backlog we will allow to build up on an input
31 * handle before we stop reading from it.
33 #define MAX_BACKLOG 32768
35 struct handle_generic
{
37 * Initial fields common to both handle_input and handle_output
40 * The three HANDLEs are set up at initialisation time and are
41 * thereafter read-only to both main thread and subthread.
42 * `moribund' is only used by the main thread; `done' is
43 * written by the main thread before signalling to the
44 * subthread. `defunct' and `busy' are used only by the main
47 HANDLE h
; /* the handle itself */
48 HANDLE ev_to_main
; /* event used to signal main thread */
49 HANDLE ev_from_main
; /* event used to signal back to us */
50 int moribund
; /* are we going to kill this soon? */
51 int done
; /* request subthread to terminate */
52 int defunct
; /* has the subthread already gone? */
53 int busy
; /* operation currently in progress? */
54 void *privdata
; /* for client to remember who they are */
57 /* ----------------------------------------------------------------------
62 * Data required by an input thread.
66 * Copy of the handle_generic structure.
68 HANDLE h
; /* the handle itself */
69 HANDLE ev_to_main
; /* event used to signal main thread */
70 HANDLE ev_from_main
; /* event used to signal back to us */
71 int moribund
; /* are we going to kill this soon? */
72 int done
; /* request subthread to terminate */
73 int defunct
; /* has the subthread already gone? */
74 int busy
; /* operation currently in progress? */
75 void *privdata
; /* for client to remember who they are */
78 * Data set by the input thread before signalling ev_to_main,
79 * and read by the main thread after receiving that signal.
81 char buffer
[4096]; /* the data read from the handle */
82 DWORD len
; /* how much data that was */
83 int readret
; /* lets us know about read errors */
86 * Callback function called by this module when data arrives on
89 handle_inputfn_t gotdata
;
93 * The actual thread procedure for an input thread.
95 static DWORD WINAPI
handle_input_threadfunc(void *param
)
97 struct handle_input
*ctx
= (struct handle_input
*) param
;
100 ctx
->readret
= ReadFile(ctx
->h
, ctx
->buffer
, sizeof(ctx
->buffer
),
105 SetEvent(ctx
->ev_to_main
);
110 WaitForSingleObject(ctx
->ev_from_main
, INFINITE
);
112 break; /* main thread told us to shut down */
119 * This is called after a succcessful read, or from the
120 * `unthrottle' function. It decides whether or not to begin a new
123 static void handle_throttle(struct handle_input
*ctx
, int backlog
)
129 * If there's a read operation already in progress, do nothing:
130 * when that completes, we'll come back here and be in a
131 * position to make a better decision.
137 * Otherwise, we must decide whether to start a new read based
138 * on the size of the backlog.
140 if (backlog
< MAX_BACKLOG
) {
141 SetEvent(ctx
->ev_from_main
);
146 /* ----------------------------------------------------------------------
151 * Data required by an output thread.
153 struct handle_output
{
155 * Copy of the handle_generic structure.
157 HANDLE h
; /* the handle itself */
158 HANDLE ev_to_main
; /* event used to signal main thread */
159 HANDLE ev_from_main
; /* event used to signal back to us */
160 int moribund
; /* are we going to kill this soon? */
161 int done
; /* request subthread to terminate */
162 int defunct
; /* has the subthread already gone? */
163 int busy
; /* operation currently in progress? */
164 void *privdata
; /* for client to remember who they are */
167 * Data set by the main thread before signalling ev_from_main,
168 * and read by the input thread after receiving that signal.
170 char *buffer
; /* the data to write */
171 DWORD len
; /* how much data there is */
174 * Data set by the input thread before signalling ev_to_main,
175 * and read by the main thread after receiving that signal.
177 DWORD lenwritten
; /* how much data we actually wrote */
178 int writeret
; /* return value from WriteFile */
181 * Data only ever read or written by the main thread.
183 bufchain queued_data
; /* data still waiting to be written */
186 * Callback function called when the backlog in the bufchain
189 handle_outputfn_t sentdata
;
192 static DWORD WINAPI
handle_output_threadfunc(void *param
)
194 struct handle_output
*ctx
= (struct handle_output
*) param
;
197 WaitForSingleObject(ctx
->ev_from_main
, INFINITE
);
199 SetEvent(ctx
->ev_to_main
);
202 ctx
->writeret
= WriteFile(ctx
->h
, ctx
->buffer
, ctx
->len
,
203 &ctx
->lenwritten
, NULL
);
204 SetEvent(ctx
->ev_to_main
);
212 static void handle_try_output(struct handle_output
*ctx
)
217 if (!ctx
->busy
&& bufchain_size(&ctx
->queued_data
)) {
218 bufchain_prefix(&ctx
->queued_data
, &senddata
, &sendlen
);
219 ctx
->buffer
= senddata
;
221 SetEvent(ctx
->ev_from_main
);
226 /* ----------------------------------------------------------------------
227 * Unified code handling both input and output threads.
233 struct handle_generic g
;
234 struct handle_input i
;
235 struct handle_output o
;
239 static tree234
*handles_by_evtomain
;
241 static int handle_cmp_evtomain(void *av
, void *bv
)
243 struct handle
*a
= (struct handle
*)av
;
244 struct handle
*b
= (struct handle
*)bv
;
246 if ((unsigned)a
->u
.g
.ev_to_main
< (unsigned)b
->u
.g
.ev_to_main
)
248 else if ((unsigned)a
->u
.g
.ev_to_main
> (unsigned)b
->u
.g
.ev_to_main
)
254 static int handle_find_evtomain(void *av
, void *bv
)
256 HANDLE
*a
= (HANDLE
*)av
;
257 struct handle
*b
= (struct handle
*)bv
;
259 if ((unsigned)*a
< (unsigned)b
->u
.g
.ev_to_main
)
261 else if ((unsigned)*a
> (unsigned)b
->u
.g
.ev_to_main
)
267 struct handle
*handle_input_new(HANDLE handle
, handle_inputfn_t gotdata
,
270 struct handle
*h
= snew(struct handle
);
274 h
->u
.i
.ev_to_main
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
275 h
->u
.i
.ev_from_main
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
276 h
->u
.i
.gotdata
= gotdata
;
277 h
->u
.i
.defunct
= FALSE
;
278 h
->u
.i
.moribund
= FALSE
;
280 h
->u
.i
.privdata
= privdata
;
282 if (!handles_by_evtomain
)
283 handles_by_evtomain
= newtree234(handle_cmp_evtomain
);
284 add234(handles_by_evtomain
, h
);
286 CreateThread(NULL
, 0, handle_input_threadfunc
,
293 struct handle
*handle_output_new(HANDLE handle
, handle_outputfn_t sentdata
,
296 struct handle
*h
= snew(struct handle
);
300 h
->u
.o
.ev_to_main
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
301 h
->u
.o
.ev_from_main
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
303 h
->u
.o
.defunct
= FALSE
;
304 h
->u
.o
.moribund
= FALSE
;
306 h
->u
.o
.privdata
= privdata
;
307 bufchain_init(&h
->u
.o
.queued_data
);
308 h
->u
.o
.sentdata
= sentdata
;
310 if (!handles_by_evtomain
)
311 handles_by_evtomain
= newtree234(handle_cmp_evtomain
);
312 add234(handles_by_evtomain
, h
);
314 CreateThread(NULL
, 0, handle_output_threadfunc
,
320 int handle_write(struct handle
*h
, const void *data
, int len
)
323 bufchain_add(&h
->u
.o
.queued_data
, data
, len
);
324 handle_try_output(&h
->u
.o
);
325 return bufchain_size(&h
->u
.o
.queued_data
);
328 HANDLE
*handle_get_events(int *nevents
)
335 * Go through our tree counting the handle objects currently
336 * engaged in useful activity.
340 if (handles_by_evtomain
) {
341 for (i
= 0; (h
= index234(handles_by_evtomain
, i
)) != NULL
; i
++) {
345 ret
= sresize(ret
, size
, HANDLE
);
347 ret
[n
++] = h
->u
.g
.ev_to_main
;
356 static void handle_destroy(struct handle
*h
)
359 bufchain_clear(&h
->u
.o
.queued_data
);
360 CloseHandle(h
->u
.g
.ev_from_main
);
361 CloseHandle(h
->u
.g
.ev_to_main
);
362 del234(handles_by_evtomain
, h
);
366 void handle_free(struct handle
*h
)
369 * If the handle is currently busy, we cannot immediately free
370 * it. Instead we must wait until it's finished its current
371 * operation, because otherwise the subthread will write to
372 * invalid memory after we free its context from under it.
374 assert(h
&& !h
->u
.g
.moribund
);
377 * Just set the moribund flag, which will be noticed next
378 * time an operation completes.
380 h
->u
.g
.moribund
= TRUE
;
381 } else if (h
->u
.g
.defunct
) {
383 * There isn't even a subthread; we can go straight to
389 * The subthread is alive but not busy, so we now signal it
390 * to die. Set the moribund flag to indicate that it will
391 * want destroying after that.
393 h
->u
.g
.moribund
= TRUE
;
396 SetEvent(h
->u
.g
.ev_from_main
);
400 void handle_got_event(HANDLE event
)
404 assert(handles_by_evtomain
);
405 h
= find234(handles_by_evtomain
, &event
, handle_find_evtomain
);
408 * This isn't an error condition. If two or more event
409 * objects were signalled during the same select operation,
410 * and processing of the first caused the second handle to
411 * be closed, then it will sometimes happen that we receive
412 * an event notification here for a handle which is already
413 * deceased. In that situation we simply do nothing.
418 if (h
->u
.g
.moribund
) {
420 * A moribund handle is already treated as dead from the
421 * external user's point of view, so do nothing with the
422 * actual event. Just signal the thread to die if
423 * necessary, or destroy the handle if not.
430 SetEvent(h
->u
.g
.ev_from_main
);
441 * A signal on an input handle means data has arrived.
443 if (h
->u
.i
.len
== 0) {
445 * EOF, or (nearly equivalently) read error.
447 h
->u
.i
.gotdata(h
, NULL
, (h
->u
.i
.readret ?
0 : -1));
448 h
->u
.i
.defunct
= TRUE
;
450 backlog
= h
->u
.i
.gotdata(h
, h
->u
.i
.buffer
, h
->u
.i
.len
);
451 handle_throttle(&h
->u
.i
, backlog
);
457 * A signal on an output handle means we have completed a
458 * write. Call the callback to indicate that the output
459 * buffer size has decreased, or to indicate an error.
461 if (!h
->u
.o
.writeret
) {
463 * Write error. Send a negative value to the callback,
464 * and mark the thread as defunct (because the output
465 * thread is terminating by now).
467 h
->u
.o
.sentdata(h
, -1);
468 h
->u
.o
.defunct
= TRUE
;
470 bufchain_consume(&h
->u
.o
.queued_data
, h
->u
.o
.lenwritten
);
471 h
->u
.o
.sentdata(h
, bufchain_size(&h
->u
.o
.queued_data
));
472 handle_try_output(&h
->u
.o
);
477 void handle_unthrottle(struct handle
*h
, int backlog
)
480 handle_throttle(&h
->u
.i
, backlog
);
483 int handle_backlog(struct handle
*h
)
486 return bufchain_size(&h
->u
.o
.queued_data
);
489 void *handle_get_privdata(struct handle
*h
)
491 return h
->u
.g
.privdata
;