From fa75aa0854eeb043bc452042cc4c4b4ab69f731a Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Mon, 11 Feb 2013 12:49:56 +1300 Subject: [PATCH] Fix Win32 event use. Incorrect use of the Ctrl+C event can lead to busy-waiting in socket and serial port IO. --- util/ctrlc.h | 4 ++++ util/sockets.c | 5 +++++ util/sport.c | 2 ++ 3 files changed, 11 insertions(+) diff --git a/util/ctrlc.h b/util/ctrlc.h index 2fbda20..a24a283 100644 --- a/util/ctrlc.h +++ b/util/ctrlc.h @@ -60,6 +60,10 @@ void ctrlc_raise(void); * object which becomes signalled when the Ctrl+C event is raised. * Implementations of Windows IO operations should allow operations to * be interrupted by the signalling of this object. + * + * The event can be manually cleared before IO operations, but this + * doesn't clear the recorded Ctrl+C event. If the event is manually + * cleared, the Ctrl+C event status should be checked *after* doing so. */ HANDLE ctrlc_win32_event(void); #endif diff --git a/util/sockets.c b/util/sockets.c index 0b498be..0b110d5 100644 --- a/util/sockets.c +++ b/util/sockets.c @@ -37,6 +37,11 @@ static void sockets_begin(SOCKET s, DWORD event) ioctlsocket(s, FIONBIO, &mode); WSAEventSelect(s, ctrlc_win32_event(), event); + + /* We explicitly check for Ctrl+C after resetting the event in + * sockets_wait(). + */ + ResetEvent(ctrlc_win32_event()); } static int sockets_wait(DWORD timeout) diff --git a/util/sport.c b/util/sport.c index fb48515..1cd16e2 100644 --- a/util/sport.c +++ b/util/sport.c @@ -256,6 +256,8 @@ static int xfer_wait(sport_t s, LPOVERLAPPED ovl) { DWORD result = 0; + ResetEvent(ctrlc_win32_event()); + while (!GetOverlappedResult(s, ovl, &result, FALSE)) { DWORD r;