summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-20 08:52:32 +0000
committermbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-20 08:52:32 +0000
commit47245d6709f7f16a65cfb3df9aeb51fb32d5a4ea (patch)
tree36051d10c826994d095b116955f42dae2de16e52
parentc4fcdca3cfec4ca060d29d10cdc06dc83aed1bf2 (diff)
downloadchromium_src-47245d6709f7f16a65cfb3df9aeb51fb32d5a4ea.zip
chromium_src-47245d6709f7f16a65cfb3df9aeb51fb32d5a4ea.tar.gz
chromium_src-47245d6709f7f16a65cfb3df9aeb51fb32d5a4ea.tar.bz2
2nd try:
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/3889004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63191 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--app/app.gyp1
-rw-r--r--app/hi_res_timer_manager.h5
-rw-r--r--app/hi_res_timer_manager_posix.cc2
-rw-r--r--app/hi_res_timer_manager_unittest.cc51
-rw-r--r--app/hi_res_timer_manager_win.cc5
-rw-r--r--base/message_loop.cc17
-rw-r--r--base/time.h11
-rw-r--r--base/time_win.cc23
8 files changed, 103 insertions, 12 deletions
diff --git a/app/app.gyp b/app/app.gyp
index 2c8e7f7..538b73d 100644
--- a/app/app.gyp
+++ b/app/app.gyp
@@ -41,6 +41,7 @@
'animation_container_unittest.cc',
'animation_unittest.cc',
'clipboard/clipboard_unittest.cc',
+ 'hi_res_timer_manager_unittest.cc',
'l10n_util_mac_unittest.mm',
'l10n_util_unittest.cc',
'multi_animation_unittest.cc',
diff --git a/app/hi_res_timer_manager.h b/app/hi_res_timer_manager.h
index 7eed795..fdc10b3 100644
--- a/app/hi_res_timer_manager.h
+++ b/app/hi_res_timer_manager.h
@@ -18,11 +18,14 @@ class HighResolutionTimerManager : public SystemMonitor::PowerObserver {
// SystemMonitor::PowerObserver:
void OnPowerStateChange(bool on_battery_power);
+ // Returns true if the hi resolution clock could be used right now.
+ bool hi_res_clock_available() const { return hi_res_clock_available_; }
+
private:
// Enable or disable the faster multimedia timer.
void UseHiResClock(bool use);
- bool hi_res_clock_used_;
+ bool hi_res_clock_available_;
DISALLOW_COPY_AND_ASSIGN(HighResolutionTimerManager);
};
diff --git a/app/hi_res_timer_manager_posix.cc b/app/hi_res_timer_manager_posix.cc
index 1398449..8a9a41f 100644
--- a/app/hi_res_timer_manager_posix.cc
+++ b/app/hi_res_timer_manager_posix.cc
@@ -7,7 +7,7 @@
// On POSIX we don't need to do anything special with the system timer.
HighResolutionTimerManager::HighResolutionTimerManager()
- : hi_res_clock_used_(false) {
+ : hi_res_clock_available_(false) {
}
HighResolutionTimerManager::~HighResolutionTimerManager() {
diff --git a/app/hi_res_timer_manager_unittest.cc b/app/hi_res_timer_manager_unittest.cc
new file mode 100644
index 0000000..1e7dbc3
--- /dev/null
+++ b/app/hi_res_timer_manager_unittest.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include "app/hi_res_timer_manager.h"
+#include "app/system_monitor.h"
+#include "base/time.h"
+
+#if defined(OS_WIN)
+TEST(HiResTimerManagerTest, ToggleOnOff) {
+ MessageLoop loop;
+ scoped_ptr<SystemMonitor> system_monitor(new SystemMonitor());
+ HighResolutionTimerManager manager;
+
+ // At this point, we don't know if the high resolution timers are on or off,
+ // it depends on what system the tests are running on (for example, if this
+ // test is running on a laptop/battery, then the SystemMonitor would have
+ // already set the PowerState to battery power; but if we're running on a
+ // desktop, then the PowerState will be non-battery power). Simulate a power
+ // level change to get to a deterministic state.
+ manager.OnPowerStateChange(/* on_battery */ false);
+
+ // Loop a few times to test power toggling.
+ for (int loop = 2; loop >= 0; --loop) {
+ // The manager has the high resolution clock enabled now.
+ EXPECT_TRUE(manager.hi_res_clock_available());
+ // But the Time class has it off, because it hasn't been activated.
+ EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
+
+ // Activate the high resolution timer.
+ base::Time::ActivateHighResolutionTimer(true);
+ EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
+
+ // Simulate a on-battery power event.
+ manager.OnPowerStateChange(/* on_battery */ true);
+ EXPECT_FALSE(manager.hi_res_clock_available());
+ EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
+
+ // Simulate a off-battery power event.
+ manager.OnPowerStateChange(/* on_battery */ false);
+ EXPECT_TRUE(manager.hi_res_clock_available());
+ EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
+
+ // De-activate the high resolution timer.
+ base::Time::ActivateHighResolutionTimer(false);
+ }
+}
+#endif // defined(OS_WIN)
+
diff --git a/app/hi_res_timer_manager_win.cc b/app/hi_res_timer_manager_win.cc
index 6fbffca..da25d70 100644
--- a/app/hi_res_timer_manager_win.cc
+++ b/app/hi_res_timer_manager_win.cc
@@ -7,7 +7,7 @@
#include "base/time.h"
HighResolutionTimerManager::HighResolutionTimerManager()
- : hi_res_clock_used_(false) {
+ : hi_res_clock_available_(false) {
SystemMonitor* system_monitor = SystemMonitor::Get();
system_monitor->AddObserver(this);
UseHiResClock(!system_monitor->BatteryPower());
@@ -23,7 +23,8 @@ void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) {
}
void HighResolutionTimerManager::UseHiResClock(bool use) {
- if (use == hi_res_clock_used_)
+ if (use == hi_res_clock_available_)
return;
+ hi_res_clock_available_ = use;
base::Time::EnableHighResolutionTimer(use);
}
diff --git a/base/message_loop.cc b/base/message_loop.cc
index 8f6c997b..0b03d3c 100644
--- a/base/message_loop.cc
+++ b/base/message_loop.cc
@@ -179,6 +179,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()) {
+ Time::ActivateHighResolutionTimer(false);
+ high_resolution_timer_expiration_ = base::TimeTicks();
+ }
+#endif
}
void MessageLoop::AddDestructionObserver(
@@ -337,9 +347,10 @@ void MessageLoop::PostTask_Helper(
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);
+ if (Time::ActivateHighResolutionTimer(true)) {
+ high_resolution_timer_expiration_ = base::TimeTicks::Now() +
+ TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
+ }
}
}
#endif
diff --git a/base/time.h b/base/time.h
index 79e30b4..305ca6b 100644
--- a/base/time.h
+++ b/base/time.h
@@ -283,10 +283,16 @@ class 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
@@ -405,6 +411,9 @@ class 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 5d3ecd6..502dd83 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.