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