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

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: phajdan feedback Created 8 years, 5 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 | « base/base.gypi ('k') | base/message_loop.cc » ('j') | no next file with comments »
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 24 matching lines...) Expand all
35 #include "base/message_pump_aurax11.h" 35 #include "base/message_pump_aurax11.h"
36 #else 36 #else
37 #include "base/message_pump_gtk.h" 37 #include "base/message_pump_gtk.h"
38 #endif 38 #endif
39 39
40 #endif 40 #endif
41 #endif 41 #endif
42 42
43 namespace base { 43 namespace base {
44 class Histogram; 44 class Histogram;
45 class RunLoop;
45 class ThreadTaskRunnerHandle; 46 class ThreadTaskRunnerHandle;
47 #if defined(OS_ANDROID)
48 class MessagePumpForUI;
49 #endif
46 } // namespace base 50 } // namespace base
47 51
48 // A MessageLoop is used to process events for a particular thread. There is 52 // A MessageLoop is used to process events for a particular thread. There is
49 // at most one MessageLoop instance per thread. 53 // at most one MessageLoop instance per thread.
50 // 54 //
51 // Events include at a minimum Task instances submitted to PostTask or those 55 // Events include at a minimum Task instances submitted to PostTask or those
52 // managed by TimerManager. Depending on the type of message pump used by the 56 // managed by TimerManager. Depending on the type of message pump used by the
53 // MessageLoop other events such as UI messages may be processed. On Windows 57 // MessageLoop other events such as UI messages may be processed. On Windows
54 // APC calls (as time permits) and signals sent to a registered set of HANDLEs 58 // APC calls (as time permits) and signals sent to a registered set of HANDLEs
55 // may also be processed. 59 // may also be processed.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 // MessageLoop::Run(). If this is not the same as the thread that calls 206 // MessageLoop::Run(). If this is not the same as the thread that calls
203 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from 207 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
204 // RefCountedThreadSafe<T>! 208 // RefCountedThreadSafe<T>!
205 template <class T> 209 template <class T>
206 void ReleaseSoon(const tracked_objects::Location& from_here, 210 void ReleaseSoon(const tracked_objects::Location& from_here,
207 const T* object) { 211 const T* object) {
208 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner( 212 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
209 this, from_here, object); 213 this, from_here, object);
210 } 214 }
211 215
216 // Deprecated: use RunLoop instead.
212 // Run the message loop. 217 // Run the message loop.
213 void Run(); 218 void Run();
214 219
220 // Deprecated: use RunLoop instead.
215 // Process all pending tasks, windows messages, etc., but don't wait/sleep. 221 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
216 // Return as soon as all items that can be run are taken care of. 222 // Return as soon as all items that can be run are taken care of.
217 void RunAllPending(); 223 void RunUntilIdle();
218 224
219 // Signals the Run method to return after it is done processing all pending 225 // TODO(jbates) remove this. crbug.com/131220. See RunUntilIdle().
220 // messages. This method may only be called on the same thread that called 226 void RunAllPending() { RunUntilIdle(); }
221 // Run, and Run must still be on the call stack. 227
228 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
229 void Quit() { QuitWhenIdle(); }
230
231 // Deprecated: use RunLoop instead.
222 // 232 //
223 // Use QuitClosure if you need to Quit another thread's MessageLoop, but note 233 // Signals the Run method to return when it becomes idle. It will continue to
224 // that doing so is fairly dangerous if the target thread makes nested calls 234 // process pending messages and future messages as long as they are enqueued.
225 // to MessageLoop::Run. The problem being that you won't know which nested 235 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
226 // run loop you are quitting, so be careful! 236 // Quit method when looping procedures (such as web pages) have been shut
227 void Quit(); 237 // down.
238 //
239 // This method may only be called on the same thread that called Run, and Run
240 // must still be on the call stack.
241 //
242 // Use QuitClosure variants if you need to Quit another thread's MessageLoop,
243 // but note that doing so is fairly dangerous if the target thread makes
244 // nested calls to MessageLoop::Run. The problem being that you won't know
245 // which nested run loop you are quitting, so be careful!
246 void QuitWhenIdle();
228 247
248 // Deprecated: use RunLoop instead.
249 //
229 // This method is a variant of Quit, that does not wait for pending messages 250 // This method is a variant of Quit, that does not wait for pending messages
230 // to be processed before returning from Run. 251 // to be processed before returning from Run.
231 void QuitNow(); 252 void QuitNow();
232 253
233 // Invokes Quit on the current MessageLoop when run. Useful to schedule an 254 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
234 // arbitrary MessageLoop to Quit. 255 static base::Closure QuitClosure() { return QuitWhenIdleClosure(); }
235 static base::Closure QuitClosure(); 256
257 // Deprecated: use RunLoop instead.
258 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
259 // arbitrary MessageLoop to QuitWhenIdle.
260 static base::Closure QuitWhenIdleClosure();
236 261
237 // Returns the type passed to the constructor. 262 // Returns the type passed to the constructor.
238 Type type() const { return type_; } 263 Type type() const { return type_; }
239 264
240 // Optional call to connect the thread name with this loop. 265 // Optional call to connect the thread name with this loop.
241 void set_thread_name(const std::string& thread_name) { 266 void set_thread_name(const std::string& thread_name) {
242 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; 267 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
243 thread_name_ = thread_name; 268 thread_name_ = thread_name;
244 } 269 }
245 const std::string& thread_name() const { return thread_name_; } 270 const std::string& thread_name() const { return thread_name_; }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 bool os_modal_loop() const { 372 bool os_modal_loop() const {
348 return os_modal_loop_; 373 return os_modal_loop_;
349 } 374 }
350 #endif // OS_WIN 375 #endif // OS_WIN
351 376
352 // Can only be called from the thread that owns the MessageLoop. 377 // Can only be called from the thread that owns the MessageLoop.
353 bool is_running() const; 378 bool is_running() const;
354 379
355 //---------------------------------------------------------------------------- 380 //----------------------------------------------------------------------------
356 protected: 381 protected:
357 struct RunState { 382 friend class base::RunLoop;
358 // Used to count how many Run() invocations are on the stack.
359 int run_depth;
360
361 // Used to record that Quit() was called, or that we should quit the pump
362 // once it becomes idle.
363 bool quit_received;
364
365 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
366 Dispatcher* dispatcher;
367 #endif
368 };
369
370 #if defined(OS_ANDROID)
371 // Android Java process manages the UI thread message loop. So its
372 // MessagePumpForUI needs to keep the RunState.
373 public:
374 #endif
375 class BASE_EXPORT AutoRunState : RunState {
376 public:
377 explicit AutoRunState(MessageLoop* loop);
378 ~AutoRunState();
379 private:
380 MessageLoop* loop_;
381 RunState* previous_state_;
382 };
383 #if defined(OS_ANDROID)
384 protected:
385 #endif
386 383
387 #if defined(OS_WIN) 384 #if defined(OS_WIN)
388 base::MessagePumpWin* pump_win() { 385 base::MessagePumpWin* pump_win() {
389 return static_cast<base::MessagePumpWin*>(pump_.get()); 386 return static_cast<base::MessagePumpWin*>(pump_.get());
390 } 387 }
391 #elif defined(OS_POSIX) 388 #elif defined(OS_POSIX)
392 base::MessagePumpLibevent* pump_libevent() { 389 base::MessagePumpLibevent* pump_libevent() {
393 return static_cast<base::MessagePumpLibevent*>(pump_.get()); 390 return static_cast<base::MessagePumpLibevent*>(pump_.get());
394 } 391 }
395 #endif 392 #endif
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 base::Histogram* message_histogram_; 486 base::Histogram* message_histogram_;
490 487
491 // A null terminated list which creates an incoming_queue of tasks that are 488 // A null terminated list which creates an incoming_queue of tasks that are
492 // acquired under a mutex for processing on this instance's thread. These 489 // acquired under a mutex for processing on this instance's thread. These
493 // tasks have not yet been sorted out into items for our work_queue_ vs items 490 // tasks have not yet been sorted out into items for our work_queue_ vs items
494 // that will be handled by the TimerManager. 491 // that will be handled by the TimerManager.
495 base::TaskQueue incoming_queue_; 492 base::TaskQueue incoming_queue_;
496 // Protect access to incoming_queue_. 493 // Protect access to incoming_queue_.
497 mutable base::Lock incoming_queue_lock_; 494 mutable base::Lock incoming_queue_lock_;
498 495
499 RunState* state_; 496 base::RunLoop* run_loop_;
500 497
501 #if defined(OS_WIN) 498 #if defined(OS_WIN)
502 base::TimeTicks high_resolution_timer_expiration_; 499 base::TimeTicks high_resolution_timer_expiration_;
503 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc 500 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
504 // which enter a modal message loop. 501 // which enter a modal message loop.
505 bool os_modal_loop_; 502 bool os_modal_loop_;
506 #endif 503 #endif
507 504
508 // The next sequence number to use for delayed tasks. 505 // The next sequence number to use for delayed tasks.
509 int next_sequence_num_; 506 int next_sequence_num_;
510 507
511 ObserverList<TaskObserver> task_observers_; 508 ObserverList<TaskObserver> task_observers_;
512 509
513 // The message loop proxy associated with this message loop, if one exists. 510 // The message loop proxy associated with this message loop, if one exists.
514 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 511 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
515 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; 512 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
516 513
517 private: 514 private:
518 template <class T, class R> friend class base::subtle::DeleteHelperInternal; 515 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
519 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; 516 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
520 517
521 void DeleteSoonInternal(const tracked_objects::Location& from_here, 518 void DeleteSoonInternal(const tracked_objects::Location& from_here,
522 void(*deleter)(const void*), 519 void(*deleter)(const void*),
523 const void* object); 520 const void* object);
524 void ReleaseSoonInternal(const tracked_objects::Location& from_here, 521 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
525 void(*releaser)(const void*), 522 void(*releaser)(const void*),
526 const void* object); 523 const void* object);
527 524
528
529 DISALLOW_COPY_AND_ASSIGN(MessageLoop); 525 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
530 }; 526 };
531 527
532 //----------------------------------------------------------------------------- 528 //-----------------------------------------------------------------------------
533 // MessageLoopForUI extends MessageLoop with methods that are particular to a 529 // MessageLoopForUI extends MessageLoop with methods that are particular to a
534 // MessageLoop instantiated with TYPE_UI. 530 // MessageLoop instantiated with TYPE_UI.
535 // 531 //
536 // This class is typically used like so: 532 // This class is typically used like so:
537 // MessageLoopForUI::current()->...call some method... 533 // MessageLoopForUI::current()->...call some method...
538 // 534 //
(...skipping 17 matching lines...) Expand all
556 #if defined(OS_ANDROID) 552 #if defined(OS_ANDROID)
557 // On Android, the UI message loop is handled by Java side. So Run() should 553 // On Android, the UI message loop is handled by Java side. So Run() should
558 // never be called. Instead use Start(), which will forward all the native UI 554 // never be called. Instead use Start(), which will forward all the native UI
559 // events to the Java message loop. 555 // events to the Java message loop.
560 void Start(); 556 void Start();
561 #elif !defined(OS_MACOSX) 557 #elif !defined(OS_MACOSX)
562 // Please see message_pump_win/message_pump_glib for definitions of these 558 // Please see message_pump_win/message_pump_glib for definitions of these
563 // methods. 559 // methods.
564 void AddObserver(Observer* observer); 560 void AddObserver(Observer* observer);
565 void RemoveObserver(Observer* observer); 561 void RemoveObserver(Observer* observer);
566 void RunWithDispatcher(Dispatcher* dispatcher);
567 void RunAllPendingWithDispatcher(Dispatcher* dispatcher);
568 562
569 protected: 563 protected:
570 // TODO(rvargas): Make this platform independent. 564 // TODO(rvargas): Make this platform independent.
571 base::MessagePumpForUI* pump_ui() { 565 base::MessagePumpForUI* pump_ui() {
572 return static_cast<base::MessagePumpForUI*>(pump_.get()); 566 return static_cast<base::MessagePumpForUI*>(pump_.get());
573 } 567 }
574 #endif // !defined(OS_MACOSX) 568 #endif // !defined(OS_MACOSX)
575 }; 569 };
576 570
577 // Do not add any member variables to MessageLoopForUI! This is important b/c 571 // Do not add any member variables to MessageLoopForUI! This is important b/c
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 #endif // defined(OS_POSIX) 645 #endif // defined(OS_POSIX)
652 }; 646 };
653 647
654 // Do not add any member variables to MessageLoopForIO! This is important b/c 648 // Do not add any member variables to MessageLoopForIO! This is important b/c
655 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra 649 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
656 // data that you need should be stored on the MessageLoop's pump_ instance. 650 // data that you need should be stored on the MessageLoop's pump_ instance.
657 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), 651 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
658 MessageLoopForIO_should_not_have_extra_member_variables); 652 MessageLoopForIO_should_not_have_extra_member_variables);
659 653
660 #endif // BASE_MESSAGE_LOOP_H_ 654 #endif // BASE_MESSAGE_LOOP_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/message_loop.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698