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> | |
12 #include <vector> | 11 #include <vector> |
13 | 12 |
14 #include "base/base_export.h" | 13 #include "base/base_export.h" |
15 #include "base/callback.h" | 14 #include "base/callback.h" |
16 #include "base/macros.h" | 15 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
18 #include "base/synchronization/condition_variable.h" | 17 #include "base/synchronization/condition_variable.h" |
19 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
20 #include "base/task_scheduler/priority_queue.h" | 19 #include "base/task_scheduler/priority_queue.h" |
21 #include "base/task_scheduler/scheduler_lock.h" | 20 #include "base/task_scheduler/scheduler_lock.h" |
22 #include "base/task_scheduler/scheduler_task_executor.h" | 21 #include "base/task_scheduler/scheduler_task_executor.h" |
23 #include "base/task_scheduler/scheduler_worker_thread.h" | 22 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 23 #include "base/task_scheduler/scheduler_worker_thread_stack.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 class DelayedTaskManager; |
33 struct SequenceSortKey; | 33 struct SequenceSortKey; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 DelayedTaskManager* delayed_task_manager); | 90 DelayedTaskManager* delayed_task_manager); |
91 | 91 |
92 bool Initialize(ThreadPriority thread_priority, size_t max_threads); | 92 bool Initialize(ThreadPriority thread_priority, size_t max_threads); |
93 | 93 |
94 // 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. |
95 void WakeUpOneThread(); | 95 void WakeUpOneThread(); |
96 | 96 |
97 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 97 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
98 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 98 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
99 | 99 |
100 // Pops one idle worker thread from |idle_worker_thread_stack_| and returns | |
101 // it. Returns nullptr if |idle_worker_thread_stack_| is empty. | |
102 SchedulerWorkerThread* PopOneIdleWorkerThread(); | |
103 | |
104 // PriorityQueue from which all threads of this thread pool get work. | 100 // PriorityQueue from which all threads of this thread pool get work. |
105 PriorityQueue shared_priority_queue_; | 101 PriorityQueue shared_priority_queue_; |
106 | 102 |
107 // All worker threads owned by this thread pool. Only modified during | 103 // All worker threads owned by this thread pool. Only modified during |
108 // initialization of the thread pool. | 104 // initialization of the thread pool. |
109 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | 105 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
110 | 106 |
111 // Synchronizes access to |idle_worker_threads_stack_| and | 107 // Synchronizes access to |idle_worker_threads_stack_| and |
112 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 108 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
113 // lock as its predecessor so that a thread can be pushed to | 109 // lock as its predecessor so that a thread can be pushed to |
114 // |idle_worker_threads_stack_| within the scope of a Transaction (more | 110 // |idle_worker_threads_stack_| within the scope of a Transaction (more |
115 // details in GetWork()). | 111 // details in GetWork()). |
116 SchedulerLock idle_worker_threads_stack_lock_; | 112 SchedulerLock idle_worker_threads_stack_lock_; |
117 | 113 |
118 // Stack of idle worker threads. | 114 // Stack of idle worker threads. |
119 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_; | 115 SchedulerWorkerThreadStack idle_worker_threads_stack_; |
120 | 116 |
121 // Signaled when all worker threads become idle. | 117 // Signaled when all worker threads become idle. |
122 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 118 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
123 | 119 |
124 // Signaled once JoinForTesting() has returned. | 120 // Signaled once JoinForTesting() has returned. |
125 WaitableEvent join_for_testing_returned_; | 121 WaitableEvent join_for_testing_returned_; |
126 | 122 |
127 // Delegate for all worker threads in this pool. | 123 // Delegate for all worker threads in this pool. |
128 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; | 124 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; |
129 | 125 |
130 TaskTracker* const task_tracker_; | 126 TaskTracker* const task_tracker_; |
131 DelayedTaskManager* const delayed_task_manager_; | 127 DelayedTaskManager* const delayed_task_manager_; |
132 | 128 |
133 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | 129 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); |
134 }; | 130 }; |
135 | 131 |
136 } // namespace internal | 132 } // namespace internal |
137 } // namespace base | 133 } // namespace base |
138 | 134 |
139 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 135 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
OLD | NEW |