| 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" | 15 #include "base/callback.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/synchronization/atomic_flag.h" | 19 #include "base/synchronization/atomic_flag.h" |
| 20 #include "base/synchronization/condition_variable.h" | 20 #include "base/synchronization/condition_variable.h" |
| 21 #include "base/task_scheduler/priority_queue.h" | 21 #include "base/task_scheduler/priority_queue.h" |
| 22 #include "base/task_scheduler/scheduler_lock.h" | 22 #include "base/task_scheduler/scheduler_lock.h" |
| 23 #include "base/task_scheduler/scheduler_worker.h" | 23 #include "base/task_scheduler/scheduler_worker.h" |
| 24 #include "base/task_scheduler/scheduler_worker_pool.h" | 24 #include "base/task_scheduler/scheduler_worker_pool.h" |
| 25 #include "base/task_scheduler/scheduler_worker_stack.h" | 25 #include "base/task_scheduler/scheduler_worker_stack.h" |
| 26 #include "base/task_scheduler/sequence.h" | 26 #include "base/task_scheduler/sequence.h" |
| 27 #include "base/task_scheduler/task.h" | 27 #include "base/task_scheduler/task.h" |
| 28 #include "base/threading/platform_thread.h" |
| 28 #include "base/time/time.h" | 29 #include "base/time/time.h" |
| 29 | 30 |
| 30 namespace base { | 31 namespace base { |
| 31 | 32 |
| 32 class HistogramBase; | 33 class HistogramBase; |
| 33 class SchedulerWorkerPoolParams; | 34 class SchedulerWorkerPoolParams; |
| 34 class TaskTraits; | 35 class TaskTraits; |
| 35 | 36 |
| 36 namespace internal { | 37 namespace internal { |
| 37 | 38 |
| 38 class DelayedTaskManager; | 39 class DelayedTaskManager; |
| 39 class TaskTracker; | 40 class TaskTracker; |
| 40 | 41 |
| 41 // A pool of workers that run Tasks. This class is thread-safe. | 42 // A pool of workers that run Tasks. This class is thread-safe. |
| 42 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 43 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
| 43 public: | 44 public: |
| 44 // Callback invoked when a Sequence isn't empty after a worker pops a Task | 45 // Callback invoked when a Sequence isn't empty after a worker pops a Task |
| 45 // from it. | 46 // from it. |
| 46 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 47 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
| 47 | 48 |
| 48 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in | 49 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in |
| 49 // production; it is always leaked. In tests, it can only be destroyed after | 50 // production; it is always leaked. In tests, it can only be destroyed after |
| 50 // JoinForTesting() has returned. | 51 // JoinForTesting() has returned. |
| 51 ~SchedulerWorkerPoolImpl() override; | 52 ~SchedulerWorkerPoolImpl() override; |
| 52 | 53 |
| 53 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| | 54 // Creates a SchedulerWorkerPoolImpl. |name| is used to label the pool's |
| 54 // specification. |re_enqueue_sequence_callback| will be invoked after a | 55 // threads ("TaskScheduler" + |name| + index) and histograms ("TaskScheduler." |
| 55 // worker of this worker pool tries to run a Task. |task_tracker| is used to | 56 // + histogram name + "." + |name|). |priority_hint| is the preferred thread |
| 56 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | 57 // priority; the actual thread priority depends on shutdown state and platform |
| 57 // posted with a delay. Returns nullptr on failure to create a worker pool | 58 // capabilities. |params| contains additional thread creation params. |
| 58 // with at least one thread. | 59 // |re_enqueue_sequence_callback| is invoked after a worker tries to run a |
| 60 // Task. |task_tracker| is used to handle shutdown behavior of Tasks. |
| 61 // |delayed_task_manager| handles Tasks posted with a delay. Returns nullptr |
| 62 // on failure to create a worker pool with at least one thread. |
| 59 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( | 63 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( |
| 64 const std::string& name, |
| 65 ThreadPriority priority_hint, |
| 60 const SchedulerWorkerPoolParams& params, | 66 const SchedulerWorkerPoolParams& params, |
| 61 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 67 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 62 TaskTracker* task_tracker, | 68 TaskTracker* task_tracker, |
| 63 DelayedTaskManager* delayed_task_manager); | 69 DelayedTaskManager* delayed_task_manager); |
| 64 | 70 |
| 65 // SchedulerWorkerPool: | 71 // SchedulerWorkerPool: |
| 66 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 72 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 67 const TaskTraits& traits) override; | 73 const TaskTraits& traits) override; |
| 68 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( | 74 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
| 69 const TaskTraits& traits) override; | 75 const TaskTraits& traits) override; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // threads, and as a result, threads running after JoinForTesting(). | 108 // threads, and as a result, threads running after JoinForTesting(). |
| 103 void DisallowWorkerDetachmentForTesting(); | 109 void DisallowWorkerDetachmentForTesting(); |
| 104 | 110 |
| 105 // Returns the number of workers alive in this worker pool. The value may | 111 // Returns the number of workers alive in this worker pool. The value may |
| 106 // change if workers are woken up or detached during this call. | 112 // change if workers are woken up or detached during this call. |
| 107 size_t NumberOfAliveWorkersForTesting(); | 113 size_t NumberOfAliveWorkersForTesting(); |
| 108 | 114 |
| 109 private: | 115 private: |
| 110 class SchedulerWorkerDelegateImpl; | 116 class SchedulerWorkerDelegateImpl; |
| 111 | 117 |
| 112 SchedulerWorkerPoolImpl(const SchedulerWorkerPoolParams& params, | 118 SchedulerWorkerPoolImpl(const std::string& name, |
| 119 const SchedulerWorkerPoolParams& params, |
| 113 TaskTracker* task_tracker, | 120 TaskTracker* task_tracker, |
| 114 DelayedTaskManager* delayed_task_manager); | 121 DelayedTaskManager* delayed_task_manager); |
| 115 | 122 |
| 116 bool Initialize( | 123 bool Initialize( |
| 124 ThreadPriority priority_hint, |
| 117 const SchedulerWorkerPoolParams& params, | 125 const SchedulerWorkerPoolParams& params, |
| 118 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 126 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 119 | 127 |
| 120 // Wakes up the last worker from this worker pool to go idle, if any. | 128 // Wakes up the last worker from this worker pool to go idle, if any. |
| 121 void WakeUpOneWorker(); | 129 void WakeUpOneWorker(); |
| 122 | 130 |
| 123 // Adds |worker| to |idle_workers_stack_|. | 131 // Adds |worker| to |idle_workers_stack_|. |
| 124 void AddToIdleWorkersStack(SchedulerWorker* worker); | 132 void AddToIdleWorkersStack(SchedulerWorker* worker); |
| 125 | 133 |
| 126 // Peeks from |idle_workers_stack_|. | 134 // Peeks from |idle_workers_stack_|. |
| 127 const SchedulerWorker* PeekAtIdleWorkersStack() const; | 135 const SchedulerWorker* PeekAtIdleWorkersStack() const; |
| 128 | 136 |
| 129 // Removes |worker| from |idle_workers_stack_|. | 137 // Removes |worker| from |idle_workers_stack_|. |
| 130 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); | 138 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); |
| 131 | 139 |
| 132 // Returns true if worker thread detachment is permitted. | 140 // Returns true if worker thread detachment is permitted. |
| 133 bool CanWorkerDetachForTesting(); | 141 bool CanWorkerDetachForTesting(); |
| 134 | 142 |
| 135 // The name of this worker pool, used to label its worker threads. | 143 // The name of this worker pool, used to label threads and histograms. |
| 136 const std::string name_; | 144 const std::string name_; |
| 137 | 145 |
| 138 // All worker owned by this worker pool. Only modified during initialization | 146 // All worker owned by this worker pool. Only modified during initialization |
| 139 // of the worker pool. | 147 // of the worker pool. |
| 140 std::vector<scoped_refptr<SchedulerWorker>> workers_; | 148 std::vector<scoped_refptr<SchedulerWorker>> workers_; |
| 141 | 149 |
| 142 // PriorityQueue from which all threads of this worker pool get work. | 150 // PriorityQueue from which all threads of this worker pool get work. |
| 143 PriorityQueue shared_priority_queue_; | 151 PriorityQueue shared_priority_queue_; |
| 144 | 152 |
| 145 // Suggested reclaim time for workers. | 153 // Suggested reclaim time for workers. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 TaskTracker* const task_tracker_; | 197 TaskTracker* const task_tracker_; |
| 190 DelayedTaskManager* const delayed_task_manager_; | 198 DelayedTaskManager* const delayed_task_manager_; |
| 191 | 199 |
| 192 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 200 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
| 193 }; | 201 }; |
| 194 | 202 |
| 195 } // namespace internal | 203 } // namespace internal |
| 196 } // namespace base | 204 } // namespace base |
| 197 | 205 |
| 198 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 206 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| OLD | NEW |