| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/scoped_task_scheduler.h" | 5 #include "base/test/scoped_task_scheduler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram_base.h" | 16 #include "base/metrics/histogram_base.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/sequence_token.h" | 18 #include "base/sequence_token.h" |
| 19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
| 22 #include "base/task_scheduler/task.h" | 22 #include "base/task_scheduler/task.h" |
| 23 #include "base/task_scheduler/task_scheduler.h" | 23 #include "base/task_scheduler/task_scheduler.h" |
| 24 #include "base/task_scheduler/task_tracker.h" | 24 #include "base/task_scheduler/task_tracker.h" |
| 25 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
| 26 #include "base/time/time.h" |
| 27 #include "build/build_config.h" |
| 28 |
| 29 #if defined(OS_WIN) |
| 30 #include "base/win/scoped_com_initializer.h" |
| 31 #endif // defined(OS_WIN) |
| 26 | 32 |
| 27 namespace base { | 33 namespace base { |
| 28 namespace test { | 34 namespace test { |
| 29 | 35 |
| 30 namespace { | 36 namespace { |
| 31 | 37 |
| 32 enum class ExecutionMode { PARALLEL, SEQUENCED, SINGLE_THREADED }; | 38 enum class ExecutionMode { PARALLEL, SEQUENCED, SINGLE_THREADED }; |
| 33 | 39 |
| 34 class TestTaskScheduler : public TaskScheduler { | 40 class TestTaskScheduler : public TaskScheduler { |
| 35 public: | 41 public: |
| 36 // |external_message_loop| is an externally provided MessageLoop on which to | 42 // |external_message_loop| is an externally provided MessageLoop on which to |
| 37 // run tasks. A MessageLoop will be created by TestTaskScheduler if | 43 // run tasks. A MessageLoop will be created by TestTaskScheduler if |
| 38 // |external_message_loop| is nullptr. | 44 // |external_message_loop| is nullptr. |
| 39 explicit TestTaskScheduler(MessageLoop* external_message_loop); | 45 explicit TestTaskScheduler(MessageLoop* external_message_loop); |
| 40 ~TestTaskScheduler() override; | 46 ~TestTaskScheduler() override; |
| 41 | 47 |
| 42 // TaskScheduler: | 48 // TaskScheduler: |
| 43 void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here, | 49 void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here, |
| 44 const TaskTraits& traits, | 50 const TaskTraits& traits, |
| 45 const Closure& task, | 51 const Closure& task, |
| 46 TimeDelta delay) override; | 52 TimeDelta delay) override; |
| 47 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 53 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 48 const TaskTraits& traits) override; | 54 const TaskTraits& traits) override; |
| 49 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( | 55 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
| 50 const TaskTraits& traits) override; | 56 const TaskTraits& traits) override; |
| 51 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( | 57 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( |
| 52 const TaskTraits& traits) override; | 58 const TaskTraits& traits) override; |
| 59 #if defined(OS_WIN) |
| 60 scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( |
| 61 const TaskTraits& traits) override; |
| 62 #endif // defined(OS_WIN) |
| 53 std::vector<const HistogramBase*> GetHistograms() const override; | 63 std::vector<const HistogramBase*> GetHistograms() const override; |
| 54 int GetMaxConcurrentTasksWithTraitsDeprecated( | 64 int GetMaxConcurrentTasksWithTraitsDeprecated( |
| 55 const TaskTraits& traits) const override; | 65 const TaskTraits& traits) const override; |
| 56 void Shutdown() override; | 66 void Shutdown() override; |
| 57 void FlushForTesting() override; | 67 void FlushForTesting() override; |
| 58 void JoinForTesting() override; | 68 void JoinForTesting() override; |
| 59 | 69 |
| 60 // Posts |task| to this TaskScheduler with |sequence_token|. Returns true on | 70 // Posts |task| to this TaskScheduler with |sequence_token|. Returns true on |
| 61 // success. | 71 // success. |
| 62 bool PostTask(std::unique_ptr<internal::Task> task, | 72 bool PostTask(std::unique_ptr<internal::Task> task, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 73 // Returns the TaskRunner to which this TaskScheduler forwards tasks. It may | 83 // Returns the TaskRunner to which this TaskScheduler forwards tasks. It may |
| 74 // be |message_loop_->task_runner()| or a reference to it saved on entry to | 84 // be |message_loop_->task_runner()| or a reference to it saved on entry to |
| 75 // RunTask(). | 85 // RunTask(). |
| 76 scoped_refptr<SingleThreadTaskRunner> MessageLoopTaskRunner() const { | 86 scoped_refptr<SingleThreadTaskRunner> MessageLoopTaskRunner() const { |
| 77 if (saved_task_runner_) | 87 if (saved_task_runner_) |
| 78 return saved_task_runner_; | 88 return saved_task_runner_; |
| 79 DCHECK(message_loop_->task_runner()); | 89 DCHECK(message_loop_->task_runner()); |
| 80 return message_loop_->task_runner(); | 90 return message_loop_->task_runner(); |
| 81 } | 91 } |
| 82 | 92 |
| 93 #if defined(OS_WIN) |
| 94 void EnsureCOMSTA() { |
| 95 if (!scoped_com_initializer_) |
| 96 scoped_com_initializer_ = MakeUnique<win::ScopedCOMInitializer>(); |
| 97 } |
| 98 #endif // defined(OS_WIN) |
| 99 |
| 83 // |message_loop_owned_| will be non-null if this TestTaskScheduler owns the | 100 // |message_loop_owned_| will be non-null if this TestTaskScheduler owns the |
| 84 // MessageLoop (wasn't provided an external one at construction). | 101 // MessageLoop (wasn't provided an external one at construction). |
| 85 // |message_loop_| will always be set and is used by this TestTaskScheduler to | 102 // |message_loop_| will always be set and is used by this TestTaskScheduler to |
| 86 // run tasks. | 103 // run tasks. |
| 87 std::unique_ptr<MessageLoop> message_loop_owned_; | 104 std::unique_ptr<MessageLoop> message_loop_owned_; |
| 88 MessageLoop* message_loop_; | 105 MessageLoop* message_loop_; |
| 89 | 106 |
| 90 // A reference to |message_loop_->task_runner()| saved on entry to RunTask(). | 107 // A reference to |message_loop_->task_runner()| saved on entry to RunTask(). |
| 91 // This is required because RunTask() overrides | 108 // This is required because RunTask() overrides |
| 92 // |message_loop_->task_runner()|. | 109 // |message_loop_->task_runner()|. |
| 93 // | 110 // |
| 94 // Note: |message_loop_->task_runner()| is accessed directly outside of | 111 // Note: |message_loop_->task_runner()| is accessed directly outside of |
| 95 // RunTask() to guarantee that ScopedTaskScheduler always uses the latest | 112 // RunTask() to guarantee that ScopedTaskScheduler always uses the latest |
| 96 // TaskRunner set by external code. | 113 // TaskRunner set by external code. |
| 97 scoped_refptr<SingleThreadTaskRunner> saved_task_runner_; | 114 scoped_refptr<SingleThreadTaskRunner> saved_task_runner_; |
| 98 | 115 |
| 116 #if defined(OS_WIN) |
| 117 // Maintains the lifetime of the COM Single-Threaded Apartment. Allocation and |
| 118 // deallocation should be done in the |message_loop_| via PostTask. |
| 119 std::unique_ptr<win::ScopedCOMInitializer> scoped_com_initializer_; |
| 120 #endif // defined(OS_WIN) |
| 121 |
| 99 // Handles shutdown behaviors and sets up the environment to run a task. | 122 // Handles shutdown behaviors and sets up the environment to run a task. |
| 100 internal::TaskTracker task_tracker_; | 123 internal::TaskTracker task_tracker_; |
| 101 | 124 |
| 102 DISALLOW_COPY_AND_ASSIGN(TestTaskScheduler); | 125 DISALLOW_COPY_AND_ASSIGN(TestTaskScheduler); |
| 103 }; | 126 }; |
| 104 | 127 |
| 105 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner { | 128 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner { |
| 106 public: | 129 public: |
| 107 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler, | 130 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler, |
| 108 ExecutionMode execution_mode, | 131 ExecutionMode execution_mode, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 new TestTaskSchedulerTaskRunner(this, ExecutionMode::SEQUENCED, traits)); | 184 new TestTaskSchedulerTaskRunner(this, ExecutionMode::SEQUENCED, traits)); |
| 162 } | 185 } |
| 163 | 186 |
| 164 scoped_refptr<SingleThreadTaskRunner> | 187 scoped_refptr<SingleThreadTaskRunner> |
| 165 TestTaskScheduler::CreateSingleThreadTaskRunnerWithTraits( | 188 TestTaskScheduler::CreateSingleThreadTaskRunnerWithTraits( |
| 166 const TaskTraits& traits) { | 189 const TaskTraits& traits) { |
| 167 return make_scoped_refptr(new TestTaskSchedulerTaskRunner( | 190 return make_scoped_refptr(new TestTaskSchedulerTaskRunner( |
| 168 this, ExecutionMode::SINGLE_THREADED, traits)); | 191 this, ExecutionMode::SINGLE_THREADED, traits)); |
| 169 } | 192 } |
| 170 | 193 |
| 194 #if defined(OS_WIN) |
| 195 scoped_refptr<SingleThreadTaskRunner> |
| 196 TestTaskScheduler::CreateCOMSTATaskRunnerWithTraits(const TaskTraits& traits) { |
| 197 EnsureCOMSTA(); |
| 198 return make_scoped_refptr(new TestTaskSchedulerTaskRunner( |
| 199 this, ExecutionMode::SINGLE_THREADED, traits)); |
| 200 } |
| 201 #endif // defined(OS_WIN) |
| 202 |
| 171 std::vector<const HistogramBase*> TestTaskScheduler::GetHistograms() const { | 203 std::vector<const HistogramBase*> TestTaskScheduler::GetHistograms() const { |
| 172 NOTREACHED(); | 204 NOTREACHED(); |
| 173 return std::vector<const HistogramBase*>(); | 205 return std::vector<const HistogramBase*>(); |
| 174 } | 206 } |
| 175 | 207 |
| 176 int TestTaskScheduler::GetMaxConcurrentTasksWithTraitsDeprecated( | 208 int TestTaskScheduler::GetMaxConcurrentTasksWithTraitsDeprecated( |
| 177 const TaskTraits& traits) const { | 209 const TaskTraits& traits) const { |
| 178 return 1; | 210 return 1; |
| 179 } | 211 } |
| 180 | 212 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); | 319 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); |
| 288 | 320 |
| 289 // Per contract, call JoinForTesting() before deleting the TaskScheduler. | 321 // Per contract, call JoinForTesting() before deleting the TaskScheduler. |
| 290 TaskScheduler::GetInstance()->JoinForTesting(); | 322 TaskScheduler::GetInstance()->JoinForTesting(); |
| 291 | 323 |
| 292 TaskScheduler::SetInstance(nullptr); | 324 TaskScheduler::SetInstance(nullptr); |
| 293 } | 325 } |
| 294 | 326 |
| 295 } // namespace test | 327 } // namespace test |
| 296 } // namespace base | 328 } // namespace base |
| OLD | NEW |