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> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "base/task_scheduler/scheduler_worker.h" | 24 #include "base/task_scheduler/scheduler_worker.h" |
| 25 #include "base/task_scheduler/scheduler_worker_pool.h" | 25 #include "base/task_scheduler/scheduler_worker_pool.h" |
| 26 #include "base/task_scheduler/scheduler_worker_pool_params.h" | 26 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 27 #include "base/task_scheduler/scheduler_worker_stack.h" | 27 #include "base/task_scheduler/scheduler_worker_stack.h" |
| 28 #include "base/task_scheduler/sequence.h" | 28 #include "base/task_scheduler/sequence.h" |
| 29 #include "base/task_scheduler/task.h" | 29 #include "base/task_scheduler/task.h" |
| 30 #include "base/task_scheduler/task_traits.h" | 30 #include "base/task_scheduler/task_traits.h" |
| 31 #include "base/threading/platform_thread.h" | 31 #include "base/threading/platform_thread.h" |
| 32 | 32 |
| 33 namespace base { | 33 namespace base { |
| 34 | |
| 35 class TimeDelta; | |
| 36 | |
| 34 namespace internal { | 37 namespace internal { |
| 35 | 38 |
| 36 class DelayedTaskManager; | 39 class DelayedTaskManager; |
| 37 class TaskTracker; | 40 class TaskTracker; |
| 38 | 41 |
| 39 // A pool of workers that run Tasks. This class is thread-safe. | 42 // A pool of workers that run Tasks. This class is thread-safe. |
| 40 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 43 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
| 41 public: | 44 public: |
| 42 // Callback invoked when a Sequence isn't empty after a worker pops a Task | 45 // Callback invoked when a Sequence isn't empty after a worker pops a Task |
| 43 // from it. | 46 // from it. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 60 TaskTracker* task_tracker, | 63 TaskTracker* task_tracker, |
| 61 DelayedTaskManager* delayed_task_manager); | 64 DelayedTaskManager* delayed_task_manager); |
| 62 | 65 |
| 63 // Waits until all workers are idle. | 66 // Waits until all workers are idle. |
| 64 void WaitForAllWorkersIdleForTesting(); | 67 void WaitForAllWorkersIdleForTesting(); |
| 65 | 68 |
| 66 // Joins all workers of this worker pool. Tasks that are already running are | 69 // Joins all workers of this worker pool. Tasks that are already running are |
| 67 // allowed to complete their execution. This can only be called once. | 70 // allowed to complete their execution. This can only be called once. |
| 68 void JoinForTesting(); | 71 void JoinForTesting(); |
| 69 | 72 |
| 73 // Disallows worker thread detachment. If the suggested reclaim time is not | |
|
fdoray
2016/07/20 14:15:43
// [...], then the test should call this before Jo
robliao
2016/07/20 19:44:01
Unless the test requires the ability to detach. I'
| |
| 74 // TimeDelta::Max(), then the test should call this before the detach code can | |
| 75 // run. The safest place to do this is before the first set of work is | |
| 76 // dispatched (the worker pool is idle and steady state) or before the last | |
| 77 // synchronization point for all workers (all threads are busy and can't be | |
| 78 // reclaimed). | |
| 79 void DisallowWorkerDetachmentForTesting(); | |
| 80 | |
| 70 // SchedulerWorkerPool: | 81 // SchedulerWorkerPool: |
| 71 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 82 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 72 const TaskTraits& traits, | 83 const TaskTraits& traits, |
| 73 ExecutionMode execution_mode) override; | 84 ExecutionMode execution_mode) override; |
| 74 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 85 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
| 75 const SequenceSortKey& sequence_sort_key) override; | 86 const SequenceSortKey& sequence_sort_key) override; |
| 76 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 87 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
| 77 scoped_refptr<Sequence> sequence, | 88 scoped_refptr<Sequence> sequence, |
| 78 SchedulerWorker* worker) override; | 89 SchedulerWorker* worker) override; |
| 79 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 90 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
| 80 scoped_refptr<Sequence> sequence, | 91 scoped_refptr<Sequence> sequence, |
| 81 SchedulerWorker* worker) override; | 92 SchedulerWorker* worker) override; |
| 82 | 93 |
| 83 private: | 94 private: |
| 95 class SchedulerSingleThreadTaskRunner; | |
| 84 class SchedulerWorkerDelegateImpl; | 96 class SchedulerWorkerDelegateImpl; |
| 85 | 97 |
| 86 SchedulerWorkerPoolImpl(StringPiece name, | 98 SchedulerWorkerPoolImpl(StringPiece name, |
| 87 SchedulerWorkerPoolParams::IORestriction | 99 SchedulerWorkerPoolParams::IORestriction |
| 88 io_restriction, | 100 io_restriction, |
| 101 const TimeDelta& suggested_reclaim_time, | |
| 89 TaskTracker* task_tracker, | 102 TaskTracker* task_tracker, |
| 90 DelayedTaskManager* delayed_task_manager); | 103 DelayedTaskManager* delayed_task_manager); |
| 91 | 104 |
| 92 bool Initialize( | 105 bool Initialize( |
| 93 ThreadPriority thread_priority, | 106 ThreadPriority thread_priority, |
| 94 size_t max_threads, | 107 size_t max_threads, |
| 95 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 108 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 96 | 109 |
| 97 // Wakes up the last worker from this worker pool to go idle, if any. | 110 // Wakes up the last worker from this worker pool to go idle, if any. |
| 98 void WakeUpOneWorker(); | 111 void WakeUpOneWorker(); |
| 99 | 112 |
| 100 // Adds |worker| to |idle_workers_stack_|. | 113 // Adds |worker| to |idle_workers_stack_|. |
| 101 void AddToIdleWorkersStack(SchedulerWorker* worker); | 114 void AddToIdleWorkersStack(SchedulerWorker* worker); |
| 102 | 115 |
| 116 // Peeks from |idle_workers_stack_|. | |
| 117 const SchedulerWorker* PeekAtIdleWorkersStack() const; | |
| 118 | |
| 103 // Removes |worker| from |idle_workers_stack_|. | 119 // Removes |worker| from |idle_workers_stack_|. |
| 104 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); | 120 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); |
| 105 | 121 |
| 122 // Returns true if worker thread detachment is permitted. | |
| 123 bool CanWorkerDetachForTesting(); | |
| 124 | |
| 106 // The name of this worker pool, used to label its worker threads. | 125 // The name of this worker pool, used to label its worker threads. |
| 107 const std::string name_; | 126 const std::string name_; |
| 108 | 127 |
| 109 // All worker owned by this worker pool. Only modified during initialization | 128 // All worker owned by this worker pool. Only modified during initialization |
| 110 // of the worker pool. | 129 // of the worker pool. |
| 111 std::vector<std::unique_ptr<SchedulerWorker>> workers_; | 130 std::vector<std::unique_ptr<SchedulerWorker>> workers_; |
| 112 | 131 |
| 113 // Synchronizes access to |next_worker_index_|. | 132 // Synchronizes access to |next_worker_index_|. |
| 114 SchedulerLock next_worker_index_lock_; | 133 SchedulerLock next_worker_index_lock_; |
| 115 | 134 |
| 116 // Index of the worker that will be assigned to the next single-threaded | 135 // Index of the worker that will be assigned to the next single-threaded |
| 117 // TaskRunner returned by this pool. | 136 // TaskRunner returned by this pool. |
| 118 size_t next_worker_index_ = 0; | 137 size_t next_worker_index_ = 0; |
| 119 | 138 |
| 120 // PriorityQueue from which all threads of this worker pool get work. | 139 // PriorityQueue from which all threads of this worker pool get work. |
| 121 PriorityQueue shared_priority_queue_; | 140 PriorityQueue shared_priority_queue_; |
| 122 | 141 |
| 123 // Indicates whether Tasks on this worker pool are allowed to make I/O calls. | 142 // Indicates whether Tasks on this worker pool are allowed to make I/O calls. |
| 124 const SchedulerWorkerPoolParams::IORestriction io_restriction_; | 143 const SchedulerWorkerPoolParams::IORestriction io_restriction_; |
| 125 | 144 |
| 145 // Suggested reclaim time for workers. | |
| 146 const TimeDelta suggested_reclaim_time_; | |
| 147 | |
| 126 // Synchronizes access to |idle_workers_stack_| and | 148 // Synchronizes access to |idle_workers_stack_| and |
| 127 // |idle_workers_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 149 // |idle_workers_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
| 128 // lock as its predecessor so that a worker can be pushed to | 150 // lock as its predecessor so that a worker can be pushed to |
| 129 // |idle_workers_stack_| within the scope of a Transaction (more | 151 // |idle_workers_stack_| within the scope of a Transaction (more |
| 130 // details in GetWork()). | 152 // details in GetWork()). |
| 131 SchedulerLock idle_workers_stack_lock_; | 153 mutable SchedulerLock idle_workers_stack_lock_; |
| 132 | 154 |
| 133 // Stack of idle workers. | 155 // Stack of idle workers. |
| 134 SchedulerWorkerStack idle_workers_stack_; | 156 SchedulerWorkerStack idle_workers_stack_; |
| 135 | 157 |
| 136 // Signaled when all workers become idle. | 158 // Signaled when all workers become idle. |
| 137 std::unique_ptr<ConditionVariable> idle_workers_stack_cv_for_testing_; | 159 std::unique_ptr<ConditionVariable> idle_workers_stack_cv_for_testing_; |
| 138 | 160 |
| 139 // Signaled once JoinForTesting() has returned. | 161 // Signaled once JoinForTesting() has returned. |
| 140 WaitableEvent join_for_testing_returned_; | 162 WaitableEvent join_for_testing_returned_; |
| 141 | 163 |
| 164 // Synchronizes access to |worker_detachment_allowed_|. | |
| 165 SchedulerLock worker_detachment_allowed_lock_; | |
| 166 | |
| 167 // Indicates to the delegates that workers are permitted to detach their | |
| 168 // threads. | |
| 169 bool worker_detachment_allowed_; | |
| 170 | |
| 142 #if DCHECK_IS_ON() | 171 #if DCHECK_IS_ON() |
| 143 // Signaled when all workers have been created. | 172 // Signaled when all workers have been created. |
| 144 WaitableEvent workers_created_; | 173 WaitableEvent workers_created_; |
| 145 #endif | 174 #endif |
| 146 | 175 |
| 147 TaskTracker* const task_tracker_; | 176 TaskTracker* const task_tracker_; |
| 148 DelayedTaskManager* const delayed_task_manager_; | 177 DelayedTaskManager* const delayed_task_manager_; |
| 149 | 178 |
| 150 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 179 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
| 151 }; | 180 }; |
| 152 | 181 |
| 153 } // namespace internal | 182 } // namespace internal |
| 154 } // namespace base | 183 } // namespace base |
| 155 | 184 |
| 156 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 185 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| OLD | NEW |