summaryrefslogtreecommitdiffstats
path: root/base/time_win.cc
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-26 19:48:34 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-26 19:48:34 +0000
commit097ae5a93a863b604fd6dbd939bed0c2be1afed2 (patch)
tree61177362b1c1bd2d1b2438acc50ccb2c061b176e /base/time_win.cc
parent902c971382790170852fe5b5d22d1b79adb31056 (diff)
downloadchromium_src-097ae5a93a863b604fd6dbd939bed0c2be1afed2.zip
chromium_src-097ae5a93a863b604fd6dbd939bed0c2be1afed2.tar.gz
chromium_src-097ae5a93a863b604fd6dbd939bed0c2be1afed2.tar.bz2
Make SystemMonitor not a Singleton and move it out of base
SystemMonitor makes an assumption that through its lifetime a MessageLoop exists and stays the same. It is difficult and error-prone to satisfy that when it is a Singleton. It has caused problems in the past. Additionally, extract HighResoltionTimerManager out of time_win.cc, eliminating yet another Singleton. TEST=none BUG=none Review URL: http://codereview.chromium.org/431008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33214 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/time_win.cc')
-rw-r--r--base/time_win.cc79
1 files changed, 12 insertions, 67 deletions
diff --git a/base/time_win.cc b/base/time_win.cc
index 9fdcab1..eedf468 100644
--- a/base/time_win.cc
+++ b/base/time_win.cc
@@ -45,7 +45,6 @@
#include "base/logging.h"
#include "base/cpu.h"
#include "base/singleton.h"
-#include "base/system_monitor.h"
using base::Time;
using base::TimeDelta;
@@ -88,65 +87,6 @@ void InitializeClock() {
initial_time = CurrentWallclockMicroseconds();
}
-class HighResolutionTimerManager : public base::SystemMonitor::PowerObserver {
- public:
- ~HighResolutionTimerManager() {
- StopMonitoring();
- UseHiResClock(false);
- }
-
- void Enable() {
- StopMonitoring();
- UseHiResClock(true);
- }
-
- void StartMonitoring() {
- if (is_monitoring_)
- return;
- is_monitoring_ = true;
- base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
- system_monitor->AddObserver(this);
- UseHiResClock(!system_monitor->BatteryPower());
- }
-
- void StopMonitoring() {
- if (!is_monitoring_)
- return;
- is_monitoring_ = false;
- base::SystemMonitor* system_monitor = base::SystemMonitor::Get();
- if (system_monitor)
- system_monitor->RemoveObserver(this);
- }
-
- // Interfaces for monitoring Power changes.
- void OnPowerStateChange(bool on_battery_power) {
- UseHiResClock(!on_battery_power);
- }
-
- private:
- HighResolutionTimerManager()
- : is_monitoring_(false),
- hi_res_clock_enabled_(false) {
- }
- friend struct DefaultSingletonTraits<HighResolutionTimerManager>;
-
- // Enable or disable the faster multimedia timer.
- void UseHiResClock(bool enabled) {
- if (enabled == hi_res_clock_enabled_)
- return;
- if (enabled)
- timeBeginPeriod(1);
- else
- timeEndPeriod(1);
- hi_res_clock_enabled_ = enabled;
- }
-
- bool is_monitoring_;
- bool hi_res_clock_enabled_;
-
- DISALLOW_COPY_AND_ASSIGN(HighResolutionTimerManager);
-};
-
} // namespace
// Time -----------------------------------------------------------------------
@@ -208,13 +148,18 @@ FILETIME Time::ToFileTime() const {
}
// static
-void Time::StartSystemMonitorObserver() {
- Singleton<HighResolutionTimerManager>()->StartMonitoring();
-}
-
-// static
-void Time::EnableHiResClockForTests() {
- Singleton<HighResolutionTimerManager>()->Enable();
+bool Time::UseHighResolutionTimer(bool use) {
+ // TODO(mbelshe): Make sure that switching the system timer resolution
+ // doesn't break Timer firing order etc. An example test would be to have
+ // two threads. One would have a bunch of timers, and another would turn the
+ // high resolution timer on and off.
+
+ MMRESULT result;
+ if (use)
+ result = timeBeginPeriod(1);
+ else
+ result = timeEndPeriod(1);
+ return (result == TIMERR_NOERROR);
}
// static