Chromium Code Reviews| 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 <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.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/location.h" | 16 #include "base/location.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop/incoming_task_queue.h" | 19 #include "base/message_loop/incoming_task_queue.h" |
| 19 #include "base/message_loop/message_loop_task_runner.h" | 20 #include "base/message_loop/message_loop_task_runner.h" |
| 20 #include "base/message_loop/message_pump.h" | 21 #include "base/message_loop/message_pump.h" |
| 21 #include "base/message_loop/timer_slack.h" | 22 #include "base/message_loop/timer_slack.h" |
| 22 #include "base/observer_list.h" | 23 #include "base/observer_list.h" |
| 23 #include "base/pending_task.h" | 24 #include "base/pending_task.h" |
| 24 #include "base/sequenced_task_runner_helpers.h" | 25 #include "base/sequenced_task_runner_helpers.h" |
| 25 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 27 #include "base/tracking_info.h" | 28 #include "base/tracking_info.h" |
| 28 | 29 |
| 29 // TODO(sky): these includes should not be necessary. Nuke them. | 30 // TODO(sky): these includes should not be necessary. Nuke them. |
| 30 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
| 31 #include "base/message_loop/message_pump_win.h" | 32 #include "base/message_loop/message_pump_win.h" |
| 32 #elif defined(OS_IOS) | 33 #elif defined(OS_IOS) |
| 33 #include "base/message_loop/message_pump_io_ios.h" | 34 #include "base/message_loop/message_pump_io_ios.h" |
| 34 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
| 35 #include "base/message_loop/message_pump_libevent.h" | 36 #include "base/message_loop/message_pump_libevent.h" |
| 36 #endif | 37 #endif |
| 37 | 38 |
| 38 namespace base { | 39 namespace base { |
| 39 | 40 |
| 41 class BindableSingleThreadTaskRunner; | |
| 40 class HistogramBase; | 42 class HistogramBase; |
| 41 class RunLoop; | 43 class RunLoop; |
| 42 class ThreadTaskRunnerHandle; | 44 class ThreadTaskRunnerHandle; |
| 43 class WaitableEvent; | 45 class WaitableEvent; |
| 44 | 46 |
| 45 // A MessageLoop is used to process events for a particular thread. There is | 47 // A MessageLoop is used to process events for a particular thread. There is |
| 46 // at most one MessageLoop instance per thread. | 48 // at most one MessageLoop instance per thread. |
| 47 // | 49 // |
| 48 // Events include at a minimum Task instances submitted to PostTask and its | 50 // Events include at a minimum Task instances submitted to PostTask and its |
| 49 // variants. Depending on the type of message pump used by the MessageLoop | 51 // variants. Depending on the type of message pump used by the MessageLoop |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 Type type() const { return type_; } | 291 Type type() const { return type_; } |
| 290 | 292 |
| 291 // Optional call to connect the thread name with this loop. | 293 // Optional call to connect the thread name with this loop. |
| 292 void set_thread_name(const std::string& thread_name) { | 294 void set_thread_name(const std::string& thread_name) { |
| 293 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; | 295 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 294 thread_name_ = thread_name; | 296 thread_name_ = thread_name; |
| 295 } | 297 } |
| 296 const std::string& thread_name() const { return thread_name_; } | 298 const std::string& thread_name() const { return thread_name_; } |
| 297 | 299 |
| 298 // Gets the TaskRunner associated with this message loop. | 300 // Gets the TaskRunner associated with this message loop. |
| 299 // TODO(skyostil): Change this to return a const reference to a refptr | |
| 300 // once the internal type matches what is being returned (crbug.com/465354). | |
|
Sami
2015/06/19 21:17:35
N.B. This can't return a BindableSingleThreadTaskR
alexclarke
2015/06/24 10:27:04
This is kind of an annoying feature of C++. Autom
| |
| 301 scoped_refptr<SingleThreadTaskRunner> task_runner() { return task_runner_; } | 301 scoped_refptr<SingleThreadTaskRunner> task_runner() { return task_runner_; } |
| 302 | 302 |
| 303 // Sets a new TaskRunner for this message loop and returns the previous one. | |
| 304 // If the message loop was already bound to a thread, this function must be | |
| 305 // called on that thread and the task runner must also belong to that thread. | |
| 306 // Note that changing the task runner will also affect the | |
| 307 // ThreadTaskRunnerHandle for the target thread. | |
| 308 scoped_refptr<BindableSingleThreadTaskRunner> SwapTaskRunner( | |
| 309 scoped_refptr<BindableSingleThreadTaskRunner> task_runner); | |
| 310 | |
| 303 // Enables or disables the recursive task processing. This happens in the case | 311 // Enables or disables the recursive task processing. This happens in the case |
| 304 // of recursive message loops. Some unwanted message loop may occurs when | 312 // of recursive message loops. Some unwanted message loop may occurs when |
| 305 // using common controls or printer functions. By default, recursive task | 313 // using common controls or printer functions. By default, recursive task |
| 306 // processing is disabled. | 314 // processing is disabled. |
| 307 // | 315 // |
| 308 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods | 316 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods |
| 309 // directly. In general nestable message loops are to be avoided. They are | 317 // directly. In general nestable message loops are to be avoided. They are |
| 310 // dangerous and difficult to get right, so please use with extreme caution. | 318 // dangerous and difficult to get right, so please use with extreme caution. |
| 311 // | 319 // |
| 312 // The specific case where tasks get queued is: | 320 // The specific case where tasks get queued is: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 402 |
| 395 //---------------------------------------------------------------------------- | 403 //---------------------------------------------------------------------------- |
| 396 protected: | 404 protected: |
| 397 scoped_ptr<MessagePump> pump_; | 405 scoped_ptr<MessagePump> pump_; |
| 398 | 406 |
| 399 private: | 407 private: |
| 400 friend class RunLoop; | 408 friend class RunLoop; |
| 401 friend class internal::IncomingTaskQueue; | 409 friend class internal::IncomingTaskQueue; |
| 402 friend class ScheduleWorkTest; | 410 friend class ScheduleWorkTest; |
| 403 friend class Thread; | 411 friend class Thread; |
| 412 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, SwapTaskRunnerBeforeBinding); | |
| 404 | 413 |
| 405 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; | 414 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; |
| 406 | 415 |
| 407 // Creates a MessageLoop without binding to a thread. | 416 // Creates a MessageLoop without binding to a thread. |
| 408 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given | 417 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given |
| 409 // to create a message pump for this message loop. Otherwise a default | 418 // to create a message pump for this message loop. Otherwise a default |
| 410 // message pump for the |type| is created. | 419 // message pump for the |type| is created. |
| 411 // | 420 // |
| 412 // It is valid to call this to create a new message loop on one thread, | 421 // It is valid to call this to create a new message loop on one thread, |
| 413 // and then pass it to the thread where the message loop actually runs. | 422 // and then pass it to the thread where the message loop actually runs. |
| 414 // The message loop's BindToCurrentThread() method must be called on the | 423 // The message loop's BindToCurrentThread() method must be called on the |
| 415 // thread the message loop runs on, before calling Run(). | 424 // thread the message loop runs on, before calling Run(). |
| 416 // Before BindToCurrentThread() is called only Post*Task() functions can | 425 // Before BindToCurrentThread() is called only Post*Task() functions can |
| 417 // be called on the message loop. | 426 // be called on the message loop. |
| 418 scoped_ptr<MessageLoop> CreateUnbound( | 427 static scoped_ptr<MessageLoop> CreateUnbound( |
| 419 Type type, | 428 Type type, |
| 420 MessagePumpFactoryCallback pump_factory); | 429 MessagePumpFactoryCallback pump_factory); |
|
kinuko
2015/06/25 05:58:08
(Aha, how I could miss this's unused)
alexclarke
2015/06/25 09:36:51
Are you intending to use this, or would you like u
kinuko
2015/06/25 14:31:12
I was intending to use this at least when I added,
| |
| 421 | 430 |
| 422 // Common private constructor. Other constructors delegate the initialization | 431 // Common private constructor. Other constructors delegate the initialization |
| 423 // to this constructor. | 432 // to this constructor. |
| 424 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); | 433 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
| 425 | 434 |
| 426 // Configure various members and bind this message loop to the current thread. | 435 // Configure various members and bind this message loop to the current thread. |
| 427 void BindToCurrentThread(); | 436 void BindToCurrentThread(); |
| 428 | 437 |
| 429 // Invokes the actual run loop using the message pump. | 438 // Invokes the actual run loop using the message pump. |
| 430 void RunHandler(); | 439 void RunHandler(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 | 524 |
| 516 RunLoop* run_loop_; | 525 RunLoop* run_loop_; |
| 517 | 526 |
| 518 ObserverList<TaskObserver> task_observers_; | 527 ObserverList<TaskObserver> task_observers_; |
| 519 | 528 |
| 520 debug::TaskAnnotator task_annotator_; | 529 debug::TaskAnnotator task_annotator_; |
| 521 | 530 |
| 522 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; | 531 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
| 523 | 532 |
| 524 // The task runner associated with this message loop. | 533 // The task runner associated with this message loop. |
| 525 scoped_refptr<internal::MessageLoopTaskRunner> task_runner_; | 534 scoped_refptr<BindableSingleThreadTaskRunner> task_runner_; |
| 526 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 535 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 527 | 536 |
| 528 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | 537 template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
| 529 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | 538 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
| 530 | 539 |
| 531 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 540 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 532 void(*deleter)(const void*), | 541 void(*deleter)(const void*), |
| 533 const void* object); | 542 const void* object); |
| 534 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | 543 void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 535 void(*releaser)(const void*), | 544 void(*releaser)(const void*), |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 673 | 682 |
| 674 // Do not add any member variables to MessageLoopForIO! This is important b/c | 683 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 675 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 684 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 676 // data that you need should be stored on the MessageLoop's pump_ instance. | 685 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 677 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 686 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 678 MessageLoopForIO_should_not_have_extra_member_variables); | 687 MessageLoopForIO_should_not_have_extra_member_variables); |
| 679 | 688 |
| 680 } // namespace base | 689 } // namespace base |
| 681 | 690 |
| 682 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 691 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |