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> |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "base/task_scheduler/scheduler_task_executor.h" | 22 #include "base/task_scheduler/scheduler_task_executor.h" |
23 #include "base/task_scheduler/scheduler_worker_thread.h" | 23 #include "base/task_scheduler/scheduler_worker_thread.h" |
24 #include "base/task_scheduler/sequence.h" | 24 #include "base/task_scheduler/sequence.h" |
25 #include "base/task_scheduler/task.h" | 25 #include "base/task_scheduler/task.h" |
26 #include "base/task_scheduler/task_traits.h" | 26 #include "base/task_scheduler/task_traits.h" |
27 #include "base/threading/platform_thread.h" | 27 #include "base/threading/platform_thread.h" |
28 | 28 |
29 namespace base { | 29 namespace base { |
30 namespace internal { | 30 namespace internal { |
31 | 31 |
| 32 class DelayedTaskManager; |
32 struct SequenceSortKey; | 33 struct SequenceSortKey; |
33 class TaskTracker; | 34 class TaskTracker; |
34 | 35 |
35 // A pool of threads that run Tasks. This class is thread-safe. | 36 // A pool of threads that run Tasks. This class is thread-safe. |
36 class BASE_EXPORT SchedulerThreadPool : public SchedulerTaskExecutor { | 37 class BASE_EXPORT SchedulerThreadPool : public SchedulerTaskExecutor { |
37 public: | 38 public: |
38 // Callback invoked when a Sequence isn't empty after a worker thread pops a | 39 // Callback invoked when a Sequence isn't empty after a worker thread pops a |
39 // Task from it. | 40 // Task from it. |
40 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 41 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
41 | 42 |
42 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not | 43 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not |
43 // allowed in production; it is always leaked. In tests, it can only be | 44 // allowed in production; it is always leaked. In tests, it can only be |
44 // destroyed after JoinForTesting() has returned. | 45 // destroyed after JoinForTesting() has returned. |
45 ~SchedulerThreadPool() override; | 46 ~SchedulerThreadPool() override; |
46 | 47 |
47 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | 48 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority |
48 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a | 49 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a |
49 // thread of this thread pool tries to run a Task. |task_tracker| is used to | 50 // thread of this thread pool tries to run a Task. |task_tracker| is used to |
50 // handle shutdown behavior of Tasks. Returns nullptr on failure to create a | 51 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks |
51 // thread pool with at least one thread. | 52 // posted with a delay. Returns nullptr on failure to create a thread pool |
| 53 // with at least one thread. |
52 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( | 54 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( |
53 ThreadPriority thread_priority, | 55 ThreadPriority thread_priority, |
54 size_t max_threads, | 56 size_t max_threads, |
55 const EnqueueSequenceCallback& enqueue_sequence_callback, | 57 const EnqueueSequenceCallback& enqueue_sequence_callback, |
56 TaskTracker* task_tracker); | 58 TaskTracker* task_tracker, |
| 59 DelayedTaskManager* delayed_task_manager); |
57 | 60 |
58 // Returns a TaskRunner whose PostTask invocations will result in scheduling | 61 // Returns a TaskRunner whose PostTask invocations will result in scheduling |
59 // Tasks with |traits| and |execution_mode| in this thread pool. | 62 // Tasks with |traits| and |execution_mode| in this thread pool. |
60 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 63 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
61 const TaskTraits& traits, | 64 const TaskTraits& traits, |
62 ExecutionMode execution_mode); | 65 ExecutionMode execution_mode); |
63 | 66 |
64 // Inserts |sequence| into this thread pool's shared priority queue with | 67 // Inserts |sequence| into this thread pool's shared priority queue with |
65 // |sequence_sort_key|. Must only be called from a worker thread to put | 68 // |sequence_sort_key|. Must only be called from a worker thread to put |
66 // |sequence| back into a PriorityQueue after running a Task from it. The | 69 // |sequence| back into a PriorityQueue after running a Task from it. The |
67 // worker thread doesn't have to belong to this thread pool. | 70 // worker thread doesn't have to belong to this thread pool. |
68 void EnqueueSequence(scoped_refptr<Sequence> sequence, | 71 void EnqueueSequence(scoped_refptr<Sequence> sequence, |
69 const SequenceSortKey& sequence_sort_key); | 72 const SequenceSortKey& sequence_sort_key); |
70 | 73 |
71 // Waits until all threads are idle. | 74 // Waits until all threads are idle. |
72 void WaitForAllWorkerThreadsIdleForTesting(); | 75 void WaitForAllWorkerThreadsIdleForTesting(); |
73 | 76 |
74 // Joins all threads of this thread pool. Tasks that are already running are | 77 // Joins all threads of this thread pool. Tasks that are already running are |
75 // allowed to complete their execution. This can only be called once. | 78 // allowed to complete their execution. This can only be called once. |
76 void JoinForTesting(); | 79 void JoinForTesting(); |
77 | 80 |
78 // SchedulerTaskExecutor: | 81 // SchedulerTaskExecutor: |
79 void PostTaskWithSequence(std::unique_ptr<Task> task, | 82 void PostTaskWithSequence(std::unique_ptr<Task> task, |
80 scoped_refptr<Sequence> sequence) override; | 83 scoped_refptr<Sequence> sequence) override; |
81 | 84 |
82 private: | 85 private: |
83 class SchedulerWorkerThreadDelegateImpl; | 86 class SchedulerWorkerThreadDelegateImpl; |
84 | 87 |
85 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback, | 88 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback, |
86 TaskTracker* task_tracker); | 89 TaskTracker* task_tracker, |
| 90 DelayedTaskManager* delayed_task_manager); |
87 | 91 |
88 bool Initialize(ThreadPriority thread_priority, size_t max_threads); | 92 bool Initialize(ThreadPriority thread_priority, size_t max_threads); |
89 | 93 |
90 // Wakes up the last thread from this thread pool to go idle, if any. | 94 // Wakes up the last thread from this thread pool to go idle, if any. |
91 void WakeUpOneThread(); | 95 void WakeUpOneThread(); |
92 | 96 |
93 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 97 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
94 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 98 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
95 | 99 |
96 // Pops one idle worker thread from |idle_worker_thread_stack_| and returns | 100 // Pops one idle worker thread from |idle_worker_thread_stack_| and returns |
(...skipping 20 matching lines...) Expand all Loading... |
117 // Signaled when all worker threads become idle. | 121 // Signaled when all worker threads become idle. |
118 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 122 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
119 | 123 |
120 // Signaled once JoinForTesting() has returned. | 124 // Signaled once JoinForTesting() has returned. |
121 WaitableEvent join_for_testing_returned_; | 125 WaitableEvent join_for_testing_returned_; |
122 | 126 |
123 // Delegate for all worker threads in this pool. | 127 // Delegate for all worker threads in this pool. |
124 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; | 128 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; |
125 | 129 |
126 TaskTracker* const task_tracker_; | 130 TaskTracker* const task_tracker_; |
| 131 DelayedTaskManager* const delayed_task_manager_; |
127 | 132 |
128 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | 133 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); |
129 }; | 134 }; |
130 | 135 |
131 } // namespace internal | 136 } // namespace internal |
132 } // namespace base | 137 } // namespace base |
133 | 138 |
134 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 139 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
OLD | NEW |