| 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_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/task_scheduler/delayed_task_manager.h" | 19 #include "base/task_scheduler/delayed_task_manager.h" |
| 20 #include "base/task_scheduler/task_tracker.h" | 20 #include "base/task_scheduler/task_tracker.h" |
| 21 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
| 22 #include "base/threading/thread_local.h" | 22 #include "base/threading/thread_local.h" |
| 23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
| 24 | 24 |
| 25 namespace base { | 25 namespace base { |
| 26 namespace internal { | 26 namespace internal { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // SchedulerThreadPool that owns the current thread, if any. | 30 // SchedulerWorkerPool that owns the current thread, if any. |
| 31 LazyInstance<ThreadLocalPointer<const SchedulerThreadPool>>::Leaky | 31 LazyInstance<ThreadLocalPointer<const SchedulerWorkerPool>>::Leaky |
| 32 tls_current_thread_pool = LAZY_INSTANCE_INITIALIZER; | 32 tls_current_worker_pool = LAZY_INSTANCE_INITIALIZER; |
| 33 | 33 |
| 34 // SchedulerWorkerThread that owns the current thread, if any. | 34 // SchedulerWorkerThread that owns the current thread, if any. |
| 35 LazyInstance<ThreadLocalPointer<const SchedulerWorkerThread>>::Leaky | 35 LazyInstance<ThreadLocalPointer<const SchedulerWorkerThread>>::Leaky |
| 36 tls_current_worker_thread = LAZY_INSTANCE_INITIALIZER; | 36 tls_current_worker_thread = LAZY_INSTANCE_INITIALIZER; |
| 37 | 37 |
| 38 // A task runner that runs tasks with the PARALLEL ExecutionMode. | 38 // A task runner that runs tasks with the PARALLEL ExecutionMode. |
| 39 class SchedulerParallelTaskRunner : public TaskRunner { | 39 class SchedulerParallelTaskRunner : public TaskRunner { |
| 40 public: | 40 public: |
| 41 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so | 41 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so |
| 42 // long as |thread_pool| is alive. | 42 // long as |worker_pool| is alive. |
| 43 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. | 43 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. |
| 44 SchedulerParallelTaskRunner(const TaskTraits& traits, | 44 SchedulerParallelTaskRunner(const TaskTraits& traits, |
| 45 SchedulerThreadPool* thread_pool) | 45 SchedulerWorkerPool* worker_pool) |
| 46 : traits_(traits), thread_pool_(thread_pool) {} | 46 : traits_(traits), worker_pool_(worker_pool) {} |
| 47 | 47 |
| 48 // TaskRunner: | 48 // TaskRunner: |
| 49 bool PostDelayedTask(const tracked_objects::Location& from_here, | 49 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 50 const Closure& closure, | 50 const Closure& closure, |
| 51 TimeDelta delay) override { | 51 TimeDelta delay) override { |
| 52 // Post the task as part of a one-off single-task Sequence. | 52 // Post the task as part of a one-off single-task Sequence. |
| 53 return thread_pool_->PostTaskWithSequence( | 53 return worker_pool_->PostTaskWithSequence( |
| 54 WrapUnique(new Task(from_here, closure, traits_, delay)), | 54 WrapUnique(new Task(from_here, closure, traits_, delay)), |
| 55 make_scoped_refptr(new Sequence), nullptr); | 55 make_scoped_refptr(new Sequence), nullptr); |
| 56 } | 56 } |
| 57 | 57 |
| 58 bool RunsTasksOnCurrentThread() const override { | 58 bool RunsTasksOnCurrentThread() const override { |
| 59 return tls_current_thread_pool.Get().Get() == thread_pool_; | 59 return tls_current_worker_pool.Get().Get() == worker_pool_; |
| 60 } | 60 } |
| 61 | 61 |
| 62 private: | 62 private: |
| 63 ~SchedulerParallelTaskRunner() override = default; | 63 ~SchedulerParallelTaskRunner() override = default; |
| 64 | 64 |
| 65 const TaskTraits traits_; | 65 const TaskTraits traits_; |
| 66 SchedulerThreadPool* const thread_pool_; | 66 SchedulerWorkerPool* const worker_pool_; |
| 67 | 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | 68 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 // A task runner that runs tasks with the SEQUENCED ExecutionMode. | 71 // A task runner that runs tasks with the SEQUENCED ExecutionMode. |
| 72 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { | 72 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { |
| 73 public: | 73 public: |
| 74 // Constructs a SchedulerSequencedTaskRunner which can be used to post tasks | 74 // Constructs a SchedulerSequencedTaskRunner which can be used to post tasks |
| 75 // so long as |thread_pool| is alive. | 75 // so long as |worker_pool| is alive. |
| 76 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. | 76 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. |
| 77 SchedulerSequencedTaskRunner(const TaskTraits& traits, | 77 SchedulerSequencedTaskRunner(const TaskTraits& traits, |
| 78 SchedulerThreadPool* thread_pool) | 78 SchedulerWorkerPool* worker_pool) |
| 79 : traits_(traits), thread_pool_(thread_pool) {} | 79 : traits_(traits), worker_pool_(worker_pool) {} |
| 80 | 80 |
| 81 // SequencedTaskRunner: | 81 // SequencedTaskRunner: |
| 82 bool PostDelayedTask(const tracked_objects::Location& from_here, | 82 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 83 const Closure& closure, | 83 const Closure& closure, |
| 84 TimeDelta delay) override { | 84 TimeDelta delay) override { |
| 85 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); | 85 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); |
| 86 task->sequenced_task_runner_ref = this; | 86 task->sequenced_task_runner_ref = this; |
| 87 | 87 |
| 88 // Post the task as part of |sequence_|. | 88 // Post the task as part of |sequence_|. |
| 89 return thread_pool_->PostTaskWithSequence(std::move(task), sequence_, | 89 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_, |
| 90 nullptr); | 90 nullptr); |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 93 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 94 const Closure& closure, | 94 const Closure& closure, |
| 95 base::TimeDelta delay) override { | 95 base::TimeDelta delay) override { |
| 96 // Tasks are never nested within the task scheduler. | 96 // Tasks are never nested within the task scheduler. |
| 97 return PostDelayedTask(from_here, closure, delay); | 97 return PostDelayedTask(from_here, closure, delay); |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool RunsTasksOnCurrentThread() const override { | 100 bool RunsTasksOnCurrentThread() const override { |
| 101 return tls_current_thread_pool.Get().Get() == thread_pool_; | 101 return tls_current_worker_pool.Get().Get() == worker_pool_; |
| 102 } | 102 } |
| 103 | 103 |
| 104 private: | 104 private: |
| 105 ~SchedulerSequencedTaskRunner() override = default; | 105 ~SchedulerSequencedTaskRunner() override = default; |
| 106 | 106 |
| 107 // Sequence for all Tasks posted through this TaskRunner. | 107 // Sequence for all Tasks posted through this TaskRunner. |
| 108 const scoped_refptr<Sequence> sequence_ = new Sequence; | 108 const scoped_refptr<Sequence> sequence_ = new Sequence; |
| 109 | 109 |
| 110 const TaskTraits traits_; | 110 const TaskTraits traits_; |
| 111 SchedulerThreadPool* const thread_pool_; | 111 SchedulerWorkerPool* const worker_pool_; |
| 112 | 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); | 113 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 // A task runner that runs tasks with the SINGLE_THREADED ExecutionMode. | 116 // A task runner that runs tasks with the SINGLE_THREADED ExecutionMode. |
| 117 class SchedulerSingleThreadTaskRunner : public SingleThreadTaskRunner { | 117 class SchedulerSingleThreadTaskRunner : public SingleThreadTaskRunner { |
| 118 public: | 118 public: |
| 119 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post | 119 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post |
| 120 // tasks so long as |thread_pool| and |worker_thread| are alive. | 120 // tasks so long as |worker_pool| and |worker_thread| are alive. |
| 121 // TODO(robliao): Find a concrete way to manage the memory of |thread_pool| | 121 // TODO(robliao): Find a concrete way to manage the memory of |worker_pool| |
| 122 // and |worker_thread|. | 122 // and |worker_thread|. |
| 123 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, | 123 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, |
| 124 SchedulerThreadPool* thread_pool, | 124 SchedulerWorkerPool* worker_pool, |
| 125 SchedulerWorkerThread* worker_thread) | 125 SchedulerWorkerThread* worker_thread) |
| 126 : traits_(traits), | 126 : traits_(traits), |
| 127 thread_pool_(thread_pool), | 127 worker_pool_(worker_pool), |
| 128 worker_thread_(worker_thread) {} | 128 worker_thread_(worker_thread) {} |
| 129 | 129 |
| 130 // SingleThreadTaskRunner: | 130 // SingleThreadTaskRunner: |
| 131 bool PostDelayedTask(const tracked_objects::Location& from_here, | 131 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 132 const Closure& closure, | 132 const Closure& closure, |
| 133 TimeDelta delay) override { | 133 TimeDelta delay) override { |
| 134 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); | 134 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); |
| 135 task->single_thread_task_runner_ref = this; | 135 task->single_thread_task_runner_ref = this; |
| 136 | 136 |
| 137 // Post the task to be executed by |worker_thread_| as part of |sequence_|. | 137 // Post the task to be executed by |worker_thread_| as part of |sequence_|. |
| 138 return thread_pool_->PostTaskWithSequence(std::move(task), sequence_, | 138 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_, |
| 139 worker_thread_); | 139 worker_thread_); |
| 140 } | 140 } |
| 141 | 141 |
| 142 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 142 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 143 const Closure& closure, | 143 const Closure& closure, |
| 144 base::TimeDelta delay) override { | 144 base::TimeDelta delay) override { |
| 145 // Tasks are never nested within the task scheduler. | 145 // Tasks are never nested within the task scheduler. |
| 146 return PostDelayedTask(from_here, closure, delay); | 146 return PostDelayedTask(from_here, closure, delay); |
| 147 } | 147 } |
| 148 | 148 |
| 149 bool RunsTasksOnCurrentThread() const override { | 149 bool RunsTasksOnCurrentThread() const override { |
| 150 return tls_current_worker_thread.Get().Get() == worker_thread_; | 150 return tls_current_worker_thread.Get().Get() == worker_thread_; |
| 151 } | 151 } |
| 152 | 152 |
| 153 private: | 153 private: |
| 154 ~SchedulerSingleThreadTaskRunner() override = default; | 154 ~SchedulerSingleThreadTaskRunner() override = default; |
| 155 | 155 |
| 156 // Sequence for all Tasks posted through this TaskRunner. | 156 // Sequence for all Tasks posted through this TaskRunner. |
| 157 const scoped_refptr<Sequence> sequence_ = new Sequence; | 157 const scoped_refptr<Sequence> sequence_ = new Sequence; |
| 158 | 158 |
| 159 const TaskTraits traits_; | 159 const TaskTraits traits_; |
| 160 SchedulerThreadPool* const thread_pool_; | 160 SchedulerWorkerPool* const worker_pool_; |
| 161 SchedulerWorkerThread* const worker_thread_; | 161 SchedulerWorkerThread* const worker_thread_; |
| 162 | 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunner); | 163 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunner); |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 // Only used in DCHECKs. | 166 // Only used in DCHECKs. |
| 167 bool ContainsWorkerThread( | 167 bool ContainsWorkerThread( |
| 168 const std::vector<std::unique_ptr<SchedulerWorkerThread>>& worker_threads, | 168 const std::vector<std::unique_ptr<SchedulerWorkerThread>>& worker_threads, |
| 169 const SchedulerWorkerThread* worker_thread) { | 169 const SchedulerWorkerThread* worker_thread) { |
| 170 auto it = std::find_if( | 170 auto it = std::find_if( |
| 171 worker_threads.begin(), worker_threads.end(), | 171 worker_threads.begin(), worker_threads.end(), |
| 172 [worker_thread](const std::unique_ptr<SchedulerWorkerThread>& i) { | 172 [worker_thread](const std::unique_ptr<SchedulerWorkerThread>& i) { |
| 173 return i.get() == worker_thread; | 173 return i.get() == worker_thread; |
| 174 }); | 174 }); |
| 175 return it != worker_threads.end(); | 175 return it != worker_threads.end(); |
| 176 } | 176 } |
| 177 | 177 |
| 178 } // namespace | 178 } // namespace |
| 179 | 179 |
| 180 class SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl | 180 class SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl |
| 181 : public SchedulerWorkerThread::Delegate { | 181 : public SchedulerWorkerThread::Delegate { |
| 182 public: | 182 public: |
| 183 // |outer| owns the worker thread for which this delegate is constructed. | 183 // |outer| owns the worker thread for which this delegate is constructed. |
| 184 // |re_enqueue_sequence_callback| is invoked when ReEnqueueSequence() is | 184 // |re_enqueue_sequence_callback| is invoked when ReEnqueueSequence() is |
| 185 // called with a non-single-threaded Sequence. |shared_priority_queue| is a | 185 // called with a non-single-threaded Sequence. |shared_priority_queue| is a |
| 186 // PriorityQueue whose transactions may overlap with the worker thread's | 186 // PriorityQueue whose transactions may overlap with the worker thread's |
| 187 // single-threaded PriorityQueue's transactions. |index| will be appended to | 187 // single-threaded PriorityQueue's transactions. |index| will be appended to |
| 188 // this thread's name to uniquely identify it. | 188 // this thread's name to uniquely identify it. |
| 189 SchedulerWorkerThreadDelegateImpl( | 189 SchedulerWorkerThreadDelegateImpl( |
| 190 SchedulerThreadPoolImpl* outer, | 190 SchedulerWorkerPoolImpl* outer, |
| 191 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 191 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 192 const PriorityQueue* shared_priority_queue, | 192 const PriorityQueue* shared_priority_queue, |
| 193 int index); | 193 int index); |
| 194 ~SchedulerWorkerThreadDelegateImpl() override; | 194 ~SchedulerWorkerThreadDelegateImpl() override; |
| 195 | 195 |
| 196 PriorityQueue* single_threaded_priority_queue() { | 196 PriorityQueue* single_threaded_priority_queue() { |
| 197 return &single_threaded_priority_queue_; | 197 return &single_threaded_priority_queue_; |
| 198 } | 198 } |
| 199 | 199 |
| 200 // SchedulerWorkerThread::Delegate: | 200 // SchedulerWorkerThread::Delegate: |
| 201 void OnMainEntry(SchedulerWorkerThread* worker_thread) override; | 201 void OnMainEntry(SchedulerWorkerThread* worker_thread) override; |
| 202 scoped_refptr<Sequence> GetWork( | 202 scoped_refptr<Sequence> GetWork( |
| 203 SchedulerWorkerThread* worker_thread) override; | 203 SchedulerWorkerThread* worker_thread) override; |
| 204 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; | 204 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; |
| 205 TimeDelta GetSleepTimeout() override; | 205 TimeDelta GetSleepTimeout() override; |
| 206 | 206 |
| 207 private: | 207 private: |
| 208 SchedulerThreadPoolImpl* outer_; | 208 SchedulerWorkerPoolImpl* outer_; |
| 209 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; | 209 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; |
| 210 | 210 |
| 211 // Single-threaded PriorityQueue for the worker thread. | 211 // Single-threaded PriorityQueue for the worker thread. |
| 212 PriorityQueue single_threaded_priority_queue_; | 212 PriorityQueue single_threaded_priority_queue_; |
| 213 | 213 |
| 214 // True if the last Sequence returned by GetWork() was extracted from | 214 // True if the last Sequence returned by GetWork() was extracted from |
| 215 // |single_threaded_priority_queue_|. | 215 // |single_threaded_priority_queue_|. |
| 216 bool last_sequence_is_single_threaded_ = false; | 216 bool last_sequence_is_single_threaded_ = false; |
| 217 | 217 |
| 218 const int index_; | 218 const int index_; |
| 219 | 219 |
| 220 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); | 220 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); |
| 221 }; | 221 }; |
| 222 | 222 |
| 223 SchedulerThreadPoolImpl::~SchedulerThreadPoolImpl() { | 223 SchedulerWorkerPoolImpl::~SchedulerWorkerPoolImpl() { |
| 224 // SchedulerThreadPool should never be deleted in production unless its | 224 // SchedulerWorkerPool should never be deleted in production unless its |
| 225 // initialization failed. | 225 // initialization failed. |
| 226 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); | 226 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); |
| 227 } | 227 } |
| 228 | 228 |
| 229 // static | 229 // static |
| 230 std::unique_ptr<SchedulerThreadPoolImpl> SchedulerThreadPoolImpl::Create( | 230 std::unique_ptr<SchedulerWorkerPoolImpl> SchedulerWorkerPoolImpl::Create( |
| 231 StringPiece name, | 231 StringPiece name, |
| 232 ThreadPriority thread_priority, | 232 ThreadPriority thread_priority, |
| 233 size_t max_threads, | 233 size_t max_threads, |
| 234 IORestriction io_restriction, | 234 IORestriction io_restriction, |
| 235 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 235 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 236 TaskTracker* task_tracker, | 236 TaskTracker* task_tracker, |
| 237 DelayedTaskManager* delayed_task_manager) { | 237 DelayedTaskManager* delayed_task_manager) { |
| 238 std::unique_ptr<SchedulerThreadPoolImpl> thread_pool( | 238 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool( |
| 239 new SchedulerThreadPoolImpl(name, io_restriction, task_tracker, | 239 new SchedulerWorkerPoolImpl(name, io_restriction, task_tracker, |
| 240 delayed_task_manager)); | 240 delayed_task_manager)); |
| 241 if (thread_pool->Initialize(thread_priority, max_threads, | 241 if (worker_pool->Initialize(thread_priority, max_threads, |
| 242 re_enqueue_sequence_callback)) { | 242 re_enqueue_sequence_callback)) { |
| 243 return thread_pool; | 243 return worker_pool; |
| 244 } | 244 } |
| 245 return nullptr; | 245 return nullptr; |
| 246 } | 246 } |
| 247 | 247 |
| 248 void SchedulerThreadPoolImpl::WaitForAllWorkerThreadsIdleForTesting() { | 248 void SchedulerWorkerPoolImpl::WaitForAllWorkerWorkersIdleForTesting() { |
| 249 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 249 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 250 while (idle_worker_threads_stack_.Size() < worker_threads_.size()) | 250 while (idle_worker_threads_stack_.Size() < worker_threads_.size()) |
| 251 idle_worker_threads_stack_cv_for_testing_->Wait(); | 251 idle_worker_threads_stack_cv_for_testing_->Wait(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void SchedulerThreadPoolImpl::JoinForTesting() { | 254 void SchedulerWorkerPoolImpl::JoinForTesting() { |
| 255 for (const auto& worker_thread : worker_threads_) | 255 for (const auto& worker_thread : worker_threads_) |
| 256 worker_thread->JoinForTesting(); | 256 worker_thread->JoinForTesting(); |
| 257 | 257 |
| 258 DCHECK(!join_for_testing_returned_.IsSignaled()); | 258 DCHECK(!join_for_testing_returned_.IsSignaled()); |
| 259 join_for_testing_returned_.Signal(); | 259 join_for_testing_returned_.Signal(); |
| 260 } | 260 } |
| 261 | 261 |
| 262 scoped_refptr<TaskRunner> SchedulerThreadPoolImpl::CreateTaskRunnerWithTraits( | 262 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( |
| 263 const TaskTraits& traits, | 263 const TaskTraits& traits, |
| 264 ExecutionMode execution_mode) { | 264 ExecutionMode execution_mode) { |
| 265 switch (execution_mode) { | 265 switch (execution_mode) { |
| 266 case ExecutionMode::PARALLEL: | 266 case ExecutionMode::PARALLEL: |
| 267 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); | 267 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); |
| 268 | 268 |
| 269 case ExecutionMode::SEQUENCED: | 269 case ExecutionMode::SEQUENCED: |
| 270 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | 270 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); |
| 271 | 271 |
| 272 case ExecutionMode::SINGLE_THREADED: { | 272 case ExecutionMode::SINGLE_THREADED: { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 283 } | 283 } |
| 284 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( | 284 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( |
| 285 traits, this, worker_threads_[worker_thread_index].get())); | 285 traits, this, worker_threads_[worker_thread_index].get())); |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 NOTREACHED(); | 289 NOTREACHED(); |
| 290 return nullptr; | 290 return nullptr; |
| 291 } | 291 } |
| 292 | 292 |
| 293 void SchedulerThreadPoolImpl::ReEnqueueSequence( | 293 void SchedulerWorkerPoolImpl::ReEnqueueSequence( |
| 294 scoped_refptr<Sequence> sequence, | 294 scoped_refptr<Sequence> sequence, |
| 295 const SequenceSortKey& sequence_sort_key) { | 295 const SequenceSortKey& sequence_sort_key) { |
| 296 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), | 296 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), |
| 297 sequence_sort_key); | 297 sequence_sort_key); |
| 298 | 298 |
| 299 // The thread calling this method just ran a Task from |sequence| and will | 299 // The thread calling this method just ran a Task from |sequence| and will |
| 300 // soon try to get another Sequence from which to run a Task. If the thread | 300 // soon try to get another Sequence from which to run a Task. If the thread |
| 301 // belongs to this pool, it will get that Sequence from | 301 // belongs to this pool, it will get that Sequence from |
| 302 // |shared_priority_queue_|. When that's the case, there is no need to wake up | 302 // |shared_priority_queue_|. When that's the case, there is no need to wake up |
| 303 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | 303 // another thread after |sequence| is inserted in |shared_priority_queue_|. If |
| 304 // we did wake up another thread, we would waste resources by having more | 304 // we did wake up another thread, we would waste resources by having more |
| 305 // threads trying to get a Sequence from |shared_priority_queue_| than the | 305 // threads trying to get a Sequence from |shared_priority_queue_| than the |
| 306 // number of Sequences in it. | 306 // number of Sequences in it. |
| 307 if (tls_current_thread_pool.Get().Get() != this) | 307 if (tls_current_worker_pool.Get().Get() != this) |
| 308 WakeUpOneThread(); | 308 WakeUpOneWorker(); |
| 309 } | 309 } |
| 310 | 310 |
| 311 bool SchedulerThreadPoolImpl::PostTaskWithSequence( | 311 bool SchedulerWorkerPoolImpl::PostTaskWithSequence( |
| 312 std::unique_ptr<Task> task, | 312 std::unique_ptr<Task> task, |
| 313 scoped_refptr<Sequence> sequence, | 313 scoped_refptr<Sequence> sequence, |
| 314 SchedulerWorkerThread* worker_thread) { | 314 SchedulerWorkerThread* worker_thread) { |
| 315 DCHECK(task); | 315 DCHECK(task); |
| 316 DCHECK(sequence); | 316 DCHECK(sequence); |
| 317 DCHECK(!worker_thread || | 317 DCHECK(!worker_thread || |
| 318 ContainsWorkerThread(worker_threads_, worker_thread)); | 318 ContainsWorkerThread(worker_threads_, worker_thread)); |
| 319 | 319 |
| 320 if (!task_tracker_->WillPostTask(task.get())) | 320 if (!task_tracker_->WillPostTask(task.get())) |
| 321 return false; | 321 return false; |
| 322 | 322 |
| 323 if (task->delayed_run_time.is_null()) { | 323 if (task->delayed_run_time.is_null()) { |
| 324 PostTaskWithSequenceNow(std::move(task), std::move(sequence), | 324 PostTaskWithSequenceNow(std::move(task), std::move(sequence), |
| 325 worker_thread); | 325 worker_thread); |
| 326 } else { | 326 } else { |
| 327 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), | 327 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), |
| 328 worker_thread, this); | 328 worker_thread, this); |
| 329 } | 329 } |
| 330 | 330 |
| 331 return true; | 331 return true; |
| 332 } | 332 } |
| 333 | 333 |
| 334 void SchedulerThreadPoolImpl::PostTaskWithSequenceNow( | 334 void SchedulerWorkerPoolImpl::PostTaskWithSequenceNow( |
| 335 std::unique_ptr<Task> task, | 335 std::unique_ptr<Task> task, |
| 336 scoped_refptr<Sequence> sequence, | 336 scoped_refptr<Sequence> sequence, |
| 337 SchedulerWorkerThread* worker_thread) { | 337 SchedulerWorkerThread* worker_thread) { |
| 338 DCHECK(task); | 338 DCHECK(task); |
| 339 DCHECK(sequence); | 339 DCHECK(sequence); |
| 340 DCHECK(!worker_thread || | 340 DCHECK(!worker_thread || |
| 341 ContainsWorkerThread(worker_threads_, worker_thread)); | 341 ContainsWorkerThread(worker_threads_, worker_thread)); |
| 342 | 342 |
| 343 // Confirm that |task| is ready to run (its delayed run time is either null or | 343 // Confirm that |task| is ready to run (its delayed run time is either null or |
| 344 // in the past). | 344 // in the past). |
| 345 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); | 345 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); |
| 346 | 346 |
| 347 // Because |worker_thread| belongs to this thread pool, we know that the type | 347 // Because |worker_thread| belongs to this worker pool, we know that the type |
| 348 // of its delegate is SchedulerWorkerThreadDelegateImpl. | 348 // of its delegate is SchedulerWorkerThreadDelegateImpl. |
| 349 PriorityQueue* const priority_queue = | 349 PriorityQueue* const priority_queue = |
| 350 worker_thread | 350 worker_thread |
| 351 ? static_cast<SchedulerWorkerThreadDelegateImpl*>( | 351 ? static_cast<SchedulerWorkerThreadDelegateImpl*>( |
| 352 worker_thread->delegate()) | 352 worker_thread->delegate()) |
| 353 ->single_threaded_priority_queue() | 353 ->single_threaded_priority_queue() |
| 354 : &shared_priority_queue_; | 354 : &shared_priority_queue_; |
| 355 DCHECK(priority_queue); | 355 DCHECK(priority_queue); |
| 356 | 356 |
| 357 const bool sequence_was_empty = sequence->PushTask(std::move(task)); | 357 const bool sequence_was_empty = sequence->PushTask(std::move(task)); |
| 358 if (sequence_was_empty) { | 358 if (sequence_was_empty) { |
| 359 // Insert |sequence| in |priority_queue| if it was empty before |task| was | 359 // Insert |sequence| in |priority_queue| if it was empty before |task| was |
| 360 // inserted into it. Otherwise, one of these must be true: | 360 // inserted into it. Otherwise, one of these must be true: |
| 361 // - |sequence| is already in a PriorityQueue (not necessarily | 361 // - |sequence| is already in a PriorityQueue (not necessarily |
| 362 // |shared_priority_queue_|), or, | 362 // |shared_priority_queue_|), or, |
| 363 // - A worker thread is running a Task from |sequence|. It will insert | 363 // - A worker thread is running a Task from |sequence|. It will insert |
| 364 // |sequence| in a PriorityQueue once it's done running the Task. | 364 // |sequence| in a PriorityQueue once it's done running the Task. |
| 365 const auto sequence_sort_key = sequence->GetSortKey(); | 365 const auto sequence_sort_key = sequence->GetSortKey(); |
| 366 priority_queue->BeginTransaction()->Push(std::move(sequence), | 366 priority_queue->BeginTransaction()->Push(std::move(sequence), |
| 367 sequence_sort_key); | 367 sequence_sort_key); |
| 368 | 368 |
| 369 // Wake up a worker thread to process |sequence|. | 369 // Wake up a worker thread to process |sequence|. |
| 370 if (worker_thread) | 370 if (worker_thread) |
| 371 worker_thread->WakeUp(); | 371 worker_thread->WakeUp(); |
| 372 else | 372 else |
| 373 WakeUpOneThread(); | 373 WakeUpOneWorker(); |
| 374 } | 374 } |
| 375 } | 375 } |
| 376 | 376 |
| 377 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 377 SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
| 378 SchedulerWorkerThreadDelegateImpl( | 378 SchedulerWorkerThreadDelegateImpl( |
| 379 SchedulerThreadPoolImpl* outer, | 379 SchedulerWorkerPoolImpl* outer, |
| 380 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 380 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 381 const PriorityQueue* shared_priority_queue, | 381 const PriorityQueue* shared_priority_queue, |
| 382 int index) | 382 int index) |
| 383 : outer_(outer), | 383 : outer_(outer), |
| 384 re_enqueue_sequence_callback_(re_enqueue_sequence_callback), | 384 re_enqueue_sequence_callback_(re_enqueue_sequence_callback), |
| 385 single_threaded_priority_queue_(shared_priority_queue), | 385 single_threaded_priority_queue_(shared_priority_queue), |
| 386 index_(index) {} | 386 index_(index) {} |
| 387 | 387 |
| 388 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 388 SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
| 389 ~SchedulerWorkerThreadDelegateImpl() = default; | 389 ~SchedulerWorkerThreadDelegateImpl() = default; |
| 390 | 390 |
| 391 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::OnMainEntry( | 391 void SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl::OnMainEntry( |
| 392 SchedulerWorkerThread* worker_thread) { | 392 SchedulerWorkerThread* worker_thread) { |
| 393 #if DCHECK_IS_ON() | 393 #if DCHECK_IS_ON() |
| 394 // Wait for |outer_->threads_created_| to avoid traversing | 394 // Wait for |outer_->threads_created_| to avoid traversing |
| 395 // |outer_->worker_threads_| while it is being filled by Initialize(). | 395 // |outer_->worker_threads_| while it is being filled by Initialize(). |
| 396 outer_->threads_created_.Wait(); | 396 outer_->threads_created_.Wait(); |
| 397 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); | 397 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); |
| 398 #endif | 398 #endif |
| 399 | 399 |
| 400 PlatformThread::SetName( | 400 PlatformThread::SetName( |
| 401 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); | 401 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); |
| 402 | 402 |
| 403 DCHECK(!tls_current_worker_thread.Get().Get()); | 403 DCHECK(!tls_current_worker_thread.Get().Get()); |
| 404 DCHECK(!tls_current_thread_pool.Get().Get()); | 404 DCHECK(!tls_current_worker_pool.Get().Get()); |
| 405 tls_current_worker_thread.Get().Set(worker_thread); | 405 tls_current_worker_thread.Get().Set(worker_thread); |
| 406 tls_current_thread_pool.Get().Set(outer_); | 406 tls_current_worker_pool.Get().Set(outer_); |
| 407 | 407 |
| 408 ThreadRestrictions::SetIOAllowed(outer_->io_restriction_ == | 408 ThreadRestrictions::SetIOAllowed(outer_->io_restriction_ == |
| 409 IORestriction::ALLOWED); | 409 IORestriction::ALLOWED); |
| 410 } | 410 } |
| 411 | 411 |
| 412 scoped_refptr<Sequence> | 412 scoped_refptr<Sequence> |
| 413 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork( | 413 SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork( |
| 414 SchedulerWorkerThread* worker_thread) { | 414 SchedulerWorkerThread* worker_thread) { |
| 415 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); | 415 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); |
| 416 | 416 |
| 417 scoped_refptr<Sequence> sequence; | 417 scoped_refptr<Sequence> sequence; |
| 418 { | 418 { |
| 419 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( | 419 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( |
| 420 outer_->shared_priority_queue_.BeginTransaction()); | 420 outer_->shared_priority_queue_.BeginTransaction()); |
| 421 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction( | 421 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction( |
| 422 single_threaded_priority_queue_.BeginTransaction()); | 422 single_threaded_priority_queue_.BeginTransaction()); |
| 423 | 423 |
| 424 if (shared_transaction->IsEmpty() && | 424 if (shared_transaction->IsEmpty() && |
| 425 single_threaded_transaction->IsEmpty()) { | 425 single_threaded_transaction->IsEmpty()) { |
| 426 single_threaded_transaction.reset(); | 426 single_threaded_transaction.reset(); |
| 427 | 427 |
| 428 // |shared_transaction| is kept alive while |worker_thread| is added to | 428 // |shared_transaction| is kept alive while |worker_thread| is added to |
| 429 // |idle_worker_threads_stack_| to avoid this race: | 429 // |idle_worker_threads_stack_| to avoid this race: |
| 430 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | 430 // 1. This thread creates a Transaction, finds |shared_priority_queue_| |
| 431 // empty and ends the Transaction. | 431 // empty and ends the Transaction. |
| 432 // 2. Other thread creates a Transaction, inserts a Sequence into | 432 // 2. Other thread creates a Transaction, inserts a Sequence into |
| 433 // |shared_priority_queue_| and ends the Transaction. This can't happen | 433 // |shared_priority_queue_| and ends the Transaction. This can't happen |
| 434 // if the Transaction of step 1 is still active because because there | 434 // if the Transaction of step 1 is still active because because there |
| 435 // can only be one active Transaction per PriorityQueue at a time. | 435 // can only be one active Transaction per PriorityQueue at a time. |
| 436 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because | 436 // 3. Other thread calls WakeUpOneWorker(). No thread is woken up because |
| 437 // |idle_worker_threads_stack_| is empty. | 437 // |idle_worker_threads_stack_| is empty. |
| 438 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | 438 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to |
| 439 // sleep. No thread runs the Sequence inserted in step 2. | 439 // sleep. No thread runs the Sequence inserted in step 2. |
| 440 outer_->AddToIdleWorkerThreadsStack(worker_thread); | 440 outer_->AddToIdleWorkerThreadsStack(worker_thread); |
| 441 return nullptr; | 441 return nullptr; |
| 442 } | 442 } |
| 443 | 443 |
| 444 // True if both PriorityQueues have Sequences and the Sequence at the top of | 444 // True if both PriorityQueues have Sequences and the Sequence at the top of |
| 445 // the shared PriorityQueue is more important. | 445 // the shared PriorityQueue is more important. |
| 446 const bool shared_sequence_is_more_important = | 446 const bool shared_sequence_is_more_important = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 458 sequence = single_threaded_transaction->PopSequence(); | 458 sequence = single_threaded_transaction->PopSequence(); |
| 459 last_sequence_is_single_threaded_ = true; | 459 last_sequence_is_single_threaded_ = true; |
| 460 } | 460 } |
| 461 } | 461 } |
| 462 DCHECK(sequence); | 462 DCHECK(sequence); |
| 463 | 463 |
| 464 outer_->RemoveFromIdleWorkerThreadsStack(worker_thread); | 464 outer_->RemoveFromIdleWorkerThreadsStack(worker_thread); |
| 465 return sequence; | 465 return sequence; |
| 466 } | 466 } |
| 467 | 467 |
| 468 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 468 void SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
| 469 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { | 469 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { |
| 470 if (last_sequence_is_single_threaded_) { | 470 if (last_sequence_is_single_threaded_) { |
| 471 // A single-threaded Sequence is always re-enqueued in the single-threaded | 471 // A single-threaded Sequence is always re-enqueued in the single-threaded |
| 472 // PriorityQueue from which it was extracted. | 472 // PriorityQueue from which it was extracted. |
| 473 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); | 473 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); |
| 474 single_threaded_priority_queue_.BeginTransaction()->Push( | 474 single_threaded_priority_queue_.BeginTransaction()->Push( |
| 475 std::move(sequence), sequence_sort_key); | 475 std::move(sequence), sequence_sort_key); |
| 476 } else { | 476 } else { |
| 477 // |re_enqueue_sequence_callback_| will determine in which PriorityQueue | 477 // |re_enqueue_sequence_callback_| will determine in which PriorityQueue |
| 478 // |sequence| must be enqueued. | 478 // |sequence| must be enqueued. |
| 479 re_enqueue_sequence_callback_.Run(std::move(sequence)); | 479 re_enqueue_sequence_callback_.Run(std::move(sequence)); |
| 480 } | 480 } |
| 481 } | 481 } |
| 482 | 482 |
| 483 TimeDelta SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 483 TimeDelta SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
| 484 GetSleepTimeout() { | 484 GetSleepTimeout() { |
| 485 return TimeDelta::Max(); | 485 return TimeDelta::Max(); |
| 486 } | 486 } |
| 487 | 487 |
| 488 SchedulerThreadPoolImpl::SchedulerThreadPoolImpl( | 488 SchedulerWorkerPoolImpl::SchedulerWorkerPoolImpl( |
| 489 StringPiece name, | 489 StringPiece name, |
| 490 IORestriction io_restriction, | 490 IORestriction io_restriction, |
| 491 TaskTracker* task_tracker, | 491 TaskTracker* task_tracker, |
| 492 DelayedTaskManager* delayed_task_manager) | 492 DelayedTaskManager* delayed_task_manager) |
| 493 : name_(name.as_string()), | 493 : name_(name.as_string()), |
| 494 io_restriction_(io_restriction), | 494 io_restriction_(io_restriction), |
| 495 idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), | 495 idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), |
| 496 idle_worker_threads_stack_cv_for_testing_( | 496 idle_worker_threads_stack_cv_for_testing_( |
| 497 idle_worker_threads_stack_lock_.CreateConditionVariable()), | 497 idle_worker_threads_stack_lock_.CreateConditionVariable()), |
| 498 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, | 498 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, |
| 499 WaitableEvent::InitialState::NOT_SIGNALED), | 499 WaitableEvent::InitialState::NOT_SIGNALED), |
| 500 #if DCHECK_IS_ON() | 500 #if DCHECK_IS_ON() |
| 501 threads_created_(WaitableEvent::ResetPolicy::MANUAL, | 501 threads_created_(WaitableEvent::ResetPolicy::MANUAL, |
| 502 WaitableEvent::InitialState::NOT_SIGNALED), | 502 WaitableEvent::InitialState::NOT_SIGNALED), |
| 503 #endif | 503 #endif |
| 504 task_tracker_(task_tracker), | 504 task_tracker_(task_tracker), |
| 505 delayed_task_manager_(delayed_task_manager) { | 505 delayed_task_manager_(delayed_task_manager) { |
| 506 DCHECK(task_tracker_); | 506 DCHECK(task_tracker_); |
| 507 DCHECK(delayed_task_manager_); | 507 DCHECK(delayed_task_manager_); |
| 508 } | 508 } |
| 509 | 509 |
| 510 bool SchedulerThreadPoolImpl::Initialize( | 510 bool SchedulerWorkerPoolImpl::Initialize( |
| 511 ThreadPriority thread_priority, | 511 ThreadPriority thread_priority, |
| 512 size_t max_threads, | 512 size_t max_threads, |
| 513 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { | 513 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { |
| 514 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 514 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 515 | 515 |
| 516 DCHECK(worker_threads_.empty()); | 516 DCHECK(worker_threads_.empty()); |
| 517 | 517 |
| 518 for (size_t i = 0; i < max_threads; ++i) { | 518 for (size_t i = 0; i < max_threads; ++i) { |
| 519 std::unique_ptr<SchedulerWorkerThread> worker_thread = | 519 std::unique_ptr<SchedulerWorkerThread> worker_thread = |
| 520 SchedulerWorkerThread::Create( | 520 SchedulerWorkerThread::Create( |
| 521 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( | 521 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( |
| 522 this, re_enqueue_sequence_callback, | 522 this, re_enqueue_sequence_callback, |
| 523 &shared_priority_queue_, static_cast<int>(i))), | 523 &shared_priority_queue_, static_cast<int>(i))), |
| 524 task_tracker_); | 524 task_tracker_); |
| 525 if (!worker_thread) | 525 if (!worker_thread) |
| 526 break; | 526 break; |
| 527 idle_worker_threads_stack_.Push(worker_thread.get()); | 527 idle_worker_threads_stack_.Push(worker_thread.get()); |
| 528 worker_threads_.push_back(std::move(worker_thread)); | 528 worker_threads_.push_back(std::move(worker_thread)); |
| 529 } | 529 } |
| 530 | 530 |
| 531 #if DCHECK_IS_ON() | 531 #if DCHECK_IS_ON() |
| 532 threads_created_.Signal(); | 532 threads_created_.Signal(); |
| 533 #endif | 533 #endif |
| 534 | 534 |
| 535 return !worker_threads_.empty(); | 535 return !worker_threads_.empty(); |
| 536 } | 536 } |
| 537 | 537 |
| 538 void SchedulerThreadPoolImpl::WakeUpOneThread() { | 538 void SchedulerWorkerPoolImpl::WakeUpOneWorker() { |
| 539 SchedulerWorkerThread* worker_thread; | 539 SchedulerWorkerThread* worker_thread; |
| 540 { | 540 { |
| 541 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 541 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 542 worker_thread = idle_worker_threads_stack_.Pop(); | 542 worker_thread = idle_worker_threads_stack_.Pop(); |
| 543 } | 543 } |
| 544 if (worker_thread) | 544 if (worker_thread) |
| 545 worker_thread->WakeUp(); | 545 worker_thread->WakeUp(); |
| 546 } | 546 } |
| 547 | 547 |
| 548 void SchedulerThreadPoolImpl::AddToIdleWorkerThreadsStack( | 548 void SchedulerWorkerPoolImpl::AddToIdleWorkerThreadsStack( |
| 549 SchedulerWorkerThread* worker_thread) { | 549 SchedulerWorkerThread* worker_thread) { |
| 550 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 550 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 551 idle_worker_threads_stack_.Push(worker_thread); | 551 idle_worker_threads_stack_.Push(worker_thread); |
| 552 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); | 552 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); |
| 553 | 553 |
| 554 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) | 554 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) |
| 555 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | 555 idle_worker_threads_stack_cv_for_testing_->Broadcast(); |
| 556 } | 556 } |
| 557 | 557 |
| 558 void SchedulerThreadPoolImpl::RemoveFromIdleWorkerThreadsStack( | 558 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkerThreadsStack( |
| 559 SchedulerWorkerThread* worker_thread) { | 559 SchedulerWorkerThread* worker_thread) { |
| 560 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 560 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 561 idle_worker_threads_stack_.Remove(worker_thread); | 561 idle_worker_threads_stack_.Remove(worker_thread); |
| 562 } | 562 } |
| 563 | 563 |
| 564 } // namespace internal | 564 } // namespace internal |
| 565 } // namespace base | 565 } // namespace base |
| OLD | NEW |