| 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 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 #elif defined(OS_POSIX) | 93 #elif defined(OS_POSIX) |
| 94 typedef MessagePumpLibevent MessagePumpForIO; | 94 typedef MessagePumpLibevent MessagePumpForIO; |
| 95 #endif | 95 #endif |
| 96 | 96 |
| 97 #if !defined(OS_NACL_SFI) | 97 #if !defined(OS_NACL_SFI) |
| 98 MessagePumpForIO* ToPumpIO(MessagePump* pump) { | 98 MessagePumpForIO* ToPumpIO(MessagePump* pump) { |
| 99 return static_cast<MessagePumpForIO*>(pump); | 99 return static_cast<MessagePumpForIO*>(pump); |
| 100 } | 100 } |
| 101 #endif // !defined(OS_NACL_SFI) | 101 #endif // !defined(OS_NACL_SFI) |
| 102 | 102 |
| 103 scoped_ptr<MessagePump> ReturnPump(scoped_ptr<MessagePump> pump) { |
| 104 return pump; |
| 105 } |
| 106 |
| 103 } // namespace | 107 } // namespace |
| 104 | 108 |
| 105 //------------------------------------------------------------------------------ | 109 //------------------------------------------------------------------------------ |
| 106 | 110 |
| 107 MessageLoop::TaskObserver::TaskObserver() { | 111 MessageLoop::TaskObserver::TaskObserver() { |
| 108 } | 112 } |
| 109 | 113 |
| 110 MessageLoop::TaskObserver::~TaskObserver() { | 114 MessageLoop::TaskObserver::~TaskObserver() { |
| 111 } | 115 } |
| 112 | 116 |
| 113 MessageLoop::DestructionObserver::~DestructionObserver() { | 117 MessageLoop::DestructionObserver::~DestructionObserver() { |
| 114 } | 118 } |
| 115 | 119 |
| 116 //------------------------------------------------------------------------------ | 120 //------------------------------------------------------------------------------ |
| 117 | 121 |
| 118 MessageLoop::MessageLoop(Type type) | 122 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
| 119 : type_(type), | 123 : type_(type), |
| 120 #if defined(OS_WIN) | 124 #if defined(OS_WIN) |
| 121 pending_high_res_tasks_(0), | 125 pending_high_res_tasks_(0), |
| 122 in_high_res_mode_(false), | 126 in_high_res_mode_(false), |
| 123 #endif | 127 #endif |
| 124 nestable_tasks_allowed_(true), | 128 nestable_tasks_allowed_(true), |
| 125 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
| 126 os_modal_loop_(false), | 130 os_modal_loop_(false), |
| 127 #endif // OS_WIN | 131 #endif // OS_WIN |
| 132 pump_factory_(pump_factory), |
| 128 message_histogram_(NULL), | 133 message_histogram_(NULL), |
| 129 run_loop_(NULL) { | 134 run_loop_(NULL), |
| 130 Init(); | 135 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
| 136 message_loop_proxy_( |
| 137 new internal::MessageLoopProxyImpl(incoming_task_queue_)) { |
| 138 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
| 139 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); |
| 140 } |
| 131 | 141 |
| 132 pump_ = CreateMessagePumpForType(type).Pass(); | 142 MessageLoop::MessageLoop(Type type) |
| 143 : MessageLoop(type, MessagePumpFactoryCallback()) { |
| 144 BindToCurrentThread(); |
| 133 } | 145 } |
| 134 | 146 |
| 135 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) | 147 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) |
| 136 : pump_(pump.Pass()), | 148 : MessageLoop(TYPE_CUSTOM, Bind(&ReturnPump, Passed(&pump))) { |
| 137 type_(TYPE_CUSTOM), | 149 BindToCurrentThread(); |
| 138 #if defined(OS_WIN) | |
| 139 pending_high_res_tasks_(0), | |
| 140 in_high_res_mode_(false), | |
| 141 #endif | |
| 142 nestable_tasks_allowed_(true), | |
| 143 #if defined(OS_WIN) | |
| 144 os_modal_loop_(false), | |
| 145 #endif // OS_WIN | |
| 146 message_histogram_(NULL), | |
| 147 run_loop_(NULL) { | |
| 148 DCHECK(pump_.get()); | |
| 149 Init(); | |
| 150 } | 150 } |
| 151 | 151 |
| 152 MessageLoop::~MessageLoop() { | 152 MessageLoop::~MessageLoop() { |
| 153 DCHECK_EQ(this, current()); | 153 // current() could be NULL if this message loop is destructed before it is |
| 154 // bound to a thread. |
| 155 DCHECK(current() == this || !current()); |
| 154 | 156 |
| 155 // iOS just attaches to the loop, it doesn't Run it. | 157 // iOS just attaches to the loop, it doesn't Run it. |
| 156 // TODO(stuartmorgan): Consider wiring up a Detach(). | 158 // TODO(stuartmorgan): Consider wiring up a Detach(). |
| 157 #if !defined(OS_IOS) | 159 #if !defined(OS_IOS) |
| 158 DCHECK(!run_loop_); | 160 DCHECK(!run_loop_); |
| 159 #endif | 161 #endif |
| 160 | 162 |
| 161 #if defined(OS_WIN) | 163 #if defined(OS_WIN) |
| 162 if (in_high_res_mode_) | 164 if (in_high_res_mode_) |
| 163 Time::ActivateHighResolutionTimer(false); | 165 Time::ActivateHighResolutionTimer(false); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 255 |
| 254 #if defined(OS_ANDROID) | 256 #if defined(OS_ANDROID) |
| 255 if (type == MessageLoop::TYPE_JAVA) | 257 if (type == MessageLoop::TYPE_JAVA) |
| 256 return scoped_ptr<MessagePump>(new MessagePumpForUI()); | 258 return scoped_ptr<MessagePump>(new MessagePumpForUI()); |
| 257 #endif | 259 #endif |
| 258 | 260 |
| 259 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); | 261 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); |
| 260 return MESSAGE_PUMP_DEFAULT; | 262 return MESSAGE_PUMP_DEFAULT; |
| 261 } | 263 } |
| 262 | 264 |
| 265 void MessageLoop::BindToCurrentThread() { |
| 266 DCHECK(!pump_); |
| 267 if (!pump_factory_.is_null()) |
| 268 pump_ = pump_factory_.Run(); |
| 269 else |
| 270 pump_ = CreateMessagePumpForType(type_); |
| 271 |
| 272 DCHECK(!current()) << "should only have one message loop per thread"; |
| 273 lazy_tls_ptr.Pointer()->Set(this); |
| 274 |
| 275 incoming_task_queue_->StartScheduling(); |
| 276 message_loop_proxy_->BindToCurrentThread(); |
| 277 thread_task_runner_handle_.reset( |
| 278 new ThreadTaskRunnerHandle(message_loop_proxy_)); |
| 279 } |
| 280 |
| 263 void MessageLoop::AddDestructionObserver( | 281 void MessageLoop::AddDestructionObserver( |
| 264 DestructionObserver* destruction_observer) { | 282 DestructionObserver* destruction_observer) { |
| 265 DCHECK_EQ(this, current()); | 283 DCHECK_EQ(this, current()); |
| 266 destruction_observers_.AddObserver(destruction_observer); | 284 destruction_observers_.AddObserver(destruction_observer); |
| 267 } | 285 } |
| 268 | 286 |
| 269 void MessageLoop::RemoveDestructionObserver( | 287 void MessageLoop::RemoveDestructionObserver( |
| 270 DestructionObserver* destruction_observer) { | 288 DestructionObserver* destruction_observer) { |
| 271 DCHECK_EQ(this, current()); | 289 DCHECK_EQ(this, current()); |
| 272 destruction_observers_.RemoveObserver(destruction_observer); | 290 destruction_observers_.RemoveObserver(destruction_observer); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 292 } | 310 } |
| 293 | 311 |
| 294 void MessageLoop::PostNonNestableDelayedTask( | 312 void MessageLoop::PostNonNestableDelayedTask( |
| 295 const tracked_objects::Location& from_here, | 313 const tracked_objects::Location& from_here, |
| 296 const Closure& task, | 314 const Closure& task, |
| 297 TimeDelta delay) { | 315 TimeDelta delay) { |
| 298 message_loop_proxy_->PostNonNestableDelayedTask(from_here, task, delay); | 316 message_loop_proxy_->PostNonNestableDelayedTask(from_here, task, delay); |
| 299 } | 317 } |
| 300 | 318 |
| 301 void MessageLoop::Run() { | 319 void MessageLoop::Run() { |
| 320 DCHECK(pump_); |
| 302 RunLoop run_loop; | 321 RunLoop run_loop; |
| 303 run_loop.Run(); | 322 run_loop.Run(); |
| 304 } | 323 } |
| 305 | 324 |
| 306 void MessageLoop::RunUntilIdle() { | 325 void MessageLoop::RunUntilIdle() { |
| 326 DCHECK(pump_); |
| 307 RunLoop run_loop; | 327 RunLoop run_loop; |
| 308 run_loop.RunUntilIdle(); | 328 run_loop.RunUntilIdle(); |
| 309 } | 329 } |
| 310 | 330 |
| 311 void MessageLoop::QuitWhenIdle() { | 331 void MessageLoop::QuitWhenIdle() { |
| 312 DCHECK_EQ(this, current()); | 332 DCHECK_EQ(this, current()); |
| 313 if (run_loop_) { | 333 if (run_loop_) { |
| 314 run_loop_->quit_when_idle_received_ = true; | 334 run_loop_->quit_when_idle_received_ = true; |
| 315 } else { | 335 } else { |
| 316 NOTREACHED() << "Must be inside Run to call Quit"; | 336 NOTREACHED() << "Must be inside Run to call Quit"; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 396 } |
| 377 | 397 |
| 378 bool MessageLoop::IsIdleForTesting() { | 398 bool MessageLoop::IsIdleForTesting() { |
| 379 // We only check the imcoming queue|, since we don't want to lock the work | 399 // We only check the imcoming queue|, since we don't want to lock the work |
| 380 // queue. | 400 // queue. |
| 381 return incoming_task_queue_->IsIdleForTesting(); | 401 return incoming_task_queue_->IsIdleForTesting(); |
| 382 } | 402 } |
| 383 | 403 |
| 384 //------------------------------------------------------------------------------ | 404 //------------------------------------------------------------------------------ |
| 385 | 405 |
| 386 void MessageLoop::Init() { | 406 void MessageLoop::ScheduleWork() { |
| 387 DCHECK(!current()) << "should only have one message loop per thread"; | 407 pump_->ScheduleWork(); |
| 388 lazy_tls_ptr.Pointer()->Set(this); | |
| 389 | |
| 390 incoming_task_queue_ = new internal::IncomingTaskQueue(this); | |
| 391 message_loop_proxy_ = | |
| 392 new internal::MessageLoopProxyImpl(incoming_task_queue_); | |
| 393 thread_task_runner_handle_.reset( | |
| 394 new ThreadTaskRunnerHandle(message_loop_proxy_)); | |
| 395 } | 408 } |
| 396 | 409 |
| 397 void MessageLoop::RunHandler() { | 410 void MessageLoop::RunHandler() { |
| 398 DCHECK_EQ(this, current()); | 411 DCHECK_EQ(this, current()); |
| 399 | 412 |
| 400 StartHistogrammer(); | 413 StartHistogrammer(); |
| 401 | 414 |
| 402 #if defined(OS_WIN) | 415 #if defined(OS_WIN) |
| 403 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 416 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
| 404 static_cast<MessagePumpForUI*>(pump_.get())-> | 417 static_cast<MessagePumpForUI*>(pump_.get())-> |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 if (work_queue_.empty()) { | 518 if (work_queue_.empty()) { |
| 506 #if defined(OS_WIN) | 519 #if defined(OS_WIN) |
| 507 pending_high_res_tasks_ += | 520 pending_high_res_tasks_ += |
| 508 incoming_task_queue_->ReloadWorkQueue(&work_queue_); | 521 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
| 509 #else | 522 #else |
| 510 incoming_task_queue_->ReloadWorkQueue(&work_queue_); | 523 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
| 511 #endif | 524 #endif |
| 512 } | 525 } |
| 513 } | 526 } |
| 514 | 527 |
| 515 void MessageLoop::ScheduleWork() { | |
| 516 pump_->ScheduleWork(); | |
| 517 } | |
| 518 | |
| 519 //------------------------------------------------------------------------------ | 528 //------------------------------------------------------------------------------ |
| 520 // Method and data for histogramming events and actions taken by each instance | 529 // Method and data for histogramming events and actions taken by each instance |
| 521 // on each thread. | 530 // on each thread. |
| 522 | 531 |
| 523 void MessageLoop::StartHistogrammer() { | 532 void MessageLoop::StartHistogrammer() { |
| 524 #if !defined(OS_NACL) // NaCl build has no metrics code. | 533 #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 525 if (enable_histogrammer_ && !message_histogram_ | 534 if (enable_histogrammer_ && !message_histogram_ |
| 526 && StatisticsRecorder::IsActive()) { | 535 && StatisticsRecorder::IsActive()) { |
| 527 DCHECK(!thread_name_.empty()); | 536 DCHECK(!thread_name_.empty()); |
| 528 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( | 537 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 persistent, | 720 persistent, |
| 712 mode, | 721 mode, |
| 713 controller, | 722 controller, |
| 714 delegate); | 723 delegate); |
| 715 } | 724 } |
| 716 #endif | 725 #endif |
| 717 | 726 |
| 718 #endif // !defined(OS_NACL_SFI) | 727 #endif // !defined(OS_NACL_SFI) |
| 719 | 728 |
| 720 } // namespace base | 729 } // namespace base |
| OLD | NEW |