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