summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authordcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-23 20:45:43 +0000
committerdcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-23 20:45:43 +0000
commit6e238baa521f8714c1d26d22868d9a83be0b6e7c (patch)
treeaf2625b6b39f46bb1b41b4ec40b85d47a49fb63f /base
parent46142951d762d840aaa2aa113e03b70e4ff0439f (diff)
downloadchromium_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')
-rw-r--r--base/synchronization/cancellation_flag_unittest.cc15
-rw-r--r--base/synchronization/waitable_event_watcher.h3
-rw-r--r--base/synchronization/waitable_event_watcher_posix.cc66
-rw-r--r--base/threading/thread.cc18
-rw-r--r--base/threading/thread.h2
-rw-r--r--base/threading/thread_unittest.cc37
-rw-r--r--base/threading/worker_pool_unittest.cc24
7 files changed, 61 insertions, 104 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;
}
diff --git a/base/threading/thread.cc b/base/threading/thread.cc
index 076580d..7188e9f 100644
--- a/base/threading/thread.cc
+++ b/base/threading/thread.cc
@@ -4,6 +4,7 @@
#include "base/threading/thread.h"
+#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_local.h"
@@ -22,14 +23,11 @@ base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool =
} // namespace
-// This task is used to trigger the message loop to exit.
-class ThreadQuitTask : public Task {
- public:
- virtual void Run() {
- MessageLoop::current()->Quit();
- Thread::SetThreadWasQuitProperly(true);
- }
-};
+// This is used to trigger the message loop to exit.
+void ThreadQuitHelper() {
+ MessageLoop::current()->Quit();
+ Thread::SetThreadWasQuitProperly(true);
+}
// Used to pass data to ThreadMain. This structure is allocated on the stack
// from within StartWithOptions.
@@ -121,7 +119,7 @@ void Thread::StopSoon() {
return;
stopping_ = true;
- message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
+ message_loop_->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper));
}
void Thread::Run(MessageLoop* message_loop) {
@@ -165,7 +163,7 @@ void Thread::ThreadMain() {
// Let the thread do extra cleanup.
CleanUp();
- // Assert that MessageLoop::Quit was called by ThreadQuitTask.
+ // Assert that MessageLoop::Quit was called by ThreadQuitHelper.
DCHECK(GetThreadWasQuitProperly());
// We can't receive messages anymore.
diff --git a/base/threading/thread.h b/base/threading/thread.h
index 3f4fa50..be8cc8f 100644
--- a/base/threading/thread.h
+++ b/base/threading/thread.h
@@ -182,7 +182,7 @@ class BASE_EXPORT Thread : PlatformThread::Delegate {
// The name of the thread. Used for debugging purposes.
std::string name_;
- friend class ThreadQuitTask;
+ friend void ThreadQuitHelper();
DISALLOW_COPY_AND_ASSIGN(Thread);
};
diff --git a/base/threading/thread_unittest.cc b/base/threading/thread_unittest.cc
index 68a24cf..a1a237f 100644
--- a/base/threading/thread_unittest.cc
+++ b/base/threading/thread_unittest.cc
@@ -6,6 +6,7 @@
#include <vector>
+#include "base/bind.h"
#include "base/message_loop.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -17,29 +18,11 @@ typedef PlatformTest ThreadTest;
namespace {
-class ToggleValue : public Task {
- public:
- explicit ToggleValue(bool* value) : value_(value) {
- ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean "
- "in base/thread_unittest");
- }
- virtual void Run() {
- *value_ = !*value_;
- }
- private:
- bool* value_;
-};
-
-class SleepSome : public Task {
- public:
- explicit SleepSome(int msec) : msec_(msec) {
- }
- virtual void Run() {
- base::PlatformThread::Sleep(msec_);
- }
- private:
- int msec_;
-};
+void ToggleValue(bool* value) {
+ ANNOTATE_BENIGN_RACE(value, "Test-only data race on boolean "
+ "in base/thread_unittest");
+ *value = !*value;
+}
class SleepInsideInitThread : public Thread {
public:
@@ -173,7 +156,7 @@ TEST_F(ThreadTest, StartWithOptions_StackSize) {
EXPECT_TRUE(a.IsRunning());
bool was_invoked = false;
- a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
+ a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue, &was_invoked));
// wait for the task to run (we could use a kernel event here
// instead to avoid busy waiting, but this is sufficient for
@@ -194,8 +177,10 @@ TEST_F(ThreadTest, TwoTasks) {
// Test that all events are dispatched before the Thread object is
// destroyed. We do this by dispatching a sleep event before the
// event that will toggle our sentinel value.
- a.message_loop()->PostTask(FROM_HERE, new SleepSome(20));
- a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked));
+ a.message_loop()->PostTask(FROM_HERE,
+ base::Bind(&base::PlatformThread::Sleep, 20));
+ a.message_loop()->PostTask(FROM_HERE, base::Bind(&ToggleValue,
+ &was_invoked));
}
EXPECT_TRUE(was_invoked);
}
diff --git a/base/threading/worker_pool_unittest.cc b/base/threading/worker_pool_unittest.cc
index cd20f47..d36cb1b 100644
--- a/base/threading/worker_pool_unittest.cc
+++ b/base/threading/worker_pool_unittest.cc
@@ -5,6 +5,7 @@
#include "base/threading/worker_pool.h"
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/message_loop.h"
#include "base/task.h"
@@ -21,19 +22,6 @@ namespace base {
namespace {
-class PostTaskTestTask : public Task {
- public:
- explicit PostTaskTestTask(WaitableEvent* event) : event_(event) {
- }
-
- void Run() {
- event_->Signal();
- }
-
- private:
- WaitableEvent* event_;
-};
-
class PostTaskAndReplyTester
: public base::RefCountedThreadSafe<PostTaskAndReplyTester> {
public:
@@ -81,8 +69,14 @@ TEST_F(WorkerPoolTest, PostTask) {
WaitableEvent test_event(false, false);
WaitableEvent long_test_event(false, false);
- WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&test_event), false);
- WorkerPool::PostTask(FROM_HERE, new PostTaskTestTask(&long_test_event), true);
+ WorkerPool::PostTask(FROM_HERE,
+ base::Bind(&WaitableEvent::Signal,
+ base::Unretained(&test_event)),
+ false);
+ WorkerPool::PostTask(FROM_HERE,
+ base::Bind(&WaitableEvent::Signal,
+ base::Unretained(&long_test_event)),
+ true);
test_event.Wait();
long_test_event.Wait();