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 28 matching lines...) Expand all Loading... |
39 class TaskTracker; | 39 class TaskTracker; |
40 | 40 |
41 // A pool of workers that run Tasks. | 41 // A pool of workers that run Tasks. |
42 // | 42 // |
43 // The pool doesn't create threads until Start() is called. Tasks can be posted | 43 // The pool doesn't create threads until Start() is called. Tasks can be posted |
44 // at any time but will not run until after Start() is called. | 44 // at any time but will not run until after Start() is called. |
45 // | 45 // |
46 // This class is thread-safe. | 46 // This class is thread-safe. |
47 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 47 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
48 public: | 48 public: |
49 // Callback invoked when a Sequence isn't empty after a worker pops a Task | |
50 // from it. | |
51 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | |
52 | |
53 // Constructs a pool without workers. | 49 // Constructs a pool without workers. |
54 // | 50 // |
55 // |name| is used to label the pool's threads ("TaskScheduler" + |name| + | 51 // |name| is used to label the pool's threads ("TaskScheduler" + |name| + |
56 // index) and histograms ("TaskScheduler." + histogram name + "." + |name| + | 52 // index) and histograms ("TaskScheduler." + histogram name + "." + |name| + |
57 // extra suffixes). |priority_hint| is the preferred thread priority; the | 53 // extra suffixes). |priority_hint| is the preferred thread priority; the |
58 // actual thread priority depends on shutdown state and platform capabilities. | 54 // actual thread priority depends on shutdown state and platform capabilities. |
59 // |re_enqueue_sequence_callback| is invoked when a Sequence isn't empty after | 55 // |task_tracker| keeps track of tasks. |delayed_task_manager| handles tasks |
60 // a worker pops a Task from it. |task_tracker| keeps track of tasks. | 56 // posted with a delay. |
61 // |delayed_task_manager| handles tasks posted with a delay. | |
62 SchedulerWorkerPoolImpl( | 57 SchedulerWorkerPoolImpl( |
63 const std::string& name, | 58 const std::string& name, |
64 ThreadPriority priority_hint, | 59 ThreadPriority priority_hint, |
65 ReEnqueueSequenceCallback re_enqueue_sequence_callback, | |
66 TaskTracker* task_tracker, | 60 TaskTracker* task_tracker, |
67 DelayedTaskManager* delayed_task_manager); | 61 DelayedTaskManager* delayed_task_manager); |
68 | 62 |
69 // Creates workers following the |params| specification, allowing existing and | 63 // Creates workers following the |params| specification, allowing existing and |
70 // future tasks to run. Can only be called once. CHECKs on failure. | 64 // future tasks to run. Can only be called once. CHECKs on failure. |
71 void Start(const SchedulerWorkerPoolParams& params); | 65 void Start(const SchedulerWorkerPoolParams& params); |
72 | 66 |
73 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in | 67 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in |
74 // production; it is always leaked. In tests, it can only be destroyed after | 68 // production; it is always leaked. In tests, it can only be destroyed after |
75 // JoinForTesting() has returned. | 69 // JoinForTesting() has returned. |
76 ~SchedulerWorkerPoolImpl() override; | 70 ~SchedulerWorkerPoolImpl() override; |
77 | 71 |
78 // SchedulerWorkerPool: | 72 // SchedulerWorkerPool: |
79 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 73 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
80 const TaskTraits& traits) override; | 74 const TaskTraits& traits) override; |
81 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( | 75 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
82 const TaskTraits& traits) override; | 76 const TaskTraits& traits) override; |
83 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | |
84 const SequenceSortKey& sequence_sort_key) override; | |
85 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 77 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
86 scoped_refptr<Sequence> sequence) override; | 78 scoped_refptr<Sequence> sequence) override; |
87 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 79 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
88 scoped_refptr<Sequence> sequence) override; | 80 scoped_refptr<Sequence> sequence) override; |
89 | 81 |
90 const HistogramBase* num_tasks_before_detach_histogram() const { | 82 const HistogramBase* num_tasks_before_detach_histogram() const { |
91 return num_tasks_before_detach_histogram_; | 83 return num_tasks_before_detach_histogram_; |
92 } | 84 } |
93 | 85 |
94 const HistogramBase* num_tasks_between_waits_histogram() const { | 86 const HistogramBase* num_tasks_between_waits_histogram() const { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 const SchedulerWorker* PeekAtIdleWorkersStack() const; | 128 const SchedulerWorker* PeekAtIdleWorkersStack() const; |
137 | 129 |
138 // Removes |worker| from |idle_workers_stack_|. | 130 // Removes |worker| from |idle_workers_stack_|. |
139 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); | 131 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); |
140 | 132 |
141 // Returns true if worker thread detachment is permitted. | 133 // Returns true if worker thread detachment is permitted. |
142 bool CanWorkerDetachForTesting(); | 134 bool CanWorkerDetachForTesting(); |
143 | 135 |
144 const std::string name_; | 136 const std::string name_; |
145 const ThreadPriority priority_hint_; | 137 const ThreadPriority priority_hint_; |
146 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; | |
147 | 138 |
148 // PriorityQueue from which all threads of this worker pool get work. | 139 // PriorityQueue from which all threads of this worker pool get work. |
149 PriorityQueue shared_priority_queue_; | 140 PriorityQueue shared_priority_queue_; |
150 | 141 |
151 // All workers owned by this worker pool. Initialized by Start() within the | 142 // All workers owned by this worker pool. Initialized by Start() within the |
152 // scope of |idle_workers_stack_lock_|. Never modified afterwards (i.e. can be | 143 // scope of |idle_workers_stack_lock_|. Never modified afterwards (i.e. can be |
153 // read without synchronization once |workers_created_.IsSet()|). | 144 // read without synchronization once |workers_created_.IsSet()|). |
154 std::vector<scoped_refptr<SchedulerWorker>> workers_; | 145 std::vector<scoped_refptr<SchedulerWorker>> workers_; |
155 | 146 |
156 // Suggested reclaim time for workers. Initialized by Start(). Never modified | 147 // Suggested reclaim time for workers. Initialized by Start(). Never modified |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 TaskTracker* const task_tracker_; | 198 TaskTracker* const task_tracker_; |
208 DelayedTaskManager* const delayed_task_manager_; | 199 DelayedTaskManager* const delayed_task_manager_; |
209 | 200 |
210 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 201 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
211 }; | 202 }; |
212 | 203 |
213 } // namespace internal | 204 } // namespace internal |
214 } // namespace base | 205 } // namespace base |
215 | 206 |
216 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 207 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
OLD | NEW |