summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qthread.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2024-04-30 18:49:27 -0700
committerThiago Macieira <thiago.macieira@intel.com>2024-07-09 10:18:18 -0700
commit452aaf340bb2cb89b6dad394b18998fa8d1c2e11 (patch)
tree17d44cf9e34f93a36c29574075411434d9076513 /src/corelib/thread/qthread.cpp
parentd05399c9932e3f00a2f4137a373552b1e781102d (diff)
QThread: replace three booleans with one state variable
There aren't 2^3 possible states for {running, isInFinish, finished}. There are only three possible states for them. There's a fourth case of NotStarted, which in my opinion is a design flaw. It needs to exist so QThread().isFinished() is false, but in a green field design we would be the same as QProcess: once a process/ thread finishes, it goes back to NotRunning. I also made the Windows version set back to NotStarted when failing to start, matching the Unix version. Drive-by use NSDMI. Change-Id: I262c3499666e4f4fbcfbfffd17cb3a48dad045dc Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/thread/qthread.cpp')
-rw-r--r--src/corelib/thread/qthread.cpp26
1 files changed, 11 insertions, 15 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index ee947c0c5f8..aa3bb00b7ba 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -127,8 +127,7 @@ QAdoptedThread::QAdoptedThread(QThreadData *data)
// thread should be running and not finished for the lifetime
// of the application (even if QCoreApplication goes away)
#if QT_CONFIG(thread)
- d_func()->running = true;
- d_func()->finished = false;
+ d_func()->threadState = QThreadPrivate::Running;
init();
d_func()->m_statusOrPendingObjects.setStatusAndClearList(
QtPrivate::getBindingStatus({}));
@@ -170,10 +169,7 @@ QScopedScopeLevelCounter::~QScopedScopeLevelCounter()
*/
QThreadPrivate::QThreadPrivate(QThreadData *d)
- : QObjectPrivate(), running(false), finished(false),
- isInFinish(false), interruptionRequested(false),
- exited(false), returnCode(-1),
- stackSize(0), priority(QThread::InheritPriority), data(d)
+ : QObjectPrivate(), data(d)
{
// INTEGRITY doesn't support self-extending stack. The default stack size for
@@ -492,12 +488,12 @@ QThread::~QThread()
Q_D(QThread);
{
QMutexLocker locker(&d->mutex);
- if (d->isInFinish) {
+ if (d->threadState == QThreadPrivate::Finishing) {
locker.unlock();
wait();
locker.relock();
}
- if (d->running && !d->finished && !d->data->isAdopted)
+ if (d->threadState == QThreadPrivate::Running && !d->data->isAdopted)
qFatal("QThread: Destroyed while thread is still running");
d->data->thread.storeRelease(nullptr);
@@ -514,7 +510,7 @@ bool QThread::isFinished() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
- return d->finished || d->isInFinish;
+ return d->threadState >= QThreadPrivate::Finishing;
}
/*!
@@ -527,7 +523,7 @@ bool QThread::isRunning() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
- return d->running && !d->isInFinish;
+ return d->threadState == QThreadPrivate::Running;
}
/*!
@@ -553,9 +549,9 @@ bool QThread::isRunning() const
void QThread::setStackSize(uint stackSize)
{
Q_D(QThread);
- QMutexLocker locker(&d->mutex);
- Q_ASSERT_X(!d->running, "QThread::setStackSize",
+ Q_ASSERT_X(!isRunning(), "QThread::setStackSize",
"cannot change stack size while the thread is running");
+ QMutexLocker locker(&d->mutex);
d->stackSize = stackSize;
}
@@ -769,7 +765,7 @@ void QThread::setPriority(Priority priority)
}
Q_D(QThread);
QMutexLocker locker(&d->mutex);
- if (!d->running) {
+ if (d->threadState != QThreadPrivate::Running) {
qWarning("QThread::setPriority: Cannot set priority, thread is not running");
return;
}
@@ -1223,7 +1219,7 @@ void QThread::requestInterruption()
return;
}
QMutexLocker locker(&d->mutex);
- if (!d->running || d->finished || d->isInFinish)
+ if (d->threadState != QThreadPrivate::Running)
return;
d->interruptionRequested.store(true, std::memory_order_relaxed);
}
@@ -1262,7 +1258,7 @@ bool QThread::isInterruptionRequested() const
return false;
// slow path: if the flag is set, take into account run status:
QMutexLocker locker(&d->mutex);
- return d->running && !d->finished && !d->isInFinish;
+ return d->threadState == QThreadPrivate::Running;
}
/*!