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 <unordered_map> | |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
14 #include "base/callback.h" | 15 #include "base/callback.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/synchronization/condition_variable.h" | 18 #include "base/synchronization/condition_variable.h" |
18 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
19 #include "base/task_scheduler/priority_queue.h" | 20 #include "base/task_scheduler/priority_queue.h" |
20 #include "base/task_scheduler/scheduler_lock.h" | 21 #include "base/task_scheduler/scheduler_lock.h" |
21 #include "base/task_scheduler/scheduler_thread_pool_interface.h" | 22 #include "base/task_scheduler/scheduler_thread_pool_interface.h" |
22 #include "base/task_scheduler/scheduler_unique_stack.h" | 23 #include "base/task_scheduler/scheduler_unique_stack.h" |
23 #include "base/task_scheduler/scheduler_worker_thread.h" | 24 #include "base/task_scheduler/scheduler_worker_thread.h" |
24 #include "base/task_scheduler/sequence.h" | 25 #include "base/task_scheduler/sequence.h" |
25 #include "base/task_scheduler/task.h" | 26 #include "base/task_scheduler/task.h" |
26 #include "base/task_scheduler/task_traits.h" | 27 #include "base/task_scheduler/task_traits.h" |
27 #include "base/threading/platform_thread.h" | 28 #include "base/threading/platform_thread.h" |
28 | 29 |
29 namespace base { | 30 namespace base { |
30 namespace internal { | 31 namespace internal { |
31 | 32 |
32 class DelayedTaskManager; | 33 class DelayedTaskManager; |
33 struct SequenceSortKey; | 34 struct SequenceSortKey; |
34 class TaskTracker; | 35 class TaskTracker; |
35 | 36 |
36 // A pool of threads that run Tasks. This class is thread-safe. | 37 // A pool of threads that run Tasks. This class is thread-safe. |
37 class BASE_EXPORT SchedulerThreadPool : public SchedulerThreadPoolInterface { | 38 class BASE_EXPORT SchedulerThreadPool : public SchedulerThreadPoolInterface { |
38 public: | 39 public: |
39 // Callback invoked when a Sequence isn't empty after a worker thread pops a | 40 // Callback invoked when a non-single-threaded Sequence isn't empty after a |
40 // Task from it. | 41 // worker thread pops a Task from it. |
41 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 42 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
42 | 43 |
43 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not | 44 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not |
44 // allowed in production; it is always leaked. In tests, it can only be | 45 // allowed in production; it is always leaked. In tests, it can only be |
45 // destroyed after JoinForTesting() has returned. | 46 // destroyed after JoinForTesting() has returned. |
46 ~SchedulerThreadPool() override; | 47 ~SchedulerThreadPool() override; |
47 | 48 |
48 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | 49 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority |
49 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a | 50 // |thread_priority|. |enqueue_sequence_callback| will be invoked when a non- |
50 // thread of this thread pool tries to run a Task. |task_tracker| is used to | 51 // single-threaded Sequence isn't empty after a worker thread pops a Task from |
51 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | 52 // it. |task_tracker| is used to handle shutdown behavior of Tasks. |
52 // posted with a delay. Returns nullptr on failure to create a thread pool | 53 // |delayed_task_manager| handles Tasks posted with a delay. Returns nullptr |
53 // with at least one thread. | 54 // on failure to create a thread pool with at least one thread. |
54 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( | 55 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( |
55 ThreadPriority thread_priority, | 56 ThreadPriority thread_priority, |
56 size_t max_threads, | 57 size_t max_threads, |
57 const EnqueueSequenceCallback& enqueue_sequence_callback, | 58 const EnqueueSequenceCallback& enqueue_sequence_callback, |
58 TaskTracker* task_tracker, | 59 TaskTracker* task_tracker, |
59 DelayedTaskManager* delayed_task_manager); | 60 DelayedTaskManager* delayed_task_manager); |
60 | 61 |
61 // Returns a TaskRunner whose PostTask invocations will result in scheduling | 62 // Returns a TaskRunner whose PostTask invocations will result in scheduling |
62 // Tasks with |traits| and |execution_mode| in this thread pool. | 63 // Tasks with |traits| and |execution_mode| in this thread pool. |
63 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 64 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
64 const TaskTraits& traits, | 65 const TaskTraits& traits, |
65 ExecutionMode execution_mode); | 66 ExecutionMode execution_mode); |
66 | 67 |
67 // Inserts |sequence| into this thread pool's shared priority queue with | 68 // Inserts |sequence| into this thread pool's shared priority queue with |
68 // |sequence_sort_key|. Must only be called from a worker thread to put | 69 // |sequence_sort_key|. Must only be called from a worker thread to put |
69 // |sequence| back into a PriorityQueue after running a Task from it. The | 70 // |sequence| back into a PriorityQueue after running a Task from it. The |
70 // worker thread doesn't have to belong to this thread pool. | 71 // worker thread doesn't have to belong to this thread pool. |
71 void EnqueueSequence(scoped_refptr<Sequence> sequence, | 72 void EnqueueSequence(scoped_refptr<Sequence> sequence, |
72 const SequenceSortKey& sequence_sort_key); | 73 const SequenceSortKey& sequence_sort_key); |
73 | 74 |
74 // Waits until all threads are idle. | 75 // Waits until all threads are idle. |
75 void WaitForAllWorkerThreadsIdleForTesting(); | 76 void WaitForAllWorkerThreadsIdleForTesting(); |
76 | 77 |
77 // Joins all threads of this thread pool. Tasks that are already running are | 78 // Joins all threads of this thread pool. Tasks that are already running are |
78 // allowed to complete their execution. This can only be called once. | 79 // allowed to complete their execution. This can only be called once. |
79 void JoinForTesting(); | 80 void JoinForTesting(); |
80 | 81 |
81 // Posts |task| to be executed as part of |sequence|. Returns true if |task| | 82 // Posts |task| to be executed as part of |sequence|. |task| will run on |
82 // is posted. | 83 // |worker_thread| if specified; otherwise it can run on a any thread owned by |
danakj
2016/04/27 18:28:43
"on any"
fdoray
2016/04/27 19:17:04
n/a with latest patch set
| |
84 // this pool. Returns true if |task| is posted. | |
83 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 85 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
84 scoped_refptr<Sequence> sequence); | 86 scoped_refptr<Sequence> sequence, |
87 SchedulerWorkerThread* worker_thread); | |
85 | 88 |
86 // SchedulerThreadPoolInterface: | 89 // SchedulerThreadPoolInterface: |
87 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 90 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
88 scoped_refptr<Sequence> sequence) override; | 91 scoped_refptr<Sequence> sequence, |
92 SchedulerWorkerThread* worker_thread) override; | |
89 | 93 |
90 private: | 94 private: |
91 class SchedulerWorkerThreadDelegateImpl; | 95 class SchedulerWorkerThreadDelegateImpl; |
92 | 96 |
93 SchedulerThreadPool(TaskTracker* task_tracker, | 97 SchedulerThreadPool(TaskTracker* task_tracker, |
94 DelayedTaskManager* delayed_task_manager); | 98 DelayedTaskManager* delayed_task_manager); |
95 | 99 |
96 bool Initialize(ThreadPriority thread_priority, | 100 bool Initialize(ThreadPriority thread_priority, |
97 size_t max_threads, | 101 size_t max_threads, |
98 const EnqueueSequenceCallback& enqueue_sequence_callback); | 102 const EnqueueSequenceCallback& enqueue_sequence_callback); |
99 | 103 |
100 // Wakes up the last thread from this thread pool to go idle, if any. | 104 // Wakes up the last thread from this thread pool to go idle, if any. |
101 void WakeUpOneThread(); | 105 void WakeUpOneThread(); |
102 | 106 |
103 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 107 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
104 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 108 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
105 | 109 |
110 // Removes |worker_thread| from |idle_worker_threads_stack_|. | |
111 void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | |
112 | |
106 // Pops one idle worker thread from |idle_worker_thread_stack_| and returns | 113 // Pops one idle worker thread from |idle_worker_thread_stack_| and returns |
107 // it. Returns nullptr if |idle_worker_thread_stack_| is empty. | 114 // it. Returns nullptr if |idle_worker_thread_stack_| is empty. |
108 SchedulerWorkerThread* PopOneIdleWorkerThread(); | 115 SchedulerWorkerThread* PopOneIdleWorkerThread(); |
109 | 116 |
110 // PriorityQueue from which all threads of this thread pool get work. | |
111 PriorityQueue shared_priority_queue_; | |
112 | |
113 // All worker threads owned by this thread pool. Only modified during | 117 // All worker threads owned by this thread pool. Only modified during |
114 // initialization of the thread pool. | 118 // initialization of the thread pool. |
115 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | 119 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
116 | 120 |
121 // Synchronizes access to |next_worker_thread_index_|. | |
122 SchedulerLock next_worker_thread_index_lock_; | |
123 | |
124 // Index of the worker thread that will be assigned to the next single- | |
125 // threaded TaskRunner returned by this pool. | |
126 size_t next_worker_thread_index_ = 0; | |
127 | |
128 // PriorityQueue from which all threads of this thread pool get work. | |
129 PriorityQueue shared_priority_queue_; | |
130 | |
131 // Single-threaded PriorityQueues. | |
132 std::unordered_map<SchedulerWorkerThread*, std::unique_ptr<PriorityQueue>> | |
133 single_threaded_priority_queues_; | |
134 | |
117 // Synchronizes access to |idle_worker_threads_stack_| and | 135 // Synchronizes access to |idle_worker_threads_stack_| and |
118 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 136 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
119 // lock as its predecessor so that a thread can be pushed to | 137 // lock as its predecessor so that a thread can be pushed to |
120 // |idle_worker_threads_stack_| within the scope of a Transaction (more | 138 // |idle_worker_threads_stack_| within the scope of a Transaction (more |
121 // details in GetWork()). | 139 // details in GetWork()). |
122 SchedulerLock idle_worker_threads_stack_lock_; | 140 SchedulerLock idle_worker_threads_stack_lock_; |
123 | 141 |
124 // Stack of idle worker threads. | 142 // Stack of idle worker threads. |
125 SchedulerUniqueStack<SchedulerWorkerThread*> idle_worker_threads_stack_; | 143 SchedulerUniqueStack<SchedulerWorkerThread*> idle_worker_threads_stack_; |
126 | 144 |
127 // Signaled when all worker threads become idle. | 145 // Signaled when all worker threads become idle. |
128 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 146 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
129 | 147 |
130 // Signaled once JoinForTesting() has returned. | 148 // Signaled once JoinForTesting() has returned. |
131 WaitableEvent join_for_testing_returned_; | 149 WaitableEvent join_for_testing_returned_; |
132 | 150 |
133 TaskTracker* const task_tracker_; | 151 TaskTracker* const task_tracker_; |
134 DelayedTaskManager* const delayed_task_manager_; | 152 DelayedTaskManager* const delayed_task_manager_; |
135 | 153 |
136 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | 154 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); |
137 }; | 155 }; |
138 | 156 |
139 } // namespace internal | 157 } // namespace internal |
140 } // namespace base | 158 } // namespace base |
141 | 159 |
142 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 160 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
OLD | NEW |