summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authormbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-15 19:20:49 +0000
committermbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-15 19:20:49 +0000
commit14255a99d1709414d9b5419329e19bf9bd8406b7 (patch)
tree18616ca60c09839db53690e8c127f5972ca76899 /base
parent3f20a2191bdac8e08bc1572913d5aa4a17df930c (diff)
downloadchromium_src-14255a99d1709414d9b5419329e19bf9bd8406b7.zip
chromium_src-14255a99d1709414d9b5419329e19bf9bd8406b7.tar.gz
chromium_src-14255a99d1709414d9b5419329e19bf9bd8406b7.tar.bz2
Reland old fix that was reverted incorrectly.
Fix regression where high resolution timers could be activated even under battery power. Add unit test to protect chromium from developers like me in the future. The fix is a one-liner in hi_res_timer_manager_win.cc. The rest of the code change is the mechanics to enable the unit test. BUG=59528 TEST=HiResTimerManagerTest.ToggleOnOff Review URL: http://codereview.chromium.org/6904117 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85413 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/message_loop.cc18
-rw-r--r--base/time.h12
-rw-r--r--base/time_win.cc23
3 files changed, 45 insertions, 8 deletions
diff --git a/base/message_loop.cc b/base/message_loop.cc
index a3520a6..059cdc25 100644
--- a/base/message_loop.cc
+++ b/base/message_loop.cc
@@ -15,6 +15,7 @@
#include "base/scoped_ptr.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_local.h"
+#include "base/time.h"
#include "base/tracked_objects.h"
#if defined(OS_MACOSX)
@@ -226,6 +227,16 @@ MessageLoop::~MessageLoop() {
// OK, now make it so that no one can find us.
lazy_tls_ptr.Pointer()->Set(NULL);
+
+#if defined(OS_WIN)
+ // If we left the high-resolution timer activated, deactivate it now.
+ // Doing this is not-critical, it is mainly to make sure we track
+ // the high resolution timer activations properly in our unit tests.
+ if (!high_resolution_timer_expiration_.is_null()) {
+ base::Time::ActivateHighResolutionTimer(false);
+ high_resolution_timer_expiration_ = base::TimeTicks();
+ }
+#endif
}
// static
@@ -576,9 +587,10 @@ TimeTicks MessageLoop::CalculateDelayedRuntime(int64 delay_ms) {
bool needs_high_res_timers =
delay_ms < (2 * base::Time::kMinLowResolutionThresholdMs);
if (needs_high_res_timers) {
- base::Time::ActivateHighResolutionTimer(true);
- high_resolution_timer_expiration_ = TimeTicks::Now() +
- TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
+ if (base::Time::ActivateHighResolutionTimer(true)) {
+ high_resolution_timer_expiration_ = TimeTicks::Now() +
+ TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
+ }
}
}
#endif
diff --git a/base/time.h b/base/time.h
index 58b0eb2..1b05e24 100644
--- a/base/time.h
+++ b/base/time.h
@@ -25,6 +25,7 @@
#include <time.h>
+#include "base/atomicops.h"
#include "base/base_api.h"
#include "base/basictypes.h"
@@ -281,10 +282,16 @@ class BASE_API Time {
// Activates or deactivates the high resolution timer based on the |activate|
// flag. If the HighResolutionTimer is not Enabled (see
// EnableHighResolutionTimer), this function will return false. Otherwise
- // returns true.
+ // returns true. Each successful activate call must be paired with a
+ // subsequent deactivate call.
// All callers to activate the high resolution timer must eventually call
// this function to deactivate the high resolution timer.
static bool ActivateHighResolutionTimer(bool activate);
+
+ // Returns true if the high resolution timer is both enabled and activated.
+ // This is provided for testing only, and is not tracked in a thread-safe
+ // way.
+ static bool IsHighResolutionTimerInUse();
#endif
// Converts an exploded structure representing either the local time or UTC
@@ -403,6 +410,9 @@ class BASE_API Time {
// when using battery power, we might elect to prevent high speed timers
// which would draw more power.
static bool high_resolution_timer_enabled_;
+ // Count of activations on the high resolution timer. Only use in tests
+ // which are single threaded.
+ static int high_resolution_timer_activated_;
#endif
// Time in microseconds in UTC.
diff --git a/base/time_win.cc b/base/time_win.cc
index 7124a74..d439a1e 100644
--- a/base/time_win.cc
+++ b/base/time_win.cc
@@ -99,6 +99,7 @@ void InitializeClock() {
const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000);
bool Time::high_resolution_timer_enabled_ = false;
+int Time::high_resolution_timer_activated_ = 0;
// static
Time Time::Now() {
@@ -162,22 +163,36 @@ void Time::EnableHighResolutionTimer(bool enable) {
}
// static
-bool Time::ActivateHighResolutionTimer(bool activate) {
- if (!high_resolution_timer_enabled_)
+bool Time::ActivateHighResolutionTimer(bool activating) {
+ if (!high_resolution_timer_enabled_ && activating)
return false;
// Using anything other than 1ms makes timers granular
// to that interval.
const int kMinTimerIntervalMs = 1;
MMRESULT result;
- if (activate)
+ if (activating) {
result = timeBeginPeriod(kMinTimerIntervalMs);
- else
+ high_resolution_timer_activated_++;
+ } else {
result = timeEndPeriod(kMinTimerIntervalMs);
+ high_resolution_timer_activated_--;
+ }
return result == TIMERR_NOERROR;
}
// static
+bool Time::IsHighResolutionTimerInUse() {
+ // Note: we should track the high_resolution_timer_activated_ value
+ // under a lock if we want it to be accurate in a system with multiple
+ // message loops. We don't do that - because we don't want to take the
+ // expense of a lock for this. We *only* track this value so that unit
+ // tests can see if the high resolution timer is on or off.
+ return high_resolution_timer_enabled_ &&
+ high_resolution_timer_activated_ > 0;
+}
+
+// static
Time Time::FromExploded(bool is_local, const Exploded& exploded) {
// Create the system struct representing our exploded time. It will either be
// in local time or UTC.