summaryrefslogtreecommitdiffstats
path: root/base/timer.cc
diff options
context:
space:
mode:
authordarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-28 20:50:12 +0000
committerdarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-28 20:50:12 +0000
commitaeab57ea8560065d6c513fcd46bb43e1bfbfd7a6 (patch)
treea63f2d36e86361d5c27122a6d6ef4098b755d7d9 /base/timer.cc
parente115558691eb08608fad56bb32f40265fdfa4ac5 (diff)
downloadchromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.zip
chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.tar.gz
chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.tar.bz2
Simplify OneShotTimer and RepeatingTimer. Fix up all consumers.
Major changes: OneShotTimer and RepeatingTimer become template classes that no longer require a Task or a Timer object. They just use PostDelayedTask. Under the hood that still uses a Timer object. The API is much simpler for consumers as they now no longer need to worry about allocating a Task or managing the lifetime of the object pointer held by the Task. I added some new unit tests to timer_unittest.cc to cover the API. I preserved the old TimerManager / Timer API for now, but I plan to soon kill it. R=brettw BUG=1346553 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1502 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/timer.cc')
-rw-r--r--base/timer.cc44
1 files changed, 16 insertions, 28 deletions
diff --git a/base/timer.cc b/base/timer.cc
index 1ee52d4..09a13f6 100644
--- a/base/timer.cc
+++ b/base/timer.cc
@@ -14,9 +14,11 @@
#include "base/message_loop.h"
#include "base/task.h"
+namespace base {
+
// A sequence number for all allocated times (used to break ties when
// comparing times in the TimerManager, and assure FIFO execution sequence).
-static base::AtomicSequenceNumber timer_id_counter_;
+static AtomicSequenceNumber timer_id_counter_;
//-----------------------------------------------------------------------------
// Timer
@@ -199,36 +201,22 @@ void TimerManager::DidChangeNextTimer() {
}
//-----------------------------------------------------------------------------
-// SimpleTimer
-
-SimpleTimer::SimpleTimer(TimeDelta delay, Task* task, bool repeating)
- : timer_(static_cast<int>(delay.InMilliseconds()), task, repeating),
- owns_task_(true) {
-}
-
-SimpleTimer::~SimpleTimer() {
- Stop();
-
- if (owns_task_)
- delete timer_.task();
-}
-
-void SimpleTimer::Start() {
- DCHECK(timer_.task());
- timer_.Reset();
- MessageLoop::current()->timer_manager()->StartTimer(&timer_);
-}
+// BaseTimer_Helper
-void SimpleTimer::Stop() {
- MessageLoop::current()->timer_manager()->StopTimer(&timer_);
+void BaseTimer_Helper::OrphanDelayedTask() {
+ if (delayed_task_) {
+ delayed_task_->timer_ = NULL;
+ delayed_task_ = NULL;
+ }
}
-bool SimpleTimer::IsRunning() const {
- return MessageLoop::current()->timer_manager()->IsTimerRunning(&timer_);
-}
+void BaseTimer_Helper::InitiateDelayedTask(TimerTask* timer_task) {
+ OrphanDelayedTask();
-void SimpleTimer::Reset() {
- DCHECK(timer_.task());
- MessageLoop::current()->timer_manager()->ResetTimer(&timer_);
+ delayed_task_ = timer_task;
+ delayed_task_->timer_ = this;
+ MessageLoop::current()->PostDelayedTask(
+ FROM_HERE, timer_task, static_cast<int>(delay_.InMilliseconds()));
}
+} // namespace base