diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-28 20:50:12 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-28 20:50:12 +0000 |
commit | aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6 (patch) | |
tree | a63f2d36e86361d5c27122a6d6ef4098b755d7d9 /net | |
parent | e115558691eb08608fad56bb32f40265fdfa4ac5 (diff) | |
download | chromium_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 'net')
-rw-r--r-- | net/base/client_socket_pool.cc | 12 | ||||
-rw-r--r-- | net/base/client_socket_pool.h | 13 | ||||
-rw-r--r-- | net/disk_cache/backend_impl.cc | 27 | ||||
-rw-r--r-- | net/disk_cache/backend_impl.h | 6 | ||||
-rw-r--r-- | net/disk_cache/stress_cache.cc | 15 |
5 files changed, 28 insertions, 45 deletions
diff --git a/net/base/client_socket_pool.cc b/net/base/client_socket_pool.cc index fcf812e..2fb6703 100644 --- a/net/base/client_socket_pool.cc +++ b/net/base/client_socket_pool.cc @@ -19,19 +19,14 @@ const int kCleanupInterval = 5; namespace net { ClientSocketPool::ClientSocketPool(int max_sockets_per_group) - : timer_(TimeDelta::FromSeconds(kCleanupInterval)), - idle_socket_count_(0), + : idle_socket_count_(0), max_sockets_per_group_(max_sockets_per_group) { - timer_.set_task(this); } ClientSocketPool::~ClientSocketPool() { - timer_.set_task(NULL); - // Clean up any idle sockets. Assert that we have no remaining active sockets // or pending requests. They should have all been cleaned up prior to the // manager being destroyed. - CloseIdleSockets(); DCHECK(group_map_.empty()); } @@ -134,7 +129,8 @@ void ClientSocketPool::MaybeCloseIdleSockets( void ClientSocketPool::IncrementIdleCount() { if (++idle_socket_count_ == 1) - timer_.Start(); + timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, + &ClientSocketPool::DoTimeout); } void ClientSocketPool::DecrementIdleCount() { @@ -177,7 +173,7 @@ void ClientSocketPool::DoReleaseSocket(const std::string& group_name, } } -void ClientSocketPool::Run() { +void ClientSocketPool::DoTimeout() { MaybeCloseIdleSockets(true); } diff --git a/net/base/client_socket_pool.h b/net/base/client_socket_pool.h index 99e4936..4b89705 100644 --- a/net/base/client_socket_pool.h +++ b/net/base/client_socket_pool.h @@ -25,8 +25,7 @@ class ClientSocketHandle; // not responsible for allocating the associated ClientSocket objects. The // consumer must do so if it gets a scoped_ptr<ClientSocket> with a null value. // -class ClientSocketPool : public base::RefCounted<ClientSocketPool>, - public Task { +class ClientSocketPool : public base::RefCounted<ClientSocketPool> { public: explicit ClientSocketPool(int max_sockets_per_group); @@ -85,9 +84,9 @@ class ClientSocketPool : public base::RefCounted<ClientSocketPool>, // Called via PostTask by ReleaseSocket. void DoReleaseSocket(const std::string& group_name, ClientSocketPtr* ptr); - // Task implementation. This method scans the idle sockets checking to see - // if any have been disconnected. - virtual void Run(); + // Called when timer_ fires. This method scans the idle sockets checking to + // see if any have been disconnected. + void DoTimeout(); // A Request is allocated per call to RequestSocket that results in // ERR_IO_PENDING. @@ -109,13 +108,15 @@ class ClientSocketPool : public base::RefCounted<ClientSocketPool>, GroupMap group_map_; // Timer used to periodically prune sockets that have been disconnected. - RepeatingTimer timer_; + base::RepeatingTimer<ClientSocketPool> timer_; // The total number of idle sockets in the system. int idle_socket_count_; // The maximum number of sockets kept per group. int max_sockets_per_group_; + + DISALLOW_COPY_AND_ASSIGN(ClientSocketPool); }; } // namespace net diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc index 18025a1..7deb343 100644 --- a/net/disk_cache/backend_impl.cc +++ b/net/disk_cache/backend_impl.cc @@ -132,23 +132,10 @@ bool DelayedCacheCleanup(const std::wstring& full_path) { return true; } -// ------------------------------------------------------------------------ - -class TimerTask : public Task { - public: - explicit TimerTask(disk_cache::BackendImpl* backend) : backend_(backend) {} - ~TimerTask() {} - - virtual void Run() { - backend_->OnStatsTimer(); - } - - private: - disk_cache::BackendImpl* backend_; -}; - } // namespace +// ------------------------------------------------------------------------ + namespace disk_cache { // If the initialization of the cache fails, and force is true, we will discard @@ -201,10 +188,8 @@ bool BackendImpl::Init() { if (!restarted_) { // Create a recurrent timer of 30 secs. int timer_delay = unit_test_ ? 1000 : 30000; - TimerTask* task = new TimerTask(this); - timer_task_ = task; - timer_ = MessageLoop::current()->timer_manager()->StartTimer(timer_delay, - task, true); + timer_.Start(TimeDelta::FromMilliseconds(timer_delay), this, + &BackendImpl::OnStatsTimer); } init_ = true; @@ -238,9 +223,7 @@ BackendImpl::~BackendImpl() { if (!init_) return; - MessageLoop::current()->timer_manager()->StopTimer(timer_); - delete timer_; - delete timer_task_; + timer_.Stop(); WaitForPendingIO(&num_pending_io_); DCHECK(!num_refs_); diff --git a/net/disk_cache/backend_impl.h b/net/disk_cache/backend_impl.h index 04a228d..7a39238 100644 --- a/net/disk_cache/backend_impl.h +++ b/net/disk_cache/backend_impl.h @@ -7,14 +7,13 @@ #ifndef NET_DISK_CACHE_BACKEND_IMPL_H__ #define NET_DISK_CACHE_BACKEND_IMPL_H__ +#include "base/timer.h" #include "net/disk_cache/block_files.h" #include "net/disk_cache/disk_cache.h" #include "net/disk_cache/rankings.h" #include "net/disk_cache/stats.h" #include "net/disk_cache/trace.h" -class Timer; - namespace disk_cache { // This class implements the Backend interface. An object of this @@ -176,8 +175,7 @@ class BackendImpl : public Backend { bool disabled_; Stats stats_; // Usage statistcs. - Task* timer_task_; - Timer* timer_; // Usage timer. + base::RepeatingTimer<BackendImpl> timer_; // Usage timer. TraceObject trace_object_; // Inits and destroys internal tracing. DISALLOW_EVIL_CONSTRUCTORS(BackendImpl); diff --git a/net/disk_cache/stress_cache.cc b/net/disk_cache/stress_cache.cc index 10058f0..864309a 100644 --- a/net/disk_cache/stress_cache.cc +++ b/net/disk_cache/stress_cache.cc @@ -143,6 +143,9 @@ class CrashTask : public Task { ~CrashTask() {} virtual void Run() { + // Keep trying to run. + RunSoon(MessageLoop::current()); + if (g_crashing) return; @@ -151,6 +154,12 @@ class CrashTask : public Task { TerminateProcess(GetCurrentProcess(), kExpectedCrash); } } + + static void RunSoon(MessageLoop* target_loop) { + int task_delay = 10000; // 10 seconds + CrashTask* task = new CrashTask(); + target_loop->PostDelayedTask(FROM_HERE, task, task_delay); + } }; // We leak everything here :) @@ -159,11 +168,7 @@ bool StartCrashThread() { if (!thread->Start()) return false; - // Create a recurrent timer of 10 secs. - int timer_delay = 10000; - CrashTask* task = new CrashTask(); - thread->message_loop()->timer_manager()->StartTimer(timer_delay, task, true); - + CrashTask::RunSoon(thread->message_loop()); return true; } |