| 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/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
| 20 #include "base/synchronization/condition_variable.h" | 20 #include "base/synchronization/condition_variable.h" |
| 21 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
| 22 #include "base/task_scheduler/priority_queue.h" | 22 #include "base/task_scheduler/priority_queue.h" |
| 23 #include "base/task_scheduler/scheduler_lock.h" | 23 #include "base/task_scheduler/scheduler_lock.h" |
| 24 #include "base/task_scheduler/scheduler_worker.h" | 24 #include "base/task_scheduler/scheduler_worker.h" |
| 25 #include "base/task_scheduler/scheduler_worker_pool.h" | 25 #include "base/task_scheduler/scheduler_worker_pool.h" |
| 26 #include "base/task_scheduler/scheduler_worker_pool_params.h" |
| 26 #include "base/task_scheduler/scheduler_worker_stack.h" | 27 #include "base/task_scheduler/scheduler_worker_stack.h" |
| 27 #include "base/task_scheduler/sequence.h" | 28 #include "base/task_scheduler/sequence.h" |
| 28 #include "base/task_scheduler/task.h" | 29 #include "base/task_scheduler/task.h" |
| 29 #include "base/task_scheduler/task_traits.h" | 30 #include "base/task_scheduler/task_traits.h" |
| 30 #include "base/threading/platform_thread.h" | 31 #include "base/threading/platform_thread.h" |
| 31 | 32 |
| 32 namespace base { | 33 namespace base { |
| 33 namespace internal { | 34 namespace internal { |
| 34 | 35 |
| 35 class DelayedTaskManager; | 36 class DelayedTaskManager; |
| 36 class TaskTracker; | 37 class TaskTracker; |
| 37 | 38 |
| 38 // A pool of workers that run Tasks. This class is thread-safe. | 39 // A pool of workers that run Tasks. This class is thread-safe. |
| 39 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 40 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
| 40 public: | 41 public: |
| 41 enum class IORestriction { | |
| 42 ALLOWED, | |
| 43 DISALLOWED, | |
| 44 }; | |
| 45 | |
| 46 // Callback invoked when a Sequence isn't empty after a worker pops a Task | 42 // Callback invoked when a Sequence isn't empty after a worker pops a Task |
| 47 // from it. | 43 // from it. |
| 48 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 44 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
| 49 | 45 |
| 50 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in | 46 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in |
| 51 // production; it is always leaked. In tests, it can only be destroyed after | 47 // production; it is always leaked. In tests, it can only be destroyed after |
| 52 // JoinForTesting() has returned. | 48 // JoinForTesting() has returned. |
| 53 ~SchedulerWorkerPoolImpl() override; | 49 ~SchedulerWorkerPoolImpl() override; |
| 54 | 50 |
| 55 // Creates a SchedulerWorkerPoolImpl labeled |name| with up to |max_threads| | 51 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| |
| 56 // threads of priority |thread_priority|. |io_restriction| indicates whether | 52 // specification. |re_enqueue_sequence_callback| will be invoked after a |
| 57 // Tasks on the constructed worker pool are allowed to make I/O calls. | 53 // worker of this worker pool tries to run a Task. |task_tracker| is used to |
| 58 // |re_enqueue_sequence_callback| will be invoked after a worker of this | 54 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks |
| 59 // worker pool tries to run a Task. |task_tracker| is used to handle shutdown | 55 // posted with a delay. Returns nullptr on failure to create a worker pool |
| 60 // behavior of Tasks. |delayed_task_manager| handles Tasks posted with a | 56 // with at least one thread. |
| 61 // delay. Returns nullptr on failure to create a worker pool with at least one | |
| 62 // thread. | |
| 63 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( | 57 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( |
| 64 StringPiece name, | 58 const SchedulerWorkerPoolParams& params, |
| 65 ThreadPriority thread_priority, | |
| 66 size_t max_threads, | |
| 67 IORestriction io_restriction, | |
| 68 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 59 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
| 69 TaskTracker* task_tracker, | 60 TaskTracker* task_tracker, |
| 70 DelayedTaskManager* delayed_task_manager); | 61 DelayedTaskManager* delayed_task_manager); |
| 71 | 62 |
| 72 // Waits until all workers are idle. | 63 // Waits until all workers are idle. |
| 73 void WaitForAllWorkersIdleForTesting(); | 64 void WaitForAllWorkersIdleForTesting(); |
| 74 | 65 |
| 75 // Joins all workers of this worker pool. Tasks that are already running are | 66 // Joins all workers of this worker pool. Tasks that are already running are |
| 76 // allowed to complete their execution. This can only be called once. | 67 // allowed to complete their execution. This can only be called once. |
| 77 void JoinForTesting(); | 68 void JoinForTesting(); |
| 78 | 69 |
| 79 // SchedulerWorkerPool: | 70 // SchedulerWorkerPool: |
| 80 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 71 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 81 const TaskTraits& traits, | 72 const TaskTraits& traits, |
| 82 ExecutionMode execution_mode) override; | 73 ExecutionMode execution_mode) override; |
| 83 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 74 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
| 84 const SequenceSortKey& sequence_sort_key) override; | 75 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, | 77 scoped_refptr<Sequence> sequence, |
| 87 SchedulerWorker* worker) override; | 78 SchedulerWorker* worker) override; |
| 88 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 79 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
| 89 scoped_refptr<Sequence> sequence, | 80 scoped_refptr<Sequence> sequence, |
| 90 SchedulerWorker* worker) override; | 81 SchedulerWorker* worker) override; |
| 91 | 82 |
| 92 private: | 83 private: |
| 93 class SchedulerWorkerDelegateImpl; | 84 class SchedulerWorkerDelegateImpl; |
| 94 | 85 |
| 95 SchedulerWorkerPoolImpl(StringPiece name, | 86 SchedulerWorkerPoolImpl(StringPiece name, |
| 96 IORestriction io_restriction, | 87 SchedulerWorkerPoolParams::IORestriction |
| 88 io_restriction, |
| 97 TaskTracker* task_tracker, | 89 TaskTracker* task_tracker, |
| 98 DelayedTaskManager* delayed_task_manager); | 90 DelayedTaskManager* delayed_task_manager); |
| 99 | 91 |
| 100 bool Initialize( | 92 bool Initialize( |
| 101 ThreadPriority thread_priority, | 93 ThreadPriority thread_priority, |
| 102 size_t max_threads, | 94 size_t max_threads, |
| 103 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 95 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 104 | 96 |
| 105 // Wakes up the last worker from this worker pool to go idle, if any. | 97 // Wakes up the last worker from this worker pool to go idle, if any. |
| 106 void WakeUpOneWorker(); | 98 void WakeUpOneWorker(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 122 SchedulerLock next_worker_index_lock_; | 114 SchedulerLock next_worker_index_lock_; |
| 123 | 115 |
| 124 // Index of the worker that will be assigned to the next single-threaded | 116 // Index of the worker that will be assigned to the next single-threaded |
| 125 // TaskRunner returned by this pool. | 117 // TaskRunner returned by this pool. |
| 126 size_t next_worker_index_ = 0; | 118 size_t next_worker_index_ = 0; |
| 127 | 119 |
| 128 // PriorityQueue from which all threads of this worker pool get work. | 120 // PriorityQueue from which all threads of this worker pool get work. |
| 129 PriorityQueue shared_priority_queue_; | 121 PriorityQueue shared_priority_queue_; |
| 130 | 122 |
| 131 // Indicates whether Tasks on this worker pool are allowed to make I/O calls. | 123 // Indicates whether Tasks on this worker pool are allowed to make I/O calls. |
| 132 const IORestriction io_restriction_; | 124 const SchedulerWorkerPoolParams::IORestriction io_restriction_; |
| 133 | 125 |
| 134 // Synchronizes access to |idle_workers_stack_| and | 126 // Synchronizes access to |idle_workers_stack_| and |
| 135 // |idle_workers_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 127 // |idle_workers_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
| 136 // lock as its predecessor so that a worker can be pushed to | 128 // lock as its predecessor so that a worker can be pushed to |
| 137 // |idle_workers_stack_| within the scope of a Transaction (more | 129 // |idle_workers_stack_| within the scope of a Transaction (more |
| 138 // details in GetWork()). | 130 // details in GetWork()). |
| 139 SchedulerLock idle_workers_stack_lock_; | 131 SchedulerLock idle_workers_stack_lock_; |
| 140 | 132 |
| 141 // Stack of idle workers. | 133 // Stack of idle workers. |
| 142 SchedulerWorkerStack idle_workers_stack_; | 134 SchedulerWorkerStack idle_workers_stack_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 155 TaskTracker* const task_tracker_; | 147 TaskTracker* const task_tracker_; |
| 156 DelayedTaskManager* const delayed_task_manager_; | 148 DelayedTaskManager* const delayed_task_manager_; |
| 157 | 149 |
| 158 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 150 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
| 159 }; | 151 }; |
| 160 | 152 |
| 161 } // namespace internal | 153 } // namespace internal |
| 162 } // namespace base | 154 } // namespace base |
| 163 | 155 |
| 164 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 156 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
| OLD | NEW |