diff options
author | mbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-29 04:58:15 +0000 |
---|---|---|
committer | mbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-29 04:58:15 +0000 |
commit | 57f030a503ed96f974a4edcb8c65c982ea8fd765 (patch) | |
tree | 07036d200a6f22c529fda6db035e874ef7f5f3d5 /base/message_loop.cc | |
parent | 13729e7753dfdaf4cc90f5050827a8ebc9875390 (diff) | |
download | chromium_src-57f030a503ed96f974a4edcb8c65c982ea8fd765.zip chromium_src-57f030a503ed96f974a4edcb8c65c982ea8fd765.tar.gz chromium_src-57f030a503ed96f974a4edcb8c65c982ea8fd765.tar.bz2 |
Change chrome from statically enabling high resolution timers on windows
to enabling them dynamically - only when the application really needs
them.
I am working on some test cases for this, and will add them. But wanted
to send out the concept for review.
In this implementation, I modify the message loop to detect when the
application has requested high resolution timers. Note that there are
multiple MessageLoops active in a single process. After a period of
time, we simply shut it off again. We could have set a timer or
kept a count of active timers, or any number of more complex algorithms.
But I think this algorithm is very simple and good enough. If an
application continues needing high resolution timers for more than 1s,
we'll turn the high-resolution timers back on again.
One last change - since we've implemented the clamp at 4ms, there isn't
a lot of point to our use of 1ms for timeBeginPeriod. I've modified
that to 2 (which is half of 4ms, our target minimal interval).
BUG=46531
TEST=MessageLoop.HighResolutionTimers
Review URL: http://codereview.chromium.org/2822035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51102 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/message_loop.cc')
-rw-r--r-- | base/message_loop.cc | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/base/message_loop.cc b/base/message_loop.cc index 3b13617..1668fd7 100644 --- a/base/message_loop.cc +++ b/base/message_loop.cc @@ -282,10 +282,36 @@ void MessageLoop::PostTask_Helper( if (delay_ms > 0) { pending_task.delayed_run_time = Time::Now() + TimeDelta::FromMilliseconds(delay_ms); + +#if defined(OS_WIN) + if (high_resolution_timer_expiration_.is_null()) { + // Windows timers are granular to 15.6ms. If we only set high-res + // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms, + // which as a percentage is pretty inaccurate. So enable high + // res timers for any timer which is within 2x of the granularity. + // This is a tradeoff between accuracy and power management. + bool needs_high_res_timers = + delay_ms < (2 * Time::kMinLowResolutionThresholdMs); + if (needs_high_res_timers) { + Time::ActivateHighResolutionTimer(true); + high_resolution_timer_expiration_ = base::TimeTicks::Now() + + TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs); + } + } +#endif } else { DCHECK_EQ(delay_ms, 0) << "delay should not be negative"; } +#if defined(OS_WIN) + if (!high_resolution_timer_expiration_.is_null()) { + if (base::TimeTicks::Now() > high_resolution_timer_expiration_) { + Time::ActivateHighResolutionTimer(false); + high_resolution_timer_expiration_ = base::TimeTicks(); + } + } +#endif + // Warning: Don't try to short-circuit, and handle this thread's tasks more // directly, as it could starve handling of foreign threads. Put every task // into this queue. |