| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
| 14 #include "base/debug/task_annotator.h" | 14 #include "base/debug/task_annotator.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/message_loop/incoming_task_queue.h" | 18 #include "base/message_loop/incoming_task_queue.h" |
| 19 #include "base/message_loop/message_loop_task_runner.h" | 19 #include "base/message_loop/message_loop_task_runner.h" |
| 20 #include "base/message_loop/message_pump.h" | 20 #include "base/message_loop/message_pump.h" |
| 21 #include "base/message_loop/timer_slack.h" | 21 #include "base/message_loop/timer_slack.h" |
| 22 #include "base/observer_list.h" | 22 #include "base/observer_list.h" |
| 23 #include "base/pending_task.h" | 23 #include "base/pending_task.h" |
| 24 #include "base/run_loop.h" |
| 24 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
| 25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 27 | 28 |
| 28 // TODO(sky): these includes should not be necessary. Nuke them. | 29 // TODO(sky): these includes should not be necessary. Nuke them. |
| 29 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 30 #include "base/message_loop/message_pump_win.h" | 31 #include "base/message_loop/message_pump_win.h" |
| 31 #elif defined(OS_IOS) | 32 #elif defined(OS_IOS) |
| 32 #include "base/message_loop/message_pump_io_ios.h" | 33 #include "base/message_loop/message_pump_io_ios.h" |
| 33 #elif defined(OS_POSIX) | 34 #elif defined(OS_POSIX) |
| 34 #include "base/message_loop/message_pump_libevent.h" | 35 #include "base/message_loop/message_pump_libevent.h" |
| 35 #endif | 36 #endif |
| 36 | 37 |
| 37 #if defined(OS_ANDROID) | 38 #if defined(OS_ANDROID) |
| 38 namespace base { | 39 namespace base { |
| 39 namespace android { | 40 namespace android { |
| 40 | 41 |
| 41 class JavaMessageHandlerFactory; | 42 class JavaMessageHandlerFactory; |
| 42 | 43 |
| 43 } // namespace android | 44 } // namespace android |
| 44 } // namespace base | 45 } // namespace base |
| 45 #endif // defined(OS_ANDROID) | 46 #endif // defined(OS_ANDROID) |
| 46 | 47 |
| 47 namespace base { | 48 namespace base { |
| 48 | 49 |
| 49 class RunLoop; | |
| 50 class ThreadTaskRunnerHandle; | 50 class ThreadTaskRunnerHandle; |
| 51 class WaitableEvent; | 51 class WaitableEvent; |
| 52 | 52 |
| 53 // A MessageLoop is used to process events for a particular thread. There is | 53 // A MessageLoop is used to process events for a particular thread. There is |
| 54 // at most one MessageLoop instance per thread. | 54 // at most one MessageLoop instance per thread. |
| 55 // | 55 // |
| 56 // Events include at a minimum Task instances submitted to the MessageLoop's | 56 // Events include at a minimum Task instances submitted to the MessageLoop's |
| 57 // TaskRunner. Depending on the type of message pump used by the MessageLoop | 57 // TaskRunner. Depending on the type of message pump used by the MessageLoop |
| 58 // other events such as UI messages may be processed. On Windows APC calls (as | 58 // other events such as UI messages may be processed. On Windows APC calls (as |
| 59 // time permits) and signals sent to a registered set of HANDLEs may also be | 59 // time permits) and signals sent to a registered set of HANDLEs may also be |
| (...skipping 14 matching lines...) Expand all Loading... |
| 74 // HRESULT hr; | 74 // HRESULT hr; |
| 75 // { | 75 // { |
| 76 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 76 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 77 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. | 77 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
| 78 // } | 78 // } |
| 79 // // Process |hr| (the result returned by DoDragDrop()). | 79 // // Process |hr| (the result returned by DoDragDrop()). |
| 80 // | 80 // |
| 81 // Please be SURE your task is reentrant (nestable) and all global variables | 81 // Please be SURE your task is reentrant (nestable) and all global variables |
| 82 // are stable and accessible before calling SetNestableTasksAllowed(true). | 82 // are stable and accessible before calling SetNestableTasksAllowed(true). |
| 83 // | 83 // |
| 84 class BASE_EXPORT MessageLoop : public MessagePump::Delegate { | 84 class BASE_EXPORT MessageLoop : public MessagePump::Delegate, |
| 85 public RunLoop::Delegate { |
| 85 public: | 86 public: |
| 86 // A MessageLoop has a particular type, which indicates the set of | 87 // A MessageLoop has a particular type, which indicates the set of |
| 87 // asynchronous events it may process in addition to tasks and timers. | 88 // asynchronous events it may process in addition to tasks and timers. |
| 88 // | 89 // |
| 89 // TYPE_DEFAULT | 90 // TYPE_DEFAULT |
| 90 // This type of ML only supports tasks and timers. | 91 // This type of ML only supports tasks and timers. |
| 91 // | 92 // |
| 92 // TYPE_UI | 93 // TYPE_UI |
| 93 // This type of ML also supports native UI events (e.g., Windows messages). | 94 // This type of ML also supports native UI events (e.g., Windows messages). |
| 94 // See also MessageLoopForUI. | 95 // See also MessageLoopForUI. |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 // specific type with a custom loop. The implementation does not call | 319 // specific type with a custom loop. The implementation does not call |
| 319 // BindToCurrentThread. If this constructor is invoked directly by a subclass, | 320 // BindToCurrentThread. If this constructor is invoked directly by a subclass, |
| 320 // then the subclass must subsequently bind the message loop. | 321 // then the subclass must subsequently bind the message loop. |
| 321 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); | 322 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
| 322 | 323 |
| 323 // Configure various members and bind this message loop to the current thread. | 324 // Configure various members and bind this message loop to the current thread. |
| 324 void BindToCurrentThread(); | 325 void BindToCurrentThread(); |
| 325 | 326 |
| 326 private: | 327 private: |
| 327 friend class internal::IncomingTaskQueue; | 328 friend class internal::IncomingTaskQueue; |
| 328 friend class RunLoop; | |
| 329 friend class ScheduleWorkTest; | 329 friend class ScheduleWorkTest; |
| 330 friend class Thread; | 330 friend class Thread; |
| 331 friend struct PendingTask; | 331 friend struct PendingTask; |
| 332 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); | 332 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); |
| 333 friend class PendingTaskTest; | 333 friend class PendingTaskTest; |
| 334 | 334 |
| 335 // Creates a MessageLoop without binding to a thread. | 335 // Creates a MessageLoop without binding to a thread. |
| 336 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given | 336 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given |
| 337 // to create a message pump for this message loop. Otherwise a default | 337 // to create a message pump for this message loop. Otherwise a default |
| 338 // message pump for the |type| is created. | 338 // message pump for the |type| is created. |
| 339 // | 339 // |
| 340 // It is valid to call this to create a new message loop on one thread, | 340 // It is valid to call this to create a new message loop on one thread, |
| 341 // and then pass it to the thread where the message loop actually runs. | 341 // and then pass it to the thread where the message loop actually runs. |
| 342 // The message loop's BindToCurrentThread() method must be called on the | 342 // The message loop's BindToCurrentThread() method must be called on the |
| 343 // thread the message loop runs on, before calling Run(). | 343 // thread the message loop runs on, before calling Run(). |
| 344 // Before BindToCurrentThread() is called, only Post*Task() functions can | 344 // Before BindToCurrentThread() is called, only Post*Task() functions can |
| 345 // be called on the message loop. | 345 // be called on the message loop. |
| 346 static std::unique_ptr<MessageLoop> CreateUnbound( | 346 static std::unique_ptr<MessageLoop> CreateUnbound( |
| 347 Type type, | 347 Type type, |
| 348 MessagePumpFactoryCallback pump_factory); | 348 MessagePumpFactoryCallback pump_factory); |
| 349 | 349 |
| 350 // Sets the ThreadTaskRunnerHandle for the current thread to point to the | 350 // Sets the ThreadTaskRunnerHandle for the current thread to point to the |
| 351 // task runner for this message loop. | 351 // task runner for this message loop. |
| 352 void SetThreadTaskRunnerHandle(); | 352 void SetThreadTaskRunnerHandle(); |
| 353 | 353 |
| 354 // Invokes the actual run loop using the message pump. | 354 // RunLoop::Delegate: |
| 355 void RunHandler(); | 355 void Run() override; |
| 356 void Quit() override; |
| 356 | 357 |
| 357 // Called to process any delayed non-nestable tasks. | 358 // Called to process any delayed non-nestable tasks. |
| 358 bool ProcessNextDelayedNonNestableTask(); | 359 bool ProcessNextDelayedNonNestableTask(); |
| 359 | 360 |
| 360 // Calls RunTask or queues the pending_task on the deferred task list if it | 361 // Calls RunTask or queues the pending_task on the deferred task list if it |
| 361 // cannot be run right now. Returns true if the task was run. | 362 // cannot be run right now. Returns true if the task was run. |
| 362 bool DeferOrRunPendingTask(PendingTask pending_task); | 363 bool DeferOrRunPendingTask(PendingTask pending_task); |
| 363 | 364 |
| 364 // Adds the pending task to delayed_work_queue_. | 365 // Adds the pending task to delayed_work_queue_. |
| 365 void AddToDelayedWorkQueue(PendingTask pending_task); | 366 void AddToDelayedWorkQueue(PendingTask pending_task); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 ObserverList<DestructionObserver> destruction_observers_; | 413 ObserverList<DestructionObserver> destruction_observers_; |
| 413 | 414 |
| 414 // A recursion block that prevents accidentally running additional tasks when | 415 // A recursion block that prevents accidentally running additional tasks when |
| 415 // insider a (accidentally induced?) nested message pump. | 416 // insider a (accidentally induced?) nested message pump. |
| 416 bool nestable_tasks_allowed_; | 417 bool nestable_tasks_allowed_; |
| 417 | 418 |
| 418 // pump_factory_.Run() is called to create a message pump for this loop | 419 // pump_factory_.Run() is called to create a message pump for this loop |
| 419 // if type_ is TYPE_CUSTOM and pump_ is null. | 420 // if type_ is TYPE_CUSTOM and pump_ is null. |
| 420 MessagePumpFactoryCallback pump_factory_; | 421 MessagePumpFactoryCallback pump_factory_; |
| 421 | 422 |
| 422 RunLoop* run_loop_; | |
| 423 | |
| 424 ObserverList<TaskObserver> task_observers_; | 423 ObserverList<TaskObserver> task_observers_; |
| 425 | 424 |
| 426 debug::TaskAnnotator task_annotator_; | 425 debug::TaskAnnotator task_annotator_; |
| 427 | 426 |
| 428 // Used to allow creating a breadcrumb of program counters in PostTask. | 427 // Used to allow creating a breadcrumb of program counters in PostTask. |
| 429 // This variable is only initialized while a task is being executed and is | 428 // This variable is only initialized while a task is being executed and is |
| 430 // meant only to store context for creating a backtrace breadcrumb. Do not | 429 // meant only to store context for creating a backtrace breadcrumb. Do not |
| 431 // attach other semantics to it without thinking through the use caes | 430 // attach other semantics to it without thinking through the use caes |
| 432 // thoroughly. | 431 // thoroughly. |
| 433 const PendingTask* current_pending_task_; | 432 const PendingTask* current_pending_task_; |
| 434 | 433 |
| 435 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; | 434 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
| 436 | 435 |
| 437 // A task runner which we haven't bound to a thread yet. | 436 // A task runner which we haven't bound to a thread yet. |
| 438 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; | 437 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
| 439 | 438 |
| 440 // The task runner associated with this message loop. | 439 // The task runner associated with this message loop. |
| 441 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 440 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 442 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 441 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 443 | 442 |
| 444 // Id of the thread this message loop is bound to. Initialized once when the | 443 // Id of the thread this message loop is bound to. Initialized once when the |
| 445 // MessageLoop is bound to its thread and constant forever after. | 444 // MessageLoop is bound to its thread and constant forever after. |
| 446 PlatformThreadId thread_id_; | 445 PlatformThreadId thread_id_; |
| 447 | 446 |
| 448 // Whether this MessageLoop is currently running in nested RunLoops. | |
| 449 bool is_nested_ = false; | |
| 450 | |
| 451 // Whether task observers are allowed. | 447 // Whether task observers are allowed. |
| 452 bool allow_task_observers_ = true; | 448 bool allow_task_observers_ = true; |
| 453 | 449 |
| 450 // An interface back to RunLoop state accessible by this RunLoop::Delegate. |
| 451 RunLoop::Delegate::Client* run_loop_client_ = nullptr; |
| 452 |
| 454 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 453 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 455 }; | 454 }; |
| 456 | 455 |
| 457 #if !defined(OS_NACL) | 456 #if !defined(OS_NACL) |
| 458 | 457 |
| 459 //----------------------------------------------------------------------------- | 458 //----------------------------------------------------------------------------- |
| 460 // MessageLoopForUI extends MessageLoop with methods that are particular to a | 459 // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 461 // MessageLoop instantiated with TYPE_UI. | 460 // MessageLoop instantiated with TYPE_UI. |
| 462 // | 461 // |
| 463 // This class is typically used like so: | 462 // This class is typically used like so: |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 | 590 |
| 592 // Do not add any member variables to MessageLoopForIO! This is important b/c | 591 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 593 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 592 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 594 // data that you need should be stored on the MessageLoop's pump_ instance. | 593 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 595 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 594 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 596 "MessageLoopForIO should not have extra member variables"); | 595 "MessageLoopForIO should not have extra member variables"); |
| 597 | 596 |
| 598 } // namespace base | 597 } // namespace base |
| 599 | 598 |
| 600 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 599 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |