Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(530)

Side by Side Diff: media/base/fake_single_thread_task_runner.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: s/base::ResetAndReturn/std::move/ Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
9 #include "base/callback_helpers.h"
7 #include "base/location.h" 10 #include "base/location.h"
8 #include "base/logging.h" 11 #include "base/logging.h"
9 #include "base/time/tick_clock.h" 12 #include "base/time/tick_clock.h"
10 13
11 namespace media { 14 namespace media {
12 15
13 FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner( 16 FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner(
14 base::SimpleTestTickClock* clock) 17 base::SimpleTestTickClock* clock)
15 : clock_(clock), fail_on_next_task_(false) {} 18 : clock_(clock), fail_on_next_task_(false) {}
16 19
17 FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {} 20 FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {}
18 21
19 bool FakeSingleThreadTaskRunner::PostDelayedTask( 22 bool FakeSingleThreadTaskRunner::PostDelayedTask(
20 const tracked_objects::Location& from_here, 23 const tracked_objects::Location& from_here,
21 const base::Closure& task, 24 base::Closure task,
22 base::TimeDelta delay) { 25 base::TimeDelta delay) {
23 if (fail_on_next_task_) { 26 if (fail_on_next_task_) {
24 LOG(FATAL) << "Infinite task posting loop detected. Possibly caused by " 27 LOG(FATAL) << "Infinite task posting loop detected. Possibly caused by "
25 << from_here.ToString() << " posting a task with delay " 28 << from_here.ToString() << " posting a task with delay "
26 << delay.InMicroseconds() << " usec."; 29 << delay.InMicroseconds() << " usec.";
27 } 30 }
28 31
29 CHECK_LE(base::TimeDelta(), delay); 32 CHECK_LE(base::TimeDelta(), delay);
30 const base::TimeTicks run_time = clock_->NowTicks() + delay; 33 const base::TimeTicks run_time = clock_->NowTicks() + delay;
31 34
32 // If there are one or more tasks with the exact same run time, schedule this 35 // 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 36 // task to occur after them. This mimics the FIFO ordering behavior when
34 // scheduling delayed tasks to be run via base::MessageLoop in a 37 // scheduling delayed tasks to be run via base::MessageLoop in a
35 // multi-threaded application. 38 // multi-threaded application.
36 if (!tasks_.empty()) { 39 if (!tasks_.empty()) {
37 const auto after_it = tasks_.lower_bound( 40 const auto after_it = tasks_.lower_bound(
38 TaskKey(run_time + base::TimeDelta::FromMicroseconds(1), 0)); 41 TaskKey(run_time + base::TimeDelta::FromMicroseconds(1), 0));
39 if (after_it != tasks_.begin()) { 42 if (after_it != tasks_.begin()) {
40 auto it = after_it; 43 auto it = after_it;
41 --it; 44 --it;
42 if (it->first.first == run_time) { 45 if (it->first.first == run_time) {
43 tasks_.insert( 46 tasks_.insert(after_it /* hint */,
44 after_it /* hint */, 47 std::make_pair(TaskKey(run_time, it->first.second + 1),
45 std::make_pair(TaskKey(run_time, it->first.second + 1), task)); 48 std::move(task)));
46 return true; 49 return true;
47 } 50 }
48 } 51 }
49 } 52 }
50 53
51 // No tasks have the exact same run time, so just do a simple insert. 54 // 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)); 55 tasks_.insert(std::make_pair(TaskKey(run_time, 0), std::move(task)));
53 return true; 56 return true;
54 } 57 }
55 58
56 bool FakeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { 59 bool FakeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const {
57 return true; 60 return true;
58 } 61 }
59 62
60 void FakeSingleThreadTaskRunner::RunTasks() { 63 void FakeSingleThreadTaskRunner::RunTasks() {
61 while (true) { 64 while (true) {
62 // Run all tasks equal or older than current time. 65 // Run all tasks equal or older than current time.
63 const auto it = tasks_.begin(); 66 const auto it = tasks_.begin();
64 if (it == tasks_.end()) 67 if (it == tasks_.end())
65 return; // No more tasks. 68 return; // No more tasks.
66 69
67 if (clock_->NowTicks() < it->first.first) 70 if (clock_->NowTicks() < it->first.first)
68 return; 71 return;
69 72
70 const base::Closure task = it->second; 73 base::Closure task = std::move(it->second);
71 tasks_.erase(it); 74 tasks_.erase(it);
72 task.Run(); 75 std::move(task).Run();
73 } 76 }
74 } 77 }
75 78
76 void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) { 79 void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) {
77 CHECK_LE(base::TimeDelta(), t); 80 CHECK_LE(base::TimeDelta(), t);
78 const base::TimeTicks run_until = clock_->NowTicks() + t; 81 const base::TimeTicks run_until = clock_->NowTicks() + t;
79 82
80 while (1) { 83 while (1) {
81 // Run up to 100000 tasks that were scheduled to run during the sleep 84 // Run up to 100000 tasks that were scheduled to run during the sleep
82 // period. 100000 should be enough for everybody (see comments below). 85 // period. 100000 should be enough for everybody (see comments below).
(...skipping 14 matching lines...) Expand all
97 // non-delayed task is being posted every time a task is popped and invoked 100 // 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 101 // 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 102 // an error when the next task is posted, where we might be able to identify
100 // the caller causing the problem via logging. 103 // the caller causing the problem via logging.
101 fail_on_next_task_ = true; 104 fail_on_next_task_ = true;
102 } 105 }
103 } 106 }
104 107
105 bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask( 108 bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask(
106 const tracked_objects::Location& from_here, 109 const tracked_objects::Location& from_here,
107 const base::Closure& task, 110 base::Closure task,
108 base::TimeDelta delay) { 111 base::TimeDelta delay) {
109 NOTIMPLEMENTED(); 112 NOTIMPLEMENTED();
110 return false; 113 return false;
111 } 114 }
112 115
113 } // namespace media 116 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698