OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_RUN_LOOP_H_ | 5 #ifndef BASE_RUN_LOOP_H_ |
6 #define BASE_RUN_LOOP_H_ | 6 #define BASE_RUN_LOOP_H_ |
7 | 7 |
8 #include <stack> | 8 #include <stack> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/sequence_checker.h" |
16 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
21 class MessagePumpForUI; | 22 class MessagePumpForUI; |
22 #endif | 23 #endif |
23 | 24 |
24 #if defined(OS_IOS) | 25 #if defined(OS_IOS) |
25 class MessagePumpUIApplication; | 26 class MessagePumpUIApplication; |
(...skipping 18 matching lines...) Expand all Loading... |
44 // MessageLoop...), but those are deprecated. | 45 // MessageLoop...), but those are deprecated. |
45 void Run(); | 46 void Run(); |
46 | 47 |
47 // Run the current RunLoop::Delegate until it doesn't find any tasks or | 48 // Run the current RunLoop::Delegate until it doesn't find any tasks or |
48 // messages in its queue (it goes idle). WARNING: This may never return! Only | 49 // messages in its queue (it goes idle). WARNING: This may never return! Only |
49 // use this when repeating tasks such as animated web pages have been shut | 50 // use this when repeating tasks such as animated web pages have been shut |
50 // down. | 51 // down. |
51 void RunUntilIdle(); | 52 void RunUntilIdle(); |
52 | 53 |
53 bool running() const { | 54 bool running() const { |
54 // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. | 55 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
55 // DCHECK(thread_checker_.CalledOnValidThread()); | |
56 return running_; | 56 return running_; |
57 } | 57 } |
58 | 58 |
59 // Quit() quits an earlier call to Run() immediately. QuitWhenIdle() quits an | 59 // Quit() quits an earlier call to Run() immediately. QuitWhenIdle() quits an |
60 // earlier call to Run() when there aren't any tasks or messages in the queue. | 60 // earlier call to Run() when there aren't any tasks or messages in the queue. |
61 // | 61 // |
62 // There can be other nested RunLoops servicing the same task queue | 62 // There can be other nested RunLoops servicing the same task queue |
63 // (MessageLoop); Quitting one RunLoop has no bearing on the others. Quit() | 63 // (MessageLoop); Quitting one RunLoop has no bearing on the others. Quit() |
64 // and QuitWhenIdle() can be called before, during or after Run(). If called | 64 // and QuitWhenIdle() can be called before, during or after Run(). If called |
65 // before Run(), Run() will return immediately when called. Calling Quit() or | 65 // before Run(), Run() will return immediately when called. Calling Quit() or |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 using RunLoopStack = std::stack<RunLoop*, std::vector<RunLoop*>>; | 168 using RunLoopStack = std::stack<RunLoop*, std::vector<RunLoop*>>; |
169 | 169 |
170 bool allow_nesting_ = true; | 170 bool allow_nesting_ = true; |
171 RunLoopStack active_run_loops_; | 171 RunLoopStack active_run_loops_; |
172 ObserverList<RunLoop::NestingObserver> nesting_observers_; | 172 ObserverList<RunLoop::NestingObserver> nesting_observers_; |
173 | 173 |
174 // True once this Delegate is bound to a thread via | 174 // True once this Delegate is bound to a thread via |
175 // RegisterDelegateForCurrentThread(). | 175 // RegisterDelegateForCurrentThread(). |
176 bool bound_ = false; | 176 bool bound_ = false; |
177 | 177 |
| 178 // Thread-affine per its use of TLS. |
178 THREAD_CHECKER(bound_thread_checker_); | 179 THREAD_CHECKER(bound_thread_checker_); |
179 | 180 |
180 Client client_interface_ = Client(this); | 181 Client client_interface_ = Client(this); |
181 | 182 |
182 DISALLOW_COPY_AND_ASSIGN(Delegate); | 183 DISALLOW_COPY_AND_ASSIGN(Delegate); |
183 }; | 184 }; |
184 | 185 |
185 // Registers |delegate| on the current thread. Must be called once and only | 186 // Registers |delegate| on the current thread. Must be called once and only |
186 // once per thread before using RunLoop methods on it. |delegate| is from then | 187 // once per thread before using RunLoop methods on it. |delegate| is from then |
187 // on forever bound to that thread (including its destruction). The returned | 188 // on forever bound to that thread (including its destruction). The returned |
(...skipping 13 matching lines...) Expand all Loading... |
201 #if defined(OS_IOS) | 202 #if defined(OS_IOS) |
202 // iOS doesn't support the blocking MessageLoop::Run, so it calls | 203 // iOS doesn't support the blocking MessageLoop::Run, so it calls |
203 // BeforeRun directly. | 204 // BeforeRun directly. |
204 friend class base::MessagePumpUIApplication; | 205 friend class base::MessagePumpUIApplication; |
205 #endif | 206 #endif |
206 | 207 |
207 // Return false to abort the Run. | 208 // Return false to abort the Run. |
208 bool BeforeRun(); | 209 bool BeforeRun(); |
209 void AfterRun(); | 210 void AfterRun(); |
210 | 211 |
211 // A copy of RunLoop::Delegate for this thread for quick access without using | 212 // A copy of RunLoop::Delegate for the thread driven by tis RunLoop for quick |
212 // TLS. | 213 // access without using TLS (also allows access to state from another sequence |
| 214 // during Run(), ref. |sequence_checker_| below). |
213 Delegate* delegate_; | 215 Delegate* delegate_; |
214 | 216 |
215 bool run_called_ = false; | 217 bool run_called_ = false; |
216 bool quit_called_ = false; | 218 bool quit_called_ = false; |
217 bool running_ = false; | 219 bool running_ = false; |
218 | 220 |
219 // Used to record that QuitWhenIdle() was called on the MessageLoop, meaning | 221 // Used to record that QuitWhenIdle() was called on the MessageLoop, meaning |
220 // that we should quit Run once it becomes idle. | 222 // that we should quit Run once it becomes idle. |
221 bool quit_when_idle_received_ = false; | 223 bool quit_when_idle_received_ = false; |
222 | 224 |
223 // RunLoop's non-static methods are affine to the thread it's running on per | 225 // RunLoop is not thread-safe. Its state may not be accessed from any other |
224 // this class' underlying use of thread-local-storage. | 226 // sequence than the thread it was constructed on. Exception: RunLoop can be |
225 base::ThreadChecker thread_checker_; | 227 // safely accessed from one other sequence (or single parallel task) during |
| 228 // Run(). |
| 229 SEQUENCE_CHECKER(sequence_checker_); |
226 | 230 |
227 // WeakPtrFactory for QuitClosure safety. | 231 // WeakPtrFactory for QuitClosure safety. |
228 base::WeakPtrFactory<RunLoop> weak_factory_; | 232 base::WeakPtrFactory<RunLoop> weak_factory_; |
229 | 233 |
230 DISALLOW_COPY_AND_ASSIGN(RunLoop); | 234 DISALLOW_COPY_AND_ASSIGN(RunLoop); |
231 }; | 235 }; |
232 | 236 |
233 } // namespace base | 237 } // namespace base |
234 | 238 |
235 #endif // BASE_RUN_LOOP_H_ | 239 #endif // BASE_RUN_LOOP_H_ |
OLD | NEW |