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 <string> |
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/logging.h" | 16 #include "base/logging.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/strings/string_piece.h" |
18 #include "base/synchronization/condition_variable.h" | 20 #include "base/synchronization/condition_variable.h" |
19 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
20 #include "base/task_scheduler/priority_queue.h" | 22 #include "base/task_scheduler/priority_queue.h" |
21 #include "base/task_scheduler/scheduler_lock.h" | 23 #include "base/task_scheduler/scheduler_lock.h" |
22 #include "base/task_scheduler/scheduler_thread_pool.h" | 24 #include "base/task_scheduler/scheduler_thread_pool.h" |
23 #include "base/task_scheduler/scheduler_worker_thread.h" | 25 #include "base/task_scheduler/scheduler_worker_thread.h" |
24 #include "base/task_scheduler/scheduler_worker_thread_stack.h" | 26 #include "base/task_scheduler/scheduler_worker_thread_stack.h" |
25 #include "base/task_scheduler/sequence.h" | 27 #include "base/task_scheduler/sequence.h" |
26 #include "base/task_scheduler/task.h" | 28 #include "base/task_scheduler/task.h" |
27 #include "base/task_scheduler/task_traits.h" | 29 #include "base/task_scheduler/task_traits.h" |
(...skipping 15 matching lines...) Expand all Loading... |
43 | 45 |
44 // Callback invoked when a Sequence isn't empty after a worker thread pops a | 46 // Callback invoked when a Sequence isn't empty after a worker thread pops a |
45 // Task from it. | 47 // Task from it. |
46 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 48 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
47 | 49 |
48 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not | 50 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not |
49 // allowed in production; it is always leaked. In tests, it can only be | 51 // allowed in production; it is always leaked. In tests, it can only be |
50 // destroyed after JoinForTesting() has returned. | 52 // destroyed after JoinForTesting() has returned. |
51 ~SchedulerThreadPoolImpl() override; | 53 ~SchedulerThreadPoolImpl() override; |
52 | 54 |
53 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | 55 // Creates a SchedulerThreadPool labeled |name| with up to |max_threads| |
54 // |thread_priority|. |io_restriction| indicates whether Tasks on the | 56 // threads of priority |thread_priority|. |io_restriction| indicates whether |
55 // constructed thread pool are allowed to make I/O calls. | 57 // Tasks on the constructed thread pool are allowed to make I/O calls. |
56 // |re_enqueue_sequence_callback| will be invoked after a thread of this | 58 // |re_enqueue_sequence_callback| will be invoked after a thread of this |
57 // thread pool tries to run a Task. |task_tracker| is used to handle shutdown | 59 // thread pool tries to run a Task. |task_tracker| is used to handle shutdown |
58 // behavior of Tasks. |delayed_task_manager| handles Tasks posted with a | 60 // behavior of Tasks. |delayed_task_manager| handles Tasks posted with a |
59 // delay. Returns nullptr on failure to create a thread pool with at least one | 61 // delay. Returns nullptr on failure to create a thread pool with at least one |
60 // thread. | 62 // thread. |
61 static std::unique_ptr<SchedulerThreadPoolImpl> Create( | 63 static std::unique_ptr<SchedulerThreadPoolImpl> Create( |
| 64 StringPiece name, |
62 ThreadPriority thread_priority, | 65 ThreadPriority thread_priority, |
63 size_t max_threads, | 66 size_t max_threads, |
64 IORestriction io_restriction, | 67 IORestriction io_restriction, |
65 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 68 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
66 TaskTracker* task_tracker, | 69 TaskTracker* task_tracker, |
67 DelayedTaskManager* delayed_task_manager); | 70 DelayedTaskManager* delayed_task_manager); |
68 | 71 |
69 // Waits until all threads are idle. | 72 // Waits until all threads are idle. |
70 void WaitForAllWorkerThreadsIdleForTesting(); | 73 void WaitForAllWorkerThreadsIdleForTesting(); |
71 | 74 |
(...skipping 10 matching lines...) Expand all Loading... |
82 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 85 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
83 scoped_refptr<Sequence> sequence, | 86 scoped_refptr<Sequence> sequence, |
84 SchedulerWorkerThread* worker_thread) override; | 87 SchedulerWorkerThread* worker_thread) override; |
85 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 88 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
86 scoped_refptr<Sequence> sequence, | 89 scoped_refptr<Sequence> sequence, |
87 SchedulerWorkerThread* worker_thread) override; | 90 SchedulerWorkerThread* worker_thread) override; |
88 | 91 |
89 private: | 92 private: |
90 class SchedulerWorkerThreadDelegateImpl; | 93 class SchedulerWorkerThreadDelegateImpl; |
91 | 94 |
92 SchedulerThreadPoolImpl(IORestriction io_restriction, | 95 SchedulerThreadPoolImpl(StringPiece name, |
| 96 IORestriction io_restriction, |
93 TaskTracker* task_tracker, | 97 TaskTracker* task_tracker, |
94 DelayedTaskManager* delayed_task_manager); | 98 DelayedTaskManager* delayed_task_manager); |
95 | 99 |
96 bool Initialize( | 100 bool Initialize( |
97 ThreadPriority thread_priority, | 101 ThreadPriority thread_priority, |
98 size_t max_threads, | 102 size_t max_threads, |
99 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 103 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
100 | 104 |
101 // Wakes up the last thread from this thread pool to go idle, if any. | 105 // Wakes up the last thread from this thread pool to go idle, if any. |
102 void WakeUpOneThread(); | 106 void WakeUpOneThread(); |
103 | 107 |
104 // Adds |worker_thread| to |idle_worker_threads_stack_|. | 108 // Adds |worker_thread| to |idle_worker_threads_stack_|. |
105 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 109 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
106 | 110 |
107 // Removes |worker_thread| from |idle_worker_threads_stack_|. | 111 // Removes |worker_thread| from |idle_worker_threads_stack_|. |
108 void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | 112 void RemoveFromIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); |
109 | 113 |
| 114 // The name of this thread pool, used to label its worker threads. |
| 115 const std::string name_; |
| 116 |
110 // All worker threads owned by this thread pool. Only modified during | 117 // All worker threads owned by this thread pool. Only modified during |
111 // initialization of the thread pool. | 118 // initialization of the thread pool. |
112 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | 119 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; |
113 | 120 |
114 // Synchronizes access to |next_worker_thread_index_|. | 121 // Synchronizes access to |next_worker_thread_index_|. |
115 SchedulerLock next_worker_thread_index_lock_; | 122 SchedulerLock next_worker_thread_index_lock_; |
116 | 123 |
117 // Index of the worker thread that will be assigned to the next single- | 124 // Index of the worker thread that will be assigned to the next single- |
118 // threaded TaskRunner returned by this pool. | 125 // threaded TaskRunner returned by this pool. |
119 size_t next_worker_thread_index_ = 0; | 126 size_t next_worker_thread_index_ = 0; |
(...skipping 28 matching lines...) Expand all Loading... |
148 TaskTracker* const task_tracker_; | 155 TaskTracker* const task_tracker_; |
149 DelayedTaskManager* const delayed_task_manager_; | 156 DelayedTaskManager* const delayed_task_manager_; |
150 | 157 |
151 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); | 158 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPoolImpl); |
152 }; | 159 }; |
153 | 160 |
154 } // namespace internal | 161 } // namespace internal |
155 } // namespace base | 162 } // namespace base |
156 | 163 |
157 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ | 164 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_IMPL_H_ |
OLD | NEW |