Skip to content

Commit 1de7bb9

Browse files
authored
Merge pull request #6007 from ab9rf/windows-console-take-2
different strategy for console exit hang
2 parents d2b2259 + eef15fe commit 1de7bb9

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

‎library/Console-windows.cpp‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
4646
#include <stdio.h>
4747
#include <fcntl.h>
4848
#include <io.h>
49+
#include <atomic>
4950
#include <iostream>
5051
#include <fstream>
5152
#include <istream>
@@ -73,7 +74,7 @@ namespace DFHack
7374
rawmode = 0;
7475
console_in = 0;
7576
console_out = 0;
76-
exit_event = 0;
77+
exiting = false;
7778
ConsoleWindow = 0;
7879
default_attributes = 0;
7980
state = con_unclaimed;
@@ -282,21 +283,17 @@ namespace DFHack
282283
inf.dwCursorPosition.X = (SHORT)plen;
283284
SetConsoleCursorPosition(console_out, inf.dwCursorPosition);
284285

285-
// exit_event comes first so shutdown wins if input is also
286-
// pending. The console input handle is signaled whenever its
287-
// input queue is non-empty, so the read below cannot block.
288-
const HANDLE wait_handles[] = { exit_event, console_in };
289286
while (1)
290287
{
291288
INPUT_RECORD rec;
292289
DWORD count;
293290
lock->unlock();
294-
DWORD wait_result = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
291+
// shutdown() queues a dummy input record to wake this read;
292+
// the exiting flag distinguishes the wake from real input.
293+
BOOL success = ReadConsoleInputA(console_in, &rec, 1, &count);
295294
lock->lock();
296-
if (wait_result != WAIT_OBJECT_0 + 1
297-
|| ReadConsoleInputA(console_in, &rec, 1, &count) == 0) {
295+
if (exiting || !success)
298296
return Console::SHUTDOWN;
299-
}
300297
if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown)
301298
continue;
302299
switch (rec.Event.KeyEvent.wVirtualKeyCode)
@@ -402,7 +399,7 @@ namespace DFHack
402399
int rawmode;
403400
HANDLE console_in;
404401
HANDLE console_out;
405-
HANDLE exit_event;
402+
std::atomic<bool> exiting;
406403
HWND ConsoleWindow;
407404
HWND MainWindow;
408405
WORD default_attributes;
@@ -486,9 +483,6 @@ bool Console::init(bool)
486483
d->console_owned = AllocConsole() != FALSE;
487484
d->ConsoleWindow = GetConsoleWindow();
488485
wlock = new std::recursive_mutex();
489-
d->exit_event = CreateEventW(NULL, TRUE, FALSE, NULL);
490-
if (!d->exit_event)
491-
return false;
492486
if (d->console_owned)
493487
{
494488
HMENU hm = GetSystemMenu(d->ConsoleWindow,false);
@@ -546,11 +540,15 @@ bool Console::init(bool)
546540
bool Console::shutdown(void)
547541
{
548542
assert(inited);
543+
d->exiting = true;
549544
std::lock_guard<std::recursive_mutex> lock{*wlock};
550-
// Wake any thread waiting on console input so it leaves prompt_loop
551-
// before we detach or reconfigure the console below.
552-
if (d->exit_event)
553-
SetEvent(d->exit_event);
545+
// Wake any thread blocked in ReadConsoleInputA by queueing a dummy
546+
// input record, so it is no longer inside a console call and can
547+
// observe the exiting flag before we detach the console below.
548+
INPUT_RECORD rec = { 0 };
549+
rec.EventType = FOCUS_EVENT;
550+
DWORD written;
551+
WriteConsoleInputA(d->console_in, &rec, 1, &written);
554552
if (d->console_owned)
555553
{
556554
FreeConsole();

0 commit comments

Comments
 (0)