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/location.h" | 14 #include "base/location.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/message_loop/message_loop_proxy.h" | 17 #include "base/message_loop/message_loop_proxy.h" |
| 17 #include "base/message_loop/message_pump.h" | 18 #include "base/message_loop/message_pump.h" |
| 18 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
| 19 #include "base/pending_task.h" | 20 #include "base/pending_task.h" |
| 20 #include "base/sequenced_task_runner_helpers.h" | 21 #include "base/sequenced_task_runner_helpers.h" |
| 21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 22 #include "base/tracking_info.h" | 23 #include "base/tracking_info.h" |
| 23 #include "base/time.h" | 24 #include "base/time.h" |
| 24 | 25 |
| 25 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 40 #include "base/message_loop/message_pump_gtk.h" | 41 #include "base/message_loop/message_pump_gtk.h" |
| 41 #endif | 42 #endif |
| 42 | 43 |
| 43 #endif | 44 #endif |
| 44 #endif | 45 #endif |
| 45 | 46 |
| 46 namespace base { | 47 namespace base { |
| 47 | 48 |
| 48 class HistogramBase; | 49 class HistogramBase; |
| 49 class MessageLoopLockTest; | 50 class MessageLoopLockTest; |
| 51 class MessageLoopProxyImpl; | |
| 50 class RunLoop; | 52 class RunLoop; |
| 51 class ThreadTaskRunnerHandle; | 53 class ThreadTaskRunnerHandle; |
| 52 #if defined(OS_ANDROID) | 54 #if defined(OS_ANDROID) |
| 53 class MessagePumpForUI; | 55 class MessagePumpForUI; |
| 54 #endif | 56 #endif |
| 55 | 57 |
| 56 // A MessageLoop is used to process events for a particular thread. There is | 58 // A MessageLoop is used to process events for a particular thread. There is |
| 57 // at most one MessageLoop instance per thread. | 59 // at most one MessageLoop instance per thread. |
| 58 // | 60 // |
| 59 // Events include at a minimum Task instances submitted to PostTask and its | 61 // Events include at a minimum Task instances submitted to PostTask and its |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 Type type() const { return type_; } | 277 Type type() const { return type_; } |
| 276 | 278 |
| 277 // Optional call to connect the thread name with this loop. | 279 // Optional call to connect the thread name with this loop. |
| 278 void set_thread_name(const std::string& thread_name) { | 280 void set_thread_name(const std::string& thread_name) { |
| 279 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; | 281 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 280 thread_name_ = thread_name; | 282 thread_name_ = thread_name; |
| 281 } | 283 } |
| 282 const std::string& thread_name() const { return thread_name_; } | 284 const std::string& thread_name() const { return thread_name_; } |
| 283 | 285 |
| 284 // Gets the message loop proxy associated with this message loop. | 286 // Gets the message loop proxy associated with this message loop. |
| 285 scoped_refptr<MessageLoopProxy> message_loop_proxy() { | 287 scoped_refptr<MessageLoopProxy> message_loop_proxy(); |
| 286 return message_loop_proxy_.get(); | |
| 287 } | |
| 288 | 288 |
| 289 // Enables or disables the recursive task processing. This happens in the case | 289 // Enables or disables the recursive task processing. This happens in the case |
| 290 // of recursive message loops. Some unwanted message loop may occurs when | 290 // of recursive message loops. Some unwanted message loop may occurs when |
| 291 // using common controls or printer functions. By default, recursive task | 291 // using common controls or printer functions. By default, recursive task |
| 292 // processing is disabled. | 292 // processing is disabled. |
| 293 // | 293 // |
| 294 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods | 294 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods |
| 295 // directly. In general nestable message loops are to be avoided. They are | 295 // directly. In general nestable message loops are to be avoided. They are |
| 296 // dangerous and difficult to get right, so please use with extreme caution. | 296 // dangerous and difficult to get right, so please use with extreme caution. |
| 297 // | 297 // |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 #if defined(OS_WIN) | 395 #if defined(OS_WIN) |
| 396 MessagePumpWin* pump_win() { | 396 MessagePumpWin* pump_win() { |
| 397 return static_cast<MessagePumpWin*>(pump_.get()); | 397 return static_cast<MessagePumpWin*>(pump_.get()); |
| 398 } | 398 } |
| 399 #elif defined(OS_POSIX) && !defined(OS_IOS) | 399 #elif defined(OS_POSIX) && !defined(OS_IOS) |
| 400 MessagePumpLibevent* pump_libevent() { | 400 MessagePumpLibevent* pump_libevent() { |
| 401 return static_cast<MessagePumpLibevent*>(pump_.get()); | 401 return static_cast<MessagePumpLibevent*>(pump_.get()); |
| 402 } | 402 } |
| 403 #endif | 403 #endif |
| 404 | 404 |
| 405 scoped_refptr<MessagePump> pump_; | 405 scoped_ptr<MessagePump> pump_; |
| 406 | 406 |
| 407 private: | 407 private: |
| 408 friend class RunLoop; | 408 friend class RunLoop; |
| 409 friend class MessageLoopLockTest; | 409 friend class MessageLoopLockTest; |
| 410 friend class MessageLoopProxyImpl; | |
| 410 | 411 |
| 411 // A function to encapsulate all the exception handling capability in the | 412 // A function to encapsulate all the exception handling capability in the |
| 412 // stacks around the running of a main message loop. It will run the message | 413 // stacks around the running of a main message loop. It will run the message |
| 413 // loop in a SEH try block or not depending on the set_SEH_restoration() | 414 // loop in a SEH try block or not depending on the set_SEH_restoration() |
| 414 // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). | 415 // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). |
| 415 void RunHandler(); | 416 void RunHandler(); |
| 416 | 417 |
| 417 #if defined(OS_WIN) | 418 #if defined(OS_WIN) |
| 418 __declspec(noinline) void RunInternalInSEHFrame(); | 419 __declspec(noinline) void RunInternalInSEHFrame(); |
| 419 #endif | 420 #endif |
| 420 | 421 |
| 421 // A surrounding stack frame around the running of the message loop that | 422 // A surrounding stack frame around the running of the message loop that |
| 422 // supports all saving and restoring of state, as is needed for any/all (ugly) | 423 // supports all saving and restoring of state, as is needed for any/all (ugly) |
| 423 // recursive calls. | 424 // recursive calls. |
| 424 void RunInternal(); | 425 void RunInternal(); |
| 425 | 426 |
| 426 // Called to process any delayed non-nestable tasks. | 427 // Called to process any delayed non-nestable tasks. |
| 427 bool ProcessNextDelayedNonNestableTask(); | 428 bool ProcessNextDelayedNonNestableTask(); |
| 428 | 429 |
| 429 // Runs the specified PendingTask. | 430 // Runs the specified PendingTask. |
| 430 void RunTask(const PendingTask& pending_task); | 431 void RunTask(const PendingTask& pending_task); |
| 431 | 432 |
| 432 // Calls RunTask or queues the pending_task on the deferred task list if it | 433 // Calls RunTask or queues the pending_task on the deferred task list if it |
| 433 // cannot be run right now. Returns true if the task was run. | 434 // cannot be run right now. Returns true if the task was run. |
| 434 bool DeferOrRunPendingTask(const PendingTask& pending_task); | 435 bool DeferOrRunPendingTask(const PendingTask& pending_task); |
| 435 | 436 |
| 436 // Adds the pending task to delayed_work_queue_. | 437 // Adds the pending task to delayed_work_queue_. |
| 437 void AddToDelayedWorkQueue(const PendingTask& pending_task); | 438 void AddToDelayedWorkQueue(const PendingTask& pending_task); |
| 438 | 439 |
| 439 // This function attempts to add pending task to our incoming_queue_. | 440 // Adds a task to our incoming_queue_. This method can be called on any thread |
| 440 // The append can only possibly fail when |use_try_lock| is true. | 441 // and the caller is responsible for synchronizing calls to this method. |
| 442 // (Currently |message_loop_proxy_->message_loop_lock_| is used). | |
| 441 // | 443 // |
| 442 // When |use_try_lock| is true, then this call will avoid blocking if | 444 // The method takes ownership of |task|. |
| 443 // the related lock is already held, and will in that case (when the | 445 void AddToIncomingQueue(const tracked_objects::Location& from_here, |
| 444 // lock is contended) fail to perform the append, and will return false. | 446 const Closure& task, |
| 445 // | 447 TimeDelta delay, |
| 446 // If the call succeeds to append to the queue, then this call | 448 bool nestable); |
| 447 // will return true. | |
| 448 // | |
| 449 // In all cases, the caller retains ownership of |pending_task|, but this | |
| 450 // function will reset the value of pending_task->task. This is needed to | |
| 451 // ensure that the posting call stack does not retain pending_task->task | |
| 452 // beyond this function call. | |
| 453 bool AddToIncomingQueue(PendingTask* pending_task, bool use_try_lock); | |
| 454 | 449 |
| 455 // Load tasks from the incoming_queue_ into work_queue_ if the latter is | 450 // Load tasks from the incoming_queue_ into work_queue_ if the latter is |
| 456 // empty. The former requires a lock to access, while the latter is directly | 451 // empty. The former requires a lock to access, while the latter is directly |
| 457 // accessible on this thread. | 452 // accessible on this thread. |
| 458 void ReloadWorkQueue(); | 453 void ReloadWorkQueue(); |
| 459 | 454 |
| 460 // Delete tasks that haven't run yet without running them. Used in the | 455 // Delete tasks that haven't run yet without running them. Used in the |
| 461 // destructor to make sure all the task's destructors get called. Returns | 456 // destructor to make sure all the task's destructors get called. Returns |
| 462 // true if some work was done. | 457 // true if some work was done. |
| 463 bool DeletePendingTasks(); | 458 bool DeletePendingTasks(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 503 bool nestable_tasks_allowed_; | 498 bool nestable_tasks_allowed_; |
| 504 | 499 |
| 505 bool exception_restoration_; | 500 bool exception_restoration_; |
| 506 | 501 |
| 507 std::string thread_name_; | 502 std::string thread_name_; |
| 508 // A profiling histogram showing the counts of various messages and events. | 503 // A profiling histogram showing the counts of various messages and events. |
| 509 HistogramBase* message_histogram_; | 504 HistogramBase* message_histogram_; |
| 510 | 505 |
| 511 // An incoming queue of tasks that are acquired under a mutex for processing | 506 // An incoming queue of tasks that are acquired under a mutex for processing |
| 512 // on this instance's thread. These tasks have not yet been sorted out into | 507 // on this instance's thread. These tasks have not yet been sorted out into |
| 513 // items for our work_queue_ vs delayed_work_queue_. | 508 // items for our work_queue_ vs delayed_work_queue_. Protected by |
| 509 // |message_loop_proxy_->message_loop_lock_|. | |
|
rvargas (doing something else)
2013/06/28 03:00:51
I'm not really comfortable with this pattern of pr
alexeypa (please no reviews)
2013/06/28 17:00:57
Done.
| |
| 514 TaskQueue incoming_queue_; | 510 TaskQueue incoming_queue_; |
| 515 // Protect access to incoming_queue_. | |
| 516 mutable Lock incoming_queue_lock_; | |
| 517 | 511 |
| 518 RunLoop* run_loop_; | 512 RunLoop* run_loop_; |
| 519 | 513 |
| 520 #if defined(OS_WIN) | 514 #if defined(OS_WIN) |
| 521 TimeTicks high_resolution_timer_expiration_; | 515 TimeTicks high_resolution_timer_expiration_; |
| 522 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc | 516 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc |
| 523 // which enter a modal message loop. | 517 // which enter a modal message loop. |
| 524 bool os_modal_loop_; | 518 bool os_modal_loop_; |
| 525 #endif | 519 #endif |
| 526 | 520 |
| 527 // The next sequence number to use for delayed tasks. Updating this counter is | 521 // The next sequence number to use for delayed tasks. Updating this counter is |
| 528 // protected by incoming_queue_lock_. | 522 // protected by |message_loop_proxy_->message_loop_lock_|. |
| 529 int next_sequence_num_; | 523 int next_sequence_num_; |
| 530 | 524 |
| 531 ObserverList<TaskObserver> task_observers_; | 525 ObserverList<TaskObserver> task_observers_; |
| 532 | 526 |
| 533 // The message loop proxy associated with this message loop, if one exists. | 527 // The message loop proxy associated with this message loop. |
| 534 scoped_refptr<MessageLoopProxy> message_loop_proxy_; | 528 scoped_refptr<MessageLoopProxyImpl> message_loop_proxy_; |
| 535 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 529 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 536 | 530 |
| 537 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | 531 template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
| 538 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | 532 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
| 539 | 533 |
| 540 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 534 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 541 void(*deleter)(const void*), | 535 void(*deleter)(const void*), |
| 542 const void* object); | 536 const void* object); |
| 543 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | 537 void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 544 void(*releaser)(const void*), | 538 void(*releaser)(const void*), |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 720 | 714 |
| 721 // Do not add any member variables to MessageLoopForIO! This is important b/c | 715 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 722 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 716 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 723 // data that you need should be stored on the MessageLoop's pump_ instance. | 717 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 724 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 718 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 725 MessageLoopForIO_should_not_have_extra_member_variables); | 719 MessageLoopForIO_should_not_have_extra_member_variables); |
| 726 | 720 |
| 727 } // namespace base | 721 } // namespace base |
| 728 | 722 |
| 729 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 723 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |