From 4678d96d874c21b69f4942e3bfdc5fc97128cf4e Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Tue, 15 Oct 2024 13:36:12 +0300 Subject: QThreadPool: handle negative expiryTimeouts When setting/getting the expiryTimeout. Negative values mean "never expire", so use duration::max(), which QDeadlineTimer interprets as QDeadline::Forever. Amends (and partially reverts) c57027199996d0f0d2ac8ebc4505c78afa54ab5a Task-number: QTBUG-129898 Pick-to: 6.8 Change-Id: I8f141cd3fc3c2ff4d21ba2d9663619bc507aeca4 Reviewed-by: Thiago Macieira --- src/corelib/thread/qthreadpool.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/corelib/thread/qthreadpool.cpp') diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 3a9c0569e7c..18d4103fdfc 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -11,6 +11,8 @@ #include #include +using namespace std::chrono_literals; + QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; @@ -113,12 +115,7 @@ void QThreadPoolThread::run() manager->waitingThreads.enqueue(this); registerThreadInactive(); // wait for work, exiting after the expiry timeout is reached - QDeadlineTimer deadline; - if (manager->expiryTimeout.count() < 0) - deadline = QDeadlineTimer::Forever; - else - deadline.setRemainingTime(manager->expiryTimeout); - runnableReady.wait(locker.mutex(), deadline); + runnableReady.wait(locker.mutex(), QDeadlineTimer(manager->expiryTimeout)); // this thread is about to be deleted, do not work or expire if (!manager->allThreads.contains(this)) { Q_ASSERT(manager->queue.isEmpty()); @@ -606,6 +603,8 @@ int QThreadPool::expiryTimeout() const using namespace std::chrono; Q_D(const QThreadPool); QMutexLocker locker(&d->mutex); + if (d->expiryTimeout == decltype(d->expiryTimeout)::max()) + return -1; return duration_cast(d->expiryTimeout).count(); } @@ -613,7 +612,10 @@ void QThreadPool::setExpiryTimeout(int expiryTimeout) { Q_D(QThreadPool); QMutexLocker locker(&d->mutex); - d->expiryTimeout = std::chrono::milliseconds(expiryTimeout); + if (expiryTimeout < 0) + d->expiryTimeout = decltype(d->expiryTimeout)::max(); + else + d->expiryTimeout = expiryTimeout * 1ms; } /*! \property QThreadPool::maxThreadCount -- cgit v1.2.3