Remove unnecessary ref-counting of QRunnable
It could never be higher than 1 anyway. Change-Id: If33c7978a4397a08e9eb091926726725d8bd3ea6 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
parent
4940f6d04c
commit
ecfda98d1f
@ -47,21 +47,18 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
class Q_CORE_EXPORT QRunnable
|
class Q_CORE_EXPORT QRunnable
|
||||||
{
|
{
|
||||||
int ref; // Qt6: Make this a bool, or make autoDelete() virtual.
|
bool m_autoDelete = true;
|
||||||
|
|
||||||
friend class QThreadPool;
|
|
||||||
friend class QThreadPoolPrivate;
|
|
||||||
friend class QThreadPoolThread;
|
|
||||||
Q_DISABLE_COPY(QRunnable)
|
Q_DISABLE_COPY(QRunnable)
|
||||||
public:
|
public:
|
||||||
virtual void run() = 0;
|
virtual void run() = 0;
|
||||||
|
|
||||||
QRunnable() : ref(0) { }
|
constexpr QRunnable() noexcept = default;
|
||||||
virtual ~QRunnable();
|
virtual ~QRunnable();
|
||||||
static QRunnable *create(std::function<void()> functionToRun);
|
static QRunnable *create(std::function<void()> functionToRun);
|
||||||
|
|
||||||
bool autoDelete() const { return ref != -1; }
|
bool autoDelete() const { return m_autoDelete; }
|
||||||
void setAutoDelete(bool _autoDelete) { ref = _autoDelete ? 0 : -1; }
|
void setAutoDelete(bool autoDelete) { m_autoDelete = autoDelete; }
|
||||||
};
|
};
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
@ -88,9 +88,8 @@ void QThreadPoolThread::run()
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
if (r) {
|
if (r) {
|
||||||
|
// If autoDelete() is false, r might already be deleted after run(), so check status now.
|
||||||
const bool del = r->autoDelete();
|
const bool del = r->autoDelete();
|
||||||
Q_ASSERT(!del || r->ref == 1);
|
|
||||||
|
|
||||||
|
|
||||||
// run the task
|
// run the task
|
||||||
locker.unlock();
|
locker.unlock();
|
||||||
@ -330,7 +329,6 @@ void QThreadPoolPrivate::clear()
|
|||||||
while (!page->isFinished()) {
|
while (!page->isFinished()) {
|
||||||
QRunnable *r = page->pop();
|
QRunnable *r = page->pop();
|
||||||
if (r && r->autoDelete()) {
|
if (r && r->autoDelete()) {
|
||||||
Q_ASSERT(r->ref == 1);
|
|
||||||
locker.unlock();
|
locker.unlock();
|
||||||
delete r;
|
delete r;
|
||||||
locker.relock();
|
locker.relock();
|
||||||
@ -371,10 +369,6 @@ bool QThreadPool::tryTake(QRunnable *runnable)
|
|||||||
d->queue.removeOne(page);
|
d->queue.removeOne(page);
|
||||||
delete page;
|
delete page;
|
||||||
}
|
}
|
||||||
if (runnable->autoDelete()) {
|
|
||||||
Q_ASSERT(runnable->ref == 1);
|
|
||||||
--runnable->ref; // undo ++ref in start()
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -393,14 +387,13 @@ void QThreadPoolPrivate::stealAndRunRunnable(QRunnable *runnable)
|
|||||||
Q_Q(QThreadPool);
|
Q_Q(QThreadPool);
|
||||||
if (!q->tryTake(runnable))
|
if (!q->tryTake(runnable))
|
||||||
return;
|
return;
|
||||||
|
// If autoDelete() is false, runnable might already be deleted after run(), so check status now.
|
||||||
const bool del = runnable->autoDelete();
|
const bool del = runnable->autoDelete();
|
||||||
|
|
||||||
runnable->run();
|
runnable->run();
|
||||||
|
|
||||||
if (del) {
|
if (del)
|
||||||
Q_ASSERT(runnable->ref == 0); // tryTake already deref'ed
|
|
||||||
delete runnable;
|
delete runnable;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -508,10 +501,6 @@ void QThreadPool::start(QRunnable *runnable, int priority)
|
|||||||
|
|
||||||
Q_D(QThreadPool);
|
Q_D(QThreadPool);
|
||||||
QMutexLocker locker(&d->mutex);
|
QMutexLocker locker(&d->mutex);
|
||||||
if (runnable->autoDelete()) {
|
|
||||||
Q_ASSERT(runnable->ref == 0);
|
|
||||||
++runnable->ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!d->tryStart(runnable)) {
|
if (!d->tryStart(runnable)) {
|
||||||
d->enqueueTask(runnable, priority);
|
d->enqueueTask(runnable, priority);
|
||||||
@ -558,22 +547,11 @@ bool QThreadPool::tryStart(QRunnable *runnable)
|
|||||||
if (!runnable)
|
if (!runnable)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (runnable->autoDelete()) {
|
|
||||||
Q_ASSERT(runnable->ref == 0);
|
|
||||||
++runnable->ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
Q_D(QThreadPool);
|
Q_D(QThreadPool);
|
||||||
QMutexLocker locker(&d->mutex);
|
QMutexLocker locker(&d->mutex);
|
||||||
if (d->tryStart(runnable))
|
if (d->tryStart(runnable))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Undo the reference above as we did not start the runnable and
|
|
||||||
// take over ownership.
|
|
||||||
if (runnable->autoDelete()) {
|
|
||||||
--runnable->ref;
|
|
||||||
Q_ASSERT(runnable->ref == 0);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user