| 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 21 matching lines...) Expand all Loading... |
| 32 #include "base/task_scheduler/task_traits.h" | 32 #include "base/task_scheduler/task_traits.h" |
| 33 #include "base/threading/platform_thread.h" | 33 #include "base/threading/platform_thread.h" |
| 34 | 34 |
| 35 namespace base { | 35 namespace base { |
| 36 | 36 |
| 37 class HistogramBase; | 37 class HistogramBase; |
| 38 class TimeDelta; | 38 class TimeDelta; |
| 39 | 39 |
| 40 namespace internal { | 40 namespace internal { |
| 41 | 41 |
| 42 class DelayedTaskManager; | |
| 43 class TaskTracker; | 42 class TaskTracker; |
| 44 | 43 |
| 45 // A pool of workers that run Tasks. This class is thread-safe. | 44 // A pool of workers that run Tasks. This class is thread-safe. |
| 46 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 45 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
| 47 public: | 46 public: |
| 48 // Callback invoked when a Sequence isn't empty after a worker pops a Task | 47 // Callback invoked when a Sequence isn't empty after a worker pops a Task |
| 49 // from it. | 48 // from it. |
| 50 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 49 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
| 51 | 50 |
| 52 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in | 51 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in |
| 53 // production; it is always leaked. In tests, it can only be destroyed after | 52 // production; it is always leaked. In tests, it can only be destroyed after |
| 54 // JoinForTesting() has returned. | 53 // JoinForTesting() has returned. |
| 55 ~SchedulerWorkerPoolImpl() override; | 54 ~SchedulerWorkerPoolImpl() override; |
| 56 | 55 |
| 57 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| | 56 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| |
| 58 // specification. |re_enqueue_sequence_callback| will be invoked after a | 57 // specification. |re_enqueue_sequence_callback| will be invoked after a |
| 59 // worker of this worker pool tries to run a Task. |task_tracker| is used to | 58 // worker of this worker pool tries to run a Task. |task_tracker| is used to |
| 60 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | 59 // handle shutdown behavior of Tasks. |service_thread_task_runner| handles |
| 61 // posted with a delay. Returns nullptr on failure to create a worker pool | 60 // Tasks posted with a delay. Returns nullptr on failure to create a worker |
| 62 // with at least one thread. | 61 // pool with at least one thread. |
| 63 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( | 62 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( |
| 64 const SchedulerWorkerPoolParams& params, | 63 const SchedulerWorkerPoolParams& params, |
| 65 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 64 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 66 TaskTracker* task_tracker, | 65 TaskTracker* task_tracker, |
| 67 DelayedTaskManager* delayed_task_manager); | 66 scoped_refptr<TaskRunner> service_thread_task_runner); |
| 68 | 67 |
| 69 // Waits until all workers are idle. | 68 // Waits until all workers are idle. |
| 70 void WaitForAllWorkersIdleForTesting(); | 69 void WaitForAllWorkersIdleForTesting(); |
| 71 | 70 |
| 72 // Joins all workers of this worker pool. Tasks that are already running are | 71 // Joins all workers of this worker pool. Tasks that are already running are |
| 73 // allowed to complete their execution. This can only be called once. | 72 // allowed to complete their execution. This can only be called once. |
| 74 void JoinForTesting(); | 73 void JoinForTesting(); |
| 75 | 74 |
| 76 // Disallows worker thread detachment. If the suggested reclaim time is not | 75 // Disallows worker thread detachment. If the suggested reclaim time is not |
| 77 // TimeDelta::Max(), then the test should call this before the detach code can | 76 // TimeDelta::Max(), then the test should call this before the detach code can |
| 78 // run. The safest place to do this is before the a set of work is dispatched | 77 // run. The safest place to do this is before the a set of work is dispatched |
| 79 // (the worker pool is idle and steady state) or before the last | 78 // (the worker pool is idle and steady state) or before the last |
| 80 // synchronization point for all workers (all threads are busy and can't be | 79 // synchronization point for all workers (all threads are busy and can't be |
| 81 // reclaimed). | 80 // reclaimed). |
| 82 void DisallowWorkerDetachmentForTesting(); | 81 void DisallowWorkerDetachmentForTesting(); |
| 83 | 82 |
| 84 // SchedulerWorkerPool: | 83 // SchedulerWorkerPool: |
| 85 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 84 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 86 const TaskTraits& traits, | 85 const TaskTraits& traits, |
| 87 ExecutionMode execution_mode) override; | 86 ExecutionMode execution_mode) override; |
| 88 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 87 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
| 89 const SequenceSortKey& sequence_sort_key) override; | 88 const SequenceSortKey& sequence_sort_key) override; |
| 90 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 89 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
| 91 scoped_refptr<Sequence> sequence, | 90 scoped_refptr<Sequence> sequence, |
| 92 SchedulerWorker* worker) override; | 91 SchedulerWorker* worker, |
| 92 TimeDelta delay) override; |
| 93 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 93 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
| 94 scoped_refptr<Sequence> sequence, | 94 scoped_refptr<Sequence> sequence, |
| 95 SchedulerWorker* worker) override; | 95 SchedulerWorker* worker) override; |
| 96 | 96 |
| 97 const HistogramBase* num_tasks_between_waits_histogram_for_testing() const { | 97 const HistogramBase* num_tasks_between_waits_histogram_for_testing() const { |
| 98 return num_tasks_between_waits_histogram_; | 98 return num_tasks_between_waits_histogram_; |
| 99 } | 99 } |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 class SchedulerSingleThreadTaskRunner; | 102 class SchedulerSingleThreadTaskRunner; |
| 103 class SchedulerWorkerDelegateImpl; | 103 class SchedulerWorkerDelegateImpl; |
| 104 | 104 |
| 105 SchedulerWorkerPoolImpl(StringPiece name, | 105 SchedulerWorkerPoolImpl( |
| 106 SchedulerWorkerPoolParams::IORestriction | 106 StringPiece name, |
| 107 io_restriction, | 107 SchedulerWorkerPoolParams::IORestriction io_restriction, |
| 108 const TimeDelta& suggested_reclaim_time, | 108 const TimeDelta& suggested_reclaim_time, |
| 109 TaskTracker* task_tracker, | 109 TaskTracker* task_tracker, |
| 110 DelayedTaskManager* delayed_task_manager); | 110 scoped_refptr<TaskRunner> service_thread_task_runner); |
| 111 | 111 |
| 112 bool Initialize( | 112 bool Initialize( |
| 113 ThreadPriority priority_hint, | 113 ThreadPriority priority_hint, |
| 114 size_t max_threads, | 114 size_t max_threads, |
| 115 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 115 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 116 | 116 |
| 117 // Wakes up the last worker from this worker pool to go idle, if any. | 117 // Wakes up the last worker from this worker pool to go idle, if any. |
| 118 void WakeUpOneWorker(); | 118 void WakeUpOneWorker(); |
| 119 | 119 |
| 120 // Adds |worker| to |idle_workers_stack_|. | 120 // Adds |worker| to |idle_workers_stack_|. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 187 |
| 188 // TaskScheduler.TaskLatency.[worker pool name].[task priority] histograms. | 188 // TaskScheduler.TaskLatency.[worker pool name].[task priority] histograms. |
| 189 // Indexed by task priority. Histograms are allocated on demand to reduce | 189 // Indexed by task priority. Histograms are allocated on demand to reduce |
| 190 // memory usage (some task priorities might never run in this | 190 // memory usage (some task priorities might never run in this |
| 191 // SchedulerThreadPoolImpl). Intentionally leaked. | 191 // SchedulerThreadPoolImpl). Intentionally leaked. |
| 192 subtle::AtomicWord | 192 subtle::AtomicWord |
| 193 task_latency_histograms_[static_cast<int>(TaskPriority::HIGHEST) + 1] = | 193 task_latency_histograms_[static_cast<int>(TaskPriority::HIGHEST) + 1] = |
| 194 {}; | 194 {}; |
| 195 | 195 |
| 196 TaskTracker* const task_tracker_; | 196 TaskTracker* const task_tracker_; |
| 197 DelayedTaskManager* const delayed_task_manager_; | 197 const scoped_refptr<TaskRunner> service_thread_task_runner_; |
| 198 | 198 |
| 199 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 199 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 } // namespace internal | 202 } // namespace internal |
| 203 } // namespace base | 203 } // namespace base |
| 204 | 204 |
| 205 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 205 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| OLD | NEW |