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/task_scheduler/scheduler_thread_pool.h" | 5 #include "base/task_scheduler/scheduler_thread_pool.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.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/sequenced_task_runner.h" |
14 #include "base/task_scheduler/utils.h" | 15 #include "base/task_scheduler/utils.h" |
15 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 namespace internal { | 19 namespace internal { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // SchedulerThreadPool that owns the current thread, if any. | 23 // SchedulerThreadPool that owns the current thread, if any. |
23 LazyInstance<ThreadLocalPointer<const SchedulerTaskExecutor>>::Leaky | 24 LazyInstance<ThreadLocalPointer<const SchedulerTaskExecutor>>::Leaky |
(...skipping 27 matching lines...) Expand all Loading... |
51 private: | 52 private: |
52 ~SchedulerParallelTaskRunner() override = default; | 53 ~SchedulerParallelTaskRunner() override = default; |
53 | 54 |
54 const TaskTraits traits_; | 55 const TaskTraits traits_; |
55 TaskTracker* const task_tracker_; | 56 TaskTracker* const task_tracker_; |
56 SchedulerTaskExecutor* const executor_; | 57 SchedulerTaskExecutor* const executor_; |
57 | 58 |
58 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | 59 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); |
59 }; | 60 }; |
60 | 61 |
| 62 // A task runner that runs tasks with the SEQUENCED ExecutionMode. |
| 63 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { |
| 64 public: |
| 65 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so |
| 66 // long as |executor| is alive. |
| 67 // TODO(robliao): Find a concrete way to manage |executor|'s memory. |
| 68 SchedulerSequencedTaskRunner(const TaskTraits& traits, |
| 69 TaskTracker* task_tracker, |
| 70 SchedulerTaskExecutor* executor) |
| 71 : traits_(traits), task_tracker_(task_tracker), executor_(executor) {} |
| 72 |
| 73 // SequencedTaskRunner: |
| 74 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 75 const Closure& closure, |
| 76 TimeDelta delay) override { |
| 77 // Post the task as part of |sequence|. |
| 78 return PostTaskToExecutor(from_here, closure, traits_, delay, sequence_, |
| 79 executor_, task_tracker_); |
| 80 } |
| 81 |
| 82 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 83 const Closure& closure, |
| 84 base::TimeDelta delay) override { |
| 85 // Tasks are never nested within the task scheduler. |
| 86 return PostDelayedTask(from_here, closure, delay); |
| 87 } |
| 88 |
| 89 bool RunsTasksOnCurrentThread() const override { |
| 90 return tls_current_thread_pool.Get().Get() == executor_; |
| 91 } |
| 92 |
| 93 private: |
| 94 ~SchedulerSequencedTaskRunner() override = default; |
| 95 |
| 96 // Sequence for all Tasks posted through this TaskRunner. |
| 97 const scoped_refptr<Sequence> sequence_ = new Sequence; |
| 98 |
| 99 const TaskTraits traits_; |
| 100 TaskTracker* const task_tracker_; |
| 101 SchedulerTaskExecutor* const executor_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); |
| 104 }; |
| 105 |
61 } // namespace | 106 } // namespace |
62 | 107 |
63 class SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl | 108 class SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl |
64 : public SchedulerWorkerThread::Delegate { | 109 : public SchedulerWorkerThread::Delegate { |
65 public: | 110 public: |
66 SchedulerWorkerThreadDelegateImpl( | 111 SchedulerWorkerThreadDelegateImpl( |
67 SchedulerThreadPool* outer, | 112 SchedulerThreadPool* outer, |
68 const EnqueueSequenceCallback& enqueue_sequence_callback); | 113 const EnqueueSequenceCallback& enqueue_sequence_callback); |
69 ~SchedulerWorkerThreadDelegateImpl() override; | 114 ~SchedulerWorkerThreadDelegateImpl() override; |
70 | 115 |
(...skipping 30 matching lines...) Expand all Loading... |
101 | 146 |
102 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( | 147 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( |
103 const TaskTraits& traits, | 148 const TaskTraits& traits, |
104 ExecutionMode execution_mode) { | 149 ExecutionMode execution_mode) { |
105 switch (execution_mode) { | 150 switch (execution_mode) { |
106 case ExecutionMode::PARALLEL: | 151 case ExecutionMode::PARALLEL: |
107 return make_scoped_refptr( | 152 return make_scoped_refptr( |
108 new SchedulerParallelTaskRunner(traits, task_tracker_, this)); | 153 new SchedulerParallelTaskRunner(traits, task_tracker_, this)); |
109 | 154 |
110 case ExecutionMode::SEQUENCED: | 155 case ExecutionMode::SEQUENCED: |
| 156 return make_scoped_refptr( |
| 157 new SchedulerSequencedTaskRunner(traits, task_tracker_, this)); |
| 158 |
111 case ExecutionMode::SINGLE_THREADED: | 159 case ExecutionMode::SINGLE_THREADED: |
112 // TODO(fdoray): Support SEQUENCED and SINGLE_THREADED TaskRunners. | 160 // TODO(fdoray): Support SINGLE_THREADED TaskRunners. |
113 NOTREACHED(); | 161 NOTREACHED(); |
114 return nullptr; | 162 return nullptr; |
115 } | 163 } |
116 | 164 |
117 NOTREACHED(); | 165 NOTREACHED(); |
118 return nullptr; | 166 return nullptr; |
119 } | 167 } |
120 | 168 |
121 void SchedulerThreadPool::EnqueueSequence( | 169 void SchedulerThreadPool::EnqueueSequence( |
122 scoped_refptr<Sequence> sequence, | 170 scoped_refptr<Sequence> sequence, |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 if (idle_worker_threads_stack_.empty()) | 316 if (idle_worker_threads_stack_.empty()) |
269 return nullptr; | 317 return nullptr; |
270 | 318 |
271 auto worker_thread = idle_worker_threads_stack_.top(); | 319 auto worker_thread = idle_worker_threads_stack_.top(); |
272 idle_worker_threads_stack_.pop(); | 320 idle_worker_threads_stack_.pop(); |
273 return worker_thread; | 321 return worker_thread; |
274 } | 322 } |
275 | 323 |
276 } // namespace internal | 324 } // namespace internal |
277 } // namespace base | 325 } // namespace base |
OLD | NEW |