From 2b39219367cd1384116b343ec3e14a459927414f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20L=C3=B6hr?= Date: Sun, 8 Nov 2015 00:43:08 +0100 Subject: [PATCH] Remove explicit Wait on Overlap Event GetOverlappedResult also waits on the event internally (See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683209(v=vs.85).aspx). So instead of explicitly waiting for it before, use its wait. To distinguish between cancel and complete, the status of the request is used, which is saved in the "Internal" member (See https://msdn.microsoft.com/en-us/library/windows/desktop/ms684342(v=vs.85).aspx). --- Source/Core/Core/HW/WiimoteReal/IOWin.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Source/Core/Core/HW/WiimoteReal/IOWin.cpp b/Source/Core/Core/HW/WiimoteReal/IOWin.cpp index f9fce8f22c..f9f38b80df 100644 --- a/Source/Core/Core/HW/WiimoteReal/IOWin.cpp +++ b/Source/Core/Core/HW/WiimoteReal/IOWin.cpp @@ -642,29 +642,26 @@ int _IORead(HANDLE &dev_handle, OVERLAPPED &hid_overlap_read, u8* buf, int index if (ERROR_IO_PENDING == read_err) { - auto const wait_result = WaitForSingleObject(hid_overlap_read.hEvent, INFINITE); - - // In case the event was signalled by IOWakeup before the read completed, cancel it. - CancelIo(dev_handle); - - if (WAIT_FAILED == wait_result) - { - WARN_LOG(WIIMOTE, "A wait error occurred on reading from Wiimote %i.", index + 1); - } - - if (!GetOverlappedResult(dev_handle, &hid_overlap_read, &bytes, FALSE)) + if (!GetOverlappedResult(dev_handle, &hid_overlap_read, &bytes, TRUE)) { auto const overlapped_err = GetLastError(); + // In case it was aborted by someone else if (ERROR_OPERATION_ABORTED == overlapped_err) { - // It was. return -1; } WARN_LOG(WIIMOTE, "GetOverlappedResult error %d on Wiimote %i.", overlapped_err, index + 1); return 0; } + // If IOWakeup sets the event so GetOverlappedResult returns prematurely, but the request is still pending + else if (hid_overlap_read.Internal == STATUS_PENDING) + { + // Don't forget to cancel it. + CancelIo(dev_handle); + return -1; + } } else {