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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 | 164 |
165 // Let interested parties have one last shot at accessing this. | 165 // Let interested parties have one last shot at accessing this. |
166 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | 166 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
167 WillDestroyCurrentMessageLoop()); | 167 WillDestroyCurrentMessageLoop()); |
168 | 168 |
169 thread_task_runner_handle_.reset(); | 169 thread_task_runner_handle_.reset(); |
170 | 170 |
171 // Tell the incoming queue that we are dying. | 171 // Tell the incoming queue that we are dying. |
172 incoming_task_queue_->WillDestroyCurrentMessageLoop(); | 172 incoming_task_queue_->WillDestroyCurrentMessageLoop(); |
173 incoming_task_queue_ = NULL; | 173 incoming_task_queue_ = NULL; |
| 174 unbound_task_runner_ = NULL; |
174 task_runner_ = NULL; | 175 task_runner_ = NULL; |
175 | 176 |
176 // OK, now make it so that no one can find us. | 177 // OK, now make it so that no one can find us. |
177 lazy_tls_ptr.Pointer()->Set(NULL); | 178 lazy_tls_ptr.Pointer()->Set(NULL); |
178 } | 179 } |
179 | 180 |
180 // static | 181 // static |
181 MessageLoop* MessageLoop::current() { | 182 MessageLoop* MessageLoop::current() { |
182 // 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 |
183 // when they have no intention of using us. | 184 // when they have no intention of using us. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 362 } |
362 | 363 |
363 bool MessageLoop::IsIdleForTesting() { | 364 bool MessageLoop::IsIdleForTesting() { |
364 // 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 |
365 // queue. | 366 // queue. |
366 return incoming_task_queue_->IsIdleForTesting(); | 367 return incoming_task_queue_->IsIdleForTesting(); |
367 } | 368 } |
368 | 369 |
369 //------------------------------------------------------------------------------ | 370 //------------------------------------------------------------------------------ |
370 | 371 |
| 372 // static |
371 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( | 373 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( |
372 Type type, MessagePumpFactoryCallback pump_factory) { | 374 Type type, MessagePumpFactoryCallback pump_factory) { |
373 return make_scoped_ptr(new MessageLoop(type, pump_factory)); | 375 return make_scoped_ptr(new MessageLoop(type, pump_factory)); |
374 } | 376 } |
375 | 377 |
376 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | 378 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
377 : type_(type), | 379 : type_(type), |
378 #if defined(OS_WIN) | 380 #if defined(OS_WIN) |
379 pending_high_res_tasks_(0), | 381 pending_high_res_tasks_(0), |
380 in_high_res_mode_(false), | 382 in_high_res_mode_(false), |
381 #endif | 383 #endif |
382 nestable_tasks_allowed_(true), | 384 nestable_tasks_allowed_(true), |
383 #if defined(OS_WIN) | 385 #if defined(OS_WIN) |
384 os_modal_loop_(false), | 386 os_modal_loop_(false), |
385 #endif // OS_WIN | 387 #endif // OS_WIN |
386 pump_factory_(pump_factory), | 388 pump_factory_(pump_factory), |
387 message_histogram_(NULL), | 389 message_histogram_(NULL), |
388 run_loop_(NULL), | 390 run_loop_(NULL), |
389 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 391 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
390 task_runner_(new internal::MessageLoopTaskRunner(incoming_task_queue_)) { | 392 unbound_task_runner_( |
| 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 task_runner_->BindToCurrentThread(); | 410 unbound_task_runner_->BindToCurrentThread(); |
| 411 unbound_task_runner_ = nullptr; |
| 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(); |
407 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); | 429 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); |
408 } | 430 } |
409 | 431 |
410 void MessageLoop::RunHandler() { | 432 void MessageLoop::RunHandler() { |
411 DCHECK_EQ(this, current()); | 433 DCHECK_EQ(this, current()); |
412 | 434 |
413 StartHistogrammer(); | 435 StartHistogrammer(); |
414 | 436 |
415 #if defined(OS_WIN) | 437 #if defined(OS_WIN) |
416 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 438 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
(...skipping 308 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 |