OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/task_scheduler/delayed_task_manager.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/task_scheduler/scheduler_task_executor.h" | |
11 | |
12 namespace base { | |
13 namespace internal { | |
14 | |
15 struct DelayedTaskManager::DelayedTask { | |
16 DelayedTask(std::unique_ptr<Task> task, | |
17 scoped_refptr<Sequence> sequence, | |
18 SchedulerTaskExecutor* executor, | |
19 uint64_t index) | |
20 : task(std::move(task)), | |
21 sequence(std::move(sequence)), | |
22 executor(executor), | |
23 index(index) {} | |
24 | |
25 DelayedTask(DelayedTask&& other) | |
danakj
2016/04/19 21:16:46
this looks like you could use =default instead of
fdoray
2016/04/19 21:46:45
Done.
| |
26 : task(std::move(other.task)), | |
27 sequence(std::move(other.sequence)), | |
28 executor(other.executor), | |
29 index(other.index) {} | |
30 | |
31 ~DelayedTask() = default; | |
32 | |
33 DelayedTask& operator=(DelayedTask&& other) { | |
danakj
2016/04/19 21:16:46
=default here too?
fdoray
2016/04/19 21:46:45
Done.
| |
34 task = std::move(other.task); | |
35 sequence = std::move(other.sequence); | |
36 executor = other.executor; | |
37 index = other.index; | |
38 return *this; | |
39 } | |
40 | |
41 // |task| will be posted to |executor| as part of |sequence| when it becomes | |
42 // ripe for execution. | |
43 std::unique_ptr<Task> task; | |
44 scoped_refptr<Sequence> sequence; | |
45 SchedulerTaskExecutor* executor; | |
46 | |
47 // Ensures that tasks that have the same |delayed_run_time| are sorted | |
48 // according to the order in which they were added to the DelayedTaskManager. | |
49 uint64_t index; | |
50 | |
51 private: | |
52 DISALLOW_COPY_AND_ASSIGN(DelayedTask); | |
53 }; | |
54 | |
55 DelayedTaskManager::DelayedTaskManager( | |
56 const Closure& on_delayed_run_time_updated) | |
57 : on_delayed_run_time_updated_(on_delayed_run_time_updated) { | |
58 DCHECK(!on_delayed_run_time_updated_.is_null()); | |
59 } | |
60 | |
61 DelayedTaskManager::~DelayedTaskManager() = default; | |
62 | |
63 void DelayedTaskManager::AddDelayedTask(std::unique_ptr<Task> task, | |
64 scoped_refptr<Sequence> sequence, | |
65 SchedulerTaskExecutor* executor) { | |
66 DCHECK(task); | |
67 DCHECK(sequence); | |
68 DCHECK(executor); | |
69 | |
70 const TimeTicks new_task_delayed_run_time = task->delayed_run_time; | |
71 TimeTicks current_delayed_run_time; | |
72 | |
73 { | |
74 AutoSchedulerLock auto_lock(lock_); | |
75 | |
76 if (!delayed_tasks_.empty()) | |
77 current_delayed_run_time = delayed_tasks_.top().task->delayed_run_time; | |
78 | |
79 delayed_tasks_.emplace(std::move(task), std::move(sequence), executor, | |
80 delayed_task_index_++); | |
danakj
2016/04/19 21:16:46
maybe start with an index of 1 instead of 0 (ie pr
fdoray
2016/04/19 21:46:45
Done.
| |
81 } | |
82 | |
83 if (current_delayed_run_time.is_null() || | |
84 new_task_delayed_run_time < current_delayed_run_time) { | |
85 on_delayed_run_time_updated_.Run(); | |
86 } | |
87 } | |
88 | |
89 void DelayedTaskManager::PostReadyTasks() { | |
90 const TimeTicks now = Now(); | |
91 | |
92 // Move delayed tasks that are ready for execution into |ready_tasks|. Don't | |
93 // post them right away to avoid imposing an unecessary lock dependency on | |
94 // PostTaskNowHelper. | |
95 std::vector<DelayedTask> ready_tasks; | |
96 | |
97 { | |
98 AutoSchedulerLock auto_lock(lock_); | |
99 while (!delayed_tasks_.empty() && | |
100 delayed_tasks_.top().task->delayed_run_time <= now) { | |
101 // The const_cast for std::move is okay since we're immediately popping | |
102 // the task from |delayed_tasks_|. See DelayedTaskComparator::operator() | |
103 // for minor debug-check implications. | |
104 ready_tasks.emplace_back( | |
danakj
2016/04/19 21:16:46
push_back?
fdoray
2016/04/19 21:46:45
Done.
| |
105 std::move(const_cast<DelayedTask&>(delayed_tasks_.top()))); | |
106 delayed_tasks_.pop(); | |
107 } | |
108 } | |
109 | |
110 // Post delayed tasks that are ready for execution. | |
111 for (auto& delayed_task : ready_tasks) { | |
112 delayed_task.executor->PostTaskWithSequence( | |
113 std::move(delayed_task.task), std::move(delayed_task.sequence)); | |
114 } | |
115 } | |
116 | |
117 TimeTicks DelayedTaskManager::GetDelayedRunTime() const { | |
118 AutoSchedulerLock auto_lock(lock_); | |
119 | |
120 if (delayed_tasks_.empty()) | |
121 return TimeTicks(); | |
122 | |
123 return delayed_tasks_.top().task->delayed_run_time; | |
124 } | |
125 | |
126 // In std::priority_queue, the largest element is on top. Therefore, this | |
127 // comparator returns true if the delayed run time of |right| is earlier than | |
128 // the delayed run time of |left|. | |
129 bool DelayedTaskManager::DelayedTaskComparator::operator()( | |
130 const DelayedTask& left, | |
131 const DelayedTask& right) const { | |
132 #ifndef NDEBUG | |
133 // Due to STL consistency checks in Windows and const_cast'ing right before | |
134 // popping the DelayedTask, a null task can be passed to this comparator in | |
135 // Debug builds. To satisfy these consistency checks, this comparator | |
136 // considers null tasks to be the larger than anything. | |
137 DCHECK(left.task || right.task); | |
138 if (!left.task) | |
139 return false; | |
140 if (!right.task) | |
141 return true; | |
142 #else | |
143 DCHECK(left.task); | |
144 DCHECK(right.task); | |
145 #endif // NDEBUG | |
146 if (left.task->delayed_run_time > right.task->delayed_run_time) | |
147 return true; | |
148 if (left.task->delayed_run_time < right.task->delayed_run_time) | |
149 return false; | |
150 return left.index > right.index; | |
151 } | |
152 | |
153 TimeTicks DelayedTaskManager::Now() const { | |
154 return TimeTicks::Now(); | |
155 } | |
156 | |
157 } // namespace internal | |
158 } // namespace base | |
OLD | NEW |