Simplify waitForState() implementation

Change-Id: I7bbb06411d0c30ab931cb17b5a1b5940b51e978e
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2021-07-16 13:05:29 +02:00
parent 5572324a88
commit eca63813d5
2 changed files with 20 additions and 20 deletions

View File

@@ -198,28 +198,27 @@ void LauncherHandle::handleSocketError(const QString &message)
bool LauncherHandle::waitForState(int msecs, WaitingForState newState, QProcess::ProcessState targetState) bool LauncherHandle::waitForState(int msecs, WaitingForState newState, QProcess::ProcessState targetState)
{ {
bool ok = false; const bool ok = doWaitForState(msecs, newState, targetState);
if (ok)
m_callerHandle->flush();
return ok;
}
bool LauncherHandle::doWaitForState(int msecs, WaitingForState newState, QProcess::ProcessState targetState)
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
// TODO: ASSERT if we are in Idle state // TODO: ASSERT if we are in Idle state
if (m_canceled) // we don't want to wait if we have canceled it before (ASSERT it?) if (m_canceled) // we don't want to wait if we have canceled it before (ASSERT it?)
return false; return false;
if (m_processState == targetState) { // It may happen, than after calling start() and before calling waitForStarted() we might have
qDebug() << "THE TARGET STATE IS ALREADY REACHED"; // reached the Running or Finished state already. In this case we return true
ok = true; // and we are going to flush pending signals synchronously.
} else if (m_finished) { // it may happen, than after calling start() and before calling waitForStarted() we might have finished already if (m_processState == targetState || m_finished)
qDebug() << "THE PROCESS HAS ALREADY FINISHED"; return true;
ok = true;
}
if (!ok) {
m_waitingFor = newState; m_waitingFor = newState;
ok = m_waitCondition.wait(&m_mutex, msecs) && !m_failed; return m_waitCondition.wait(&m_mutex, msecs) && !m_failed;
}
}
if (ok) // since we are in caller's thread, m_callerHandle must be still valid
m_callerHandle->flush();
return ok;
} }
void LauncherHandle::cancel() void LauncherHandle::cancel()

View File

@@ -117,6 +117,7 @@ private:
}; };
// called from other thread // called from other thread
bool waitForState(int msecs, WaitingForState newState, QProcess::ProcessState targetState); bool waitForState(int msecs, WaitingForState newState, QProcess::ProcessState targetState);
bool doWaitForState(int msecs, WaitingForState newState, QProcess::ProcessState targetState);
void doStart(); void doStart();