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 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 } | 360 } |
361 | 361 |
362 bool MessageLoop::IsIdleForTesting() { | 362 bool MessageLoop::IsIdleForTesting() { |
363 // We only check the imcoming queue|, since we don't want to lock the work | 363 // We only check the imcoming queue|, since we don't want to lock the work |
364 // queue. | 364 // queue. |
365 return incoming_task_queue_->IsIdleForTesting(); | 365 return incoming_task_queue_->IsIdleForTesting(); |
366 } | 366 } |
367 | 367 |
368 //------------------------------------------------------------------------------ | 368 //------------------------------------------------------------------------------ |
369 | 369 |
| 370 // static |
370 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( | 371 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( |
371 Type type, MessagePumpFactoryCallback pump_factory) { | 372 Type type, MessagePumpFactoryCallback pump_factory) { |
372 return make_scoped_ptr(new MessageLoop(type, pump_factory)); | 373 return make_scoped_ptr(new MessageLoop(type, pump_factory)); |
373 } | 374 } |
374 | 375 |
375 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | 376 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
376 : type_(type), | 377 : type_(type), |
377 #if defined(OS_WIN) | 378 #if defined(OS_WIN) |
378 pending_high_res_tasks_(0), | 379 pending_high_res_tasks_(0), |
379 in_high_res_mode_(false), | 380 in_high_res_mode_(false), |
(...skipping 16 matching lines...) Expand all Loading... |
396 if (!pump_factory_.is_null()) | 397 if (!pump_factory_.is_null()) |
397 pump_ = pump_factory_.Run(); | 398 pump_ = pump_factory_.Run(); |
398 else | 399 else |
399 pump_ = CreateMessagePumpForType(type_); | 400 pump_ = CreateMessagePumpForType(type_); |
400 | 401 |
401 DCHECK(!current()) << "should only have one message loop per thread"; | 402 DCHECK(!current()) << "should only have one message loop per thread"; |
402 lazy_tls_ptr.Pointer()->Set(this); | 403 lazy_tls_ptr.Pointer()->Set(this); |
403 | 404 |
404 incoming_task_queue_->StartScheduling(); | 405 incoming_task_queue_->StartScheduling(); |
405 task_runner_->BindToCurrentThread(); | 406 task_runner_->BindToCurrentThread(); |
406 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); | 407 scoped_refptr<BindableSingleThreadTaskRunner> previous_task_runner = |
| 408 SwapTaskRunner(task_runner_.Pass()); |
| 409 DCHECK(previous_task_runner == NULL); |
| 410 } |
| 411 |
| 412 scoped_refptr<BindableSingleThreadTaskRunner> MessageLoop::SwapTaskRunner( |
| 413 scoped_refptr<BindableSingleThreadTaskRunner> task_runner) { |
| 414 // If this message loop was already bound to a thread, ensure the task runner |
| 415 // belongs to the same thread and update the thread task runner handle. |
| 416 // Otherwise the task runner will be bound later in BindToCurrentThread(). |
| 417 if (pump_) { |
| 418 DCHECK_EQ(this, current()); |
| 419 DCHECK(task_runner->BelongsToCurrentThread()); |
| 420 // This reset() is needed or ThreadTaskRunnerHandle::Get() will crash. |
| 421 thread_task_runner_handle_.reset(); |
| 422 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner)); |
| 423 } |
| 424 task_runner_.swap(task_runner); |
| 425 return task_runner; |
407 } | 426 } |
408 | 427 |
409 void MessageLoop::RunHandler() { | 428 void MessageLoop::RunHandler() { |
410 DCHECK_EQ(this, current()); | 429 DCHECK_EQ(this, current()); |
411 | 430 |
412 StartHistogrammer(); | 431 StartHistogrammer(); |
413 | 432 |
414 #if defined(OS_WIN) | 433 #if defined(OS_WIN) |
415 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 434 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
416 static_cast<MessagePumpForUI*>(pump_.get())-> | 435 static_cast<MessagePumpForUI*>(pump_.get())-> |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 persistent, | 746 persistent, |
728 mode, | 747 mode, |
729 controller, | 748 controller, |
730 delegate); | 749 delegate); |
731 } | 750 } |
732 #endif | 751 #endif |
733 | 752 |
734 #endif // !defined(OS_NACL_SFI) | 753 #endif // !defined(OS_NACL_SFI) |
735 | 754 |
736 } // namespace base | 755 } // namespace base |
OLD | NEW |