Chromium Code Reviews| 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 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 } | 116 } |
| 117 | 117 |
| 118 MessageLoop::TaskObserver::~TaskObserver() { | 118 MessageLoop::TaskObserver::~TaskObserver() { |
| 119 } | 119 } |
| 120 | 120 |
| 121 MessageLoop::DestructionObserver::~DestructionObserver() { | 121 MessageLoop::DestructionObserver::~DestructionObserver() { |
| 122 } | 122 } |
| 123 | 123 |
| 124 MessageLoop::NestingObserver::~NestingObserver() {} | 124 MessageLoop::NestingObserver::~NestingObserver() {} |
| 125 | 125 |
| 126 MessageLoop::RecentTimeObserver::~RecentTimeObserver() {} | |
| 127 | |
| 126 //------------------------------------------------------------------------------ | 128 //------------------------------------------------------------------------------ |
| 127 | 129 |
| 128 MessageLoop::MessageLoop(Type type) | 130 MessageLoop::MessageLoop(Type type) |
| 129 : MessageLoop(type, MessagePumpFactoryCallback()) { | 131 : MessageLoop(type, MessagePumpFactoryCallback()) { |
| 130 BindToCurrentThread(); | 132 BindToCurrentThread(); |
| 131 } | 133 } |
| 132 | 134 |
| 133 MessageLoop::MessageLoop(std::unique_ptr<MessagePump> pump) | 135 MessageLoop::MessageLoop(std::unique_ptr<MessagePump> pump) |
| 134 : MessageLoop(TYPE_CUSTOM, Bind(&ReturnPump, Passed(&pump))) { | 136 : MessageLoop(TYPE_CUSTOM, Bind(&ReturnPump, Passed(&pump))) { |
| 135 BindToCurrentThread(); | 137 BindToCurrentThread(); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 void MessageLoop::AddNestingObserver(NestingObserver* observer) { | 270 void MessageLoop::AddNestingObserver(NestingObserver* observer) { |
| 269 DCHECK_EQ(this, current()); | 271 DCHECK_EQ(this, current()); |
| 270 nesting_observers_.AddObserver(observer); | 272 nesting_observers_.AddObserver(observer); |
| 271 } | 273 } |
| 272 | 274 |
| 273 void MessageLoop::RemoveNestingObserver(NestingObserver* observer) { | 275 void MessageLoop::RemoveNestingObserver(NestingObserver* observer) { |
| 274 DCHECK_EQ(this, current()); | 276 DCHECK_EQ(this, current()); |
| 275 nesting_observers_.RemoveObserver(observer); | 277 nesting_observers_.RemoveObserver(observer); |
| 276 } | 278 } |
| 277 | 279 |
| 280 void MessageLoop::AddRecentTimeObserver(RecentTimeObserver* observer) { | |
| 281 DCHECK_EQ(this, current()); | |
| 282 recent_time_observers_.AddObserver(observer); | |
| 283 } | |
| 284 | |
| 285 void MessageLoop::RemoveRecentTimeObserver(RecentTimeObserver* observer) { | |
| 286 DCHECK_EQ(this, current()); | |
| 287 recent_time_observers_.RemoveObserver(observer); | |
| 288 } | |
| 289 | |
| 278 void MessageLoop::PostTask( | 290 void MessageLoop::PostTask( |
| 279 const tracked_objects::Location& from_here, | 291 const tracked_objects::Location& from_here, |
| 280 const Closure& task) { | 292 const Closure& task) { |
| 281 task_runner_->PostTask(from_here, task); | 293 task_runner_->PostTask(from_here, task); |
| 282 } | 294 } |
| 283 | 295 |
| 284 void MessageLoop::PostDelayedTask( | 296 void MessageLoop::PostDelayedTask( |
| 285 const tracked_objects::Location& from_here, | 297 const tracked_objects::Location& from_here, |
| 286 const Closure& task, | 298 const Closure& task, |
| 287 TimeDelta delay) { | 299 TimeDelta delay) { |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 602 return true; | 614 return true; |
| 603 } | 615 } |
| 604 } while (!work_queue_.empty()); | 616 } while (!work_queue_.empty()); |
| 605 } | 617 } |
| 606 | 618 |
| 607 // Nothing happened. | 619 // Nothing happened. |
| 608 return false; | 620 return false; |
| 609 } | 621 } |
| 610 | 622 |
| 611 bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) { | 623 bool MessageLoop::DoDelayedWork(TimeTicks* next_delayed_work_time) { |
| 624 recent_time_ = TimeTicks::Now(); | |
|
tdresser
2016/05/30 19:07:46
This is probably the scariest part of this patch.
panicker
2016/06/07 19:58:02
Looks like this is undoing the optimization added
tdresser
2016/06/07 20:23:16
I'm not very worried about calling now even when t
| |
| 625 | |
| 626 FOR_EACH_OBSERVER(RecentTimeObserver, recent_time_observers_, | |
| 627 OnUpdateRecentTime(recent_time_)); | |
|
panicker
2016/06/07 19:58:02
To me it seems iffy to call an override-able metho
tdresser
2016/06/07 20:23:15
It isn't completely clear to me how you'd make thi
panicker
2016/06/07 21:04:39
Yeah a global-ish getter could work, although not
| |
| 628 | |
| 612 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { | 629 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) { |
| 613 recent_time_ = *next_delayed_work_time = TimeTicks(); | 630 *next_delayed_work_time = TimeTicks(); |
| 614 return false; | 631 return false; |
| 615 } | 632 } |
| 616 | 633 |
| 617 // When we "fall behind", there will be a lot of tasks in the delayed work | 634 // When we "fall behind", there will be a lot of tasks in the delayed work |
| 618 // queue that are ready to run. To increase efficiency when we fall behind, | 635 // queue that are ready to run. To increase efficiency when we fall behind, |
| 619 // we will only call Time::Now() intermittently, and then process all tasks | 636 // we will only call Time::Now() intermittently, and then process all tasks |
| 620 // that are ready to run before calling it again. As a result, the more we | 637 // that are ready to run before calling it again. As a result, the more we |
| 621 // fall behind (and have a lot of ready-to-run delayed tasks), the more | 638 // fall behind (and have a lot of ready-to-run delayed tasks), the more |
| 622 // efficient we'll be at handling the tasks. | 639 // efficient we'll be at handling the tasks. |
| 623 | 640 |
| 624 TimeTicks next_run_time = delayed_work_queue_.top().delayed_run_time; | 641 TimeTicks next_run_time = delayed_work_queue_.top().delayed_run_time; |
| 625 if (next_run_time > recent_time_) { | 642 if (next_run_time > recent_time_) { |
| 626 recent_time_ = TimeTicks::Now(); // Get a better view of Now(); | |
| 627 if (next_run_time > recent_time_) { | 643 if (next_run_time > recent_time_) { |
| 628 *next_delayed_work_time = next_run_time; | 644 *next_delayed_work_time = next_run_time; |
| 629 return false; | 645 return false; |
| 630 } | 646 } |
| 631 } | 647 } |
| 632 | 648 |
| 633 PendingTask pending_task = delayed_work_queue_.top(); | 649 PendingTask pending_task = delayed_work_queue_.top(); |
| 634 delayed_work_queue_.pop(); | 650 delayed_work_queue_.pop(); |
| 635 | 651 |
| 636 if (!delayed_work_queue_.empty()) | 652 if (!delayed_work_queue_.empty()) |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 740 persistent, | 756 persistent, |
| 741 mode, | 757 mode, |
| 742 controller, | 758 controller, |
| 743 delegate); | 759 delegate); |
| 744 } | 760 } |
| 745 #endif | 761 #endif |
| 746 | 762 |
| 747 #endif // !defined(OS_NACL_SFI) | 763 #endif // !defined(OS_NACL_SFI) |
| 748 | 764 |
| 749 } // namespace base | 765 } // namespace base |
| OLD | NEW |