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 "components/domain_reliability/dispatcher.h" | 5 #include "components/domain_reliability/dispatcher.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ptr_util.h" | |
11 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
12 #include "base/stl_util.h" | |
13 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
14 #include "components/domain_reliability/util.h" | 15 #include "components/domain_reliability/util.h" |
15 | 16 |
16 namespace domain_reliability { | 17 namespace domain_reliability { |
17 | 18 |
18 struct DomainReliabilityDispatcher::Task { | 19 struct DomainReliabilityDispatcher::Task { |
19 Task(const base::Closure& closure, | 20 Task(const base::Closure& closure, |
20 std::unique_ptr<MockableTime::Timer> timer, | 21 std::unique_ptr<MockableTime::Timer> timer, |
21 base::TimeDelta min_delay, | 22 base::TimeDelta min_delay, |
22 base::TimeDelta max_delay); | 23 base::TimeDelta max_delay); |
(...skipping 15 matching lines...) Expand all Loading... | |
38 timer(std::move(timer)), | 39 timer(std::move(timer)), |
39 min_delay(min_delay), | 40 min_delay(min_delay), |
40 max_delay(max_delay), | 41 max_delay(max_delay), |
41 eligible(false) {} | 42 eligible(false) {} |
42 | 43 |
43 DomainReliabilityDispatcher::Task::~Task() {} | 44 DomainReliabilityDispatcher::Task::~Task() {} |
44 | 45 |
45 DomainReliabilityDispatcher::DomainReliabilityDispatcher(MockableTime* time) | 46 DomainReliabilityDispatcher::DomainReliabilityDispatcher(MockableTime* time) |
46 : time_(time) {} | 47 : time_(time) {} |
47 | 48 |
48 DomainReliabilityDispatcher::~DomainReliabilityDispatcher() { | 49 DomainReliabilityDispatcher::~DomainReliabilityDispatcher() {} |
49 // TODO(juliatuttle): STLElementDeleter? | |
50 base::STLDeleteElements(&tasks_); | |
51 } | |
52 | 50 |
53 void DomainReliabilityDispatcher::ScheduleTask( | 51 void DomainReliabilityDispatcher::ScheduleTask( |
54 const base::Closure& closure, | 52 const base::Closure& closure, |
55 base::TimeDelta min_delay, | 53 base::TimeDelta min_delay, |
56 base::TimeDelta max_delay) { | 54 base::TimeDelta max_delay) { |
57 DCHECK(!closure.is_null()); | 55 DCHECK(!closure.is_null()); |
58 // Would be DCHECK_LE, but you can't << a TimeDelta. | 56 // Would be DCHECK_LE, but you can't << a TimeDelta. |
59 DCHECK(min_delay <= max_delay); | 57 DCHECK(min_delay <= max_delay); |
60 | 58 |
61 Task* task = new Task(closure, time_->CreateTimer(), min_delay, max_delay); | 59 std::unique_ptr<Task> owned_task = base::MakeUnique<Task>( |
62 tasks_.insert(task); | 60 closure, time_->CreateTimer(), min_delay, max_delay); |
61 Task* task = owned_task.get(); | |
62 tasks_.insert(std::move(owned_task)); | |
63 if (max_delay.InMicroseconds() < 0) | 63 if (max_delay.InMicroseconds() < 0) |
64 RunAndDeleteTask(task); | 64 RunAndDeleteTask(task); |
65 else if (min_delay.InMicroseconds() < 0) | 65 else if (min_delay.InMicroseconds() < 0) |
66 MakeTaskEligible(task); | 66 MakeTaskEligible(task); |
67 else | 67 else |
68 MakeTaskWaiting(task); | 68 MakeTaskWaiting(task); |
69 } | 69 } |
70 | 70 |
71 void DomainReliabilityDispatcher::RunEligibleTasks() { | 71 void DomainReliabilityDispatcher::RunEligibleTasks() { |
72 // Move all eligible tasks to a separate set so that eligible_tasks_.erase in | 72 // Move all eligible tasks to a separate set so that eligible_tasks_.erase in |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 base::Unretained(this), | 106 base::Unretained(this), |
107 task)); | 107 task)); |
108 } | 108 } |
109 | 109 |
110 void DomainReliabilityDispatcher::RunAndDeleteTask(Task* task) { | 110 void DomainReliabilityDispatcher::RunAndDeleteTask(Task* task) { |
111 DCHECK(task); | 111 DCHECK(task); |
112 DCHECK(!task->closure.is_null()); | 112 DCHECK(!task->closure.is_null()); |
113 task->closure.Run(); | 113 task->closure.Run(); |
114 if (task->eligible) | 114 if (task->eligible) |
115 eligible_tasks_.erase(task); | 115 eligible_tasks_.erase(task); |
116 tasks_.erase(task); | 116 |
117 delete task; | 117 auto it = std::find_if(tasks_.begin(), tasks_.end(), |
118 [task](const std::unique_ptr<Task>& task_ptr) { | |
119 return task_ptr.get() == task; | |
120 }); | |
davidben
2016/08/30 00:52:54
This switches from O(lg N) to O(N) which is a bit
Julia Tuttle
2016/08/30 13:46:37
Shouldn't be an issue. At absolute worst it will b
davidben
2016/08/30 13:49:16
Hrm. Wouldn't that bound increase once we have the
Julia Tuttle
2016/08/30 13:59:53
Nah, I'm hoping that ends up going through the Rep
| |
121 | |
122 DCHECK(it != tasks_.end()); | |
123 tasks_.erase(it); | |
118 } | 124 } |
119 | 125 |
120 } // namespace domain_reliability | 126 } // namespace domain_reliability |
OLD | NEW |