From 0830e5018560ef084baf351452151d8777119312 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 5 Nov 2021 14:55:10 +0100 Subject: [PATCH] Fix "forever" timeout in QtcProcess::waitFor... methods Change-Id: I57aac503599fa94f530e073164b86b5247702ce5 Reviewed-by: Alessandro Portale --- src/libs/utils/launchersocket.cpp | 16 +++++----------- src/libs/utils/launchersocket.h | 3 ++- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/libs/utils/launchersocket.cpp b/src/libs/utils/launchersocket.cpp index fc7b865e676..1dc3b45e328 100644 --- a/src/libs/utils/launchersocket.cpp +++ b/src/libs/utils/launchersocket.cpp @@ -30,7 +30,6 @@ #include "qtcassert.h" #include -#include #include #include @@ -542,14 +541,11 @@ bool CallerHandle::isCalledFromLaunchersThread() const bool LauncherHandle::waitForSignal(int msecs, CallerHandle::SignalType newSignal) { QTC_ASSERT(!isCalledFromLaunchersThread(), return false); - QElapsedTimer timer; - timer.start(); + QDeadlineTimer deadline(msecs); while (true) { - const int remainingMsecs = msecs - timer.elapsed(); - if (remainingMsecs <= 0) + if (deadline.hasExpired()) break; - const bool timedOut = !doWaitForSignal(qMax(remainingMsecs, 0), newSignal); - if (timedOut) + if (!doWaitForSignal(deadline, newSignal)) break; m_awaitingShouldContinue = true; // TODO: make it recursive? const QList flushedSignals = m_callerHandle->flushFor(newSignal); @@ -563,14 +559,12 @@ bool LauncherHandle::waitForSignal(int msecs, CallerHandle::SignalType newSignal return true; if (wasCanceled) return true; // or false? is false only in case of timeout? - if (timer.hasExpired(msecs)) - break; } return false; } // Called from caller's thread exclusively. -bool LauncherHandle::doWaitForSignal(int msecs, CallerHandle::SignalType newSignal) +bool LauncherHandle::doWaitForSignal(QDeadlineTimer deadline, CallerHandle::SignalType newSignal) { QMutexLocker locker(&m_mutex); QTC_ASSERT(isCalledFromCallersThread(), return false); @@ -585,7 +579,7 @@ bool LauncherHandle::doWaitForSignal(int msecs, CallerHandle::SignalType newSign return true; m_waitingFor = newSignal; - const bool ret = m_waitCondition.wait(&m_mutex, msecs); + const bool ret = m_waitCondition.wait(&m_mutex, deadline); m_waitingFor = CallerHandle::SignalType::NoSignal; return ret; } diff --git a/src/libs/utils/launchersocket.h b/src/libs/utils/launchersocket.h index 9708cf0430c..42b5fa757e8 100644 --- a/src/libs/utils/launchersocket.h +++ b/src/libs/utils/launchersocket.h @@ -28,6 +28,7 @@ #include "launcherpackets.h" #include "processutils.h" +#include #include #include #include @@ -213,7 +214,7 @@ public: private: // Called from caller's thread exclusively. - bool doWaitForSignal(int msecs, CallerHandle::SignalType newSignal); + bool doWaitForSignal(QDeadlineTimer deadline, CallerHandle::SignalType newSignal); // Called from launcher's thread exclusively. Call me with mutex locked. void wakeUpIfWaitingFor(CallerHandle::SignalType newSignal);