Skip to content

Commit

Permalink
QThreadPool: handle negative expiryTimeouts
Browse files Browse the repository at this point in the history
When setting/getting the expiryTimeout.

Negative values mean "never expire", so use duration::max(), which
QDeadlineTimer interprets as QDeadline::Forever.

Amends (and partially reverts) c570271

Task-number: QTBUG-129898
Pick-to: 6.8
Change-Id: I8f141cd3fc3c2ff4d21ba2d9663619bc507aeca4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
ahmadsamir committed Oct 20, 2024
1 parent d630804 commit 4678d96
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/corelib/thread/qthreadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <algorithm>
#include <memory>

using namespace std::chrono_literals;

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -606,14 +603,19 @@ 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();
}

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
Expand Down

0 comments on commit 4678d96

Please sign in to comment.