diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 1 | ||||
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/deferred_sequenced_task_runner.cc | 99 | ||||
-rw-r--r-- | base/deferred_sequenced_task_runner.h | 79 | ||||
-rw-r--r-- | base/deferred_sequenced_task_runner_unittest.cc | 184 |
5 files changed, 365 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp index 290961e..b026dcb 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -463,6 +463,7 @@ 'debug/trace_event_unittest.cc', 'debug/trace_event_unittest.h', 'debug/trace_event_win_unittest.cc', + 'deferred_sequenced_task_runner_unittest.cc', 'environment_unittest.cc', 'file_util_unittest.cc', 'file_version_info_unittest.cc', diff --git a/base/base.gypi b/base/base.gypi index f26c4c7..164aa23 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -137,6 +137,8 @@ 'debug/trace_event_impl.cc', 'debug/trace_event_impl.h', 'debug/trace_event_win.cc', + 'deferred_sequenced_task_runner.cc', + 'deferred_sequenced_task_runner.h', 'environment.cc', 'environment.h', 'file_descriptor_posix.h', diff --git a/base/deferred_sequenced_task_runner.cc b/base/deferred_sequenced_task_runner.cc new file mode 100644 index 0000000..c96704c --- /dev/null +++ b/base/deferred_sequenced_task_runner.cc @@ -0,0 +1,99 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/deferred_sequenced_task_runner.h" + +#include "base/bind.h" +#include "base/logging.h" + +namespace base { + +DeferredSequencedTaskRunner::DeferredTask::DeferredTask() { +} + +DeferredSequencedTaskRunner::DeferredTask::~DeferredTask() { +} + +DeferredSequencedTaskRunner::DeferredSequencedTaskRunner( + const scoped_refptr<SequencedTaskRunner>& target_task_runner) + : started_(false), + target_task_runner_(target_task_runner) { +} + +DeferredSequencedTaskRunner::~DeferredSequencedTaskRunner() { +} + +bool DeferredSequencedTaskRunner::PostDelayedTask( + const tracked_objects::Location& from_here, + const Closure& task, + TimeDelta delay) { + AutoLock lock(lock_); + if (started_) { + DCHECK(deferred_tasks_queue_.empty()); + return target_task_runner_->PostDelayedTask(from_here, task, delay); + } + + QueueDeferredTask(from_here, task, delay, false /* is_non_nestable */); + return true; +} + +bool DeferredSequencedTaskRunner::RunsTasksOnCurrentThread() const { + return target_task_runner_->RunsTasksOnCurrentThread(); +} + +bool DeferredSequencedTaskRunner::PostNonNestableDelayedTask( + const tracked_objects::Location& from_here, + const Closure& task, + TimeDelta delay) { + AutoLock lock(lock_); + if (started_) { + DCHECK(deferred_tasks_queue_.empty()); + return target_task_runner_->PostNonNestableDelayedTask(from_here, + task, + delay); + } + QueueDeferredTask(from_here, task, delay, true /* is_non_nestable */); + return true; +} + +void DeferredSequencedTaskRunner::QueueDeferredTask( + const tracked_objects::Location& from_here, + const Closure& task, + TimeDelta delay, + bool is_non_nestable) { + DeferredTask deferred_task; + deferred_task.posted_from = from_here; + deferred_task.task = task; + deferred_task.delay = delay; + deferred_task.is_non_nestable = is_non_nestable; + deferred_tasks_queue_.push_back(deferred_task); +} + + +void DeferredSequencedTaskRunner::Start() { + AutoLock lock(lock_); + DCHECK(!started_); + started_ = true; + for (std::vector<DeferredTask>::iterator i = deferred_tasks_queue_.begin(); + i != deferred_tasks_queue_.end(); + ++i) { + const DeferredTask& task = *i; + if (task.is_non_nestable) { + target_task_runner_->PostNonNestableDelayedTask(task.posted_from, + task.task, + task.delay); + } else { + target_task_runner_->PostDelayedTask(task.posted_from, + task.task, + task.delay); + } + // Replace the i-th element in the |deferred_tasks_queue_| with an empty + // |DelayedTask| to ensure that |task| is destroyed before the next task + // is posted. + *i = DeferredTask(); + } + deferred_tasks_queue_.clear(); +} + +} // namespace base diff --git a/base/deferred_sequenced_task_runner.h b/base/deferred_sequenced_task_runner.h new file mode 100644 index 0000000..d1cdb43 --- /dev/null +++ b/base/deferred_sequenced_task_runner.h @@ -0,0 +1,79 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_DEFERRED_SEQUENCED_TASKRUNNER_H_ +#define BASE_DEFERRED_SEQUENCED_TASKRUNNER_H_ + +#include <vector> + +#include "base/base_export.h" +#include "base/basictypes.h" +#include "base/callback.h" +#include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" +#include "base/sequenced_task_runner.h" +#include "base/synchronization/lock.h" +#include "base/time.h" +#include "base/tracked_objects.h" + +namespace base { + +// A DeferredSequencedTaskRunner is a subclass of SequencedTaskRunner that +// queues up all requests until the first call to Start() is issued. +class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner { + public: + explicit DeferredSequencedTaskRunner( + const scoped_refptr<SequencedTaskRunner>& target_runner); + + // TaskRunner implementation + virtual bool PostDelayedTask(const tracked_objects::Location& from_here, + const Closure& task, + TimeDelta delay) OVERRIDE; + virtual bool RunsTasksOnCurrentThread() const OVERRIDE; + + // SequencedTaskRunner implementation + virtual bool PostNonNestableDelayedTask( + const tracked_objects::Location& from_here, + const Closure& task, + TimeDelta delay) OVERRIDE; + + // Start the execution - posts all queued tasks to the target executor. The + // deferred tasks are posted with their initial delay, meaning that the task + // execution delay is actually measured from Start. + // Fails when called a second time. + void Start(); + + private: + struct DeferredTask { + DeferredTask(); + ~DeferredTask(); + + tracked_objects::Location posted_from; + Closure task; + // The delay this task was initially posted with. + TimeDelta delay; + bool is_non_nestable; + }; + + virtual ~DeferredSequencedTaskRunner(); + + // Creates a |Task| object and adds it to |deferred_tasks_queue_|. + void QueueDeferredTask(const tracked_objects::Location& from_here, + const Closure& task, + TimeDelta delay, + bool is_non_nestable); + + // // Protects |started_| and |deferred_tasks_queue_|. + mutable Lock lock_; + + bool started_; + const scoped_refptr<SequencedTaskRunner> target_task_runner_; + std::vector<DeferredTask> deferred_tasks_queue_; + + DISALLOW_COPY_AND_ASSIGN(DeferredSequencedTaskRunner); +}; + +} // namespace base + +#endif // BASE_DEFERRED_SEQUENCED_TASKRUNNER_H_ diff --git a/base/deferred_sequenced_task_runner_unittest.cc b/base/deferred_sequenced_task_runner_unittest.cc new file mode 100644 index 0000000..c033ba8 --- /dev/null +++ b/base/deferred_sequenced_task_runner_unittest.cc @@ -0,0 +1,184 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/deferred_sequenced_task_runner.h" + +#include "base/basictypes.h" +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/memory/ref_counted.h" +#include "base/message_loop.h" +#include "base/message_loop_proxy.h" +#include "base/threading/non_thread_safe.h" +#include "base/threading/thread.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class DeferredSequencedTaskRunnerTest : public testing::Test, + public base::NonThreadSafe { + public: + class ExecuteTaskOnDestructor : + public base::RefCounted<ExecuteTaskOnDestructor> { + public: + ExecuteTaskOnDestructor( + DeferredSequencedTaskRunnerTest* executor, + int task_id) + : executor_(executor), + task_id_(task_id) { + } + private: + friend class base::RefCounted<ExecuteTaskOnDestructor>; + virtual ~ExecuteTaskOnDestructor() { + executor_->ExecuteTask(task_id_); + } + DeferredSequencedTaskRunnerTest* executor_; + int task_id_; + }; + + void ExecuteTask(int task_id) { + base::AutoLock lock(lock_); + executed_task_ids_.push_back(task_id); + } + + void PostExecuteTask(int task_id) { + runner_->PostTask(FROM_HERE, + base::Bind(&DeferredSequencedTaskRunnerTest::ExecuteTask, + base::Unretained(this), + task_id)); + } + + void StartRunner() { + runner_->Start(); + } + + void DoNothing(ExecuteTaskOnDestructor* object) { + } + + protected: + DeferredSequencedTaskRunnerTest() : + loop_(), + runner_( + new base::DeferredSequencedTaskRunner(loop_.message_loop_proxy())) { + } + + MessageLoop loop_; + scoped_refptr<base::DeferredSequencedTaskRunner> runner_; + mutable base::Lock lock_; + std::vector<int> executed_task_ids_; +}; + +TEST_F(DeferredSequencedTaskRunnerTest, Stopped) { + PostExecuteTask(1); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); +} + +TEST_F(DeferredSequencedTaskRunnerTest, Start) { + StartRunner(); + PostExecuteTask(1); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1)); +} + +TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) { + StartRunner(); + for (int i = 1; i < 5; ++i) + PostExecuteTask(i); + + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4)); +} + +TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) { + PostExecuteTask(1); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); + + StartRunner(); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1)); + + PostExecuteTask(2); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2)); +} + +TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) { + for (int i = 1; i < 5; ++i) + PostExecuteTask(i); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre()); + + StartRunner(); + for (int i = 5; i < 9; ++i) + PostExecuteTask(i); + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); +} + +TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) { + { + base::Thread thread1("DeferredSequencedTaskRunnerTestThread1"); + base::Thread thread2("DeferredSequencedTaskRunnerTestThread2"); + thread1.Start(); + thread2.Start(); + for (int i = 0; i < 5; ++i) { + thread1.message_loop()->PostTask( + FROM_HERE, + base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask, + base::Unretained(this), + 2 * i)); + thread2.message_loop()->PostTask( + FROM_HERE, + base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask, + base::Unretained(this), + 2 * i + 1)); + if (i == 2) { + thread1.message_loop()->PostTask( + FROM_HERE, + base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner, + base::Unretained(this))); + } + } + } + + loop_.RunUntilIdle(); + EXPECT_THAT(executed_task_ids_, + testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))); +} + +TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) { + { + base::Thread thread("DeferredSequencedTaskRunnerTestThread"); + thread.Start(); + runner_ = + new base::DeferredSequencedTaskRunner(thread.message_loop_proxy()); + for (int i = 0; i < 5; ++i) { + { + // Use a block to ensure that no reference to |short_lived_object| + // is kept on the main thread after it is posted to |runner_|. + scoped_refptr<ExecuteTaskOnDestructor> short_lived_object = + new ExecuteTaskOnDestructor(this, 2 * i); + runner_->PostTask( + FROM_HERE, + base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing, + base::Unretained(this), + short_lived_object)); + } + // |short_lived_object| with id |2 * i| should be destroyed before the + // task |2 * i + 1| is executed. + PostExecuteTask(2 * i + 1); + } + StartRunner(); + } + + // All |short_lived_object| with id |2 * i| are destroyed before the task + // |2 * i + 1| is executed. + EXPECT_THAT(executed_task_ids_, + testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); +} + +} // namespace |