| 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_IMPL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <unordered_map> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/base_export.h" | 14 #include "base/base_export.h" |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/logging.h" |
| 15 #include "base/macros.h" | 17 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 17 #include "base/synchronization/condition_variable.h" | 19 #include "base/synchronization/condition_variable.h" |
| 18 #include "base/task_runner.h" | 20 #include "base/task_runner.h" |
| 19 #include "base/task_scheduler/priority_queue.h" | 21 #include "base/task_scheduler/priority_queue.h" |
| 20 #include "base/task_scheduler/scheduler_lock.h" | 22 #include "base/task_scheduler/scheduler_lock.h" |
| 21 #include "base/task_scheduler/scheduler_thread_pool.h" | 23 #include "base/task_scheduler/scheduler_thread_pool.h" |
| 22 #include "base/task_scheduler/scheduler_worker_thread.h" | 24 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 23 #include "base/task_scheduler/scheduler_worker_thread_stack.h" | 25 #include "base/task_scheduler/scheduler_worker_thread_stack.h" |
| 24 #include "base/task_scheduler/sequence.h" | 26 #include "base/task_scheduler/sequence.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // allowed to complete their execution. This can only be called once. | 67 // allowed to complete their execution. This can only be called once. |
| 66 void JoinForTesting(); | 68 void JoinForTesting(); |
| 67 | 69 |
| 68 // SchedulerThreadPool: | 70 // SchedulerThreadPool: |
| 69 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 71 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 70 const TaskTraits& traits, | 72 const TaskTraits& traits, |
| 71 ExecutionMode execution_mode) override; | 73 ExecutionMode execution_mode) override; |
| 72 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 74 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
| 73 const SequenceSortKey& sequence_sort_key) override; | 75 const SequenceSortKey& sequence_sort_key) override; |
| 74 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 76 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
| 75 scoped_refptr<Sequence> sequence) override; | 77 scoped_refptr<Sequence> sequence, |
| 78 SchedulerWorkerThread* worker_thread) override; |
| 76 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 79 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
| 77 scoped_refptr<Sequence> sequence) override; | 80 scoped_refptr<Sequence> sequence, |
| 81 SchedulerWorkerThread* worker_thread) override; |
| 78 | 82 |
| 79 private: | 83 private: |
| 80 class SchedulerWorkerThreadDelegateImpl; | 84 class SchedulerWorkerThreadDelegateImpl; |
| 81 | 85 |
| 82 SchedulerThreadPoolImpl( | 86 SchedulerThreadPoolImpl(TaskTracker* task_tracker, |
| 83 TaskTracker* task_tracker, | 87 DelayedTaskManager* delayed_task_manager); |
| 84 DelayedTaskManager* delayed_task_manager); | |
| 85 | 88 |
| 86 bool Initialize( | 89 bool Initialize( |
| 87 ThreadPriority thread_priority, | 90 ThreadPriority thread_priority, |
| 88 size_t max_threads, | 91 size_t max_threads, |
| 89 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 92 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
| 90 | 93 |
| 91 // Wakes up the last thread from this thread pool to go idle, if any. | 94 // Wakes up the last thread from this thread pool to go idle, if any. |
| 92 void WakeUpOneThread(); | 95 void WakeUpOneThread(); |
| 93 | 96 |
| 94 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 97 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
| 95 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 98 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
| 96 | 99 |
| 97 // PriorityQueue from which all threads of this thread pool get work. | 100 // Removes |worker_thread| from |idle_worker_threads_stack_|. |
| 98 PriorityQueue shared_priority_queue_; | 101 void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
| 99 | 102 |
| 100 // All worker threads owned by this thread pool. Only modified during | 103 // All worker threads owned by this thread pool. Only modified during |
| 101 // initialization of the thread pool. | 104 // initialization of the thread pool. |
| 102 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | 105 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
| 103 | 106 |
| 107 // Synchronizes access to |next_worker_thread_index_|. |
| 108 SchedulerLock next_worker_thread_index_lock_; |
| 109 |
| 110 // Index of the worker thread that will be assigned to the next single- |
| 111 // threaded TaskRunner returned by this pool. |
| 112 size_t next_worker_thread_index_ = 0; |
| 113 |
| 114 // PriorityQueue from which all threads of this thread pool get work. |
| 115 PriorityQueue shared_priority_queue_; |
| 116 |
| 104 // Synchronizes access to |idle_worker_threads_stack_| and | 117 // Synchronizes access to |idle_worker_threads_stack_| and |
| 105 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 118 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
| 106 // lock as its predecessor so that a thread can be pushed to | 119 // lock as its predecessor so that a thread can be pushed to |
| 107 // |idle_worker_threads_stack_| within the scope of a Transaction (more | 120 // |idle_worker_threads_stack_| within the scope of a Transaction (more |
| 108 // details in GetWork()). | 121 // details in GetWork()). |
| 109 SchedulerLock idle_worker_threads_stack_lock_; | 122 SchedulerLock idle_worker_threads_stack_lock_; |
| 110 | 123 |
| 111 // Stack of idle worker threads. | 124 // Stack of idle worker threads. |
| 112 SchedulerWorkerThreadStack idle_worker_threads_stack_; | 125 SchedulerWorkerThreadStack idle_worker_threads_stack_; |
| 113 | 126 |
| 114 // Signaled when all worker threads become idle. | 127 // Signaled when all worker threads become idle. |
| 115 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | 128 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; |
| 116 | 129 |
| 117 // Signaled once JoinForTesting() has returned. | 130 // Signaled once JoinForTesting() has returned. |
| 118 WaitableEvent join_for_testing_returned_; | 131 WaitableEvent join_for_testing_returned_; |
| 119 | 132 |
| 133 #if DCHECK_IS_ON() |
| 134 // Signaled when all threads have been created. |
| 135 WaitableEvent threads_created_; |
| 136 #endif // DCHECK_IS_ON() |
| 137 |
| 120 TaskTracker* const task_tracker_; | 138 TaskTracker* const task_tracker_; |
| 121 DelayedTaskManager* const delayed_task_manager_; | 139 DelayedTaskManager* const delayed_task_manager_; |
| 122 | 140 |
| 123 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); | 141 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); |
| 124 }; | 142 }; |
| 125 | 143 |
| 126 } // namespace internal | 144 } // namespace internal |
| 127 } // namespace base | 145 } // namespace base |
| 128 | 146 |
| 129 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 147 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
| OLD | NEW |