Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/scheduler/base/real_time_domain.cc

Issue 1468443002: Reduce the number of delayed tasks on chromium run loop (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Weak pointer Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/scheduler/base/real_time_domain.h" 5 #include "components/scheduler/base/real_time_domain.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "components/scheduler/base/task_queue_impl.h" 8 #include "components/scheduler/base/task_queue_impl.h"
9 #include "components/scheduler/base/task_queue_manager_delegate.h" 9 #include "components/scheduler/base/task_queue_manager_delegate.h"
10 10
11 namespace scheduler { 11 namespace scheduler {
12 12
13 RealTimeDomain::RealTimeDomain( 13 RealTimeDomain::RealTimeDomain() : weak_factory_(this) {}
14
15 RealTimeDomain::~RealTimeDomain() {}
16
17 void RealTimeDomain::OnRegisterWithTaskQueueManager(
14 TaskQueueManagerDelegate* task_queue_manager_delegate, 18 TaskQueueManagerDelegate* task_queue_manager_delegate,
15 base::Closure do_work_closure) 19 base::Closure do_work_closure) {
16 : task_queue_manager_delegate_(task_queue_manager_delegate), 20 task_queue_manager_delegate_ = task_queue_manager_delegate;
17 do_work_closure_(do_work_closure) { 21 do_work_closure_ = do_work_closure;
18 DCHECK(task_queue_manager_delegate_); 22 DCHECK(task_queue_manager_delegate_);
19 } 23 }
20 24
21 RealTimeDomain::~RealTimeDomain() {}
22
23 LazyNow RealTimeDomain::CreateLazyNow() { 25 LazyNow RealTimeDomain::CreateLazyNow() {
26 DCHECK(task_queue_manager_delegate_);
24 return LazyNow(task_queue_manager_delegate_); 27 return LazyNow(task_queue_manager_delegate_);
25 } 28 }
26 29
27 void RealTimeDomain::RequestWakeup(base::TimeDelta delay) { 30 void RealTimeDomain::RequestWakeup(LazyNow* lazy_now, base::TimeDelta delay) {
28 task_queue_manager_delegate_->PostDelayedTask(FROM_HERE, do_work_closure_, 31 PostWrappedDoWork(lazy_now->Now(), lazy_now->Now() + delay);
29 delay);
30 } 32 }
31 33
32 bool RealTimeDomain::MaybeAdvanceTime() { 34 bool RealTimeDomain::MaybeAdvanceTime() {
35 base::TimeTicks next_run_time;
36 if (!NextScheduledRunTime(&next_run_time))
37 return false;
38
39 DCHECK(task_queue_manager_delegate_);
40 base::TimeTicks now = task_queue_manager_delegate_->NowTicks();
41 if (now >= next_run_time)
42 return true;
43
44 PostWrappedDoWork(now, next_run_time);
33 return false; 45 return false;
34 } 46 }
35 47
48 void RealTimeDomain::PostWrappedDoWork(base::TimeTicks now,
49 base::TimeTicks run_time) {
50 DCHECK_GE(run_time, now);
51 DCHECK(task_queue_manager_delegate_);
52 if (pending_wakeups_.insert(run_time).second) {
53 task_queue_manager_delegate_->PostDelayedTask(
54 FROM_HERE,
55 base::Bind(&RealTimeDomain::WrappedDoWorkTask,
56 weak_factory_.GetWeakPtr(), run_time),
57 run_time - now);
58 }
59 }
60
61 void RealTimeDomain::WrappedDoWorkTask(base::TimeTicks run_time) {
62 pending_wakeups_.erase(run_time);
63 do_work_closure_.Run();
64 }
65
36 void RealTimeDomain::AsValueIntoInternal( 66 void RealTimeDomain::AsValueIntoInternal(
37 base::trace_event::TracedValue* state) const {} 67 base::trace_event::TracedValue* state) const {}
38 68
39 const char* RealTimeDomain::GetName() const { 69 const char* RealTimeDomain::GetName() const {
40 return "RealTimeDomain"; 70 return "RealTimeDomain";
41 } 71 }
42 72
43 } // namespace scheduler 73 } // namespace scheduler
OLDNEW
« no previous file with comments | « components/scheduler/base/real_time_domain.h ('k') | components/scheduler/base/task_queue_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698