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_WORKER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 // SchedulerLock. | 91 // SchedulerLock. |
92 virtual void OnDetach() = 0; | 92 virtual void OnDetach() = 0; |
93 | 93 |
94 // Called by a thread right before the main function exits. | 94 // Called by a thread right before the main function exits. |
95 virtual void OnMainExit() {} | 95 virtual void OnMainExit() {} |
96 }; | 96 }; |
97 | 97 |
98 enum class InitialState { ALIVE, DETACHED }; | 98 enum class InitialState { ALIVE, DETACHED }; |
99 | 99 |
100 // Creates a SchedulerWorker that runs Tasks from Sequences returned by | 100 // Creates a SchedulerWorker that runs Tasks from Sequences returned by |
101 // |delegate|. |priority_hint| is the preferred thread priority; the actual | 101 // |delegate|. No actual thread will be created for this SchedulerWorker |
102 // thread priority depends on shutdown state and platform capabilities. | 102 // before Start() is called. |priority_hint| is the preferred thread priority; |
103 // |task_tracker| is used to handle shutdown behavior of Tasks. If | 103 // the actual thread priority depends on shutdown state and platform |
104 // |worker_state| is DETACHED, the thread will be created upon a WakeUp(). | 104 // capabilities. |task_tracker| is used to handle shutdown behavior of Tasks. |
105 // Returns nullptr if creating the underlying platform thread fails during | 105 // |backward_compatibility| indicates whether backward compatibility is |
106 // Create(). |backward_compatibility| indicates whether backward compatibility | 106 // enabled. Either JoinForTesting() or Cleanup() must be called before |
107 // is enabled. Either JoinForTesting() or Cleanup() must be called before | |
108 // releasing the last external reference. | 107 // releasing the last external reference. |
109 static scoped_refptr<SchedulerWorker> Create( | 108 SchedulerWorker(ThreadPriority priority_hint, |
110 ThreadPriority priority_hint, | 109 std::unique_ptr<Delegate> delegate, |
111 std::unique_ptr<Delegate> delegate, | 110 TaskTracker* task_tracker, |
112 TaskTracker* task_tracker, | 111 SchedulerBackwardCompatibility backward_compatibility = |
113 InitialState initial_state, | 112 SchedulerBackwardCompatibility::DISABLED); |
114 SchedulerBackwardCompatibility backward_compatibility = | |
115 SchedulerBackwardCompatibility::DISABLED); | |
116 | 113 |
117 // Wakes up this SchedulerWorker if it wasn't already awake. After this | 114 // Allows this SchedulerWorker to be backed by a thread. If |initial_state| is |
118 // is called, this SchedulerWorker will run Tasks from Sequences | 115 // ALIVE and JoinForTesting()/Cleanup() weren't called, a thread is created |
119 // returned by the GetWork() method of its delegate until it returns nullptr. | 116 // immediately. Otherwise, creation is delayed until the next WakeUp(). |
gab
2017/04/10 18:04:56
"immediately (in a wait state pending a WakeUp() c
fdoray
2017/04/10 19:05:00
Done.
| |
120 // WakeUp() may fail if the worker is detached and it fails to allocate a new | 117 // Returns true on success. |
121 // worker. If this happens, there will be no call to GetWork(). | 118 bool Start(InitialState initial_state = InitialState::ALIVE); |
119 | |
120 /// Wakes up this SchedulerWorker if it wasn't already awake. After this is | |
121 // called, this SchedulerWorker will run Tasks from Sequences returned by the | |
122 // GetWork() method of its delegate until it returns nullptr. WakeUp() may | |
123 // fail if the worker is detached and it fails to allocate a new worker. If | |
124 // this happens, there will be no call to GetWork(). No-op if Start() wasn't | |
125 // called. | |
122 void WakeUp(); | 126 void WakeUp(); |
123 | 127 |
124 SchedulerWorker::Delegate* delegate() { return delegate_.get(); } | 128 SchedulerWorker::Delegate* delegate() { return delegate_.get(); } |
125 | 129 |
126 // Joins this SchedulerWorker. If a Task is already running, it will be | 130 // Joins this SchedulerWorker. If a Task is already running, it will be |
127 // allowed to complete its execution. This can only be called once. | 131 // allowed to complete its execution. This can only be called once. |
128 // | 132 // |
129 // Note: A thread that detaches before JoinForTesting() is called may still be | 133 // Note: A thread that detaches before JoinForTesting() is called may still be |
130 // running after JoinForTesting() returns. However, it can't run tasks after | 134 // running after JoinForTesting() returns. However, it can't run tasks after |
131 // JoinForTesting() returns. | 135 // JoinForTesting() returns. |
(...skipping 16 matching lines...) Expand all Loading... | |
148 private: | 152 private: |
149 friend class RefCountedThreadSafe<SchedulerWorker>; | 153 friend class RefCountedThreadSafe<SchedulerWorker>; |
150 class Thread; | 154 class Thread; |
151 enum class DetachNotify { | 155 enum class DetachNotify { |
152 // Do not notify any component. | 156 // Do not notify any component. |
153 SILENT, | 157 SILENT, |
154 // Notify the delegate. | 158 // Notify the delegate. |
155 DELEGATE, | 159 DELEGATE, |
156 }; | 160 }; |
157 | 161 |
158 SchedulerWorker(ThreadPriority thread_priority, | |
159 std::unique_ptr<Delegate> delegate, | |
160 TaskTracker* task_tracker, | |
161 SchedulerBackwardCompatibility backward_compatibility); | |
162 ~SchedulerWorker(); | 162 ~SchedulerWorker(); |
163 | 163 |
164 // Returns ownership of the thread instance when appropriate so that it can be | 164 // Returns ownership of the thread instance when appropriate so that it can be |
165 // freed upon termination of the thread. If ownership transfer is not | 165 // freed upon termination of the thread. If ownership transfer is not |
166 // possible, returns nullptr. | 166 // possible, returns nullptr. |
167 std::unique_ptr<SchedulerWorker::Thread> DetachThreadObject( | 167 std::unique_ptr<SchedulerWorker::Thread> DetachThreadObject( |
168 DetachNotify detach_notify); | 168 DetachNotify detach_notify); |
169 | 169 |
170 void CreateThread(); | 170 void CreateThread(); |
171 | 171 |
172 void CreateThreadAssertSynchronized(); | 172 void CreateThreadAssertSynchronized(); |
173 | 173 |
174 bool ShouldExit(); | 174 bool ShouldExit(); |
175 | 175 |
176 // Synchronizes access to |thread_| (read+write) as well as |should_exit_| | 176 // Synchronizes access to |thread_| (read+write), |started_| (read+write) and |
177 // (write-only). See Cleanup() for details. | 177 // |should_exit_| (write-only). See Cleanup() for details. |
178 mutable SchedulerLock thread_lock_; | 178 mutable SchedulerLock thread_lock_; |
179 | 179 |
180 AtomicFlag should_exit_; | |
181 | |
182 // The underlying thread for this SchedulerWorker. | 180 // The underlying thread for this SchedulerWorker. |
183 // The thread object will be cleaned up by the running thread unless we join | 181 // The thread object will be cleaned up by the running thread unless we join |
184 // against the thread. Joining requires the thread object to remain alive for | 182 // against the thread. Joining requires the thread object to remain alive for |
185 // the Thread::Join() call. | 183 // the Thread::Join() call. |
186 std::unique_ptr<Thread> thread_; | 184 std::unique_ptr<Thread> thread_; |
187 | 185 |
186 bool started_ = false; | |
187 | |
188 AtomicFlag should_exit_; | |
189 | |
188 const ThreadPriority priority_hint_; | 190 const ThreadPriority priority_hint_; |
189 | 191 |
190 const std::unique_ptr<Delegate> delegate_; | 192 const std::unique_ptr<Delegate> delegate_; |
191 TaskTracker* const task_tracker_; | 193 TaskTracker* const task_tracker_; |
192 | 194 |
193 #if defined(OS_WIN) | 195 #if defined(OS_WIN) |
194 const SchedulerBackwardCompatibility backward_compatibility_; | 196 const SchedulerBackwardCompatibility backward_compatibility_; |
195 #endif | 197 #endif |
196 | 198 |
197 // Set once JoinForTesting() has been called. | 199 // Set once JoinForTesting() has been called. |
198 AtomicFlag join_called_for_testing_; | 200 AtomicFlag join_called_for_testing_; |
199 | 201 |
200 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); | 202 DISALLOW_COPY_AND_ASSIGN(SchedulerWorker); |
201 }; | 203 }; |
202 | 204 |
203 } // namespace internal | 205 } // namespace internal |
204 } // namespace base | 206 } // namespace base |
205 | 207 |
206 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ | 208 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_H_ |
OLD | NEW |