From 2e7899f477b498af1724ed98d61752f26fd1da49 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 20 Jun 2016 15:27:23 -0700 Subject: Port QWinOverlappedIoNotifier to QDeadlineTimer Change-Id: Ib57b52598e2f452985e9fffd1459eb18587c99e3 Reviewed-by: Joerg Bornemann --- src/serialport/qserialport_p.h | 3 ++- src/serialport/qserialport_win.cpp | 18 +++++++----------- src/serialport/qwinoverlappedionotifier.cpp | 26 ++++++++++++-------------- src/serialport/qwinoverlappedionotifier_p.h | 5 +++-- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h index a742e0f..0e4f8bd 100644 --- a/src/serialport/qserialport_p.h +++ b/src/serialport/qserialport_p.h @@ -56,6 +56,7 @@ #include "qserialport.h" #include +#include #if defined(Q_OS_WIN32) # include @@ -181,7 +182,7 @@ public: bool setDcb(DCB *dcb); bool getDcb(DCB *dcb); - OVERLAPPED *waitForNotified(int msecs); + OVERLAPPED *waitForNotified(QDeadlineTimer deadline); qint64 queuedBytesCount(QSerialPort::Direction direction) const; diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp index 7ae40d9..f90a7ef 100644 --- a/src/serialport/qserialport_win.cpp +++ b/src/serialport/qserialport_win.cpp @@ -344,12 +344,10 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs) const qint64 initialReadBufferSize = buffer.size(); qint64 currentReadBufferSize = initialReadBufferSize; - QElapsedTimer stopWatch; - stopWatch.start(); + QDeadlineTimer deadline(msecs); do { - const OVERLAPPED *overlapped = waitForNotified( - qt_subtract_from_timeout(msecs, stopWatch.elapsed())); + const OVERLAPPED *overlapped = waitForNotified(deadline); if (!overlapped) return false; @@ -365,7 +363,7 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs) } } - } while (msecs == -1 || qt_subtract_from_timeout(msecs, stopWatch.elapsed()) > 0); + } while (!deadline.hasExpired()); return false; } @@ -378,12 +376,10 @@ bool QSerialPortPrivate::waitForBytesWritten(int msecs) if (!writeStarted && !_q_startAsyncWrite()) return false; - QElapsedTimer stopWatch; - stopWatch.start(); + QDeadlineTimer deadline(msecs); for (;;) { - const OVERLAPPED *overlapped = waitForNotified( - qt_subtract_from_timeout(msecs, stopWatch.elapsed())); + const OVERLAPPED *overlapped = waitForNotified(deadline); if (!overlapped) return false; @@ -636,9 +632,9 @@ qint64 QSerialPortPrivate::writeData(const char *data, qint64 maxSize) return maxSize; } -OVERLAPPED *QSerialPortPrivate::waitForNotified(int msecs) +OVERLAPPED *QSerialPortPrivate::waitForNotified(QDeadlineTimer deadline) { - OVERLAPPED *overlapped = notifier->waitForAnyNotified(msecs); + OVERLAPPED *overlapped = notifier->waitForAnyNotified(deadline); if (!overlapped) { setError(getSystemError(WAIT_TIMEOUT)); return nullptr; diff --git a/src/serialport/qwinoverlappedionotifier.cpp b/src/serialport/qwinoverlappedionotifier.cpp index d7745ae..6ec7463 100644 --- a/src/serialport/qwinoverlappedionotifier.cpp +++ b/src/serialport/qwinoverlappedionotifier.cpp @@ -122,7 +122,7 @@ public: { } - OVERLAPPED *waitForAnyNotified(int msecs); + OVERLAPPED *waitForAnyNotified(QDeadlineTimer deadline); void notify(DWORD numberOfBytes, DWORD errorCode, OVERLAPPED *overlapped); void _q_notified(); OVERLAPPED *dispatchNextIoResult(); @@ -312,17 +312,20 @@ void QWinOverlappedIoNotifier::setEnabled(bool enabled) d->iocp->unregisterNotifier(d); } -OVERLAPPED *QWinOverlappedIoNotifierPrivate::waitForAnyNotified(int msecs) +OVERLAPPED *QWinOverlappedIoNotifierPrivate::waitForAnyNotified(QDeadlineTimer deadline) { if (!iocp->isRunning()) { qWarning("Called QWinOverlappedIoNotifier::waitForAnyNotified on inactive notifier."); return 0; } + DWORD msecs = deadline.remainingTime(); if (msecs == 0) iocp->drainQueue(); + if (msecs == -1) + msecs = INFINITE; - const DWORD wfso = WaitForSingleObject(hSemaphore, msecs == -1 ? INFINITE : DWORD(msecs)); + const DWORD wfso = WaitForSingleObject(hSemaphore, msecs); switch (wfso) { case WAIT_OBJECT_0: return dispatchNextIoResult(); @@ -359,11 +362,11 @@ private: * operation. In case no I/O operation was completed during the \a msec timeout, this function * returns a null pointer. */ -OVERLAPPED *QWinOverlappedIoNotifier::waitForAnyNotified(int msecs) +OVERLAPPED *QWinOverlappedIoNotifier::waitForAnyNotified(QDeadlineTimer deadline) { Q_D(QWinOverlappedIoNotifier); QScopedAtomicIntIncrementor saii(d->waiting); - OVERLAPPED *result = d->waitForAnyNotified(msecs); + OVERLAPPED *result = d->waitForAnyNotified(deadline); return result; } @@ -373,23 +376,18 @@ OVERLAPPED *QWinOverlappedIoNotifier::waitForAnyNotified(int msecs) * The function returns true if the notified signal was emitted for * the I/O operation that corresponds to the OVERLAPPED object. */ -bool QWinOverlappedIoNotifier::waitForNotified(int msecs, OVERLAPPED *overlapped) +bool QWinOverlappedIoNotifier::waitForNotified(QDeadlineTimer deadline, OVERLAPPED *overlapped) { Q_D(QWinOverlappedIoNotifier); QScopedAtomicIntIncrementor saii(d->waiting); - int t = msecs; - QElapsedTimer stopWatch; - stopWatch.start(); - forever { - OVERLAPPED *triggeredOverlapped = waitForAnyNotified(t); + while (!deadline.hasExpired()) { + OVERLAPPED *triggeredOverlapped = waitForAnyNotified(deadline); if (!triggeredOverlapped) return false; if (triggeredOverlapped == overlapped) return true; - t = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); - if (t == 0) - return false; } + return false; } /*! diff --git a/src/serialport/qwinoverlappedionotifier_p.h b/src/serialport/qwinoverlappedionotifier_p.h index 0cd0e4c..9ee998b 100644 --- a/src/serialport/qwinoverlappedionotifier_p.h +++ b/src/serialport/qwinoverlappedionotifier_p.h @@ -53,6 +53,7 @@ #include #include +#include typedef struct _OVERLAPPED OVERLAPPED; @@ -75,8 +76,8 @@ public: Qt::HANDLE handle() const; void setEnabled(bool enabled); - OVERLAPPED *waitForAnyNotified(int msecs); - bool waitForNotified(int msecs, OVERLAPPED *overlapped); + OVERLAPPED *waitForAnyNotified(QDeadlineTimer deadline); + bool waitForNotified(QDeadlineTimer deadline, OVERLAPPED *overlapped); Q_SIGNALS: void notified(quint32 numberOfBytes, quint32 errorCode, OVERLAPPED *overlapped); -- cgit v1.2.1