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_WORKER_THREAD_H_ | |
6 #define BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
7 | |
8 #include "base/base_export.h" | |
9 #include "base/callback.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/single_thread_task_runner.h" | |
14 #include "base/synchronization/condition_variable.h" | |
15 #include "base/task_scheduler/priority_queue.h" | |
16 #include "base/task_scheduler/scheduler_lock.h" | |
17 #include "base/task_scheduler/sequence.h" | |
18 #include "base/task_scheduler/task_traits.h" | |
19 #include "base/threading/platform_thread.h" | |
20 | |
21 namespace base { | |
22 namespace internal { | |
23 | |
24 class TaskTracker; | |
25 | |
26 // A thread that runs tasks from a shared and a single-threaded priority queue. | |
27 // This class is thread-safe. | |
28 class BASE_EXPORT WorkerThread : public PlatformThread::Delegate { | |
29 public: | |
30 // Callback invoked by |worker_thread| to reinsert |sequence| in the | |
gab
2016/03/21 19:11:55
The callback contract can't really dictate who cal
fdoray
2016/03/24 19:21:10
Done.
| |
31 // appropriate priority queue it has executed one of its tasks. | |
gab
2016/03/21 19:11:55
missing "after" before "it"?
fdoray
2016/03/24 19:21:10
Done.
| |
32 using ReinsertSequenceCallback = | |
33 Callback<void(scoped_refptr<Sequence> sequence, | |
34 const WorkerThread* worker_thread)>; | |
35 | |
36 // Callback invoked when |worker_thread| becomes idle. | |
37 using BecomesIdleCallback = Callback<void(WorkerThread* worker_thread)>; | |
38 | |
39 // Creates a WorkerThread with priority |thread_priority| that runs tasks from | |
40 // a single-thread priority queue and from |shared_priority_queue|. | |
gab
2016/03/21 19:11:54
s/a single-thread priority queue/its single-thread
fdoray
2016/03/24 19:21:10
Done.
| |
41 // |reinsert_sequence_callback| is invoked to reinsert a sequence in the | |
42 // appropriate priority queue after one of its tasks has been executed. | |
43 // |task_tracker| is used to handle shutdown behavior of tasks. | |
44 static scoped_ptr<WorkerThread> CreateWorkerThread( | |
45 ThreadPriority thread_priority, | |
46 PriorityQueue* shared_priority_queue, | |
47 const ReinsertSequenceCallback& reinsert_sequence_callback, | |
48 const BecomesIdleCallback& becomes_idle_callback, | |
49 TaskTracker* task_tracker); | |
50 | |
51 // A WorkerThread must only be destroyed after JoinForTesting() has returned. | |
gab
2016/03/21 19:11:55
Hmmm, isn't the WorkerThread destroyed in the prod
fdoray
2016/03/24 19:21:10
Done.
| |
52 ~WorkerThread() override; | |
53 | |
54 // Wakes up the WorkerThread. It is guaranteed that the WorkerThread will stay | |
55 // awake until both of its PriorityQueues become empty after this is called. | |
56 // Returns false if the WorkerThread was already awake. | |
57 bool WakeUp(); | |
58 | |
59 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in | |
60 // scheduling tasks on this WorkerThread with |traits|. | |
gab
2016/03/21 19:11:54
s/tasks on this WorkerThread with |traits|/tasks w
fdoray
2016/03/24 19:21:10
Done.
| |
61 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits( | |
62 const TaskTraits& traits); | |
63 | |
64 // Joins the WorkerThread. If a task is already running, it will be allowed | |
65 // to complete its execution. This can only be called once. | |
66 void JoinForTesting(); | |
67 | |
68 private: | |
69 WorkerThread(ThreadPriority thread_priority, | |
70 PriorityQueue* shared_priority_queue, | |
71 const ReinsertSequenceCallback& reinsert_sequence_callback, | |
72 const BecomesIdleCallback& becomes_idle_callback, | |
73 TaskTracker* task_tracker); | |
74 | |
75 // Extracts the sequence with the highest priority from | |
76 // |shared_priority_queue_| or |single_thread_priority_queue_|. | |
77 // |single_thread| is set to true if the returned task comes from | |
78 // |single_thread_priority_queue_|. | |
79 scoped_refptr<Sequence> GetWork(bool* single_thread); | |
gab
2016/03/21 19:11:54
s/single_thread/is_single_thread/
fdoray
2016/03/24 19:21:10
Done.
| |
80 | |
81 // Waits until |is_awake_| becomes true. | |
82 void WaitUntilWakeUp(); | |
gab
2016/03/21 19:11:55
WaitUntilWokenUp or WaitForWakeUp?
fdoray
2016/03/24 19:21:10
Done (method gone).
| |
83 | |
84 // PlatformThread::Delegate: | |
85 void ThreadMain() override; | |
86 | |
87 // Platform thread managed by this WorkerThread. | |
88 PlatformThreadHandle thread_handle_; | |
89 | |
90 // Synchronizes access to |wake_up_cv_|, |is_awake_| and | |
91 // |should_exit_for_testing_|. | |
92 SchedulerLock lock_; | |
93 | |
94 // Condition variable signaled when |is_awake_| becomes true. | |
95 mutable scoped_ptr<ConditionVariable> wake_up_cv_; | |
96 | |
97 // True from the time WakeUp() is called to the time the WorkerThread doesn't | |
gab
2016/03/21 19:11:55
s/the WorkerThread/this WorkerThread/
(same below
fdoray
2016/03/24 19:21:10
Done.
| |
98 // have any remaining work to do. | |
99 bool is_awake_; | |
100 | |
101 // True once JoinForTesting() has been called. | |
102 bool should_exit_for_testing_; | |
103 | |
104 // The single-threaded priority queue from which the WorkerThread gets work. | |
105 PriorityQueue single_thread_priority_queue_; | |
106 | |
107 // The shared priority queue from which the WorkerThread gets work. | |
108 PriorityQueue* const shared_priority_queue_; | |
109 | |
110 const ReinsertSequenceCallback reinsert_sequence_callback_; | |
111 const BecomesIdleCallback becomes_idle_callback_; | |
112 TaskTracker* const task_tracker_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(WorkerThread); | |
115 }; | |
116 | |
117 } // namespace internal | |
118 } // namespace base | |
119 | |
120 #endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
OLD | NEW |