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