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 // Shared PriorityQueue of a thread's SchedulerThreadPool. Not set for threads | 23 // Shared PriorityQueue of a thread's SchedulerThreadPool. Not set for threads |
23 // that don't belong to a SchedulerThreadPool. | 24 // that don't belong to a SchedulerThreadPool. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 private: | 57 private: |
57 ~SchedulerParallelTaskRunner() override = default; | 58 ~SchedulerParallelTaskRunner() override = default; |
58 | 59 |
59 const TaskTraits traits_; | 60 const TaskTraits traits_; |
60 PriorityQueue* const priority_queue_; | 61 PriorityQueue* const priority_queue_; |
61 TaskTracker* const task_tracker_; | 62 TaskTracker* const task_tracker_; |
62 | 63 |
63 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | 64 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); |
64 }; | 65 }; |
65 | 66 |
| 67 // A task runner that runs tasks with the SEQUENCED ExecutionMode. |
| 68 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { |
| 69 public: |
| 70 SchedulerSequencedTaskRunner(const TaskTraits& traits, |
| 71 PriorityQueue* priority_queue, |
| 72 TaskTracker* task_tracker) |
| 73 : traits_(traits), |
| 74 priority_queue_(priority_queue), |
| 75 task_tracker_(task_tracker) {} |
| 76 |
| 77 // SequencedTaskRunner: |
| 78 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 79 const Closure& closure, |
| 80 TimeDelta delay) override { |
| 81 // TODO(fdoray): Support delayed tasks. |
| 82 DCHECK(delay.is_zero()); |
| 83 |
| 84 return PostTaskHelper(WrapUnique(new Task(from_here, closure, traits_)), |
| 85 sequence_, priority_queue_, task_tracker_); |
| 86 } |
| 87 |
| 88 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 89 const Closure& closure, |
| 90 base::TimeDelta delay) override { |
| 91 // Tasks are never nested within the task scheduler. |
| 92 return PostDelayedTask(from_here, closure, delay); |
| 93 } |
| 94 |
| 95 bool RunsTasksOnCurrentThread() const override { |
| 96 return tls_current_shared_priority_queue.Get().Get() == priority_queue_; |
| 97 } |
| 98 |
| 99 private: |
| 100 ~SchedulerSequencedTaskRunner() override = default; |
| 101 |
| 102 // Sequence in which all Tasks posted through this TaskRunner are inserted. |
| 103 const scoped_refptr<Sequence> sequence_ = new Sequence; |
| 104 |
| 105 const TaskTraits traits_; |
| 106 PriorityQueue* const priority_queue_; |
| 107 TaskTracker* const task_tracker_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); |
| 110 }; |
| 111 |
66 } // namespace | 112 } // namespace |
67 | 113 |
68 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( | 114 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( |
69 ThreadPriority thread_priority, | 115 ThreadPriority thread_priority, |
70 size_t max_threads, | 116 size_t max_threads, |
71 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | 117 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, |
72 TaskTracker* task_tracker) { | 118 TaskTracker* task_tracker) { |
73 std::unique_ptr<SchedulerThreadPool> thread_pool( | 119 std::unique_ptr<SchedulerThreadPool> thread_pool( |
74 new SchedulerThreadPool(ran_task_from_sequence_callback, task_tracker)); | 120 new SchedulerThreadPool(ran_task_from_sequence_callback, task_tracker)); |
75 thread_pool->Initialize(thread_priority, max_threads); | 121 thread_pool->Initialize(thread_priority, max_threads); |
76 if (thread_pool->worker_threads_.empty()) | 122 if (thread_pool->worker_threads_.empty()) |
77 return nullptr; | 123 return nullptr; |
78 return thread_pool; | 124 return thread_pool; |
79 } | 125 } |
80 | 126 |
81 SchedulerThreadPool::~SchedulerThreadPool() { | 127 SchedulerThreadPool::~SchedulerThreadPool() { |
82 AutoSchedulerLock auto_lock(join_for_testing_returned_lock_); | 128 AutoSchedulerLock auto_lock(join_for_testing_returned_lock_); |
83 DCHECK(join_for_testing_returned_ || worker_threads_.empty()); | 129 DCHECK(join_for_testing_returned_ || worker_threads_.empty()); |
84 } | 130 } |
85 | 131 |
86 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( | 132 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( |
87 const TaskTraits& traits, | 133 const TaskTraits& traits, |
88 ExecutionMode execution_mode) { | 134 ExecutionMode execution_mode) { |
89 switch (execution_mode) { | 135 switch (execution_mode) { |
90 case ExecutionMode::PARALLEL: | 136 case ExecutionMode::PARALLEL: |
91 return make_scoped_refptr(new SchedulerParallelTaskRunner( | 137 return make_scoped_refptr(new SchedulerParallelTaskRunner( |
92 traits, &shared_priority_queue_, task_tracker_)); | 138 traits, &shared_priority_queue_, task_tracker_)); |
93 | 139 |
94 case ExecutionMode::SEQUENCED: | 140 case ExecutionMode::SEQUENCED: |
| 141 return make_scoped_refptr(new SchedulerSequencedTaskRunner( |
| 142 traits, &shared_priority_queue_, task_tracker_)); |
| 143 |
95 case ExecutionMode::SINGLE_THREADED: | 144 case ExecutionMode::SINGLE_THREADED: |
96 // TODO(fdoray): Support SEQUENCED and SINGLE_THREADED TaskRunners. | 145 // TODO(fdoray): Support SINGLE_THREADED TaskRunners. |
97 NOTREACHED(); | 146 NOTREACHED(); |
98 return nullptr; | 147 return nullptr; |
99 } | 148 } |
100 | 149 |
101 NOTREACHED(); | 150 NOTREACHED(); |
102 return nullptr; | 151 return nullptr; |
103 } | 152 } |
104 | 153 |
105 void SchedulerThreadPool::InsertSequenceAfterTaskRan( | 154 void SchedulerThreadPool::InsertSequenceAfterTaskRan( |
106 scoped_refptr<Sequence> sequence, | 155 scoped_refptr<Sequence> sequence, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 return sequence_and_sort_key.sequence; | 276 return sequence_and_sort_key.sequence; |
228 } | 277 } |
229 | 278 |
230 void SchedulerThreadPool::RanTaskFromSequence( | 279 void SchedulerThreadPool::RanTaskFromSequence( |
231 scoped_refptr<Sequence> sequence) { | 280 scoped_refptr<Sequence> sequence) { |
232 ran_task_from_sequence_callback_.Run(std::move(sequence)); | 281 ran_task_from_sequence_callback_.Run(std::move(sequence)); |
233 } | 282 } |
234 | 283 |
235 } // namespace internal | 284 } // namespace internal |
236 } // namespace base | 285 } // namespace base |
OLD | NEW |