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
Change-Id: I8f141cd3fc3c2ff4d21ba2d9663619bc507aeca4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 4678d96d874c21b69f4942e3bfdc5fc97128cf4e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
bb10
Ahmad Samir 2024-10-15 13:36:12 +03:00 committed by Qt Cherry-pick Bot
parent ad883b78ac
commit 1487d7edb3
1 changed files with 9 additions and 7 deletions

View File

@ -11,6 +11,8 @@
#include <algorithm>
#include <memory>
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<milliseconds>(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