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> | |
9 | |
10 #include <memory> | 8 #include <memory> |
11 #include <vector> | |
12 | 9 |
13 #include "base/base_export.h" | 10 #include "base/base_export.h" |
14 #include "base/callback.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
17 #include "base/synchronization/condition_variable.h" | |
18 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
19 #include "base/task_scheduler/priority_queue.h" | |
20 #include "base/task_scheduler/scheduler_lock.h" | |
21 #include "base/task_scheduler/scheduler_task_executor.h" | |
22 #include "base/task_scheduler/scheduler_worker_thread.h" | |
23 #include "base/task_scheduler/scheduler_worker_thread_stack.h" | |
24 #include "base/task_scheduler/sequence.h" | 13 #include "base/task_scheduler/sequence.h" |
25 #include "base/task_scheduler/task.h" | 14 #include "base/task_scheduler/task.h" |
26 #include "base/task_scheduler/task_traits.h" | 15 #include "base/task_scheduler/task_traits.h" |
27 #include "base/threading/platform_thread.h" | |
28 | 16 |
29 namespace base { | 17 namespace base { |
30 namespace internal { | 18 namespace internal { |
31 | 19 |
32 class DelayedTaskManager; | |
33 struct SequenceSortKey; | 20 struct SequenceSortKey; |
34 class TaskTracker; | |
35 | 21 |
36 // A pool of threads that run Tasks. This class is thread-safe. | 22 // Interface for a thread pool. |
37 class BASE_EXPORT SchedulerThreadPool : public SchedulerTaskExecutor { | 23 class BASE_EXPORT SchedulerThreadPool { |
38 public: | 24 public: |
39 // Callback invoked when a Sequence isn't empty after a worker thread pops a | 25 virtual ~SchedulerThreadPool() = default; |
40 // Task from it. | |
41 using EnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | |
42 | |
43 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not | |
44 // allowed in production; it is always leaked. In tests, it can only be | |
45 // destroyed after JoinForTesting() has returned. | |
46 ~SchedulerThreadPool() override; | |
47 | |
48 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | |
49 // |thread_priority|. |enqueue_sequence_callback| will be invoked after a | |
50 // thread of this thread pool tries to run a Task. |task_tracker| is used to | |
51 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | |
52 // posted with a delay. Returns nullptr on failure to create a thread pool | |
53 // with at least one thread. | |
54 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( | |
55 ThreadPriority thread_priority, | |
56 size_t max_threads, | |
57 const EnqueueSequenceCallback& enqueue_sequence_callback, | |
58 TaskTracker* task_tracker, | |
59 DelayedTaskManager* delayed_task_manager); | |
60 | 26 |
61 // Returns a TaskRunner whose PostTask invocations will result in scheduling | 27 // Returns a TaskRunner whose PostTask invocations will result in scheduling |
62 // Tasks with |traits| and |execution_mode| in this thread pool. | 28 // Tasks with |traits| and |execution_mode| in this thread pool. |
63 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 29 virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
64 const TaskTraits& traits, | 30 const TaskTraits& traits, |
65 ExecutionMode execution_mode); | 31 ExecutionMode execution_mode) = 0; |
66 | 32 |
67 // Inserts |sequence| into this thread pool's shared priority queue with | 33 // Inserts |sequence| into this thread pool's shared priority queue with |
68 // |sequence_sort_key|. Must only be called from a worker thread to put | 34 // |sequence_sort_key|. Must only be called from a worker thread to put |
69 // |sequence| back into a PriorityQueue after running a Task from it. The | 35 // |sequence| back into a PriorityQueue after running a Task from it. The |
70 // worker thread doesn't have to belong to this thread pool. | 36 // worker thread doesn't have to belong to this thread pool. |
71 void EnqueueSequence(scoped_refptr<Sequence> sequence, | 37 virtual void EnqueueSequence(scoped_refptr<Sequence> sequence, |
gab
2016/04/25 17:52:43
How about we call this ReEnqueueSequence?
(I know
fdoray
2016/04/25 19:14:50
Done.
| |
72 const SequenceSortKey& sequence_sort_key); | 38 const SequenceSortKey& sequence_sort_key) = 0; |
73 | 39 |
74 // Waits until all threads are idle. | 40 // Posts |task| to be executed as part of |sequence|. Returns true if |task| |
75 void WaitForAllWorkerThreadsIdleForTesting(); | 41 // is posted. |
42 virtual bool PostTaskWithSequence(std::unique_ptr<Task> task, | |
43 scoped_refptr<Sequence> sequence) = 0; | |
76 | 44 |
77 // Joins all threads of this thread pool. Tasks that are already running are | 45 // Posts |task| to be executed by this thread pool as part of |sequence|. The |
78 // allowed to complete their execution. This can only be called once. | 46 // scheduler's TaskTracker must have allowed |task| to be posted before this |
79 void JoinForTesting(); | 47 // is called. This must only be called after |task|'s delayed run time. |
80 | 48 virtual void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
81 // SchedulerTaskExecutor: | 49 scoped_refptr<Sequence> sequence) = 0; |
82 void PostTaskWithSequence(std::unique_ptr<Task> task, | |
83 scoped_refptr<Sequence> sequence) override; | |
84 | |
85 private: | |
86 class SchedulerWorkerThreadDelegateImpl; | |
87 | |
88 SchedulerThreadPool(const EnqueueSequenceCallback& enqueue_sequence_callback, | |
89 TaskTracker* task_tracker, | |
90 DelayedTaskManager* delayed_task_manager); | |
91 | |
92 bool Initialize(ThreadPriority thread_priority, size_t max_threads); | |
93 | |
94 // Wakes up the last thread from this thread pool to go idle, if any. | |
95 void WakeUpOneThread(); | |
96 | |
97 // Adds |worker_thread| to |idle_worker_threads_stack_|. | |
98 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | |
99 | |
100 // PriorityQueue from which all threads of this thread pool get work. | |
101 PriorityQueue shared_priority_queue_; | |
102 | |
103 // All worker threads owned by this thread pool. Only modified during | |
104 // initialization of the thread pool. | |
105 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | |
106 | |
107 // Synchronizes access to |idle_worker_threads_stack_| and | |
108 // |idle_worker_threads_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | |
109 // lock as its predecessor so that a thread can be pushed to | |
110 // |idle_worker_threads_stack_| within the scope of a Transaction (more | |
111 // details in GetWork()). | |
112 SchedulerLock idle_worker_threads_stack_lock_; | |
113 | |
114 // Stack of idle worker threads. | |
115 SchedulerWorkerThreadStack idle_worker_threads_stack_; | |
116 | |
117 // Signaled when all worker threads become idle. | |
118 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_for_testing_; | |
119 | |
120 // Signaled once JoinForTesting() has returned. | |
121 WaitableEvent join_for_testing_returned_; | |
122 | |
123 // Delegate for all worker threads in this pool. | |
124 std::unique_ptr<SchedulerWorkerThread::Delegate> worker_thread_delegate_; | |
125 | |
126 TaskTracker* const task_tracker_; | |
127 DelayedTaskManager* const delayed_task_manager_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | |
130 }; | 50 }; |
131 | 51 |
132 } // namespace internal | 52 } // namespace internal |
133 } // namespace base | 53 } // namespace base |
134 | 54 |
135 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | 55 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ |
OLD | NEW |