Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: base/message_loop.h

Issue 10479018: Add base::RunLoop and update ui_test_utils to use it to reduce flakiness (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added printfs to debug timeout which doesn't happen locally Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/message_loop.cc » ('j') | base/message_loop.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_MESSAGE_LOOP_H_ 5 #ifndef BASE_MESSAGE_LOOP_H_
6 #define BASE_MESSAGE_LOOP_H_ 6 #define BASE_MESSAGE_LOOP_H_
7 #pragma once 7 #pragma once
8 8
9 #include <queue> 9 #include <queue>
10 #include <string> 10 #include <string>
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 this, from_here, object); 217 this, from_here, object);
218 } 218 }
219 219
220 // Run the message loop. 220 // Run the message loop.
221 void Run(); 221 void Run();
222 222
223 // Process all pending tasks, windows messages, etc., but don't wait/sleep. 223 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
224 // Return as soon as all items that can be run are taken care of. 224 // Return as soon as all items that can be run are taken care of.
225 void RunAllPending(); 225 void RunAllPending();
226 226
227 // Signals the Run method to return after it is done processing all pending 227 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
228 // messages. This method may only be called on the same thread that called 228 void Quit() { QuitWhenIdle(); }
229 // Run, and Run must still be on the call stack. 229
230 // Signals the Run method to return when it becomes idle. It will continue to
231 // process pending messages and future messages as long as they are enqueued.
232 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
233 // Quit method when looping procedures (such as web pages) have been shut
234 // down.
230 // 235 //
231 // Use QuitClosure if you need to Quit another thread's MessageLoop, but note 236 // This method may only be called on the same thread that called Run, and Run
232 // that doing so is fairly dangerous if the target thread makes nested calls 237 // must still be on the call stack.
233 // to MessageLoop::Run. The problem being that you won't know which nested 238 //
234 // run loop you are quitting, so be careful! 239 // Use Quit*Closure variants if you need to Quit another thread's MessageLoop,
235 void Quit(); 240 // but note that doing so is fairly dangerous if the target thread makes
241 // nested calls to MessageLoop::Run. The problem being that you won't know
242 // which nested run loop you are quitting, so be careful!
243 void QuitWhenIdle();
236 244
237 // This method is a variant of Quit, that does not wait for pending messages 245 // This method is a variant of Quit, that does not wait for pending messages
238 // to be processed before returning from Run. 246 // to be processed before returning from Run.
239 void QuitNow(); 247 void QuitNow();
240 248
241 // Invokes Quit on the current MessageLoop when run. Useful to schedule an 249 // This method is a variant of QuitNow that applies to a specific nested call
242 // arbitrary MessageLoop to Quit. 250 // to MessageLoop::Run(). |run_id| is returned by an earlier call to
243 static base::Closure QuitClosure(); 251 // next_run_id(). If the corresponding MessageLoop::Run call has already
252 // completed, nothing happens. If there are one or more nested calls to
253 // MessageLoop::Run inside the target Run call, the QuitNow is deferred until
254 // all nested Run calls have completed.
255 void QuitNowByID(int run_id);
jar (doing other things) 2012/06/14 04:50:29 This is an interesting extension. My general feeli
jbates 2012/06/15 02:20:38 Done as discussed over chat.
256
257 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
258 static base::Closure QuitClosure() { return QuitWhenIdleClosure(); }
259
260 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
261 // arbitrary MessageLoop to QuitWhenIdle.
262 static base::Closure QuitWhenIdleClosure();
263
264 // Construct a Closure that will call QuitNow(). Useful to schedule an
265 // arbitrary MessageLoop to QuitNow.
266 static base::Closure QuitNowClosure();
267
268 // Construct a Closure that will call QuitNowByID(run_id). Useful to schedule
269 // an arbitrary MessageLoop to QuitNowByID.
270 static base::Closure QuitNowByIDClosure(int run_id);
271
272 // Get the run id for the next call to MessageLoop::Run. Use this
273 // value to quit a specific nested call to MessageLoop::Run. For example:
274 //
275 // // Record next_run_id before running the MessageLoop:
276 // run_id_ = MessageLoop::current()->next_run_id();
277 // MessageLoop::current()->Run();
jar (doing other things) 2012/06/14 04:50:29 With the proposed API (preventing others from mani
278 //
279 // // Then, during a task or callback during the above call to Run:
280 // MessageLoop::current()->QuitNowByID(run_id_);
281 int next_run_id() const { return current_run_state_id_ + 1; }
244 282
245 // Returns the type passed to the constructor. 283 // Returns the type passed to the constructor.
246 Type type() const { return type_; } 284 Type type() const { return type_; }
247 285
248 // Optional call to connect the thread name with this loop. 286 // Optional call to connect the thread name with this loop.
249 void set_thread_name(const std::string& thread_name) { 287 void set_thread_name(const std::string& thread_name) {
250 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; 288 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
251 thread_name_ = thread_name; 289 thread_name_ = thread_name;
252 } 290 }
253 const std::string& thread_name() const { return thread_name_; } 291 const std::string& thread_name() const { return thread_name_; }
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 397
360 // Can only be called from the thread that owns the MessageLoop. 398 // Can only be called from the thread that owns the MessageLoop.
361 bool is_running() const; 399 bool is_running() const;
362 400
363 //---------------------------------------------------------------------------- 401 //----------------------------------------------------------------------------
364 protected: 402 protected:
365 struct RunState { 403 struct RunState {
366 // Used to count how many Run() invocations are on the stack. 404 // Used to count how many Run() invocations are on the stack.
367 int run_depth; 405 int run_depth;
368 406
407 // Used to distinguish this RunState from other nested RunStates.
408 int run_state_id;
409
369 // Used to record that Quit() was called, or that we should quit the pump 410 // Used to record that Quit() was called, or that we should quit the pump
370 // once it becomes idle. 411 // once it becomes idle.
371 bool quit_received; 412 bool quit_received;
372 413
414 // Used to record that QuitNow() was called at this run_depth.
415 bool quit_now_received;
416
373 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) 417 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
374 Dispatcher* dispatcher; 418 Dispatcher* dispatcher;
375 #endif 419 #endif
420 RunState* previous_state;
376 }; 421 };
377 422
378 #if defined(OS_ANDROID) 423 #if defined(OS_ANDROID)
379 // Android Java process manages the UI thread message loop. So its 424 // Android Java process manages the UI thread message loop. So its
380 // MessagePumpForUI needs to keep the RunState. 425 // MessagePumpForUI needs to keep the RunState.
381 public: 426 public:
382 #endif 427 #endif
383 class BASE_EXPORT AutoRunState : RunState { 428 class BASE_EXPORT AutoRunState : RunState {
384 public: 429 public:
385 explicit AutoRunState(MessageLoop* loop); 430 explicit AutoRunState(MessageLoop* loop);
386 ~AutoRunState(); 431 ~AutoRunState();
387 private: 432 private:
388 MessageLoop* loop_; 433 MessageLoop* loop_;
389 RunState* previous_state_;
390 }; 434 };
391 #if defined(OS_ANDROID) 435 #if defined(OS_ANDROID)
392 protected: 436 protected:
393 #endif 437 #endif
394 438
395 #if defined(OS_WIN) 439 #if defined(OS_WIN)
396 base::MessagePumpWin* pump_win() { 440 base::MessagePumpWin* pump_win() {
397 return static_cast<base::MessagePumpWin*>(pump_.get()); 441 return static_cast<base::MessagePumpWin*>(pump_.get());
398 } 442 }
399 #elif defined(OS_POSIX) 443 #elif defined(OS_POSIX)
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 559
516 // The next sequence number to use for delayed tasks. 560 // The next sequence number to use for delayed tasks.
517 int next_sequence_num_; 561 int next_sequence_num_;
518 562
519 ObserverList<TaskObserver> task_observers_; 563 ObserverList<TaskObserver> task_observers_;
520 564
521 // The message loop proxy associated with this message loop, if one exists. 565 // The message loop proxy associated with this message loop, if one exists.
522 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 566 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
523 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; 567 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
524 568
569 // Unique identifier (within the scope of this MessageLoop) for AutoRunState
570 // instances (AutoRunState is associated with calls to Run).
jar (doing other things) 2012/06/14 04:50:29 It might be nice to expand on this statement. Is
jbates 2012/06/15 02:20:38 Removed.
571 int current_run_state_id_;
572
525 private: 573 private:
526 template <class T, class R> friend class base::subtle::DeleteHelperInternal; 574 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
527 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; 575 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
528 576
529 void DeleteSoonInternal(const tracked_objects::Location& from_here, 577 void DeleteSoonInternal(const tracked_objects::Location& from_here,
530 void(*deleter)(const void*), 578 void(*deleter)(const void*),
531 const void* object); 579 const void* object);
532 void ReleaseSoonInternal(const tracked_objects::Location& from_here, 580 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
533 void(*releaser)(const void*), 581 void(*releaser)(const void*),
534 const void* object); 582 const void* object);
535 583
jar (doing other things) 2012/06/14 04:50:29 nit: extra blank line probably wasn't intentional.
536 584
537 DISALLOW_COPY_AND_ASSIGN(MessageLoop); 585 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
538 }; 586 };
539 587
540 //----------------------------------------------------------------------------- 588 //-----------------------------------------------------------------------------
541 // MessageLoopForUI extends MessageLoop with methods that are particular to a 589 // MessageLoopForUI extends MessageLoop with methods that are particular to a
542 // MessageLoop instantiated with TYPE_UI. 590 // MessageLoop instantiated with TYPE_UI.
543 // 591 //
544 // This class is typically used like so: 592 // This class is typically used like so:
545 // MessageLoopForUI::current()->...call some method... 593 // MessageLoopForUI::current()->...call some method...
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 #endif // defined(OS_POSIX) 707 #endif // defined(OS_POSIX)
660 }; 708 };
661 709
662 // Do not add any member variables to MessageLoopForIO! This is important b/c 710 // Do not add any member variables to MessageLoopForIO! This is important b/c
663 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra 711 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
664 // data that you need should be stored on the MessageLoop's pump_ instance. 712 // data that you need should be stored on the MessageLoop's pump_ instance.
665 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), 713 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
666 MessageLoopForIO_should_not_have_extra_member_variables); 714 MessageLoopForIO_should_not_have_extra_member_variables);
667 715
668 #endif // BASE_MESSAGE_LOOP_H_ 716 #endif // BASE_MESSAGE_LOOP_H_
OLDNEW
« no previous file with comments | « no previous file | base/message_loop.cc » ('j') | base/message_loop.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698