diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-10-01 08:21:10 (GMT) |
---|---|---|
committer | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-12-20 15:49:54 (GMT) |
commit | 6c1180505a0ac55bfd0eac87c2a61103a8fdec14 (patch) | |
tree | a1967610204ebefeaa7f5884cabd3e6daeaf125a | |
parent | 3b6a84de5c5ed2e1970ad2b396292babb9173227 (diff) | |
download | Qt-6c1180505a0ac55bfd0eac87c2a61103a8fdec14.zip Qt-6c1180505a0ac55bfd0eac87c2a61103a8fdec14.tar.gz Qt-6c1180505a0ac55bfd0eac87c2a61103a8fdec14.tar.bz2 |
Store and track spin times in nanosecond resolution
Use the new QElapsedTimer::nsecsElapsed() and store all values in qint64
instead of int.
The maximum spin time threshold is now 1000000ns, or 1ms. Spinning for longer
than 1ms is just a waste of time, CPU, and battery.
Reviewed-by: joao
-rw-r--r-- | src/corelib/thread/qmutex.cpp | 9 | ||||
-rw-r--r-- | src/corelib/thread/qmutex_p.h | 6 |
2 files changed, 8 insertions, 7 deletions
diff --git a/src/corelib/thread/qmutex.cpp b/src/corelib/thread/qmutex.cpp index 72f87b7..03e623f 100644 --- a/src/corelib/thread/qmutex.cpp +++ b/src/corelib/thread/qmutex.cpp @@ -443,16 +443,17 @@ void QMutex::lockInternal() QElapsedTimer elapsedTimer; elapsedTimer.start(); do { - if (elapsedTimer.hasExpired(d->maximumSpinTime)) { + qint64 spinTime = elapsedTimer.nsecsElapsed(); + if (spinTime > d->maximumSpinTime) { // didn't get the lock, wait for it, since we're not going to gain anything by spinning more - int spinTime = elapsedTimer.restart(); + elapsedTimer.start(); bool isLocked = d->wait(); Q_ASSERT_X(isLocked, "QMutex::lock", "Internal error, infinite wait has timed out."); Q_UNUSED(isLocked); - int maximumSpinTime = d->maximumSpinTime; - int waitTime = elapsedTimer.elapsed(); + qint64 maximumSpinTime = d->maximumSpinTime; + qint64 waitTime = elapsedTimer.nsecsElapsed(); // adjust the spin count when spinning does not benefit contention performance if (spinTime + waitTime > QMutexPrivate::MaximumSpinTimeThreshold) { // long waits, stop spinning diff --git a/src/corelib/thread/qmutex_p.h b/src/corelib/thread/qmutex_p.h index 6de42ad..fa6e879 100644 --- a/src/corelib/thread/qmutex_p.h +++ b/src/corelib/thread/qmutex_p.h @@ -72,9 +72,9 @@ public: bool wait(int timeout = -1); void wakeUp(); - // half of a frame (in ms) at 60fps - enum { MaximumSpinTimeThreshold = 8 }; - volatile int maximumSpinTime; + // 1ms = 1000000ns + enum { MaximumSpinTimeThreshold = 1000000 }; + volatile qint64 maximumSpinTime; Qt::HANDLE owner; uint count; |