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> |
| 8 |
7 #include "base/location.h" | 9 #include "base/location.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/time/tick_clock.h" | 11 #include "base/time/tick_clock.h" |
10 | 12 |
11 namespace media { | 13 namespace media { |
12 | 14 |
13 FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner( | 15 FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner( |
14 base::SimpleTestTickClock* clock) | 16 base::SimpleTestTickClock* clock) |
15 : clock_(clock), fail_on_next_task_(false) {} | 17 : clock_(clock), fail_on_next_task_(false) {} |
16 | 18 |
17 FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {} | 19 FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {} |
18 | 20 |
19 bool FakeSingleThreadTaskRunner::PostDelayedTask( | 21 bool FakeSingleThreadTaskRunner::PostDelayedTask( |
20 const tracked_objects::Location& from_here, | 22 const tracked_objects::Location& from_here, |
21 const base::Closure& task, | 23 base::Closure task, |
22 base::TimeDelta delay) { | 24 base::TimeDelta delay) { |
23 if (fail_on_next_task_) { | 25 if (fail_on_next_task_) { |
24 LOG(FATAL) << "Infinite task posting loop detected. Possibly caused by " | 26 LOG(FATAL) << "Infinite task posting loop detected. Possibly caused by " |
25 << from_here.ToString() << " posting a task with delay " | 27 << from_here.ToString() << " posting a task with delay " |
26 << delay.InMicroseconds() << " usec."; | 28 << delay.InMicroseconds() << " usec."; |
27 } | 29 } |
28 | 30 |
29 CHECK_LE(base::TimeDelta(), delay); | 31 CHECK_LE(base::TimeDelta(), delay); |
30 const base::TimeTicks run_time = clock_->NowTicks() + delay; | 32 const base::TimeTicks run_time = clock_->NowTicks() + delay; |
31 | 33 |
32 // If there are one or more tasks with the exact same run time, schedule this | 34 // If there are one or more tasks with the exact same run time, schedule this |
33 // task to occur after them. This mimics the FIFO ordering behavior when | 35 // task to occur after them. This mimics the FIFO ordering behavior when |
34 // scheduling delayed tasks to be run via base::MessageLoop in a | 36 // scheduling delayed tasks to be run via base::MessageLoop in a |
35 // multi-threaded application. | 37 // multi-threaded application. |
36 if (!tasks_.empty()) { | 38 if (!tasks_.empty()) { |
37 const auto after_it = tasks_.lower_bound( | 39 const auto after_it = tasks_.lower_bound( |
38 TaskKey(run_time + base::TimeDelta::FromMicroseconds(1), 0)); | 40 TaskKey(run_time + base::TimeDelta::FromMicroseconds(1), 0)); |
39 if (after_it != tasks_.begin()) { | 41 if (after_it != tasks_.begin()) { |
40 auto it = after_it; | 42 auto it = after_it; |
41 --it; | 43 --it; |
42 if (it->first.first == run_time) { | 44 if (it->first.first == run_time) { |
43 tasks_.insert( | 45 tasks_.insert(after_it /* hint */, |
44 after_it /* hint */, | 46 std::make_pair(TaskKey(run_time, it->first.second + 1), |
45 std::make_pair(TaskKey(run_time, it->first.second + 1), task)); | 47 std::move(task))); |
46 return true; | 48 return true; |
47 } | 49 } |
48 } | 50 } |
49 } | 51 } |
50 | 52 |
51 // No tasks have the exact same run time, so just do a simple insert. | 53 // No tasks have the exact same run time, so just do a simple insert. |
52 tasks_.insert(std::make_pair(TaskKey(run_time, 0), task)); | 54 tasks_.insert(std::make_pair(TaskKey(run_time, 0), std::move(task))); |
53 return true; | 55 return true; |
54 } | 56 } |
55 | 57 |
56 bool FakeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { | 58 bool FakeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { |
57 return true; | 59 return true; |
58 } | 60 } |
59 | 61 |
60 void FakeSingleThreadTaskRunner::RunTasks() { | 62 void FakeSingleThreadTaskRunner::RunTasks() { |
61 while (true) { | 63 while (true) { |
62 // Run all tasks equal or older than current time. | 64 // Run all tasks equal or older than current time. |
63 const auto it = tasks_.begin(); | 65 const auto it = tasks_.begin(); |
64 if (it == tasks_.end()) | 66 if (it == tasks_.end()) |
65 return; // No more tasks. | 67 return; // No more tasks. |
66 | 68 |
67 if (clock_->NowTicks() < it->first.first) | 69 if (clock_->NowTicks() < it->first.first) |
68 return; | 70 return; |
69 | 71 |
70 const base::Closure task = it->second; | 72 base::Closure task = std::move(it->second); |
71 tasks_.erase(it); | 73 tasks_.erase(it); |
72 task.Run(); | 74 std::move(task).Run(); |
73 } | 75 } |
74 } | 76 } |
75 | 77 |
76 void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) { | 78 void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) { |
77 CHECK_LE(base::TimeDelta(), t); | 79 CHECK_LE(base::TimeDelta(), t); |
78 const base::TimeTicks run_until = clock_->NowTicks() + t; | 80 const base::TimeTicks run_until = clock_->NowTicks() + t; |
79 | 81 |
80 while (1) { | 82 while (1) { |
81 // 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 |
82 // period. 100000 should be enough for everybody (see comments below). | 84 // period. 100000 should be enough for everybody (see comments below). |
(...skipping 14 matching lines...) Expand all Loading... |
97 // 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 |
98 // 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 |
99 // 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 |
100 // the caller causing the problem via logging. | 102 // the caller causing the problem via logging. |
101 fail_on_next_task_ = true; | 103 fail_on_next_task_ = true; |
102 } | 104 } |
103 } | 105 } |
104 | 106 |
105 bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask( | 107 bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask( |
106 const tracked_objects::Location& from_here, | 108 const tracked_objects::Location& from_here, |
107 const base::Closure& task, | 109 base::Closure task, |
108 base::TimeDelta delay) { | 110 base::TimeDelta delay) { |
109 NOTIMPLEMENTED(); | 111 NOTIMPLEMENTED(); |
110 return false; | 112 return false; |
111 } | 113 } |
112 | 114 |
113 } // namespace media | 115 } // namespace media |
OLD | NEW |