| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "cc/test/ordered_simple_task_runner.h" | 5 #include "cc/test/ordered_simple_task_runner.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 130 |
| 131 size_t OrderedSimpleTaskRunner::NumPendingTasks() const { | 131 size_t OrderedSimpleTaskRunner::NumPendingTasks() const { |
| 132 return pending_tasks_.size(); | 132 return pending_tasks_.size(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 bool OrderedSimpleTaskRunner::HasPendingTasks() const { | 135 bool OrderedSimpleTaskRunner::HasPendingTasks() const { |
| 136 return pending_tasks_.size() > 0; | 136 return pending_tasks_.size() > 0; |
| 137 } | 137 } |
| 138 | 138 |
| 139 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() { | 139 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() { |
| 140 RemoveCancelledTasks(); | |
| 141 | |
| 142 if (pending_tasks_.size() <= 0) { | 140 if (pending_tasks_.size() <= 0) { |
| 143 return AbsoluteMaxNow(); | 141 return AbsoluteMaxNow(); |
| 144 } | 142 } |
| 145 | 143 |
| 146 return pending_tasks_.begin()->GetTimeToRun(); | 144 return pending_tasks_.begin()->GetTimeToRun(); |
| 147 } | 145 } |
| 148 | 146 |
| 149 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() { | 147 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() { |
| 150 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
| 151 RemoveCancelledTasks(); | |
| 152 | 149 |
| 153 if (pending_tasks_.size() <= 0) { | 150 if (pending_tasks_.size() <= 0) { |
| 154 return AbsoluteMaxNow() - base::TimeTicks(); | 151 return AbsoluteMaxNow() - base::TimeTicks(); |
| 155 } | 152 } |
| 156 | 153 |
| 157 base::TimeDelta delay = NextTaskTime() - now_src_->NowTicks(); | 154 base::TimeDelta delay = NextTaskTime() - now_src_->NowTicks(); |
| 158 if (delay > base::TimeDelta()) | 155 if (delay > base::TimeDelta()) |
| 159 return delay; | 156 return delay; |
| 160 return base::TimeDelta(); | 157 return base::TimeDelta(); |
| 161 } | 158 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_)); | 191 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_)); |
| 195 | 192 |
| 196 // If to advance now or not | 193 // If to advance now or not |
| 197 if (!advance_now_) { | 194 if (!advance_now_) { |
| 198 modifiable_conditions.push_back(NowBefore(now_src_->NowTicks())); | 195 modifiable_conditions.push_back(NowBefore(now_src_->NowTicks())); |
| 199 } else { | 196 } else { |
| 200 modifiable_conditions.push_back(AdvanceNow()); | 197 modifiable_conditions.push_back(AdvanceNow()); |
| 201 } | 198 } |
| 202 | 199 |
| 203 while (pending_tasks_.size() > 0) { | 200 while (pending_tasks_.size() > 0) { |
| 204 // Skip canceled tasks. | |
| 205 if (pending_tasks_.begin()->task.IsCancelled()) { | |
| 206 pending_tasks_.erase(pending_tasks_.begin()); | |
| 207 continue; | |
| 208 } | |
| 209 // Check if we should continue to run pending tasks. | 201 // Check if we should continue to run pending tasks. |
| 210 bool condition_success = true; | 202 bool condition_success = true; |
| 211 for (std::vector<base::Callback<bool(void)>>::iterator it = | 203 for (std::vector<base::Callback<bool(void)>>::iterator it = |
| 212 modifiable_conditions.begin(); | 204 modifiable_conditions.begin(); |
| 213 it != modifiable_conditions.end(); | 205 it != modifiable_conditions.end(); |
| 214 it++) { | 206 it++) { |
| 215 condition_success = it->Run(); | 207 condition_success = it->Run(); |
| 216 if (!condition_success) | 208 if (!condition_success) |
| 217 break; | 209 break; |
| 218 } | 210 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 } | 337 } |
| 346 | 338 |
| 347 bool OrderedSimpleTaskRunner::AdvanceNowCallback() { | 339 bool OrderedSimpleTaskRunner::AdvanceNowCallback() { |
| 348 base::TimeTicks next_task_time = NextTaskTime(); | 340 base::TimeTicks next_task_time = NextTaskTime(); |
| 349 if (now_src_->NowTicks() < next_task_time) { | 341 if (now_src_->NowTicks() < next_task_time) { |
| 350 now_src_->Advance(next_task_time - now_src_->NowTicks()); | 342 now_src_->Advance(next_task_time - now_src_->NowTicks()); |
| 351 } | 343 } |
| 352 return true; | 344 return true; |
| 353 } | 345 } |
| 354 | 346 |
| 355 void OrderedSimpleTaskRunner::RemoveCancelledTasks() { | |
| 356 std::set<TestOrderablePendingTask>::iterator it = pending_tasks_.begin(); | |
| 357 while (it != pending_tasks_.end()) { | |
| 358 if (it->task.IsCancelled()) { | |
| 359 it = pending_tasks_.erase(it); | |
| 360 } else { | |
| 361 it++; | |
| 362 } | |
| 363 } | |
| 364 } | |
| 365 | |
| 366 } // namespace cc | 347 } // namespace cc |
| OLD | NEW |