QProcess: migrate to QDeadlineTimer
Replacing QElapsedTimer with QDeadlineTimer simplifies the code in waiting functions, which also improves readability. Change-Id: I56aedd356b547b6735ed0985dc81be706e292437 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
6f2dc8d7f2
commit
55245c769b
@ -89,7 +89,7 @@ QT_END_NAMESPACE
|
|||||||
#include "qprocess_p.h"
|
#include "qprocess_p.h"
|
||||||
|
|
||||||
#include <qbytearray.h>
|
#include <qbytearray.h>
|
||||||
#include <qelapsedtimer.h>
|
#include <qdeadlinetimer.h>
|
||||||
#include <qcoreapplication.h>
|
#include <qcoreapplication.h>
|
||||||
#include <qsocketnotifier.h>
|
#include <qsocketnotifier.h>
|
||||||
#include <qtimer.h>
|
#include <qtimer.h>
|
||||||
@ -1766,7 +1766,7 @@ bool QProcess::waitForStarted(int msecs)
|
|||||||
{
|
{
|
||||||
Q_D(QProcess);
|
Q_D(QProcess);
|
||||||
if (d->processState == QProcess::Starting)
|
if (d->processState == QProcess::Starting)
|
||||||
return d->waitForStarted(msecs);
|
return d->waitForStarted(QDeadlineTimer(msecs));
|
||||||
|
|
||||||
return d->processState == QProcess::Running;
|
return d->processState == QProcess::Running;
|
||||||
}
|
}
|
||||||
@ -1783,7 +1783,7 @@ bool QProcess::waitForReadyRead(int msecs)
|
|||||||
return false;
|
return false;
|
||||||
if (d->currentReadChannel == QProcess::StandardError && d->stderrChannel.closed)
|
if (d->currentReadChannel == QProcess::StandardError && d->stderrChannel.closed)
|
||||||
return false;
|
return false;
|
||||||
return d->waitForReadyRead(msecs);
|
return d->waitForReadyRead(QDeadlineTimer(msecs));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \reimp
|
/*! \reimp
|
||||||
@ -1793,16 +1793,15 @@ bool QProcess::waitForBytesWritten(int msecs)
|
|||||||
Q_D(QProcess);
|
Q_D(QProcess);
|
||||||
if (d->processState == QProcess::NotRunning)
|
if (d->processState == QProcess::NotRunning)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
QDeadlineTimer deadline(msecs);
|
||||||
if (d->processState == QProcess::Starting) {
|
if (d->processState == QProcess::Starting) {
|
||||||
QElapsedTimer stopWatch;
|
bool started = d->waitForStarted(deadline);
|
||||||
stopWatch.start();
|
|
||||||
bool started = waitForStarted(msecs);
|
|
||||||
if (!started)
|
if (!started)
|
||||||
return false;
|
return false;
|
||||||
msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return d->waitForBytesWritten(msecs);
|
return d->waitForBytesWritten(deadline);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -1829,16 +1828,15 @@ bool QProcess::waitForFinished(int msecs)
|
|||||||
Q_D(QProcess);
|
Q_D(QProcess);
|
||||||
if (d->processState == QProcess::NotRunning)
|
if (d->processState == QProcess::NotRunning)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
QDeadlineTimer deadline(msecs);
|
||||||
if (d->processState == QProcess::Starting) {
|
if (d->processState == QProcess::Starting) {
|
||||||
QElapsedTimer stopWatch;
|
bool started = d->waitForStarted(deadline);
|
||||||
stopWatch.start();
|
|
||||||
bool started = waitForStarted(msecs);
|
|
||||||
if (!started)
|
if (!started)
|
||||||
return false;
|
return false;
|
||||||
msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return d->waitForFinished(msecs);
|
return d->waitForFinished(deadline);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -57,6 +57,7 @@
|
|||||||
#include "QtCore/qhash.h"
|
#include "QtCore/qhash.h"
|
||||||
#include "QtCore/qmap.h"
|
#include "QtCore/qmap.h"
|
||||||
#include "QtCore/qshareddata.h"
|
#include "QtCore/qshareddata.h"
|
||||||
|
#include "QtCore/qdeadlinetimer.h"
|
||||||
#include "private/qiodevice_p.h"
|
#include "private/qiodevice_p.h"
|
||||||
|
|
||||||
QT_REQUIRE_CONFIG(processenvironment);
|
QT_REQUIRE_CONFIG(processenvironment);
|
||||||
@ -369,10 +370,10 @@ public:
|
|||||||
QProcess::ExitStatus exitStatus = QProcess::NormalExit;
|
QProcess::ExitStatus exitStatus = QProcess::NormalExit;
|
||||||
bool crashed = false;
|
bool crashed = false;
|
||||||
|
|
||||||
bool waitForStarted(int msecs = 30000);
|
bool waitForStarted(const QDeadlineTimer &deadline);
|
||||||
bool waitForReadyRead(int msecs = 30000);
|
bool waitForReadyRead(const QDeadlineTimer &deadline);
|
||||||
bool waitForBytesWritten(int msecs = 30000);
|
bool waitForBytesWritten(const QDeadlineTimer &deadline);
|
||||||
bool waitForFinished(int msecs = 30000);
|
bool waitForFinished(const QDeadlineTimer &deadline);
|
||||||
|
|
||||||
qint64 bytesAvailableInChannel(const Channel *channel) const;
|
qint64 bytesAvailableInChannel(const Channel *channel) const;
|
||||||
qint64 readFromChannel(const Channel *channel, char *data, qint64 maxlen);
|
qint64 readFromChannel(const Channel *channel, char *data, qint64 maxlen);
|
||||||
|
@ -104,7 +104,6 @@ QT_END_NAMESPACE
|
|||||||
#include <qmutex.h>
|
#include <qmutex.h>
|
||||||
#include <qsocketnotifier.h>
|
#include <qsocketnotifier.h>
|
||||||
#include <qthread.h>
|
#include <qthread.h>
|
||||||
#include <qelapsedtimer.h>
|
|
||||||
|
|
||||||
#ifdef Q_OS_QNX
|
#ifdef Q_OS_QNX
|
||||||
# include <sys/neutrino.h>
|
# include <sys/neutrino.h>
|
||||||
@ -152,7 +151,7 @@ struct QProcessPoller
|
|||||||
{
|
{
|
||||||
QProcessPoller(const QProcessPrivate &proc);
|
QProcessPoller(const QProcessPrivate &proc);
|
||||||
|
|
||||||
int poll(int timeout);
|
int poll(const QDeadlineTimer &deadline);
|
||||||
|
|
||||||
pollfd &stdinPipe() { return pfds[0]; }
|
pollfd &stdinPipe() { return pfds[0]; }
|
||||||
pollfd &stdoutPipe() { return pfds[1]; }
|
pollfd &stdoutPipe() { return pfds[1]; }
|
||||||
@ -183,10 +182,10 @@ QProcessPoller::QProcessPoller(const QProcessPrivate &proc)
|
|||||||
childStartedPipe().fd = proc.childStartedPipe[0];
|
childStartedPipe().fd = proc.childStartedPipe[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
int QProcessPoller::poll(int timeout)
|
int QProcessPoller::poll(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
const nfds_t nfds = (childStartedPipe().fd == -1) ? 4 : 5;
|
const nfds_t nfds = (childStartedPipe().fd == -1) ? 4 : 5;
|
||||||
return qt_poll_msecs(pfds, nfds, timeout);
|
return qt_poll_msecs(pfds, nfds, deadline.remainingTime());
|
||||||
}
|
}
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
|
||||||
@ -697,11 +696,12 @@ void QProcessPrivate::killProcess()
|
|||||||
::kill(pid_t(pid), SIGKILL);
|
::kill(pid_t(pid), SIGKILL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForStarted(int msecs)
|
bool QProcessPrivate::waitForStarted(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
|
const qint64 msecs = deadline.remainingTime();
|
||||||
#if defined (QPROCESS_DEBUG)
|
#if defined (QPROCESS_DEBUG)
|
||||||
qDebug("QProcessPrivate::waitForStarted(%d) waiting for child to start (fd = %d)", msecs,
|
qDebug("QProcessPrivate::waitForStarted(%lld) waiting for child to start (fd = %d)",
|
||||||
childStartedPipe[0]);
|
msecs, childStartedPipe[0]);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pollfd pfd = qt_make_pollfd(childStartedPipe[0], POLLIN);
|
pollfd pfd = qt_make_pollfd(childStartedPipe[0], POLLIN);
|
||||||
@ -709,7 +709,7 @@ bool QProcessPrivate::waitForStarted(int msecs)
|
|||||||
if (qt_poll_msecs(&pfd, 1, msecs) == 0) {
|
if (qt_poll_msecs(&pfd, 1, msecs) == 0) {
|
||||||
setError(QProcess::Timedout);
|
setError(QProcess::Timedout);
|
||||||
#if defined (QPROCESS_DEBUG)
|
#if defined (QPROCESS_DEBUG)
|
||||||
qDebug("QProcessPrivate::waitForStarted(%d) == false (timed out)", msecs);
|
qDebug("QProcessPrivate::waitForStarted(%lld) == false (timed out)", msecs);
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -721,20 +721,16 @@ bool QProcessPrivate::waitForStarted(int msecs)
|
|||||||
return startedEmitted;
|
return startedEmitted;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForReadyRead(int msecs)
|
bool QProcessPrivate::waitForReadyRead(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
#if defined (QPROCESS_DEBUG)
|
#if defined (QPROCESS_DEBUG)
|
||||||
qDebug("QProcessPrivate::waitForReadyRead(%d)", msecs);
|
qDebug("QProcessPrivate::waitForReadyRead(%lld)", deadline.remainingTime());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QElapsedTimer stopWatch;
|
|
||||||
stopWatch.start();
|
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
QProcessPoller poller(*this);
|
QProcessPoller poller(*this);
|
||||||
|
|
||||||
int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
|
int ret = poller.poll(deadline);
|
||||||
int ret = poller.poll(timeout);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
break;
|
break;
|
||||||
@ -775,20 +771,16 @@ bool QProcessPrivate::waitForReadyRead(int msecs)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForBytesWritten(int msecs)
|
bool QProcessPrivate::waitForBytesWritten(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
#if defined (QPROCESS_DEBUG)
|
#if defined (QPROCESS_DEBUG)
|
||||||
qDebug("QProcessPrivate::waitForBytesWritten(%d)", msecs);
|
qDebug("QProcessPrivate::waitForBytesWritten(%lld)", deadline.remainingTime());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QElapsedTimer stopWatch;
|
|
||||||
stopWatch.start();
|
|
||||||
|
|
||||||
while (!writeBuffer.isEmpty()) {
|
while (!writeBuffer.isEmpty()) {
|
||||||
QProcessPoller poller(*this);
|
QProcessPoller poller(*this);
|
||||||
|
|
||||||
int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
|
int ret = poller.poll(deadline);
|
||||||
int ret = poller.poll(timeout);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
break;
|
break;
|
||||||
@ -826,20 +818,16 @@ bool QProcessPrivate::waitForBytesWritten(int msecs)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForFinished(int msecs)
|
bool QProcessPrivate::waitForFinished(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
#if defined (QPROCESS_DEBUG)
|
#if defined (QPROCESS_DEBUG)
|
||||||
qDebug("QProcessPrivate::waitForFinished(%d)", msecs);
|
qDebug("QProcessPrivate::waitForFinished(%lld)", deadline.remainingTime());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QElapsedTimer stopWatch;
|
|
||||||
stopWatch.start();
|
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
QProcessPoller poller(*this);
|
QProcessPoller poller(*this);
|
||||||
|
|
||||||
int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
|
int ret = poller.poll(deadline);
|
||||||
int ret = poller.poll(timeout);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
break;
|
break;
|
||||||
|
@ -46,7 +46,6 @@
|
|||||||
|
|
||||||
#include <qdatetime.h>
|
#include <qdatetime.h>
|
||||||
#include <qdir.h>
|
#include <qdir.h>
|
||||||
#include <qelapsedtimer.h>
|
|
||||||
#include <qfileinfo.h>
|
#include <qfileinfo.h>
|
||||||
#include <qrandom.h>
|
#include <qrandom.h>
|
||||||
#include <qwineventnotifier.h>
|
#include <qwineventnotifier.h>
|
||||||
@ -657,7 +656,7 @@ void QProcessPrivate::killProcess()
|
|||||||
TerminateProcess(pid->hProcess, 0xf291);
|
TerminateProcess(pid->hProcess, 0xf291);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForStarted(int)
|
bool QProcessPrivate::waitForStarted(const QDeadlineTimer &)
|
||||||
{
|
{
|
||||||
if (processStarted())
|
if (processStarted())
|
||||||
return true;
|
return true;
|
||||||
@ -695,9 +694,9 @@ bool QProcessPrivate::drainOutputPipes()
|
|||||||
return someReadyReadEmitted;
|
return someReadyReadEmitted;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForReadyRead(int msecs)
|
bool QProcessPrivate::waitForReadyRead(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
QIncrementalSleepTimer timer(msecs);
|
QIncrementalSleepTimer timer(deadline.remainingTime());
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
if (!writeBuffer.isEmpty() && !_q_canWrite())
|
if (!writeBuffer.isEmpty() && !_q_canWrite())
|
||||||
@ -727,9 +726,9 @@ bool QProcessPrivate::waitForReadyRead(int msecs)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForBytesWritten(int msecs)
|
bool QProcessPrivate::waitForBytesWritten(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
QIncrementalSleepTimer timer(msecs);
|
QIncrementalSleepTimer timer(deadline.remainingTime());
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
bool pendingDataInPipe = stdinChannel.writer && stdinChannel.writer->bytesToWrite();
|
bool pendingDataInPipe = stdinChannel.writer && stdinChannel.writer->bytesToWrite();
|
||||||
@ -785,13 +784,13 @@ bool QProcessPrivate::waitForBytesWritten(int msecs)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QProcessPrivate::waitForFinished(int msecs)
|
bool QProcessPrivate::waitForFinished(const QDeadlineTimer &deadline)
|
||||||
{
|
{
|
||||||
#if defined QPROCESS_DEBUG
|
#if defined QPROCESS_DEBUG
|
||||||
qDebug("QProcessPrivate::waitForFinished(%d)", msecs);
|
qDebug("QProcessPrivate::waitForFinished(%lld)", deadline.remainingTime());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QIncrementalSleepTimer timer(msecs);
|
QIncrementalSleepTimer timer(deadline.remainingTime());
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
if (!writeBuffer.isEmpty() && !_q_canWrite())
|
if (!writeBuffer.isEmpty() && !_q_canWrite())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user