| 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_THREAD_POOL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <stack> | 11 #include <stack> |
| 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/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/synchronization/condition_variable.h" | 18 #include "base/synchronization/condition_variable.h" |
| 19 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 20 #include "base/task_scheduler/priority_queue.h" | 20 #include "base/task_scheduler/priority_queue.h" |
| 21 #include "base/task_scheduler/scheduler_lock.h" | 21 #include "base/task_scheduler/scheduler_lock.h" |
| 22 #include "base/task_scheduler/scheduler_stack.h" |
| 22 #include "base/task_scheduler/scheduler_task_executor.h" | 23 #include "base/task_scheduler/scheduler_task_executor.h" |
| 23 #include "base/task_scheduler/scheduler_worker_thread.h" | 24 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 24 #include "base/task_scheduler/sequence.h" | 25 #include "base/task_scheduler/sequence.h" |
| 25 #include "base/task_scheduler/task.h" | 26 #include "base/task_scheduler/task.h" |
| 26 #include "base/task_scheduler/task_traits.h" | 27 #include "base/task_scheduler/task_traits.h" |
| 27 #include "base/threading/platform_thread.h" | 28 #include "base/threading/platform_thread.h" |
| 28 | 29 |
| 29 namespace base { | 30 namespace base { |
| 30 namespace internal { | 31 namespace internal { |
| 31 | 32 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 DelayedTaskManager* delayed_task_manager); | 91 DelayedTaskManager* delayed_task_manager); |
| 91 | 92 |
| 92 bool Initialize(ThreadPriority thread_priority, size_t max_threads); | 93 bool Initialize(ThreadPriority thread_priority, size_t max_threads); |
| 93 | 94 |
| 94 // Wakes up the last thread from this thread pool to go idle, if any. | 95 // Wakes up the last thread from this thread pool to go idle, if any. |
| 95 void WakeUpOneThread(); | 96 void WakeUpOneThread(); |
| 96 | 97 |
| 97 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 98 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
| 98 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 99 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
| 99 | 100 |
| 100 // Pops one idle worker thread from |idle_worker_thread_stack_| and returns | 101 // Removes |worker_thread| from |idle_worker_threads_stack_|. |
| 101 // it. Returns nullptr if |idle_worker_thread_stack_| is empty. | 102 void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
| 102 SchedulerWorkerThread* PopOneIdleWorkerThread(); | |
| 103 | 103 |
| 104 // PriorityQueue from which all threads of this thread pool get work. | 104 // PriorityQueue from which all threads of this thread pool get work. |
| 105 PriorityQueue shared_priority_queue_; | 105 PriorityQueue shared_priority_queue_; |
| 106 | 106 |
| 107 // All worker threads owned by this thread pool. Only modified during | 107 // All worker threads owned by this thread pool. Only modified during |
| 108 // initialization of the thread pool. | 108 // initialization of the thread pool. |
| 109 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | 109 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
| 110 | 110 |
| 111 // Synchronizes the creation of SINGLE_THREADED TaskRunners. |
| 112 SchedulerLock single_thread_task_runner_creation_lock_; |
| 113 |
| 111 // Synchronizes access to |idle_worker_threads_stack_| and | 114 // Synchronizes access to |idle_worker_threads_stack_| and |
| 112 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 115 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
| 113 // lock as its predecessor so that a thread can be pushed to | 116 // lock as its predecessor so that a thread can be pushed to |
| 114 // |idle_worker_threads_stack_| within the scope of a Transaction (more | 117 // |idle_worker_threads_stack_| within the scope of a Transaction (more |
| 115 // details in GetWork()). | 118 // details in GetWork()). |
| 116 SchedulerLock idle_worker_threads_stack_lock_; | 119 SchedulerLock idle_worker_threads_stack_lock_; |
| 117 | 120 |
| 118 // Stack of idle worker threads. | 121 // Stack of idle worker threads. |
| 119 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_; | 122 SchedulerStack<SchedulerWorkerThread*> idle_worker_threads_stack_; |
| 120 | 123 |
| 121 // Signaled when all worker threads become idle. | 124 // Signaled when all worker threads become idle. |
| 122 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 125 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
| 123 | 126 |
| 124 // Signaled once JoinForTesting() has returned. | 127 // Signaled once JoinForTesting() has returned. |
| 125 WaitableEvent join_for_testing_returned_; | 128 WaitableEvent join_for_testing_returned_; |
| 126 | 129 |
| 127 // Delegate for all worker threads in this pool. | 130 // Delegate for all worker threads in this pool. |
| 128 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; | 131 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; |
| 129 | 132 |
| 130 TaskTracker* const task_tracker_; | 133 TaskTracker* const task_tracker_; |
| 131 DelayedTaskManager* const delayed_task_manager_; | 134 DelayedTaskManager* const delayed_task_manager_; |
| 132 | 135 |
| 133 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | 136 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); |
| 134 }; | 137 }; |
| 135 | 138 |
| 136 } // namespace internal | 139 } // namespace internal |
| 137 } // namespace base | 140 } // namespace base |
| 138 | 141 |
| 139 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 142 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
| OLD | NEW |