| 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" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_pump_default.h" | 14 #include "base/message_loop/message_pump_default.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 18 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 19 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/threading/thread_local.h" | 20 #include "base/threading/thread_local.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "base/trace_event/trace_event.h" |
| 22 #include "base/tracked_objects.h" | 23 #include "base/tracked_objects.h" |
| 23 | 24 |
| 24 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
| 25 #include "base/message_loop/message_pump_mac.h" | 26 #include "base/message_loop/message_pump_mac.h" |
| 26 #endif | 27 #endif |
| 27 #if defined(OS_POSIX) && !defined(OS_IOS) | 28 #if defined(OS_POSIX) && !defined(OS_IOS) |
| 28 #include "base/message_loop/message_pump_libevent.h" | 29 #include "base/message_loop/message_pump_libevent.h" |
| 29 #endif | 30 #endif |
| 30 #if defined(OS_ANDROID) | 31 #if defined(OS_ANDROID) |
| 31 #include "base/message_loop/message_pump_android.h" | 32 #include "base/message_loop/message_pump_android.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 164 |
| 164 // Let interested parties have one last shot at accessing this. | 165 // Let interested parties have one last shot at accessing this. |
| 165 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | 166 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
| 166 WillDestroyCurrentMessageLoop()); | 167 WillDestroyCurrentMessageLoop()); |
| 167 | 168 |
| 168 thread_task_runner_handle_.reset(); | 169 thread_task_runner_handle_.reset(); |
| 169 | 170 |
| 170 // Tell the incoming queue that we are dying. | 171 // Tell the incoming queue that we are dying. |
| 171 incoming_task_queue_->WillDestroyCurrentMessageLoop(); | 172 incoming_task_queue_->WillDestroyCurrentMessageLoop(); |
| 172 incoming_task_queue_ = NULL; | 173 incoming_task_queue_ = NULL; |
| 173 message_loop_proxy_ = NULL; | 174 unbound_task_runner_ = NULL; |
| 175 task_runner_ = NULL; |
| 174 | 176 |
| 175 // OK, now make it so that no one can find us. | 177 // OK, now make it so that no one can find us. |
| 176 lazy_tls_ptr.Pointer()->Set(NULL); | 178 lazy_tls_ptr.Pointer()->Set(NULL); |
| 177 } | 179 } |
| 178 | 180 |
| 179 // static | 181 // static |
| 180 MessageLoop* MessageLoop::current() { | 182 MessageLoop* MessageLoop::current() { |
| 181 // TODO(darin): sadly, we cannot enable this yet since people call us even | 183 // TODO(darin): sadly, we cannot enable this yet since people call us even |
| 182 // when they have no intention of using us. | 184 // when they have no intention of using us. |
| 183 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 185 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 252 |
| 251 void MessageLoop::RemoveDestructionObserver( | 253 void MessageLoop::RemoveDestructionObserver( |
| 252 DestructionObserver* destruction_observer) { | 254 DestructionObserver* destruction_observer) { |
| 253 DCHECK_EQ(this, current()); | 255 DCHECK_EQ(this, current()); |
| 254 destruction_observers_.RemoveObserver(destruction_observer); | 256 destruction_observers_.RemoveObserver(destruction_observer); |
| 255 } | 257 } |
| 256 | 258 |
| 257 void MessageLoop::PostTask( | 259 void MessageLoop::PostTask( |
| 258 const tracked_objects::Location& from_here, | 260 const tracked_objects::Location& from_here, |
| 259 const Closure& task) { | 261 const Closure& task) { |
| 260 message_loop_proxy_->PostTask(from_here, task); | 262 task_runner_->PostTask(from_here, task); |
| 261 } | 263 } |
| 262 | 264 |
| 263 void MessageLoop::PostDelayedTask( | 265 void MessageLoop::PostDelayedTask( |
| 264 const tracked_objects::Location& from_here, | 266 const tracked_objects::Location& from_here, |
| 265 const Closure& task, | 267 const Closure& task, |
| 266 TimeDelta delay) { | 268 TimeDelta delay) { |
| 267 message_loop_proxy_->PostDelayedTask(from_here, task, delay); | 269 task_runner_->PostDelayedTask(from_here, task, delay); |
| 268 } | 270 } |
| 269 | 271 |
| 270 void MessageLoop::PostNonNestableTask( | 272 void MessageLoop::PostNonNestableTask( |
| 271 const tracked_objects::Location& from_here, | 273 const tracked_objects::Location& from_here, |
| 272 const Closure& task) { | 274 const Closure& task) { |
| 273 message_loop_proxy_->PostNonNestableTask(from_here, task); | 275 task_runner_->PostNonNestableTask(from_here, task); |
| 274 } | 276 } |
| 275 | 277 |
| 276 void MessageLoop::PostNonNestableDelayedTask( | 278 void MessageLoop::PostNonNestableDelayedTask( |
| 277 const tracked_objects::Location& from_here, | 279 const tracked_objects::Location& from_here, |
| 278 const Closure& task, | 280 const Closure& task, |
| 279 TimeDelta delay) { | 281 TimeDelta delay) { |
| 280 message_loop_proxy_->PostNonNestableDelayedTask(from_here, task, delay); | 282 task_runner_->PostNonNestableDelayedTask(from_here, task, delay); |
| 281 } | 283 } |
| 282 | 284 |
| 283 void MessageLoop::Run() { | 285 void MessageLoop::Run() { |
| 284 DCHECK(pump_); | 286 DCHECK(pump_); |
| 285 RunLoop run_loop; | 287 RunLoop run_loop; |
| 286 run_loop.Run(); | 288 run_loop.Run(); |
| 287 } | 289 } |
| 288 | 290 |
| 289 void MessageLoop::RunUntilIdle() { | 291 void MessageLoop::RunUntilIdle() { |
| 290 DCHECK(pump_); | 292 DCHECK(pump_); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 } | 362 } |
| 361 | 363 |
| 362 bool MessageLoop::IsIdleForTesting() { | 364 bool MessageLoop::IsIdleForTesting() { |
| 363 // We only check the imcoming queue|, since we don't want to lock the work | 365 // We only check the imcoming queue|, since we don't want to lock the work |
| 364 // queue. | 366 // queue. |
| 365 return incoming_task_queue_->IsIdleForTesting(); | 367 return incoming_task_queue_->IsIdleForTesting(); |
| 366 } | 368 } |
| 367 | 369 |
| 368 //------------------------------------------------------------------------------ | 370 //------------------------------------------------------------------------------ |
| 369 | 371 |
| 372 // static |
| 370 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( | 373 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( |
| 371 Type type, MessagePumpFactoryCallback pump_factory) { | 374 Type type, MessagePumpFactoryCallback pump_factory) { |
| 372 return make_scoped_ptr(new MessageLoop(type, pump_factory)); | 375 return make_scoped_ptr(new MessageLoop(type, pump_factory)); |
| 373 } | 376 } |
| 374 | 377 |
| 375 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | 378 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
| 376 : type_(type), | 379 : type_(type), |
| 377 #if defined(OS_WIN) | 380 #if defined(OS_WIN) |
| 378 pending_high_res_tasks_(0), | 381 pending_high_res_tasks_(0), |
| 379 in_high_res_mode_(false), | 382 in_high_res_mode_(false), |
| 380 #endif | 383 #endif |
| 381 nestable_tasks_allowed_(true), | 384 nestable_tasks_allowed_(true), |
| 382 #if defined(OS_WIN) | 385 #if defined(OS_WIN) |
| 383 os_modal_loop_(false), | 386 os_modal_loop_(false), |
| 384 #endif // OS_WIN | 387 #endif // OS_WIN |
| 385 pump_factory_(pump_factory), | 388 pump_factory_(pump_factory), |
| 386 message_histogram_(NULL), | 389 message_histogram_(NULL), |
| 387 run_loop_(NULL), | 390 run_loop_(NULL), |
| 388 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 391 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
| 389 message_loop_proxy_( | 392 unbound_task_runner_( |
| 390 new internal::MessageLoopProxyImpl(incoming_task_queue_)) { | 393 new internal::MessageLoopTaskRunner(incoming_task_queue_)), |
| 394 task_runner_(unbound_task_runner_) { |
| 391 // If type is TYPE_CUSTOM non-null pump_factory must be given. | 395 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
| 392 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); | 396 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); |
| 393 } | 397 } |
| 394 | 398 |
| 395 void MessageLoop::BindToCurrentThread() { | 399 void MessageLoop::BindToCurrentThread() { |
| 396 DCHECK(!pump_); | 400 DCHECK(!pump_); |
| 397 if (!pump_factory_.is_null()) | 401 if (!pump_factory_.is_null()) |
| 398 pump_ = pump_factory_.Run(); | 402 pump_ = pump_factory_.Run(); |
| 399 else | 403 else |
| 400 pump_ = CreateMessagePumpForType(type_); | 404 pump_ = CreateMessagePumpForType(type_); |
| 401 | 405 |
| 402 DCHECK(!current()) << "should only have one message loop per thread"; | 406 DCHECK(!current()) << "should only have one message loop per thread"; |
| 403 lazy_tls_ptr.Pointer()->Set(this); | 407 lazy_tls_ptr.Pointer()->Set(this); |
| 404 | 408 |
| 405 incoming_task_queue_->StartScheduling(); | 409 incoming_task_queue_->StartScheduling(); |
| 406 message_loop_proxy_->BindToCurrentThread(); | 410 unbound_task_runner_->BindToCurrentThread(); |
| 407 thread_task_runner_handle_.reset( | 411 unbound_task_runner_ = nullptr; |
| 408 new ThreadTaskRunnerHandle(message_loop_proxy_)); | 412 SetThreadTaskRunnerHandle(); |
| 413 } |
| 414 |
| 415 void MessageLoop::SetTaskRunner( |
| 416 scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 417 DCHECK_EQ(this, current()); |
| 418 DCHECK(task_runner->BelongsToCurrentThread()); |
| 419 DCHECK(!unbound_task_runner_); |
| 420 task_runner_ = task_runner.Pass(); |
| 421 SetThreadTaskRunnerHandle(); |
| 422 } |
| 423 |
| 424 void MessageLoop::SetThreadTaskRunnerHandle() { |
| 425 DCHECK_EQ(this, current()); |
| 426 // Clear the previous thread task runner first because only one can exist at |
| 427 // a time. |
| 428 thread_task_runner_handle_.reset(); |
| 429 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); |
| 409 } | 430 } |
| 410 | 431 |
| 411 void MessageLoop::RunHandler() { | 432 void MessageLoop::RunHandler() { |
| 412 DCHECK_EQ(this, current()); | 433 DCHECK_EQ(this, current()); |
| 413 | 434 |
| 414 StartHistogrammer(); | 435 StartHistogrammer(); |
| 415 | 436 |
| 416 #if defined(OS_WIN) | 437 #if defined(OS_WIN) |
| 417 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 438 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
| 418 static_cast<MessagePumpForUI*>(pump_.get())-> | 439 static_cast<MessagePumpForUI*>(pump_.get())-> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 446 pending_high_res_tasks_--; | 467 pending_high_res_tasks_--; |
| 447 CHECK_GE(pending_high_res_tasks_, 0); | 468 CHECK_GE(pending_high_res_tasks_, 0); |
| 448 } | 469 } |
| 449 #endif | 470 #endif |
| 450 | 471 |
| 451 // Execute the task and assume the worst: It is probably not reentrant. | 472 // Execute the task and assume the worst: It is probably not reentrant. |
| 452 nestable_tasks_allowed_ = false; | 473 nestable_tasks_allowed_ = false; |
| 453 | 474 |
| 454 HistogramEvent(kTaskRunEvent); | 475 HistogramEvent(kTaskRunEvent); |
| 455 | 476 |
| 477 TRACE_TASK_EXECUTION("toplevel", pending_task); |
| 478 |
| 456 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 479 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 457 WillProcessTask(pending_task)); | 480 WillProcessTask(pending_task)); |
| 458 task_annotator_.RunTask( | 481 task_annotator_.RunTask("MessageLoop::PostTask", pending_task); |
| 459 "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task); | |
| 460 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 482 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 461 DidProcessTask(pending_task)); | 483 DidProcessTask(pending_task)); |
| 462 | 484 |
| 463 nestable_tasks_allowed_ = true; | 485 nestable_tasks_allowed_ = true; |
| 464 } | 486 } |
| 465 | 487 |
| 466 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { | 488 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { |
| 467 if (pending_task.nestable || run_loop_->run_depth_ == 1) { | 489 if (pending_task.nestable || run_loop_->run_depth_ == 1) { |
| 468 RunTask(pending_task); | 490 RunTask(pending_task); |
| 469 // Show that we ran a task (Note: a new one might arrive as a | 491 // Show that we ran a task (Note: a new one might arrive as a |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 persistent, | 747 persistent, |
| 726 mode, | 748 mode, |
| 727 controller, | 749 controller, |
| 728 delegate); | 750 delegate); |
| 729 } | 751 } |
| 730 #endif | 752 #endif |
| 731 | 753 |
| 732 #endif // !defined(OS_NACL_SFI) | 754 #endif // !defined(OS_NACL_SFI) |
| 733 | 755 |
| 734 } // namespace base | 756 } // namespace base |
| OLD | NEW |