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