| 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 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 | 512 |
| 513 void MessageLoop::ReloadWorkQueue() { | 513 void MessageLoop::ReloadWorkQueue() { |
| 514 // We can improve performance of our loading tasks from the incoming queue to | 514 // We can improve performance of our loading tasks from the incoming queue to |
| 515 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to | 515 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to |
| 516 // load. That reduces the number of locks-per-task significantly when our | 516 // load. That reduces the number of locks-per-task significantly when our |
| 517 // queues get large. | 517 // queues get large. |
| 518 if (work_queue_.empty()) | 518 if (work_queue_.empty()) |
| 519 incoming_task_queue_->ReloadWorkQueue(&work_queue_); | 519 incoming_task_queue_->ReloadWorkQueue(&work_queue_); |
| 520 } | 520 } |
| 521 | 521 |
| 522 TaskQueue& MessageLoop::SelectNextWorkQueue() { |
| 523 return work_queue_; |
| 524 } |
| 525 |
| 522 void MessageLoop::ScheduleWork(bool was_empty) { | 526 void MessageLoop::ScheduleWork(bool was_empty) { |
| 523 // The Android UI message loop needs to get notified each time | 527 // The Android UI message loop needs to get notified each time |
| 524 // a task is added to the incoming queue. | 528 // a task is added to the incoming queue. |
| 525 if (was_empty || AlwaysNotifyPump(type_)) | 529 if (was_empty || AlwaysNotifyPump(type_)) |
| 526 pump_->ScheduleWork(); | 530 pump_->ScheduleWork(); |
| 527 } | 531 } |
| 528 | 532 |
| 529 //------------------------------------------------------------------------------ | 533 //------------------------------------------------------------------------------ |
| 530 // Method and data for histogramming events and actions taken by each instance | 534 // Method and data for histogramming events and actions taken by each instance |
| 531 // on each thread. | 535 // on each thread. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 553 } | 557 } |
| 554 | 558 |
| 555 bool MessageLoop::DoWork() { | 559 bool MessageLoop::DoWork() { |
| 556 if (!nestable_tasks_allowed_) { | 560 if (!nestable_tasks_allowed_) { |
| 557 // Task can't be executed right now. | 561 // Task can't be executed right now. |
| 558 return false; | 562 return false; |
| 559 } | 563 } |
| 560 | 564 |
| 561 for (;;) { | 565 for (;;) { |
| 562 ReloadWorkQueue(); | 566 ReloadWorkQueue(); |
| 563 if (work_queue_.empty()) | 567 |
| 568 TaskQueue& current_work_queue = SelectNextWorkQueue(); |
| 569 |
| 570 if (current_work_queue.empty()) |
| 564 break; | 571 break; |
| 565 | 572 |
| 566 // Execute oldest task. | 573 // Execute oldest task. |
| 567 do { | 574 do { |
| 568 PendingTask pending_task = work_queue_.front(); | 575 PendingTask pending_task = current_work_queue.front(); |
| 569 work_queue_.pop(); | 576 current_work_queue.pop(); |
| 570 if (!pending_task.delayed_run_time.is_null()) { | 577 if (!pending_task.delayed_run_time.is_null()) { |
| 571 AddToDelayedWorkQueue(pending_task); | 578 AddToDelayedWorkQueue(pending_task); |
| 572 // If we changed the topmost task, then it is time to reschedule. | 579 // If we changed the topmost task, then it is time to reschedule. |
| 573 if (delayed_work_queue_.top().task.Equals(pending_task.task)) | 580 if (delayed_work_queue_.top().task.Equals(pending_task.task)) |
| 574 pump_->ScheduleDelayedWork(pending_task.delayed_run_time); | 581 pump_->ScheduleDelayedWork(pending_task.delayed_run_time); |
| 575 } else { | 582 } else { |
| 576 if (DeferOrRunPendingTask(pending_task)) | 583 if (DeferOrRunPendingTask(pending_task)) |
| 577 return true; | 584 return true; |
| 578 } | 585 } |
| 579 } while (!work_queue_.empty()); | 586 } while (!current_work_queue.empty()); |
| 580 } | 587 } |
| 581 | 588 |
| 582 // Nothing happened. | 589 // Nothing happened. |
| 583 return false; | 590 return false; |
| 584 } | 591 } |
| 585 | 592 |
| 586 bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) { | 593 bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) { |
| 587 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { | 594 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { |
| 588 recent_time_ = *next_delayed_work_time = TimeTicks(); | 595 recent_time_ = *next_delayed_work_time = TimeTicks(); |
| 589 return false; | 596 return false; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 persistent, | 740 persistent, |
| 734 mode, | 741 mode, |
| 735 controller, | 742 controller, |
| 736 delegate); | 743 delegate); |
| 737 } | 744 } |
| 738 #endif | 745 #endif |
| 739 | 746 |
| 740 #endif // !defined(OS_NACL) | 747 #endif // !defined(OS_NACL) |
| 741 | 748 |
| 742 } // namespace base | 749 } // namespace base |
| OLD | NEW |