| 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 "media/base/fake_single_thread_task_runner.h" | 5 #include "media/base/fake_single_thread_task_runner.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/time/tick_clock.h" | 11 #include "base/time/tick_clock.h" |
| 12 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner( | 15 FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner( |
| 16 base::SimpleTestTickClock* clock) | 16 base::SimpleTestTickClock* clock) |
| 17 : clock_(clock), fail_on_next_task_(false) {} | 17 : clock_(clock), fail_on_next_task_(false) {} |
| 18 | 18 |
| 19 FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {} | 19 FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {} |
| 20 | 20 |
| 21 bool FakeSingleThreadTaskRunner::PostDelayedTask( | 21 bool FakeSingleThreadTaskRunner::PostDelayedTask( |
| 22 const tracked_objects::Location& from_here, | 22 const tracked_objects::Location& from_here, |
| 23 base::Closure task, | 23 base::OnceClosure task, |
| 24 base::TimeDelta delay) { | 24 base::TimeDelta delay) { |
| 25 if (fail_on_next_task_) { | 25 if (fail_on_next_task_) { |
| 26 LOG(FATAL) << "Infinite task posting loop detected. Possibly caused by " | 26 LOG(FATAL) << "Infinite task posting loop detected. Possibly caused by " |
| 27 << from_here.ToString() << " posting a task with delay " | 27 << from_here.ToString() << " posting a task with delay " |
| 28 << delay.InMicroseconds() << " usec."; | 28 << delay.InMicroseconds() << " usec."; |
| 29 } | 29 } |
| 30 | 30 |
| 31 CHECK_LE(base::TimeDelta(), delay); | 31 CHECK_LE(base::TimeDelta(), delay); |
| 32 const base::TimeTicks run_time = clock_->NowTicks() + delay; | 32 const base::TimeTicks run_time = clock_->NowTicks() + delay; |
| 33 | 33 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 62 void FakeSingleThreadTaskRunner::RunTasks() { | 62 void FakeSingleThreadTaskRunner::RunTasks() { |
| 63 while (true) { | 63 while (true) { |
| 64 // Run all tasks equal or older than current time. | 64 // Run all tasks equal or older than current time. |
| 65 const auto it = tasks_.begin(); | 65 const auto it = tasks_.begin(); |
| 66 if (it == tasks_.end()) | 66 if (it == tasks_.end()) |
| 67 return; // No more tasks. | 67 return; // No more tasks. |
| 68 | 68 |
| 69 if (clock_->NowTicks() < it->first.first) | 69 if (clock_->NowTicks() < it->first.first) |
| 70 return; | 70 return; |
| 71 | 71 |
| 72 base::Closure task = std::move(it->second); | 72 base::OnceClosure task = std::move(it->second); |
| 73 tasks_.erase(it); | 73 tasks_.erase(it); |
| 74 std::move(task).Run(); | 74 std::move(task).Run(); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) { | 78 void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) { |
| 79 CHECK_LE(base::TimeDelta(), t); | 79 CHECK_LE(base::TimeDelta(), t); |
| 80 const base::TimeTicks run_until = clock_->NowTicks() + t; | 80 const base::TimeTicks run_until = clock_->NowTicks() + t; |
| 81 | 81 |
| 82 while (1) { | 82 while (1) { |
| 83 // Run up to 100000 tasks that were scheduled to run during the sleep | 83 // Run up to 100000 tasks that were scheduled to run during the sleep |
| 84 // period. 100000 should be enough for everybody (see comments below). | 84 // period. 100000 should be enough for everybody (see comments below). |
| 85 for (int i = 0; i < 100000; i++) { | 85 for (int i = 0; i < 100000; i++) { |
| 86 const auto it = tasks_.begin(); | 86 const auto it = tasks_.begin(); |
| 87 if (it == tasks_.end() || run_until < it->first.first) { | 87 if (it == tasks_.end() || run_until < it->first.first) { |
| 88 clock_->Advance(run_until - clock_->NowTicks()); | 88 clock_->Advance(run_until - clock_->NowTicks()); |
| 89 return; | 89 return; |
| 90 } | 90 } |
| 91 | 91 |
| 92 clock_->Advance(it->first.first - clock_->NowTicks()); | 92 clock_->Advance(it->first.first - clock_->NowTicks()); |
| 93 const base::Closure task = it->second; | 93 base::OnceClosure task = std::move(it->second); |
| 94 tasks_.erase(it); | 94 tasks_.erase(it); |
| 95 task.Run(); | 95 std::move(task).Run(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // If this point is reached, there's likely some sort of case where a new | 98 // If this point is reached, there's likely some sort of case where a new |
| 99 // non-delayed task is being posted every time a task is popped and invoked | 99 // non-delayed task is being posted every time a task is popped and invoked |
| 100 // from the queue. If that happens, set fail_on_next_task_ to true and throw | 100 // from the queue. If that happens, set fail_on_next_task_ to true and throw |
| 101 // an error when the next task is posted, where we might be able to identify | 101 // an error when the next task is posted, where we might be able to identify |
| 102 // the caller causing the problem via logging. | 102 // the caller causing the problem via logging. |
| 103 fail_on_next_task_ = true; | 103 fail_on_next_task_ = true; |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask( | 107 bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask( |
| 108 const tracked_objects::Location& from_here, | 108 const tracked_objects::Location& from_here, |
| 109 base::Closure task, | 109 base::OnceClosure task, |
| 110 base::TimeDelta delay) { | 110 base::TimeDelta delay) { |
| 111 NOTIMPLEMENTED(); | 111 NOTIMPLEMENTED(); |
| 112 return false; | 112 return false; |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace media | 115 } // namespace media |
| OLD | NEW |