| 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 27 matching lines...) Expand all Loading... |
| 38 class DelayedTaskManager; | 38 class DelayedTaskManager; |
| 39 class TaskTracker; | 39 class TaskTracker; |
| 40 | 40 |
| 41 // A pool of workers that run Tasks. This class is thread-safe. | 41 // A pool of workers that run Tasks. This class is thread-safe. |
| 42 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 42 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
| 43 public: | 43 public: |
| 44 // Callback invoked when a Sequence isn't empty after a worker pops a Task | 44 // Callback invoked when a Sequence isn't empty after a worker pops a Task |
| 45 // from it. | 45 // from it. |
| 46 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 46 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
| 47 | 47 |
| 48 // Callback invoked during destruction of a SingleThreadTaskRunner. |
| 49 using UnregisterSingleThreadWorkerPoolCallback = |
| 50 Callback<void(const SchedulerWorkerPoolImpl* scheduler_worker_pool)>; |
| 51 |
| 48 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in | 52 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in |
| 49 // production; it is always leaked. In tests, it can only be destroyed after | 53 // production; it is always leaked. In tests, it can only be destroyed after |
| 50 // JoinForTesting() has returned. | 54 // JoinForTesting() has returned. |
| 51 ~SchedulerWorkerPoolImpl() override; | 55 ~SchedulerWorkerPoolImpl() override; |
| 52 | 56 |
| 53 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| | 57 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| |
| 54 // specification. |re_enqueue_sequence_callback| will be invoked after a | 58 // specification. |re_enqueue_sequence_callback| will be invoked after a |
| 55 // worker of this worker pool tries to run a Task. |task_tracker| is used to | 59 // worker of this worker pool tries to run a Task. |task_tracker| is used to |
| 56 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | 60 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks |
| 57 // posted with a delay. Returns nullptr on failure to create a worker pool | 61 // posted with a delay. Returns nullptr on failure to create a worker pool |
| 58 // with at least one thread. | 62 // with at least one thread. |
| 59 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( | 63 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( |
| 60 const SchedulerWorkerPoolParams& params, | 64 const SchedulerWorkerPoolParams& params, |
| 61 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 65 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 62 TaskTracker* task_tracker, | 66 TaskTracker* task_tracker, |
| 63 DelayedTaskManager* delayed_task_manager); | 67 DelayedTaskManager* delayed_task_manager); |
| 64 | 68 |
| 69 static std::unique_ptr<SchedulerWorkerPoolImpl> CreateSingleThreadWorkerPool( |
| 70 const SchedulerWorkerPoolParams& params, |
| 71 UnregisterSingleThreadWorkerPoolCallback |
| 72 unregister_single_thread_worker_callback, |
| 73 TaskTracker* task_tracker, |
| 74 DelayedTaskManager* delayed_task_manager); |
| 75 |
| 65 // SchedulerWorkerPool: | 76 // SchedulerWorkerPool: |
| 66 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 77 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 67 const TaskTraits& traits) override; | 78 const TaskTraits& traits) override; |
| 68 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( | 79 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
| 69 const TaskTraits& traits) override; | 80 const TaskTraits& traits) override; |
| 70 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( | 81 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( |
| 71 const TaskTraits& traits) override; | 82 const TaskTraits& traits) override; |
| 72 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 83 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
| 73 const SequenceSortKey& sequence_sort_key) override; | 84 const SequenceSortKey& sequence_sort_key) override; |
| 74 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 85 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 102 void DisallowWorkerDetachmentForTesting(); | 113 void DisallowWorkerDetachmentForTesting(); |
| 103 | 114 |
| 104 // Returns the number of workers alive in this worker pool. The value may | 115 // Returns the number of workers alive in this worker pool. The value may |
| 105 // change if workers are woken up or detached during this call. | 116 // change if workers are woken up or detached during this call. |
| 106 size_t NumberOfAliveWorkersForTesting(); | 117 size_t NumberOfAliveWorkersForTesting(); |
| 107 | 118 |
| 108 private: | 119 private: |
| 109 class SchedulerSingleThreadTaskRunner; | 120 class SchedulerSingleThreadTaskRunner; |
| 110 class SchedulerWorkerDelegateImpl; | 121 class SchedulerWorkerDelegateImpl; |
| 111 | 122 |
| 123 static std::unique_ptr<SchedulerWorkerPoolImpl> CreateInternal( |
| 124 const SchedulerWorkerPoolParams& params, |
| 125 TaskTracker* task_tracker, |
| 126 DelayedTaskManager* delayed_task_manager, |
| 127 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 128 UnregisterSingleThreadWorkerPoolCallback |
| 129 unregister_single_thread_worker_callback); |
| 130 |
| 112 SchedulerWorkerPoolImpl(const SchedulerWorkerPoolParams& params, | 131 SchedulerWorkerPoolImpl(const SchedulerWorkerPoolParams& params, |
| 113 TaskTracker* task_tracker, | 132 TaskTracker* task_tracker, |
| 114 DelayedTaskManager* delayed_task_manager); | 133 DelayedTaskManager* delayed_task_manager, |
| 134 UnregisterSingleThreadWorkerPoolCallback |
| 135 unregister_single_thread_worker_pool_callback = |
| 136 UnregisterSingleThreadWorkerPoolCallback()); |
| 115 | 137 |
| 116 bool Initialize( | 138 bool Initialize( |
| 117 const SchedulerWorkerPoolParams& params, | 139 const SchedulerWorkerPoolParams& params, |
| 118 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 140 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 119 | 141 |
| 120 // Wakes up |worker|. | 142 // Wakes up |worker|. |
| 121 void WakeUpWorker(SchedulerWorker* worker); | 143 void WakeUpWorker(SchedulerWorker* worker); |
| 122 | 144 |
| 123 // Wakes up the last worker from this worker pool to go idle, if any. | 145 // Wakes up the last worker from this worker pool to go idle, if any. |
| 124 void WakeUpOneWorker(); | 146 void WakeUpOneWorker(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // Intentionally leaked. | 214 // Intentionally leaked. |
| 193 HistogramBase* const num_tasks_before_detach_histogram_; | 215 HistogramBase* const num_tasks_before_detach_histogram_; |
| 194 | 216 |
| 195 // TaskScheduler.NumTasksBetweenWaits.[worker pool name] histogram. | 217 // TaskScheduler.NumTasksBetweenWaits.[worker pool name] histogram. |
| 196 // Intentionally leaked. | 218 // Intentionally leaked. |
| 197 HistogramBase* const num_tasks_between_waits_histogram_; | 219 HistogramBase* const num_tasks_between_waits_histogram_; |
| 198 | 220 |
| 199 TaskTracker* const task_tracker_; | 221 TaskTracker* const task_tracker_; |
| 200 DelayedTaskManager* const delayed_task_manager_; | 222 DelayedTaskManager* const delayed_task_manager_; |
| 201 | 223 |
| 224 const UnregisterSingleThreadWorkerPoolCallback |
| 225 unregister_single_thread_worker_pool_callback_; |
| 226 |
| 202 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 227 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
| 203 }; | 228 }; |
| 204 | 229 |
| 205 } // namespace internal | 230 } // namespace internal |
| 206 } // namespace base | 231 } // namespace base |
| 207 | 232 |
| 208 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 233 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| OLD | NEW |