[PATCH v2] Cygwin: console: Improve the performance of peek_console
Takashi Yano
takashi.yano@nifty.ne.jp
Tue Sep 15 08:42:45 GMT 2026
Previously, peek_console() used PeekConsoleInput() to confirm whether
the console input buffer has some input records. However, this need
attaching to the console if the process does not attach to the console.
To reduce that overhead, this patch uses WaitForSingleObject() with
console input handle instead. WaitForSingleObject() works without
attaching to the console, so this simplifies the peek_console()
code.
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
v2: Do not remove PeekConsoleInputW() call.
winsup/cygwin/select.cc | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 48f66c954..99c52311f 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1160,6 +1160,14 @@ peek_console (select_record *me, bool)
return me->read_ready = true;
else
{
+ if (WaitForSingleObject (h, 0) != WAIT_OBJECT_0)
+ {
+ fh->release_input_mutex ();
+ break;
+ }
+ /* Also check using PeekConsoleInputW(), because windows terminal
+ may leave input handle signalled when erasing non-key events.
+ https://github.com/microsoft/terminal/blob/main/src/host/inputBuffer.cpp#L347-L352 */
fh->acquire_input_mutex (mutex_timeout);
acquire_attach_mutex (mutex_timeout);
DWORD resume_pid = fh->attach_console (fh->get_owner ());
--
2.51.0
More information about the Cygwin-patches
mailing list