| 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 | 301 scoped_refptr<SingleThreadTaskRunner> task_runner() { |
| 300 // once the internal type matches what is being returned (crbug.com/465354). | 302 return task_runner_; |
| 301 scoped_refptr<SingleThreadTaskRunner> task_runner() { return task_runner_; } | 303 } |
| 304 |
| 305 // Sets a new TaskRunner for this message loop and returns the previous one. |
| 306 // If the message loop was already bound to a thread, this function must be |
| 307 // called on that thread and the task runner must also belong to that thread. |
| 308 // Note that changing the task runner will also affect the |
| 309 // ThreadTaskRunnerHandle for the target thread. |
| 310 scoped_refptr<BindableSingleThreadTaskRunner> SwapTaskRunner( |
| 311 scoped_refptr<BindableSingleThreadTaskRunner> task_runner); |
| 302 | 312 |
| 303 // Enables or disables the recursive task processing. This happens in the case | 313 // Enables or disables the recursive task processing. This happens in the case |
| 304 // of recursive message loops. Some unwanted message loop may occurs when | 314 // of recursive message loops. Some unwanted message loop may occurs when |
| 305 // using common controls or printer functions. By default, recursive task | 315 // using common controls or printer functions. By default, recursive task |
| 306 // processing is disabled. | 316 // processing is disabled. |
| 307 // | 317 // |
| 308 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods | 318 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods |
| 309 // directly. In general nestable message loops are to be avoided. They are | 319 // 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. | 320 // dangerous and difficult to get right, so please use with extreme caution. |
| 311 // | 321 // |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 | 404 |
| 395 //---------------------------------------------------------------------------- | 405 //---------------------------------------------------------------------------- |
| 396 protected: | 406 protected: |
| 397 scoped_ptr<MessagePump> pump_; | 407 scoped_ptr<MessagePump> pump_; |
| 398 | 408 |
| 399 private: | 409 private: |
| 400 friend class RunLoop; | 410 friend class RunLoop; |
| 401 friend class internal::IncomingTaskQueue; | 411 friend class internal::IncomingTaskQueue; |
| 402 friend class ScheduleWorkTest; | 412 friend class ScheduleWorkTest; |
| 403 friend class Thread; | 413 friend class Thread; |
| 414 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, SwapTaskRunnerBeforeBinding); |
| 404 | 415 |
| 405 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; | 416 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; |
| 406 | 417 |
| 407 // Creates a MessageLoop without binding to a thread. | 418 // Creates a MessageLoop without binding to a thread. |
| 408 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given | 419 // 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 | 420 // to create a message pump for this message loop. Otherwise a default |
| 410 // message pump for the |type| is created. | 421 // message pump for the |type| is created. |
| 411 // | 422 // |
| 412 // It is valid to call this to create a new message loop on one thread, | 423 // 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. | 424 // and then pass it to the thread where the message loop actually runs. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 | 527 |
| 517 RunLoop* run_loop_; | 528 RunLoop* run_loop_; |
| 518 | 529 |
| 519 ObserverList<TaskObserver> task_observers_; | 530 ObserverList<TaskObserver> task_observers_; |
| 520 | 531 |
| 521 debug::TaskAnnotator task_annotator_; | 532 debug::TaskAnnotator task_annotator_; |
| 522 | 533 |
| 523 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; | 534 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
| 524 | 535 |
| 525 // The task runner associated with this message loop. | 536 // The task runner associated with this message loop. |
| 526 scoped_refptr<internal::MessageLoopTaskRunner> task_runner_; | 537 scoped_refptr<BindableSingleThreadTaskRunner> task_runner_; |
| 527 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 538 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 528 | 539 |
| 529 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | 540 template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
| 530 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | 541 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
| 531 | 542 |
| 532 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 543 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 533 void(*deleter)(const void*), | 544 void(*deleter)(const void*), |
| 534 const void* object); | 545 const void* object); |
| 535 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | 546 void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 536 void(*releaser)(const void*), | 547 void(*releaser)(const void*), |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 | 685 |
| 675 // Do not add any member variables to MessageLoopForIO! This is important b/c | 686 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 676 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 687 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 677 // data that you need should be stored on the MessageLoop's pump_ instance. | 688 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 678 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 689 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 679 MessageLoopForIO_should_not_have_extra_member_variables); | 690 MessageLoopForIO_should_not_have_extra_member_variables); |
| 680 | 691 |
| 681 } // namespace base | 692 } // namespace base |
| 682 | 693 |
| 683 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 694 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |