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_worker_thread.h" | |
23 #include "base/task_scheduler/sequence.h" | |
24 #include "base/task_scheduler/task_traits.h" | |
25 #include "base/threading/platform_thread.h" | |
26 | |
27 namespace base { | |
28 namespace internal { | |
29 | |
30 struct SequenceSortKey; | |
31 class TaskTracker; | |
32 | |
33 // A pool of threads that run Tasks. This class is thread-safe. | |
34 class BASE_EXPORT SchedulerThreadPool : public SchedulerWorkerThread::Delegate { | |
danakj
2016/04/07 23:08:44
nit: maybe consider putting the delegate in its ow
fdoray
2016/04/08 14:53:03
gab@: You asked me to nest Delegate inside Schedul
gab
2016/04/08 17:56:00
I think it's fine as an inner class, @dana: was th
danakj
2016/04/08 18:05:32
Not that, but that nested classes can't be forward
fdoray
2016/04/08 19:00:05
I'll move SchedulerWorkerThread::Delegate to its o
gab
2016/04/08 21:10:50
Hmmm but why would another file want to use the De
fdoray
2016/04/11 13:52:23
I will move the definition of *SchedulerWorkerThre
| |
35 public: | |
36 // Callback invoked after a worker thread has tried to run a Task from | |
37 // |sequence| (a TaskTracker might have prevented the Task from running). | |
38 using RanTaskFromSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | |
39 | |
40 // Creates a SchedulerThreadPool with up to |max_threads| threads of priority | |
41 // |thread_priority|. |ran_task_from_sequence_callback| will be invoked after | |
42 // a thread of this thread pool tries to run a Task. |task_tracker| is used to | |
43 // handle shutdown behavior of Tasks. Returns nullptr when it's not possible | |
44 // to create a thread pool with at least one thread. | |
45 static std::unique_ptr<SchedulerThreadPool> CreateThreadPool( | |
46 ThreadPriority thread_priority, | |
47 size_t max_threads, | |
48 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
49 TaskTracker* task_tracker); | |
50 | |
51 // Destroying a SchedulerThreadPool returned by CreateThreadPool() is not | |
52 // allowed in production; it is always leaked. In tests, it can only be | |
53 // destroyed after JoinForTesting() has returned. | |
54 ~SchedulerThreadPool() override; | |
55 | |
56 // Returns a TaskRunner whose PostTask invocations will result in scheduling | |
57 // Tasks with |traits| and |execution_mode| in this thread pool. | |
58 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | |
59 const TaskTraits& traits, | |
60 ExecutionMode execution_mode); | |
61 | |
62 // Inserts |sequence| into this thread pool's shared priority queue with | |
63 // |sequence_sort_key|. Must only be used by a worker thread to put |sequence| | |
64 // back into a PriorityQueue after running a Task from it. | |
65 void InsertSequenceAfterTaskRan(scoped_refptr<Sequence> sequence, | |
66 const SequenceSortKey& sequence_sort_key); | |
67 | |
68 // Waits until all threads are idle. | |
69 void WaitForAllWorkerThreadsIdleForTesting(); | |
70 | |
71 // Joins all threads of this thread pool. Tasks that are already running are | |
72 // allowed to complete their execution. This can only be called once. | |
73 void JoinForTesting(); | |
74 | |
75 private: | |
76 SchedulerThreadPool( | |
77 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
78 TaskTracker* task_tracker); | |
79 | |
80 void Initialize(ThreadPriority thread_priority, size_t max_threads); | |
81 | |
82 // Wakes up one thread from this thread pool if they aren't all busy. | |
83 void WakeUpOneThread(); | |
84 | |
85 // Adds |worker_thread| to |idle_worker_threads_stack_|. | |
86 void AddToIdleWorkerThreadsStack(SchedulerWorkerThread* worker_thread); | |
87 | |
88 // SchedulerWorkerThread::Delegate: | |
danakj
2016/04/07 23:08:44
nit: not really sure why these are private, you ca
fdoray
2016/04/08 14:53:03
I'd like to keep these methods private to document
fdoray
2016/04/08 15:41:10
Moved all these methods to SchedulerWorkerThreadDe
| |
89 void OnMainEntry() override; | |
90 void OnMainExit() override; | |
91 scoped_refptr<Sequence> GetWork( | |
92 SchedulerWorkerThread* worker_thread) override; | |
93 void RanTaskFromSequence(scoped_refptr<Sequence> sequence) override; | |
94 | |
95 // PriorityQueue from which all threads of this thread pool get work. | |
96 PriorityQueue shared_priority_queue_; | |
97 | |
98 // All worker threads owned by this thread pool. This is only modified by the | |
99 // constructor. | |
100 std::vector<std::unique_ptr<SchedulerWorkerThread>> worker_threads_; | |
101 | |
102 // Synchronizes access to |idle_worker_threads_stack_| and | |
103 // |idle_worker_threads_cv_|. | |
104 SchedulerLock idle_worker_threads_stack_lock_; | |
105 | |
106 // Stack of idle worker threads. The last worker thread that became idle is on | |
107 // top of the stack. | |
108 std::stack<SchedulerWorkerThread*> idle_worker_threads_stack_; | |
109 | |
110 // Signaled when all worker threads become idle. | |
111 std::unique_ptr<ConditionVariable> idle_worker_threads_stack_cv_; | |
112 | |
113 // Synchronizes access to |join_for_testing_returned_|. | |
114 SchedulerLock join_for_testing_returned_lock_; | |
115 | |
116 // True once JoinForTesting() has returned. | |
117 bool join_for_testing_returned_ = false; | |
118 | |
119 const RanTaskFromSequenceCallback ran_task_from_sequence_callback_; | |
120 TaskTracker* const task_tracker_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(SchedulerThreadPool); | |
123 }; | |
124 | |
125 } // namespace internal | |
126 } // namespace base | |
127 | |
128 #endif // BASE_TASK_SCHEDULER_SCHEDULER_THREAD_POOL_H_ | |
OLD | NEW |