diff options
author | dcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-23 20:45:43 +0000 |
---|---|---|
committer | dcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-23 20:45:43 +0000 |
commit | 6e238baa521f8714c1d26d22868d9a83be0b6e7c (patch) | |
tree | af2625b6b39f46bb1b41b4ec40b85d47a49fb63f /base/synchronization | |
parent | 46142951d762d840aaa2aa113e03b70e4ff0439f (diff) | |
download | chromium_src-6e238baa521f8714c1d26d22868d9a83be0b6e7c.zip chromium_src-6e238baa521f8714c1d26d22868d9a83be0b6e7c.tar.gz chromium_src-6e238baa521f8714c1d26d22868d9a83be0b6e7c.tar.bz2 |
Remove custom Task implementations in base.
I left a few unconverted in this one, because they made my head hurt.
BUG=none
TEST=base_unittests
Review URL: http://codereview.chromium.org/8653006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111407 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/synchronization')
-rw-r--r-- | base/synchronization/cancellation_flag_unittest.cc | 15 | ||||
-rw-r--r-- | base/synchronization/waitable_event_watcher.h | 3 | ||||
-rw-r--r-- | base/synchronization/waitable_event_watcher_posix.cc | 66 |
3 files changed, 32 insertions, 52 deletions
diff --git a/base/synchronization/cancellation_flag_unittest.cc b/base/synchronization/cancellation_flag_unittest.cc index e4b5830..9a845de 100644 --- a/base/synchronization/cancellation_flag_unittest.cc +++ b/base/synchronization/cancellation_flag_unittest.cc @@ -6,6 +6,7 @@ #include "base/synchronization/cancellation_flag.h" +#include "base/bind.h" #include "base/logging.h" #include "base/message_loop.h" #include "base/spin_wait.h" @@ -22,17 +23,11 @@ namespace { // Define our test class. //------------------------------------------------------------------------------ -class CancelTask : public Task { - public: - explicit CancelTask(CancellationFlag* flag) : flag_(flag) {} - virtual void Run() { +void CancelHelper(CancellationFlag* flag) { #if GTEST_HAS_DEATH_TEST - ASSERT_DEBUG_DEATH(flag_->Set(), ""); + ASSERT_DEBUG_DEATH(flag->Set(), ""); #endif - } - private: - CancellationFlag* flag_; -}; +} TEST(CancellationFlagTest, SimpleSingleThreadedTest) { CancellationFlag flag; @@ -61,7 +56,7 @@ TEST(CancellationFlagTest, SetOnDifferentThreadDeathTest) { ASSERT_TRUE(t.IsRunning()); CancellationFlag flag; - t.message_loop()->PostTask(FROM_HERE, new CancelTask(&flag)); + t.message_loop()->PostTask(FROM_HERE, base::Bind(&CancelHelper, &flag)); } } // namespace diff --git a/base/synchronization/waitable_event_watcher.h b/base/synchronization/waitable_event_watcher.h index 5bfe5f7..966a468 100644 --- a/base/synchronization/waitable_event_watcher.h +++ b/base/synchronization/waitable_event_watcher.h @@ -11,6 +11,7 @@ #if defined(OS_WIN) #include "base/win/object_watcher.h" #else +#include "base/callback.h" #include "base/message_loop.h" #include "base/synchronization/waitable_event.h" #endif @@ -150,7 +151,7 @@ class BASE_EXPORT WaitableEventWatcher MessageLoop* message_loop_; scoped_refptr<Flag> cancel_flag_; AsyncWaiter* waiter_; - AsyncCallbackTask* callback_task_; + base::Closure callback_; scoped_refptr<WaitableEvent::WaitableEventKernel> kernel_; #endif diff --git a/base/synchronization/waitable_event_watcher_posix.cc b/base/synchronization/waitable_event_watcher_posix.cc index cdb7df690..3b0ba70 100644 --- a/base/synchronization/waitable_event_watcher_posix.cc +++ b/base/synchronization/waitable_event_watcher_posix.cc @@ -4,6 +4,7 @@ #include "base/synchronization/waitable_event_watcher.h" +#include "base/bind.h" #include "base/location.h" #include "base/message_loop.h" #include "base/synchronization/lock.h" @@ -52,18 +53,17 @@ class Flag : public RefCountedThreadSafe<Flag> { // ----------------------------------------------------------------------------- class AsyncWaiter : public WaitableEvent::Waiter { public: - AsyncWaiter(MessageLoop* message_loop, Task* task, Flag* flag) + AsyncWaiter(MessageLoop* message_loop, + const base::Closure& callback, + Flag* flag) : message_loop_(message_loop), - cb_task_(task), + callback_(callback), flag_(flag) { } bool Fire(WaitableEvent* event) { - if (flag_->value()) { - // If the callback has been canceled, we don't enqueue the task, we just - // delete it instead. - delete cb_task_; - } else { - message_loop_->PostTask(FROM_HERE, cb_task_); + // Post the callback if we haven't been cancelled. + if (!flag_->value()) { + message_loop_->PostTask(FROM_HERE, callback_); } // We are removed from the wait-list by the WaitableEvent itself. It only @@ -82,47 +82,31 @@ class AsyncWaiter : public WaitableEvent::Waiter { private: MessageLoop *const message_loop_; - Task *const cb_task_; + base::Closure callback_; scoped_refptr<Flag> flag_; }; // ----------------------------------------------------------------------------- // For async waits we need to make a callback in a MessageLoop thread. We do -// this by posting this task, which calls the delegate and keeps track of when +// this by posting a callback, which calls the delegate and keeps track of when // the event is canceled. // ----------------------------------------------------------------------------- -class AsyncCallbackTask : public Task { - public: - AsyncCallbackTask(Flag* flag, WaitableEventWatcher::Delegate* delegate, - WaitableEvent* event) - : flag_(flag), - delegate_(delegate), - event_(event) { - } - - void Run() { - // Runs in MessageLoop thread. - if (!flag_->value()) { - // This is to let the WaitableEventWatcher know that the event has occured - // because it needs to be able to return NULL from GetWatchedObject - flag_->Set(); - delegate_->OnWaitableEventSignaled(event_); - } - - // We are deleted by the MessageLoop +void AsyncCallbackHelper(Flag* flag, + WaitableEventWatcher::Delegate* delegate, + WaitableEvent* event) { + // Runs in MessageLoop thread. + if (!flag->value()) { + // This is to let the WaitableEventWatcher know that the event has occured + // because it needs to be able to return NULL from GetWatchedObject + flag->Set(); + delegate->OnWaitableEventSignaled(event); } - - private: - scoped_refptr<Flag> flag_; - WaitableEventWatcher::Delegate *const delegate_; - WaitableEvent *const event_; -}; +} WaitableEventWatcher::WaitableEventWatcher() : message_loop_(NULL), cancel_flag_(NULL), waiter_(NULL), - callback_task_(NULL), event_(NULL), delegate_(NULL) { } @@ -143,7 +127,7 @@ bool WaitableEventWatcher::StartWatching // A user may call StartWatching from within the callback function. In this // case, we won't know that we have finished watching, expect that the Flag - // will have been set in AsyncCallbackTask::Run() + // will have been set in AsyncCallbackHelper(). if (cancel_flag_.get() && cancel_flag_->value()) { if (message_loop_) { message_loop_->RemoveDestructionObserver(this); @@ -156,7 +140,7 @@ bool WaitableEventWatcher::StartWatching DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; cancel_flag_ = new Flag; - callback_task_ = new AsyncCallbackTask(cancel_flag_, delegate, event); + callback_ = base::Bind(&AsyncCallbackHelper, cancel_flag_, delegate, event); WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); AutoLock locked(kernel->lock_); @@ -170,7 +154,7 @@ bool WaitableEventWatcher::StartWatching // No hairpinning - we can't call the delegate directly here. We have to // enqueue a task on the MessageLoop as normal. - current_ml->PostTask(FROM_HERE, callback_task_); + current_ml->PostTask(FROM_HERE, callback_); return true; } @@ -178,7 +162,7 @@ bool WaitableEventWatcher::StartWatching current_ml->AddDestructionObserver(this); kernel_ = kernel; - waiter_ = new AsyncWaiter(current_ml, callback_task_, cancel_flag_); + waiter_ = new AsyncWaiter(current_ml, callback_, cancel_flag_); event->Enqueue(waiter_); return true; @@ -238,7 +222,7 @@ void WaitableEventWatcher::StopWatching() { // have been enqueued with the MessageLoop because the waiter was never // signaled) delete waiter_; - delete callback_task_; + callback_.Reset(); cancel_flag_ = NULL; return; } |