Index: cc/test/ordered_simple_task_runner.cc |
diff --git a/cc/test/ordered_simple_task_runner.cc b/cc/test/ordered_simple_task_runner.cc |
index 188ffe72b5f8adea6208635d68263b3139811000..95411920b7d8cada075c35d11cb76e97a5795dda 100644 |
--- a/cc/test/ordered_simple_task_runner.cc |
+++ b/cc/test/ordered_simple_task_runner.cc |
@@ -3,12 +3,11 @@ |
// found in the LICENSE file. |
#include "cc/test/ordered_simple_task_runner.h" |
+#include "base/debug/trace_event.h" |
#include <algorithm> |
#include <deque> |
-#include "base/logging.h" |
- |
namespace { |
bool TestPendingTaskComparator(const base::TestPendingTask& lhs, |
@@ -20,22 +19,154 @@ bool TestPendingTaskComparator(const base::TestPendingTask& lhs, |
namespace cc { |
-OrderedSimpleTaskRunner::OrderedSimpleTaskRunner() {} |
+OrderedSimpleTaskRunner::OrderedSimpleTaskRunner(bool advance_now) |
+ : advance_now_(advance_now), now_(), inside_run_pending_tasks_(false) { |
+} |
OrderedSimpleTaskRunner::~OrderedSimpleTaskRunner() {} |
+void OrderedSimpleTaskRunner::SortTasks( |
+ std::deque<base::TestPendingTask>& tasks) { |
+ std::stable_sort(tasks.begin(), tasks.end(), TestPendingTaskComparator); |
+} |
+ |
+bool OrderedSimpleTaskRunner::PostDelayedTask( |
+ const tracked_objects::Location& from_here, |
+ const base::Closure& task, |
+ base::TimeDelta delay) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ base::TestPendingTask pt( |
+ from_here, task, Now(), delay, base::TestPendingTask::NESTABLE); |
+ TRACE_EVENT_INSTANT2("cc", |
brianderson
2014/07/17 05:26:03
What categories do other cc testing files use?
|
+ "OrderedSimpleTaskRunner::PostDelayedTask", |
+ TRACE_EVENT_SCOPE_THREAD, |
+ "RunAt", |
+ pt.GetTimeToRun(), |
+ "Function", |
+ pt.location.ToString()); |
+ pending_tasks_.push_back(pt); |
+ SortTasks(pending_tasks_); |
+ return true; |
+} |
+ |
+bool OrderedSimpleTaskRunner::PostNonNestableDelayedTask( |
+ const tracked_objects::Location& from_here, |
+ const base::Closure& task, |
+ base::TimeDelta delay) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ base::TestPendingTask pt( |
+ from_here, task, Now(), delay, base::TestPendingTask::NON_NESTABLE); |
+ TRACE_EVENT_INSTANT2("cc", |
+ "OrderedSimpleTaskRunner::PostNonNestableDelayedTask", |
+ TRACE_EVENT_SCOPE_THREAD, |
+ "RunAt", |
+ pt.GetTimeToRun(), |
+ "Function", |
+ pt.location.ToString()); |
+ pending_tasks_.push_back(pt); |
+ SortTasks(pending_tasks_); |
+ return true; |
+} |
+ |
void OrderedSimpleTaskRunner::RunPendingTasks() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ if (inside_run_pending_tasks_) |
+ return; |
+ inside_run_pending_tasks_ = true; |
+ |
// Swap with a local variable to avoid re-entrancy problems. |
std::deque<base::TestPendingTask> tasks_to_run; |
tasks_to_run.swap(pending_tasks_); |
- std::stable_sort(tasks_to_run.begin(), |
- tasks_to_run.end(), |
- TestPendingTaskComparator); |
+ |
+ TRACE_EVENT_INSTANT1("cc", |
+ "OrderedSimpleTaskRunner::RunPendingTasks pendingTask", |
+ TRACE_EVENT_SCOPE_THREAD, |
+ "waiting", |
+ tasks_to_run.size()); |
for (std::deque<base::TestPendingTask>::iterator it = tasks_to_run.begin(); |
- it != tasks_to_run.end(); ++it) { |
- it->task.Run(); |
+ it != tasks_to_run.end(); |
+ ++it) { |
+ TRACE_EVENT_INSTANT2("cc", |
+ "OrderedSimpleTaskRunner::RunPendingTasks pendingTask", |
+ TRACE_EVENT_SCOPE_THREAD, |
+ "RunAt", |
+ it->GetTimeToRun(), |
+ "Function", |
+ it->location.ToString()); |
} |
+ |
+ std::deque<base::TestPendingTask>::iterator it = tasks_to_run.begin(); |
+ for (; it != tasks_to_run.end(); ++it) { |
+ base::TimeTicks task_time = it->GetTimeToRun(); |
+ |
+ // Have new items been added to pending_tasks which need to run before this |
+ // task? |
+ if (pending_tasks_.size() > 0 && |
+ task_time > pending_tasks_.begin()->GetTimeToRun()) { |
+ break; |
brianderson
2014/07/17 05:26:03
So this function doesn't run all pending tasks if
mithro-old
2014/07/17 06:51:58
See the discussion on the https://codereview.chrom
|
+ } |
+ |
+ // Is the task in the future? |
+ if (task_time > Now()) { |
+ if (!advance_now_) |
+ break; |
+ else |
+ SetNow(task_time); |
+ } |
+ |
+ { |
+ TRACE_EVENT2("cc", |
+ "OrderedSimpleTaskRunner::RunPendingTasks running", |
+ "RunAt", |
+ it->GetTimeToRun(), |
+ "Function", |
+ it->location.ToString()); |
+ it->task.Run(); |
+ } |
+ } |
+ |
+ // Push any remaining tasks back into the pending queue |
+ for (; it != tasks_to_run.end(); ++it) { |
+ TRACE_EVENT_INSTANT2( |
+ "cc", |
+ "OrderedSimpleTaskRunner::RunPendingTasks requeuing pendingTask", |
+ TRACE_EVENT_SCOPE_THREAD, |
+ "RunAt", |
+ it->GetTimeToRun(), |
+ "Function", |
+ it->location.ToString()); |
+ pending_tasks_.push_front(*it); |
+ } |
+ std::stable_sort( |
+ pending_tasks_.begin(), pending_tasks_.end(), TestPendingTaskComparator); |
+ |
+ inside_run_pending_tasks_ = false; |
+} |
+ |
+base::TimeTicks OrderedSimpleTaskRunner::Now() { |
+ return now_; |
+} |
+ |
+base::TimeDelta OrderedSimpleTaskRunner::DelayToNextPendingTask() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ base::TimeDelta delay = pending_tasks_.front().GetTimeToRun() - Now(); |
+ if (delay > base::TimeDelta()) |
+ return delay; |
+ return base::TimeDelta(); |
+} |
+ |
+void OrderedSimpleTaskRunner::SetAutoAdvanceNowToPendingTasks( |
+ bool advance_now) { |
+ advance_now_ = advance_now; |
+} |
+ |
+void OrderedSimpleTaskRunner::SetNow(base::TimeTicks time) { |
+ TRACE_EVENT2( |
+ "cc", "OrderedSimpleTaskRunner::SetNow", "current", time, "new", time); |
+ DCHECK(time >= now_); |
+ now_ = time; |
} |
} // namespace cc |