| 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 TRACE_TASK("OrderedSimpleTaskRunner::PostNonNestableDelayedTask", pt); | 121 TRACE_TASK("OrderedSimpleTaskRunner::PostNonNestableDelayedTask", pt); |
| 122 pending_tasks_.insert(pt); | 122 pending_tasks_.insert(pt); |
| 123 return true; | 123 return true; |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool OrderedSimpleTaskRunner::RunsTasksOnCurrentThread() const { | 126 bool OrderedSimpleTaskRunner::RunsTasksOnCurrentThread() const { |
| 127 DCHECK(thread_checker_.CalledOnValidThread()); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
| 128 return true; | 128 return true; |
| 129 } | 129 } |
| 130 | 130 |
| 131 size_t OrderedSimpleTaskRunner::NumPendingTasks() const { | 131 size_t OrderedSimpleTaskRunner::NumPendingTasks() { |
| 132 RemovePendingCancelledTasks(); |
| 132 return pending_tasks_.size(); | 133 return pending_tasks_.size(); |
| 133 } | 134 } |
| 134 | 135 |
| 135 bool OrderedSimpleTaskRunner::HasPendingTasks() const { | 136 bool OrderedSimpleTaskRunner::HasPendingTasks() { |
| 137 RemovePendingCancelledTasks(); |
| 136 return pending_tasks_.size() > 0; | 138 return pending_tasks_.size() > 0; |
| 137 } | 139 } |
| 138 | 140 |
| 139 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() { | 141 base::TimeTicks OrderedSimpleTaskRunner::NextTaskTime() { |
| 142 RemovePendingCancelledTasks(); |
| 143 |
| 140 if (pending_tasks_.size() <= 0) { | 144 if (pending_tasks_.size() <= 0) { |
| 141 return AbsoluteMaxNow(); | 145 return AbsoluteMaxNow(); |
| 142 } | 146 } |
| 143 | 147 |
| 144 return pending_tasks_.begin()->GetTimeToRun(); | 148 return pending_tasks_.begin()->GetTimeToRun(); |
| 145 } | 149 } |
| 146 | 150 |
| 147 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() { | 151 base::TimeDelta OrderedSimpleTaskRunner::DelayToNextTaskTime() { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 RemovePendingCancelledTasks(); |
| 149 | 154 |
| 150 if (pending_tasks_.size() <= 0) { | 155 if (pending_tasks_.size() <= 0) { |
| 151 return AbsoluteMaxNow() - base::TimeTicks(); | 156 return AbsoluteMaxNow() - base::TimeTicks(); |
| 152 } | 157 } |
| 153 | 158 |
| 154 base::TimeDelta delay = NextTaskTime() - now_src_->NowTicks(); | 159 base::TimeDelta delay = NextTaskTime() - now_src_->NowTicks(); |
| 155 if (delay > base::TimeDelta()) | 160 if (delay > base::TimeDelta()) |
| 156 return delay; | 161 return delay; |
| 157 return base::TimeDelta(); | 162 return base::TimeDelta(); |
| 158 } | 163 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_)); | 196 modifiable_conditions.push_back(TaskRunCountBelow(max_tasks_)); |
| 192 | 197 |
| 193 // If to advance now or not | 198 // If to advance now or not |
| 194 if (!advance_now_) { | 199 if (!advance_now_) { |
| 195 modifiable_conditions.push_back(NowBefore(now_src_->NowTicks())); | 200 modifiable_conditions.push_back(NowBefore(now_src_->NowTicks())); |
| 196 } else { | 201 } else { |
| 197 modifiable_conditions.push_back(AdvanceNow()); | 202 modifiable_conditions.push_back(AdvanceNow()); |
| 198 } | 203 } |
| 199 | 204 |
| 200 while (pending_tasks_.size() > 0) { | 205 while (pending_tasks_.size() > 0) { |
| 206 // Skip canceled tasks. |
| 207 if (pending_tasks_.begin()->task.IsCancelled()) { |
| 208 pending_tasks_.erase(pending_tasks_.begin()); |
| 209 continue; |
| 210 } |
| 201 // Check if we should continue to run pending tasks. | 211 // Check if we should continue to run pending tasks. |
| 202 bool condition_success = true; | 212 bool condition_success = true; |
| 203 for (std::vector<base::Callback<bool(void)>>::iterator it = | 213 for (std::vector<base::Callback<bool(void)>>::iterator it = |
| 204 modifiable_conditions.begin(); | 214 modifiable_conditions.begin(); |
| 205 it != modifiable_conditions.end(); | 215 it != modifiable_conditions.end(); |
| 206 it++) { | 216 it++) { |
| 207 condition_success = it->Run(); | 217 condition_success = it->Run(); |
| 208 if (!condition_success) | 218 if (!condition_success) |
| 209 break; | 219 break; |
| 210 } | 220 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 347 } |
| 338 | 348 |
| 339 bool OrderedSimpleTaskRunner::AdvanceNowCallback() { | 349 bool OrderedSimpleTaskRunner::AdvanceNowCallback() { |
| 340 base::TimeTicks next_task_time = NextTaskTime(); | 350 base::TimeTicks next_task_time = NextTaskTime(); |
| 341 if (now_src_->NowTicks() < next_task_time) { | 351 if (now_src_->NowTicks() < next_task_time) { |
| 342 now_src_->Advance(next_task_time - now_src_->NowTicks()); | 352 now_src_->Advance(next_task_time - now_src_->NowTicks()); |
| 343 } | 353 } |
| 344 return true; | 354 return true; |
| 345 } | 355 } |
| 346 | 356 |
| 357 void OrderedSimpleTaskRunner::RemovePendingCancelledTasks() { |
| 358 // Remove canceled tasks. |
| 359 while (!pending_tasks_.empty() && |
| 360 pending_tasks_.begin()->task.IsCancelled()) { |
| 361 pending_tasks_.erase(pending_tasks_.begin()); |
| 362 } |
| 363 } |
| 364 |
| 347 } // namespace cc | 365 } // namespace cc |
| OLD | NEW |