| 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 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <stack> | 11 #include <stack> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/synchronization/condition_variable.h" | 18 #include "base/synchronization/condition_variable.h" |
| 19 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 20 #include "base/task_scheduler/priority_queue.h" | 20 #include "base/task_scheduler/priority_queue.h" |
| 21 #include "base/task_scheduler/scheduler_lock.h" | 21 #include "base/task_scheduler/scheduler_lock.h" |
| 22 #include "base/task_scheduler/scheduler_worker_thread.h" | 22 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 23 #include "base/task_scheduler/sequence.h" | 23 #include "base/task_scheduler/sequence.h" |
| 24 #include "base/task_scheduler/task_traits.h" | 24 #include "base/task_scheduler/task_traits.h" |
| 25 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
| 26 | 26 |
| 27 namespace base { | 27 namespace base { |
| 28 namespace internal { | 28 namespace internal { |
| 29 | 29 |
| 30 class DelayedTaskManager; |
| 30 struct SequenceSortKey; | 31 struct SequenceSortKey; |
| 31 class TaskTracker; | 32 class TaskTracker; |
| 32 | 33 |
| 33 // A pool of threads that run Tasks. This class is thread-safe. | 34 // A pool of threads that run Tasks. This class is thread-safe. |
| 34 class BASE_EXPORT SchedulerThreadPool { | 35 class BASE_EXPORT SchedulerThreadPool { |
| 35 public: | 36 public: |
| 36 // Callback invoked when a Sequence isn't empty after a worker thread pops a | 37 // Callback invoked when a Sequence isn't empty after a worker thread pops a |
| 37 // Task from it. | 38 // Task from it. |
| 38 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 39 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
| 39 | 40 |
| 40 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | 41 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority |
| 41 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a | 42 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a |
| 42 // thread of this thread pool tries to run a Task. |task_tracker| is used to | 43 // thread of this thread pool tries to run a Task. Tasks posted to TaskRunners |
| 43 // handle shutdown behavior of Tasks. Returns nullptr on failure to create a | 44 // returned by CreateTaskRunnerWithTraits with a delay are handled by |
| 44 // thread pool with at least one thread. | 45 // |delayed_task_manager|. |task_tracker| handles shutdown behavior of Tasks. |
| 46 // Returns nullptr on failure to create a thread pool with at least one |
| 47 // thread. |
| 45 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( | 48 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( |
| 46 ThreadPriority thread_priority, | 49 ThreadPriority thread_priority, |
| 47 size_t max_threads, | 50 size_t max_threads, |
| 48 const EnqueueSequenceCallback& enqueue_sequence_callback, | 51 const EnqueueSequenceCallback& enqueue_sequence_callback, |
| 49 TaskTracker* task_tracker); | 52 TaskTracker* task_tracker, |
| 53 DelayedTaskManager* delayed_task_manager); |
| 50 | 54 |
| 51 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not | 55 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not |
| 52 // allowed in production; it is always leaked. In tests, it can only be | 56 // allowed in production; it is always leaked. In tests, it can only be |
| 53 // destroyed after JoinForTesting() has returned. | 57 // destroyed after JoinForTesting() has returned. |
| 54 ~SchedulerThreadPool(); | 58 ~SchedulerThreadPool(); |
| 55 | 59 |
| 56 // Returns a TaskRunner whose PostTask invocations will result in scheduling | 60 // Returns a TaskRunner whose PostTask invocations will result in scheduling |
| 57 // Tasks with |traits| and |execution_mode| in this thread pool. | 61 // Tasks with |traits| and |execution_mode| in this thread pool. |
| 58 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 62 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 59 const TaskTraits& traits, | 63 const TaskTraits& traits, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 70 void WaitForAllWorkerThreadsIdleForTesting(); | 74 void WaitForAllWorkerThreadsIdleForTesting(); |
| 71 | 75 |
| 72 // Joins all threads of this thread pool. Tasks that are already running are | 76 // Joins all threads of this thread pool. Tasks that are already running are |
| 73 // allowed to complete their execution. This can only be called once. | 77 // allowed to complete their execution. This can only be called once. |
| 74 void JoinForTesting(); | 78 void JoinForTesting(); |
| 75 | 79 |
| 76 private: | 80 private: |
| 77 class SchedulerWorkerThreadDelegateImpl; | 81 class SchedulerWorkerThreadDelegateImpl; |
| 78 | 82 |
| 79 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback, | 83 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback, |
| 80 TaskTracker* task_tracker); | 84 TaskTracker* task_tracker, |
| 85 DelayedTaskManager* delayed_task_manager); |
| 81 | 86 |
| 82 bool Initialize(ThreadPriority thread_priority, size_t max_threads); | 87 bool Initialize(ThreadPriority thread_priority, size_t max_threads); |
| 83 | 88 |
| 84 // Wakes up the last thread from this thread pool to go idle, if any. | 89 // Wakes up the last thread from this thread pool to go idle, if any. |
| 85 void WakeUpOneThread(); | 90 void WakeUpOneThread(); |
| 86 | 91 |
| 87 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 92 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
| 88 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 93 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
| 89 | 94 |
| 90 // PriorityQueue from which all threads of this thread pool get work. | 95 // PriorityQueue from which all threads of this thread pool get work. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 106 // Signaled when all worker threads become idle. | 111 // Signaled when all worker threads become idle. |
| 107 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 112 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
| 108 | 113 |
| 109 // Signaled once JoinForTesting() has returned. | 114 // Signaled once JoinForTesting() has returned. |
| 110 WaitableEvent join_for_testing_returned_; | 115 WaitableEvent join_for_testing_returned_; |
| 111 | 116 |
| 112 // Delegate for all worker threads in this pool. | 117 // Delegate for all worker threads in this pool. |
| 113 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; | 118 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; |
| 114 | 119 |
| 115 TaskTracker* const task_tracker_; | 120 TaskTracker* const task_tracker_; |
| 121 DelayedTaskManager* const delayed_task_manager_; |
| 116 | 122 |
| 117 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | 123 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); |
| 118 }; | 124 }; |
| 119 | 125 |
| 120 } // namespace internal | 126 } // namespace internal |
| 121 } // namespace base | 127 } // namespace base |
| 122 | 128 |
| 123 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 129 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
| OLD | NEW |