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
20 * (It's terribly annoying having to spawn a subthread for each
21 * direction of each handle. Technically it isn't necessary for
22 * serial ports, since we could use overlapped I/O within the main
23 * thread and wait directly on the event objects in the OVERLAPPED
24 * structures. However, we can't use this trick for some types of
25 * file handle at all - for some reason Windows restricts use of
26 * OVERLAPPED to files which were opened with the overlapped flag -
27 * and so we must use threads for those. This being the case, it's
28 * simplest just to use threads for everything rather than trying
29 * to keep track of multiple completely separate mechanisms.)
36 /* ----------------------------------------------------------------------
37 * Generic definitions.
41 * Maximum amount of backlog we will allow to build up on an input
42 * handle before we stop reading from it.
44 #define MAX_BACKLOG 32768
46 struct handle_generic {
48 * Initial fields common to both handle_input and handle_output
51 * The three HANDLEs are set up at initialisation time and are
52 * thereafter read-only to both main thread and subthread.
53 * `moribund' is only used by the main thread; `done' is
54 * written by the main thread before signalling to the
55 * subthread. `defunct' and `busy' are used only by the main
58 HANDLE h; /* the handle itself */
59 HANDLE ev_to_main; /* event used to signal main thread */
60 HANDLE ev_from_main; /* event used to signal back to us */
61 int moribund; /* are we going to kill this soon? */
62 int done; /* request subthread to terminate */
63 int defunct; /* has the subthread already gone? */
64 int busy; /* operation currently in progress? */
65 void *privdata; /* for client to remember who they are */
68 typedef enum { HT_INPUT, HT_OUTPUT, HT_FOREIGN } HandleType;
70 /* ----------------------------------------------------------------------
75 * Data required by an input thread.
79 * Copy of the handle_generic structure.
81 HANDLE h; /* the handle itself */
82 HANDLE ev_to_main; /* event used to signal main thread */
83 HANDLE ev_from_main; /* event used to signal back to us */
84 int moribund; /* are we going to kill this soon? */
85 int done; /* request subthread to terminate */
86 int defunct; /* has the subthread already gone? */
87 int busy; /* operation currently in progress? */
88 void *privdata; /* for client to remember who they are */
91 * Data set at initialisation and then read-only.
96 * Data set by the input thread before signalling ev_to_main,
97 * and read by the main thread after receiving that signal.
99 char buffer[4096]; /* the data read from the handle */
100 DWORD len; /* how much data that was */
101 int readerr; /* lets us know about read errors */
104 * Callback function called by this module when data arrives on
107 handle_inputfn_t gotdata;
111 * The actual thread procedure for an input thread.
113 static DWORD WINAPI handle_input_threadfunc(void *param)
115 struct handle_input *ctx = (struct handle_input *) param;
116 OVERLAPPED ovl, *povl;
118 int readret, readlen, finished;
120 if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {
122 oev = CreateEvent(NULL, TRUE, FALSE, NULL);
127 if (ctx->flags & HANDLE_FLAG_UNITBUFFER)
130 readlen = sizeof(ctx->buffer);
134 memset(povl, 0, sizeof(OVERLAPPED));
137 readret = ReadFile(ctx->h, ctx->buffer,readlen, &ctx->len, povl);
139 ctx->readerr = GetLastError();
142 if (povl && !readret && ctx->readerr == ERROR_IO_PENDING) {
143 WaitForSingleObject(povl->hEvent, INFINITE);
144 readret = GetOverlappedResult(ctx->h, povl, &ctx->len, FALSE);
146 ctx->readerr = GetLastError();
153 * Windows apparently sends ERROR_BROKEN_PIPE when a
154 * pipe we're reading from is closed normally from the
155 * writing end. This is ludicrous; if that situation
156 * isn't a natural EOF, _nothing_ is. So if we get that
157 * particular error, we pretend it's EOF.
159 if (ctx->readerr == ERROR_BROKEN_PIPE)
164 if (readret && ctx->len == 0 &&
165 (ctx->flags & HANDLE_FLAG_IGNOREEOF))
169 * If we just set ctx->len to 0, that means the read operation
170 * has returned end-of-file. Telling that to the main thread
171 * will cause it to set its 'defunct' flag and dispose of the
172 * handle structure at the next opportunity, in which case we
173 * mustn't touch ctx at all after the SetEvent. (Hence we do
174 * even _this_ check before the SetEvent.)
176 finished = (ctx->len == 0);
178 SetEvent(ctx->ev_to_main);
183 WaitForSingleObject(ctx->ev_from_main, INFINITE);
186 * The main thread has asked us to shut down. Send back an
187 * event indicating that we've done so. Hereafter we must
188 * not touch ctx at all, because the main thread might
191 SetEvent(ctx->ev_to_main);
203 * This is called after a succcessful read, or from the
204 * `unthrottle' function. It decides whether or not to begin a new
207 static void handle_throttle(struct handle_input *ctx, int backlog)
213 * If there's a read operation already in progress, do nothing:
214 * when that completes, we'll come back here and be in a
215 * position to make a better decision.
221 * Otherwise, we must decide whether to start a new read based
222 * on the size of the backlog.
224 if (backlog < MAX_BACKLOG) {
225 SetEvent(ctx->ev_from_main);
230 /* ----------------------------------------------------------------------
235 * Data required by an output thread.
237 struct handle_output {
239 * Copy of the handle_generic structure.
241 HANDLE h; /* the handle itself */
242 HANDLE ev_to_main; /* event used to signal main thread */
243 HANDLE ev_from_main; /* event used to signal back to us */
244 int moribund; /* are we going to kill this soon? */
245 int done; /* request subthread to terminate */
246 int defunct; /* has the subthread already gone? */
247 int busy; /* operation currently in progress? */
248 void *privdata; /* for client to remember who they are */
251 * Data set at initialisation and then read-only.
256 * Data set by the main thread before signalling ev_from_main,
257 * and read by the input thread after receiving that signal.
259 char *buffer; /* the data to write */
260 DWORD len; /* how much data there is */
263 * Data set by the input thread before signalling ev_to_main,
264 * and read by the main thread after receiving that signal.
266 DWORD lenwritten; /* how much data we actually wrote */
267 int writeerr; /* return value from WriteFile */
270 * Data only ever read or written by the main thread.
272 bufchain queued_data; /* data still waiting to be written */
273 enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
276 * Callback function called when the backlog in the bufchain
279 handle_outputfn_t sentdata;
282 static DWORD WINAPI handle_output_threadfunc(void *param)
284 struct handle_output *ctx = (struct handle_output *) param;
285 OVERLAPPED ovl, *povl;
289 if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {
291 oev = CreateEvent(NULL, TRUE, FALSE, NULL);
297 WaitForSingleObject(ctx->ev_from_main, INFINITE);
300 * The main thread has asked us to shut down. Send back an
301 * event indicating that we've done so. Hereafter we must
302 * not touch ctx at all, because the main thread might
305 SetEvent(ctx->ev_to_main);
309 memset(povl, 0, sizeof(OVERLAPPED));
313 writeret = WriteFile(ctx->h, ctx->buffer, ctx->len,
314 &ctx->lenwritten, povl);
316 ctx->writeerr = GetLastError();
319 if (povl && !writeret && GetLastError() == ERROR_IO_PENDING) {
320 writeret = GetOverlappedResult(ctx->h, povl,
321 &ctx->lenwritten, TRUE);
323 ctx->writeerr = GetLastError();
328 SetEvent(ctx->ev_to_main);
331 * The write operation has suffered an error. Telling that
332 * to the main thread will cause it to set its 'defunct'
333 * flag and dispose of the handle structure at the next
334 * opportunity, so we must not touch ctx at all after
347 static void handle_try_output(struct handle_output *ctx)
352 if (!ctx->busy && bufchain_size(&ctx->queued_data)) {
353 bufchain_prefix(&ctx->queued_data, &senddata, &sendlen);
354 ctx->buffer = senddata;
356 SetEvent(ctx->ev_from_main);
358 } else if (!ctx->busy && bufchain_size(&ctx->queued_data) == 0 &&
359 ctx->outgoingeof == EOF_PENDING) {
361 ctx->h = INVALID_HANDLE_VALUE;
362 ctx->outgoingeof = EOF_SENT;
366 /* ----------------------------------------------------------------------
367 * 'Foreign events'. These are handle structures which just contain a
368 * single event object passed to us by another module such as
369 * winnps.c, so that they can make use of our handle_get_events /
370 * handle_got_event mechanism for communicating with application main
373 struct handle_foreign {
375 * Copy of the handle_generic structure.
377 HANDLE h; /* the handle itself */
378 HANDLE ev_to_main; /* event used to signal main thread */
379 HANDLE ev_from_main; /* event used to signal back to us */
380 int moribund; /* are we going to kill this soon? */
381 int done; /* request subthread to terminate */
382 int defunct; /* has the subthread already gone? */
383 int busy; /* operation currently in progress? */
384 void *privdata; /* for client to remember who they are */
387 * Our own data, just consisting of knowledge of who to call back.
389 void (*callback)(void *);
393 /* ----------------------------------------------------------------------
394 * Unified code handling both input and output threads.
400 struct handle_generic g;
401 struct handle_input i;
402 struct handle_output o;
403 struct handle_foreign f;
407 static tree234 *handles_by_evtomain;
409 static int handle_cmp_evtomain(void *av, void *bv)
411 struct handle *a = (struct handle *)av;
412 struct handle *b = (struct handle *)bv;
414 if ((uintptr_t)a->u.g.ev_to_main < (uintptr_t)b->u.g.ev_to_main)
416 else if ((uintptr_t)a->u.g.ev_to_main > (uintptr_t)b->u.g.ev_to_main)
422 static int handle_find_evtomain(void *av, void *bv)
424 HANDLE *a = (HANDLE *)av;
425 struct handle *b = (struct handle *)bv;
427 if ((uintptr_t)*a < (uintptr_t)b->u.g.ev_to_main)
429 else if ((uintptr_t)*a > (uintptr_t)b->u.g.ev_to_main)
435 struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,
436 void *privdata, int flags)
438 struct handle *h = snew(struct handle);
439 DWORD in_threadid; /* required for Win9x */
443 h->u.i.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL);
444 h->u.i.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL);
445 h->u.i.gotdata = gotdata;
446 h->u.i.defunct = FALSE;
447 h->u.i.moribund = FALSE;
449 h->u.i.privdata = privdata;
450 h->u.i.flags = flags;
452 if (!handles_by_evtomain)
453 handles_by_evtomain = newtree234(handle_cmp_evtomain);
454 add234(handles_by_evtomain, h);
456 CreateThread(NULL, 0, handle_input_threadfunc,
457 &h->u.i, 0, &in_threadid);
463 struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
464 void *privdata, int flags)
466 struct handle *h = snew(struct handle);
467 DWORD out_threadid; /* required for Win9x */
471 h->u.o.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL);
472 h->u.o.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL);
474 h->u.o.defunct = FALSE;
475 h->u.o.moribund = FALSE;
477 h->u.o.privdata = privdata;
478 bufchain_init(&h->u.o.queued_data);
479 h->u.o.outgoingeof = EOF_NO;
480 h->u.o.sentdata = sentdata;
481 h->u.o.flags = flags;
483 if (!handles_by_evtomain)
484 handles_by_evtomain = newtree234(handle_cmp_evtomain);
485 add234(handles_by_evtomain, h);
487 CreateThread(NULL, 0, handle_output_threadfunc,
488 &h->u.o, 0, &out_threadid);
493 struct handle *handle_add_foreign_event(HANDLE event,
494 void (*callback)(void *), void *ctx)
496 struct handle *h = snew(struct handle);
498 h->type = HT_FOREIGN;
499 h->u.f.h = INVALID_HANDLE_VALUE;
500 h->u.f.ev_to_main = event;
501 h->u.f.ev_from_main = INVALID_HANDLE_VALUE;
502 h->u.f.defunct = TRUE; /* we have no thread in the first place */
503 h->u.f.moribund = FALSE;
505 h->u.f.privdata = NULL;
506 h->u.f.callback = callback;
510 if (!handles_by_evtomain)
511 handles_by_evtomain = newtree234(handle_cmp_evtomain);
512 add234(handles_by_evtomain, h);
517 int handle_write(struct handle *h, const void *data, int len)
519 assert(h->type == HT_OUTPUT);
520 assert(h->u.o.outgoingeof == EOF_NO);
521 bufchain_add(&h->u.o.queued_data, data, len);
522 handle_try_output(&h->u.o);
523 return bufchain_size(&h->u.o.queued_data);
526 void handle_write_eof(struct handle *h)
529 * This function is called when we want to proactively send an
530 * end-of-file notification on the handle. We can only do this by
531 * actually closing the handle - so never call this on a
532 * bidirectional handle if we're still interested in its incoming
535 assert(h->type == HT_OUTPUT);
536 if (!h->u.o.outgoingeof == EOF_NO) {
537 h->u.o.outgoingeof = EOF_PENDING;
538 handle_try_output(&h->u.o);
542 HANDLE *handle_get_events(int *nevents)
549 * Go through our tree counting the handle objects currently
550 * engaged in useful activity.
554 if (handles_by_evtomain) {
555 for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) {
559 ret = sresize(ret, size, HANDLE);
561 ret[n++] = h->u.g.ev_to_main;
570 static void handle_destroy(struct handle *h)
572 if (h->type == HT_OUTPUT)
573 bufchain_clear(&h->u.o.queued_data);
574 CloseHandle(h->u.g.ev_from_main);
575 CloseHandle(h->u.g.ev_to_main);
576 del234(handles_by_evtomain, h);
580 void handle_free(struct handle *h)
582 assert(h && !h->u.g.moribund);
583 if (h->u.g.busy && h->type != HT_FOREIGN) {
585 * If the handle is currently busy, we cannot immediately free
586 * it, because its subthread is in the middle of something.
587 * (Exception: foreign handles don't have a subthread.)
589 * Instead we must wait until it's finished its current
590 * operation, because otherwise the subthread will write to
591 * invalid memory after we free its context from under it. So
592 * we set the moribund flag, which will be noticed next time
593 * an operation completes.
595 h->u.g.moribund = TRUE;
596 } else if (h->u.g.defunct) {
598 * There isn't even a subthread; we can go straight to
604 * The subthread is alive but not busy, so we now signal it
605 * to die. Set the moribund flag to indicate that it will
606 * want destroying after that.
608 h->u.g.moribund = TRUE;
611 SetEvent(h->u.g.ev_from_main);
615 void handle_got_event(HANDLE event)
619 assert(handles_by_evtomain);
620 h = find234(handles_by_evtomain, &event, handle_find_evtomain);
623 * This isn't an error condition. If two or more event
624 * objects were signalled during the same select operation,
625 * and processing of the first caused the second handle to
626 * be closed, then it will sometimes happen that we receive
627 * an event notification here for a handle which is already
628 * deceased. In that situation we simply do nothing.
633 if (h->u.g.moribund) {
635 * A moribund handle is one which we have either already
636 * signalled to die, or are waiting until its current I/O op
637 * completes to do so. Either way, it's treated as already
638 * dead from the external user's point of view, so we ignore
639 * the actual I/O result. We just signal the thread to die if
640 * we haven't yet done so, or destroy the handle if not.
647 SetEvent(h->u.g.ev_from_main);
659 * A signal on an input handle means data has arrived.
661 if (h->u.i.len == 0) {
663 * EOF, or (nearly equivalently) read error.
665 h->u.i.defunct = TRUE;
666 h->u.i.gotdata(h, NULL, -h->u.i.readerr);
668 backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len);
669 handle_throttle(&h->u.i, backlog);
677 * A signal on an output handle means we have completed a
678 * write. Call the callback to indicate that the output
679 * buffer size has decreased, or to indicate an error.
681 if (h->u.o.writeerr) {
683 * Write error. Send a negative value to the callback,
684 * and mark the thread as defunct (because the output
685 * thread is terminating by now).
687 h->u.o.defunct = TRUE;
688 h->u.o.sentdata(h, -h->u.o.writeerr);
690 bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten);
691 h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data));
692 handle_try_output(&h->u.o);
697 /* Just call the callback. */
698 h->u.f.callback(h->u.f.ctx);
703 void handle_unthrottle(struct handle *h, int backlog)
705 assert(h->type == HT_INPUT);
706 handle_throttle(&h->u.i, backlog);
709 int handle_backlog(struct handle *h)
711 assert(h->type == HT_OUTPUT);
712 return bufchain_size(&h->u.o.queued_data);
715 void *handle_get_privdata(struct handle *h)
717 return h->u.g.privdata;