win32: use overlapped events for streams

Any file handle created with FLAG_OVERLAPPED must have
ReadFile()/WriteFile() called with an OVERLAPPED structure.
Failing to do so will give unspecified results, invalid read/write or
corruption.

Without FLAG_OVERLAPPED, it is not possible to read and write
concurrently, even with two seperate threads, created by 2 input and
output gio streams. Also, only with FLAG_OVERLAPPED may an IO
operation be asynchronous and thus be cancellable.

We may want to call ReOpenFile() to make sure the FLAG is set, but
this API is only available since Vista+.

According to MSDN doc, adding the OVERLAPPED argument for IO operation
on handles without FLAG_OVERLAPPED is allowed, and indeed the existing
test still passes.

v2:
- update GetLastError() after _g_win32_overlap_wait_result ()
- split the unrelated ERROR_MORE_DATA handling

https://bugzilla.gnome.org/show_bug.cgi?id=679288
This commit is contained in:
Marc-André Lureau
2012-07-02 21:45:41 +02:00
parent 96a0c589ee
commit 23d80a04da
4 changed files with 121 additions and 25 deletions

View File

@@ -168,3 +168,31 @@ _g_fd_source_new (int fd,
return source;
}
#ifdef G_OS_WIN32
gboolean
_g_win32_overlap_wait_result (HANDLE hfile,
OVERLAPPED *overlap,
DWORD *transferred,
GCancellable *cancellable G_GNUC_UNUSED)
{
GPollFD pollfd[1] = { 0, };
gboolean result = FALSE;
gint num, npoll;
pollfd[0].fd = (gint)overlap->hEvent;
pollfd[0].events = G_IO_IN;
num = 1;
npoll = g_poll (pollfd, num, -1);
if (npoll <= 0)
/* error out, should never happen */
goto end;
/* either cancelled or IO completed, either way get the result */
result = GetOverlappedResult (overlap->hEvent, overlap, transferred, TRUE);
end:
return result;
}
#endif