Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 | 128 |
| 129 MessageLoop::MessageLoop(Type type) | 129 MessageLoop::MessageLoop(Type type) |
| 130 : type_(type), | 130 : type_(type), |
| 131 nestable_tasks_allowed_(true), | 131 nestable_tasks_allowed_(true), |
| 132 exception_restoration_(false), | 132 exception_restoration_(false), |
| 133 message_histogram_(NULL), | 133 message_histogram_(NULL), |
| 134 state_(NULL), | 134 state_(NULL), |
| 135 #ifdef OS_WIN | 135 #ifdef OS_WIN |
| 136 os_modal_loop_(false), | 136 os_modal_loop_(false), |
| 137 #endif // OS_WIN | 137 #endif // OS_WIN |
| 138 next_sequence_num_(0) { | 138 next_sequence_num_(0), |
| 139 current_run_state_id_(0) { | |
| 139 DCHECK(!current()) << "should only have one message loop per thread"; | 140 DCHECK(!current()) << "should only have one message loop per thread"; |
| 140 lazy_tls_ptr.Pointer()->Set(this); | 141 lazy_tls_ptr.Pointer()->Set(this); |
| 141 | 142 |
| 142 message_loop_proxy_ = new base::MessageLoopProxyImpl(); | 143 message_loop_proxy_ = new base::MessageLoopProxyImpl(); |
| 143 thread_task_runner_handle_.reset( | 144 thread_task_runner_handle_.reset( |
| 144 new base::ThreadTaskRunnerHandle(message_loop_proxy_)); | 145 new base::ThreadTaskRunnerHandle(message_loop_proxy_)); |
| 145 | 146 |
| 146 // TODO(rvargas): Get rid of the OS guards. | 147 // TODO(rvargas): Get rid of the OS guards. |
| 147 #if defined(OS_WIN) | 148 #if defined(OS_WIN) |
| 148 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() | 149 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 AutoRunState save_state(this); | 307 AutoRunState save_state(this); |
| 307 RunHandler(); | 308 RunHandler(); |
| 308 } | 309 } |
| 309 | 310 |
| 310 void MessageLoop::RunAllPending() { | 311 void MessageLoop::RunAllPending() { |
| 311 AutoRunState save_state(this); | 312 AutoRunState save_state(this); |
| 312 state_->quit_received = true; // Means run until we would otherwise block. | 313 state_->quit_received = true; // Means run until we would otherwise block. |
| 313 RunHandler(); | 314 RunHandler(); |
| 314 } | 315 } |
| 315 | 316 |
| 316 void MessageLoop::Quit() { | 317 void MessageLoop::QuitWhenIdle() { |
| 317 DCHECK_EQ(this, current()); | 318 DCHECK_EQ(this, current()); |
| 318 if (state_) { | 319 if (state_) { |
| 319 state_->quit_received = true; | 320 state_->quit_received = true; |
| 320 } else { | 321 } else { |
| 321 NOTREACHED() << "Must be inside Run to call Quit"; | 322 NOTREACHED() << "Must be inside Run to call Quit"; |
| 322 } | 323 } |
| 323 } | 324 } |
| 324 | 325 |
| 325 void MessageLoop::QuitNow() { | 326 void MessageLoop::QuitNow() { |
| 326 DCHECK_EQ(this, current()); | 327 DCHECK_EQ(this, current()); |
| 327 if (state_) { | 328 if (state_) { |
| 328 pump_->Quit(); | 329 pump_->Quit(); |
| 329 } else { | 330 } else { |
| 330 NOTREACHED() << "Must be inside Run to call Quit"; | 331 NOTREACHED() << "Must be inside Run to call Quit"; |
| 331 } | 332 } |
| 332 } | 333 } |
| 333 | 334 |
| 334 static void QuitCurrent() { | 335 void MessageLoop::QuitNowByID(int run_id) { |
| 335 MessageLoop::current()->Quit(); | 336 DCHECK_EQ(this, current()); |
| 337 if (!state_) | |
| 338 return; | |
| 339 if (state_->run_state_id == run_id) | |
| 340 QuitNow(); | |
| 341 | |
| 342 // Search back through RunStates for run_id. | |
| 343 RunState* run_state = state_; | |
| 344 while (run_state) { | |
| 345 if (run_state->run_state_id == run_id) { | |
| 346 run_state->quit_now_received = true; | |
| 347 break; | |
| 348 } | |
| 349 run_state = run_state->previous_state; | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 static void QuitWhenIdleCurrent() { | |
| 354 MessageLoop::current()->QuitWhenIdle(); | |
| 355 } | |
| 356 | |
| 357 static void QuitNowCurrent() { | |
| 358 MessageLoop::current()->QuitNow(); | |
| 359 } | |
| 360 | |
| 361 static void QuitNowByIDCurrent(int run_id) { | |
|
jar (doing other things)
2012/06/14 04:50:29
nit: I think the word "current" is in the wrong pl
jbates
2012/06/15 02:20:38
Done.
| |
| 362 MessageLoop::current()->QuitNowByID(run_id); | |
| 336 } | 363 } |
| 337 | 364 |
| 338 // static | 365 // static |
| 339 base::Closure MessageLoop::QuitClosure() { | 366 base::Closure MessageLoop::QuitWhenIdleClosure() { |
| 340 return base::Bind(&QuitCurrent); | 367 return base::Bind(&QuitWhenIdleCurrent); |
| 368 } | |
| 369 | |
| 370 // static | |
| 371 base::Closure MessageLoop::QuitNowClosure() { | |
| 372 return base::Bind(&QuitNowCurrent); | |
| 373 } | |
| 374 | |
| 375 // static | |
| 376 base::Closure MessageLoop::QuitNowByIDClosure(int run_id) { | |
| 377 return base::Bind(&QuitNowByIDCurrent, run_id); | |
| 341 } | 378 } |
| 342 | 379 |
| 343 void MessageLoop::SetNestableTasksAllowed(bool allowed) { | 380 void MessageLoop::SetNestableTasksAllowed(bool allowed) { |
| 344 if (nestable_tasks_allowed_ != allowed) { | 381 if (nestable_tasks_allowed_ != allowed) { |
| 345 nestable_tasks_allowed_ = allowed; | 382 nestable_tasks_allowed_ = allowed; |
| 346 if (!nestable_tasks_allowed_) | 383 if (!nestable_tasks_allowed_) |
| 347 return; | 384 return; |
| 348 // Start the native pump if we are not already pumping. | 385 // Start the native pump if we are not already pumping. |
| 349 pump_->ScheduleWork(); | 386 pump_->ScheduleWork(); |
| 350 } | 387 } |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 713 void(*releaser)(const void*), | 750 void(*releaser)(const void*), |
| 714 const void* object) { | 751 const void* object) { |
| 715 PostNonNestableTask(from_here, base::Bind(releaser, object)); | 752 PostNonNestableTask(from_here, base::Bind(releaser, object)); |
| 716 } | 753 } |
| 717 | 754 |
| 718 //------------------------------------------------------------------------------ | 755 //------------------------------------------------------------------------------ |
| 719 // MessageLoop::AutoRunState | 756 // MessageLoop::AutoRunState |
| 720 | 757 |
| 721 MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) { | 758 MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) { |
| 722 // Make the loop reference us. | 759 // Make the loop reference us. |
| 723 previous_state_ = loop_->state_; | 760 previous_state = loop_->state_; |
| 724 if (previous_state_) { | 761 if (previous_state) { |
| 725 run_depth = previous_state_->run_depth + 1; | 762 run_depth = previous_state->run_depth + 1; |
| 726 } else { | 763 } else { |
| 727 run_depth = 1; | 764 run_depth = 1; |
| 728 } | 765 } |
| 729 loop_->state_ = this; | 766 loop_->state_ = this; |
| 730 | 767 |
| 768 run_state_id = ++loop_->current_run_state_id_; | |
| 769 | |
| 731 // Initialize the other fields: | 770 // Initialize the other fields: |
| 732 quit_received = false; | 771 quit_received = false; |
| 772 quit_now_received = false; | |
| 733 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 773 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 734 dispatcher = NULL; | 774 dispatcher = NULL; |
| 735 #endif | 775 #endif |
| 736 } | 776 } |
| 737 | 777 |
| 738 MessageLoop::AutoRunState::~AutoRunState() { | 778 MessageLoop::AutoRunState::~AutoRunState() { |
| 739 loop_->state_ = previous_state_; | 779 loop_->state_ = previous_state; |
| 780 if (previous_state && previous_state->quit_now_received) | |
| 781 MessageLoop::current()->QuitNow(); | |
| 740 } | 782 } |
| 741 | 783 |
| 742 //------------------------------------------------------------------------------ | 784 //------------------------------------------------------------------------------ |
| 743 // MessageLoopForUI | 785 // MessageLoopForUI |
| 744 | 786 |
| 745 #if defined(OS_WIN) | 787 #if defined(OS_WIN) |
| 746 void MessageLoopForUI::DidProcessMessage(const MSG& message) { | 788 void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 747 pump_win()->DidProcessMessage(message); | 789 pump_win()->DidProcessMessage(message); |
| 748 } | 790 } |
| 749 #endif // defined(OS_WIN) | 791 #endif // defined(OS_WIN) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 801 Watcher *delegate) { | 843 Watcher *delegate) { |
| 802 return pump_libevent()->WatchFileDescriptor( | 844 return pump_libevent()->WatchFileDescriptor( |
| 803 fd, | 845 fd, |
| 804 persistent, | 846 persistent, |
| 805 static_cast<base::MessagePumpLibevent::Mode>(mode), | 847 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 806 controller, | 848 controller, |
| 807 delegate); | 849 delegate); |
| 808 } | 850 } |
| 809 | 851 |
| 810 #endif | 852 #endif |
| OLD | NEW |