Patch from Gert-Jan Vons: create an event handle to go in the
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Mon, 12 Jan 2009 20:41:28 +0000 (20:41 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Mon, 12 Jan 2009 20:41:28 +0000 (20:41 +0000)
OVERLAPPED structure in output threads, as we already do for input
threads. This apparently sorts out a hanging issue with serial ports
when trying to do simultaneous read and write, because (GJV says,
and it sounds plausible to me) in the absence of that event object
Windows signals the file handle itself to notify GetOverlappedResult
that it can return - and since the file handle might be being
signalled by a read operation instead, that leads to ambiguity.
Using an explicit event object in both directions means Windows
always knows which way the data is going.

Also a trivial fix in handle_output_new(), which was referencing the
wrong element of a union due to a copy and paste error. (Since the
result was address-taken and cast to void *, this wasn't a
functional error, but it was conceptually wrong.)

git-svn-id: svn://svn.tartarus.org/sgt/putty@8410 cda61777-01e9-0310-a592-d414129be87e

windows/winhandl.c

index f300962..c0e8232 100644 (file)
@@ -262,12 +262,15 @@ static DWORD WINAPI handle_output_threadfunc(void *param)
 {
     struct handle_output *ctx = (struct handle_output *) param;
     OVERLAPPED ovl, *povl;
+    HANDLE oev;
     int writeret;
 
-    if (ctx->flags & HANDLE_FLAG_OVERLAPPED)
+    if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {
        povl = &ovl;
-    else
+       oev = CreateEvent(NULL, TRUE, FALSE, NULL);
+    } else {
        povl = NULL;
+    }
 
     while (1) {
        WaitForSingleObject(ctx->ev_from_main, INFINITE);
@@ -275,8 +278,11 @@ static DWORD WINAPI handle_output_threadfunc(void *param)
            SetEvent(ctx->ev_to_main);
            break;
        }
-       if (povl)
+       if (povl) {
            memset(povl, 0, sizeof(OVERLAPPED));
+           povl->hEvent = oev;
+       }
+
        writeret = WriteFile(ctx->h, ctx->buffer, ctx->len,
                             &ctx->lenwritten, povl);
        if (!writeret)
@@ -297,6 +303,9 @@ static DWORD WINAPI handle_output_threadfunc(void *param)
            break;
     }
 
+    if (povl)
+       CloseHandle(oev);
+
     return 0;
 }
 
@@ -407,7 +416,7 @@ struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,
     add234(handles_by_evtomain, h);
 
     CreateThread(NULL, 0, handle_output_threadfunc,
-                &h->u.i, 0, &out_threadid);
+                &h->u.o, 0, &out_threadid);
 
     return h;
 }