diff options
author | David Faure <david.faure@kdab.com> | 2013-03-15 19:48:59 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-03-23 02:06:47 +0100 |
commit | d4adee7851790dd6949424de083756f49af891db (patch) | |
tree | 00416d4afc19fd37ce6f3a121d842ee0914ad5e7 /src/corelib/thread/qthread_win.cpp | |
parent | c550a5d42c1188c0ccd11c205665e2608dbec61f (diff) |
Fix race condition in QThread::setPriority
The value of priority was read without the mutex locked, from within
the thread.
Had to extract a QThreadPrivate::setPriority method so that it can be called
with the mutex already locked. So if the main thread calls setPriority
while the thread is starting, it will be either be before or after the
"re-set priority" code at thread startup, but at least not in the middle of it.
Change-Id: I7a054f68623f61482c749274da66f3b2dcd8bcee
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/thread/qthread_win.cpp')
-rw-r--r-- | src/corelib/thread/qthread_win.cpp | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index ddf499f3bb..a0fac8eff2 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -588,55 +588,49 @@ void QThread::setTerminationEnabled(bool enabled) } } -void QThread::setPriority(Priority priority) +// Caller must hold the mutex +void QThreadPrivate::setPriority(QThread::Priority threadPriority) { - Q_D(QThread); - QMutexLocker locker(&d->mutex); - if (!d->running) { - qWarning("QThread::setPriority: Cannot set priority, thread is not running"); - return; - } - // copied from start() with a few modifications: int prio; - d->priority = priority; - switch (d->priority) { - case IdlePriority: + priority = threadPriority; + switch (priority) { + case QThread::IdlePriority: prio = THREAD_PRIORITY_IDLE; break; - case LowestPriority: + case QThread::LowestPriority: prio = THREAD_PRIORITY_LOWEST; break; - case LowPriority: + case QThread::LowPriority: prio = THREAD_PRIORITY_BELOW_NORMAL; break; - case NormalPriority: + case QThread::NormalPriority: prio = THREAD_PRIORITY_NORMAL; break; - case HighPriority: + case QThread::HighPriority: prio = THREAD_PRIORITY_ABOVE_NORMAL; break; - case HighestPriority: + case QThread::HighestPriority: prio = THREAD_PRIORITY_HIGHEST; break; - case TimeCriticalPriority: + case QThread::TimeCriticalPriority: prio = THREAD_PRIORITY_TIME_CRITICAL; break; - case InheritPriority: + case QThread::InheritPriority: default: qWarning("QThread::setPriority: Argument cannot be InheritPriority"); return; } - if (!SetThreadPriority(d->handle, prio)) { + if (!SetThreadPriority(handle, prio)) { qErrnoWarning("QThread::setPriority: Failed to set thread priority"); } } |