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