From 3ec9e6ec87e5db3fb7fcc7b6a6cd41219d41cf93 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Tue, 12 Sep 2017 07:41:39 +0100 Subject: Limit how long cRoot::InputThread may block (#4019) Limit how long cRoot::InputThread may block Only calls `std::getline` when there is input available which removes the need to "notify" the input thread. Fixes #2494 and fixes #3177 --- src/Root.cpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Root.cpp b/src/Root.cpp index 1de5d9b7c..9c92a10b9 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -82,11 +82,35 @@ cRoot::~cRoot() void cRoot::InputThread(cRoot & a_Params) { cLogCommandOutputCallback Output; + AString Command; while (a_Params.m_InputThreadRunFlag.test_and_set() && std::cin.good()) { - AString Command; + #ifndef _WIN32 + static const std::chrono::microseconds PollPeriod = std::chrono::milliseconds{ 100 }; + + timeval Timeout{ 0, 0 }; + Timeout.tv_usec = PollPeriod.count(); + + fd_set ReadSet; + FD_ZERO(&ReadSet); + FD_SET(STDIN_FILENO, &ReadSet); + + if (select(STDIN_FILENO + 1, &ReadSet, nullptr, nullptr, &Timeout) <= 0) + { + // Don't call getline because there's nothing to read + continue; + } + #endif + std::getline(std::cin, Command); + + if (!a_Params.m_InputThreadRunFlag.test_and_set()) + { + // Already shutting down, can't execute commands + break; + } + if (!Command.empty()) { // Execute and clear command string when submitted @@ -323,15 +347,7 @@ void cRoot::Start(std::unique_ptr a_OverridesRepo) m_InputThread.join(); } #else - if (m_InputThread.get_id() != std::thread::id()) - { - if (pthread_kill(m_InputThread.native_handle(), SIGKILL) != 0) - { - LOGWARN("Couldn't notify the input thread; the server will hang before shutdown!"); - m_TerminateEventRaised = true; - m_InputThread.detach(); - } - } + m_InputThread.join(); #endif if (m_TerminateEventRaised) -- cgit v1.2.3