From 6c1180505a0ac55bfd0eac87c2a61103a8fdec14 Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Fri, 1 Oct 2010 10:21:10 +0200 Subject: 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 --- src/corelib/thread/qmutex.cpp | 9 +++++---- 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; -- cgit v0.12