Chromium Code Reviews| 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_WORKER_POOL_IMPL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 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/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
| 20 #include "base/synchronization/atomic_flag.h" | |
| 21 #include "base/synchronization/condition_variable.h" | 20 #include "base/synchronization/condition_variable.h" |
| 22 #include "base/task_scheduler/priority_queue.h" | 21 #include "base/task_scheduler/priority_queue.h" |
| 23 #include "base/task_scheduler/scheduler_lock.h" | 22 #include "base/task_scheduler/scheduler_lock.h" |
| 24 #include "base/task_scheduler/scheduler_worker.h" | 23 #include "base/task_scheduler/scheduler_worker.h" |
| 25 #include "base/task_scheduler/scheduler_worker_pool.h" | 24 #include "base/task_scheduler/scheduler_worker_pool.h" |
| 26 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 25 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 27 #include "base/task_scheduler/scheduler_worker_stack.h" | 26 #include "base/task_scheduler/scheduler_worker_stack.h" |
| 28 #include "base/task_scheduler/sequence.h" | 27 #include "base/task_scheduler/sequence.h" |
| 29 #include "base/task_scheduler/task.h" | 28 #include "base/task_scheduler/task.h" |
| 30 #include "base/threading/platform_thread.h" | 29 #include "base/threading/platform_thread.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 | 89 |
| 91 void GetHistograms(std::vector<const HistogramBase*>* histograms) const; | 90 void GetHistograms(std::vector<const HistogramBase*>* histograms) const; |
| 92 | 91 |
| 93 // Waits until all workers are idle. | 92 // Waits until all workers are idle. |
| 94 void WaitForAllWorkersIdleForTesting(); | 93 void WaitForAllWorkersIdleForTesting(); |
| 95 | 94 |
| 96 // Joins all workers of this worker pool. Tasks that are already running are | 95 // Joins all workers of this worker pool. Tasks that are already running are |
| 97 // allowed to complete their execution. This can only be called once. | 96 // allowed to complete their execution. This can only be called once. |
| 98 void JoinForTesting(); | 97 void JoinForTesting(); |
| 99 | 98 |
| 100 // Disallows worker thread detachment. If the suggested reclaim time is not | |
| 101 // TimeDelta::Max(), then the test should call this before the detach code can | |
| 102 // run. The safest place to do this is before the a set of work is dispatched | |
| 103 // (the worker pool is idle and steady state) or before the last | |
| 104 // synchronization point for all workers (all threads are busy and can't be | |
| 105 // reclaimed). | |
| 106 void DisallowWorkerDetachmentForTesting(); | |
|
robliao
2017/01/17 19:01:27
From what I can tell, we need to disallow detachme
fdoray
2017/01/17 20:43:13
After JoinForTesting() returns, the thread is eith
robliao
2017/01/17 21:09:03
Can JoinForTesting call DisallowWorkerDetachmentFo
fdoray
2017/01/17 23:51:00
The hard part is making sure that DisallowWorkerDe
robliao
2017/01/18 00:58:19
With this code change, you can now call it at any
fdoray
2017/01/19 16:37:40
What would be the benefit of calling DisallowWorke
robliao
2017/01/19 16:57:29
The intent here would be to first do a disallow de
| |
| 107 | |
| 108 // Returns the number of workers alive in this worker pool. The value may | 99 // Returns the number of workers alive in this worker pool. The value may |
| 109 // change if workers are woken up or detached during this call. | 100 // change if workers are woken up or detached during this call. |
| 110 size_t NumberOfAliveWorkersForTesting(); | 101 size_t NumberOfAliveWorkersForTesting(); |
| 111 | 102 |
| 112 private: | 103 private: |
| 113 class SchedulerSingleThreadTaskRunner; | 104 class SchedulerSingleThreadTaskRunner; |
| 114 class SchedulerWorkerDelegateImpl; | 105 class SchedulerWorkerDelegateImpl; |
| 115 | 106 |
| 116 SchedulerWorkerPoolImpl(StringPiece name, | 107 SchedulerWorkerPoolImpl(StringPiece name, |
| 117 TimeDelta suggested_reclaim_time, | 108 TimeDelta suggested_reclaim_time, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 132 | 123 |
| 133 // Adds |worker| to |idle_workers_stack_|. | 124 // Adds |worker| to |idle_workers_stack_|. |
| 134 void AddToIdleWorkersStack(SchedulerWorker* worker); | 125 void AddToIdleWorkersStack(SchedulerWorker* worker); |
| 135 | 126 |
| 136 // Peeks from |idle_workers_stack_|. | 127 // Peeks from |idle_workers_stack_|. |
| 137 const SchedulerWorker* PeekAtIdleWorkersStack() const; | 128 const SchedulerWorker* PeekAtIdleWorkersStack() const; |
| 138 | 129 |
| 139 // Removes |worker| from |idle_workers_stack_|. | 130 // Removes |worker| from |idle_workers_stack_|. |
| 140 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); | 131 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); |
| 141 | 132 |
| 142 // Returns true if worker thread detachment is permitted. | |
| 143 bool CanWorkerDetachForTesting(); | |
| 144 | |
| 145 // The name of this worker pool, used to label its worker threads. | 133 // The name of this worker pool, used to label its worker threads. |
| 146 const std::string name_; | 134 const std::string name_; |
| 147 | 135 |
| 148 // All worker owned by this worker pool. Only modified during initialization | 136 // All worker owned by this worker pool. Only modified during initialization |
| 149 // of the worker pool. | 137 // of the worker pool. |
| 150 std::vector<std::unique_ptr<SchedulerWorker>> workers_; | 138 std::vector<std::unique_ptr<SchedulerWorker>> workers_; |
| 151 | 139 |
| 152 // Synchronizes access to |next_worker_index_|. | 140 // Synchronizes access to |next_worker_index_|. |
| 153 SchedulerLock next_worker_index_lock_; | 141 SchedulerLock next_worker_index_lock_; |
| 154 | 142 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 175 // timeout expires, even if its WakeUp() method hasn't been called). A worker | 163 // timeout expires, even if its WakeUp() method hasn't been called). A worker |
| 176 // is pushed on this stack when it receives nullptr from GetWork(). | 164 // is pushed on this stack when it receives nullptr from GetWork(). |
| 177 SchedulerWorkerStack idle_workers_stack_; | 165 SchedulerWorkerStack idle_workers_stack_; |
| 178 | 166 |
| 179 // Signaled when all workers become idle. | 167 // Signaled when all workers become idle. |
| 180 std::unique_ptr<ConditionVariable> idle_workers_stack_cv_for_testing_; | 168 std::unique_ptr<ConditionVariable> idle_workers_stack_cv_for_testing_; |
| 181 | 169 |
| 182 // Signaled once JoinForTesting() has returned. | 170 // Signaled once JoinForTesting() has returned. |
| 183 WaitableEvent join_for_testing_returned_; | 171 WaitableEvent join_for_testing_returned_; |
| 184 | 172 |
| 185 // Indicates to the delegates that workers are not permitted to detach their | |
| 186 // threads. | |
| 187 AtomicFlag worker_detachment_disallowed_; | |
| 188 | |
| 189 #if DCHECK_IS_ON() | 173 #if DCHECK_IS_ON() |
| 190 // Signaled when all workers have been created. | 174 // Signaled when all workers have been created. |
| 191 WaitableEvent workers_created_; | 175 WaitableEvent workers_created_; |
| 192 #endif | 176 #endif |
| 193 | 177 |
| 194 // TaskScheduler.DetachDuration.[worker pool name] histogram. Intentionally | 178 // TaskScheduler.DetachDuration.[worker pool name] histogram. Intentionally |
| 195 // leaked. | 179 // leaked. |
| 196 HistogramBase* const detach_duration_histogram_; | 180 HistogramBase* const detach_duration_histogram_; |
| 197 | 181 |
| 198 // TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram. | 182 // TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram. |
| 199 // Intentionally leaked. | 183 // Intentionally leaked. |
| 200 HistogramBase* const num_tasks_before_detach_histogram_; | 184 HistogramBase* const num_tasks_before_detach_histogram_; |
| 201 | 185 |
| 202 // TaskScheduler.NumTasksBetweenWaits.[worker pool name] histogram. | 186 // TaskScheduler.NumTasksBetweenWaits.[worker pool name] histogram. |
| 203 // Intentionally leaked. | 187 // Intentionally leaked. |
| 204 HistogramBase* const num_tasks_between_waits_histogram_; | 188 HistogramBase* const num_tasks_between_waits_histogram_; |
| 205 | 189 |
| 206 TaskTracker* const task_tracker_; | 190 TaskTracker* const task_tracker_; |
| 207 DelayedTaskManager* const delayed_task_manager_; | 191 DelayedTaskManager* const delayed_task_manager_; |
| 208 | 192 |
| 209 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 193 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
| 210 }; | 194 }; |
| 211 | 195 |
| 212 } // namespace internal | 196 } // namespace internal |
| 213 } // namespace base | 197 } // namespace base |
| 214 | 198 |
| 215 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 199 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| OLD | NEW |