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_IMPL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/logging.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.h" | 22 #include "base/task_scheduler/scheduler_thread_pool.h" |
22 #include "base/task_scheduler/scheduler_worker_thread.h" | 23 #include "base/task_scheduler/scheduler_worker_thread.h" |
23 #include "base/task_scheduler/scheduler_worker_thread_stack.h" | 24 #include "base/task_scheduler/scheduler_worker_thread_stack.h" |
24 #include "base/task_scheduler/sequence.h" | 25 #include "base/task_scheduler/sequence.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // allowed to complete their execution. This can only be called once. | 66 // allowed to complete their execution. This can only be called once. |
66 void JoinForTesting(); | 67 void JoinForTesting(); |
67 | 68 |
68 // SchedulerThreadPool: | 69 // SchedulerThreadPool: |
69 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 70 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
70 const TaskTraits& traits, | 71 const TaskTraits& traits, |
71 ExecutionMode execution_mode) override; | 72 ExecutionMode execution_mode) override; |
72 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 73 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
73 const SequenceSortKey& sequence_sort_key) override; | 74 const SequenceSortKey& sequence_sort_key) override; |
74 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 75 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
75 scoped_refptr<Sequence> sequence) override; | 76 scoped_refptr<Sequence> sequence, |
| 77 SchedulerWorkerThread* worker_thread) override; |
76 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 78 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
77 scoped_refptr<Sequence> sequence) override; | 79 scoped_refptr<Sequence> sequence, |
| 80 SchedulerWorkerThread* worker_thread) override; |
78 | 81 |
79 private: | 82 private: |
80 class SchedulerWorkerThreadDelegateImpl; | 83 class SchedulerWorkerThreadDelegateImpl; |
81 | 84 |
82 SchedulerThreadPoolImpl( | 85 SchedulerThreadPoolImpl(TaskTracker* task_tracker, |
83 TaskTracker* task_tracker, | 86 DelayedTaskManager* delayed_task_manager); |
84 DelayedTaskManager* delayed_task_manager); | |
85 | 87 |
86 bool Initialize( | 88 bool Initialize( |
87 ThreadPriority thread_priority, | 89 ThreadPriority thread_priority, |
88 size_t max_threads, | 90 size_t max_threads, |
89 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 91 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
90 | 92 |
91 // Wakes up the last thread from this thread pool to go idle, if any. | 93 // Wakes up the last thread from this thread pool to go idle, if any. |
92 void WakeUpOneThread(); | 94 void WakeUpOneThread(); |
93 | 95 |
94 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 96 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
95 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 97 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
96 | 98 |
97 // PriorityQueue from which all threads of this thread pool get work. | 99 // Removes |worker_thread| from |idle_worker_threads_stack_|. |
98 PriorityQueue shared_priority_queue_; | 100 void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
99 | 101 |
100 // All worker threads owned by this thread pool. Only modified during | 102 // All worker threads owned by this thread pool. Only modified during |
101 // initialization of the thread pool. | 103 // initialization of the thread pool. |
102 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | 104 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
103 | 105 |
| 106 // Synchronizes access to |next_worker_thread_index_|. |
| 107 SchedulerLock next_worker_thread_index_lock_; |
| 108 |
| 109 // Index of the worker thread that will be assigned to the next single- |
| 110 // threaded TaskRunner returned by this pool. |
| 111 size_t next_worker_thread_index_ = 0; |
| 112 |
| 113 // PriorityQueue from which all threads of this thread pool get work. |
| 114 PriorityQueue shared_priority_queue_; |
| 115 |
104 // Synchronizes access to |idle_worker_threads_stack_| and | 116 // Synchronizes access to |idle_worker_threads_stack_| and |
105 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 117 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
106 // lock as its predecessor so that a thread can be pushed to | 118 // lock as its predecessor so that a thread can be pushed to |
107 // |idle_worker_threads_stack_| within the scope of a Transaction (more | 119 // |idle_worker_threads_stack_| within the scope of a Transaction (more |
108 // details in GetWork()). | 120 // details in GetWork()). |
109 SchedulerLock idle_worker_threads_stack_lock_; | 121 SchedulerLock idle_worker_threads_stack_lock_; |
110 | 122 |
111 // Stack of idle worker threads. | 123 // Stack of idle worker threads. |
112 SchedulerWorkerThreadStack idle_worker_threads_stack_; | 124 SchedulerWorkerThreadStack idle_worker_threads_stack_; |
113 | 125 |
114 // Signaled when all worker threads become idle. | 126 // Signaled when all worker threads become idle. |
115 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 127 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
116 | 128 |
117 // Signaled once JoinForTesting() has returned. | 129 // Signaled once JoinForTesting() has returned. |
118 WaitableEvent join_for_testing_returned_; | 130 WaitableEvent join_for_testing_returned_; |
119 | 131 |
| 132 #if DCHECK_IS_ON() |
| 133 // Signaled when all threads have been created. |
| 134 WaitableEvent threads_created_; |
| 135 #endif |
| 136 |
120 TaskTracker* const task_tracker_; | 137 TaskTracker* const task_tracker_; |
121 DelayedTaskManager* const delayed_task_manager_; | 138 DelayedTaskManager* const delayed_task_manager_; |
122 | 139 |
123 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); | 140 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); |
124 }; | 141 }; |
125 | 142 |
126 } // namespace internal | 143 } // namespace internal |
127 } // namespace base | 144 } // namespace base |
128 | 145 |
129 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 146 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
OLD | NEW |