OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/deferred_sequenced_task_runner.h" | 5 #include "base/deferred_sequenced_task_runner.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 | 11 |
10 namespace base { | 12 namespace base { |
11 | 13 |
12 DeferredSequencedTaskRunner::DeferredTask::DeferredTask() | 14 DeferredSequencedTaskRunner::DeferredTask::DeferredTask() |
13 : is_non_nestable(false) { | 15 : is_non_nestable(false) { |
14 } | 16 } |
15 | 17 |
16 DeferredSequencedTaskRunner::DeferredTask::DeferredTask( | 18 DeferredSequencedTaskRunner::DeferredTask::DeferredTask(DeferredTask&& other) = |
17 const DeferredTask& other) = default; | 19 default; |
18 | 20 |
19 DeferredSequencedTaskRunner::DeferredTask::~DeferredTask() { | 21 DeferredSequencedTaskRunner::DeferredTask::~DeferredTask() { |
20 } | 22 } |
21 | 23 |
| 24 DeferredSequencedTaskRunner::DeferredTask& |
| 25 DeferredSequencedTaskRunner::DeferredTask::operator=(DeferredTask&& other) = |
| 26 default; |
| 27 |
22 DeferredSequencedTaskRunner::DeferredSequencedTaskRunner( | 28 DeferredSequencedTaskRunner::DeferredSequencedTaskRunner( |
23 scoped_refptr<SequencedTaskRunner> target_task_runner) | 29 scoped_refptr<SequencedTaskRunner> target_task_runner) |
24 : started_(false), target_task_runner_(std::move(target_task_runner)) {} | 30 : started_(false), target_task_runner_(std::move(target_task_runner)) {} |
25 | 31 |
26 DeferredSequencedTaskRunner::~DeferredSequencedTaskRunner() { | 32 DeferredSequencedTaskRunner::~DeferredSequencedTaskRunner() { |
27 } | 33 } |
28 | 34 |
29 bool DeferredSequencedTaskRunner::PostDelayedTask( | 35 bool DeferredSequencedTaskRunner::PostDelayedTask( |
30 const tracked_objects::Location& from_here, | 36 const tracked_objects::Location& from_here, |
31 const Closure& task, | 37 Closure task, |
32 TimeDelta delay) { | 38 TimeDelta delay) { |
33 AutoLock lock(lock_); | 39 AutoLock lock(lock_); |
34 if (started_) { | 40 if (started_) { |
35 DCHECK(deferred_tasks_queue_.empty()); | 41 DCHECK(deferred_tasks_queue_.empty()); |
36 return target_task_runner_->PostDelayedTask(from_here, task, delay); | 42 return target_task_runner_->PostDelayedTask(from_here, std::move(task), |
| 43 delay); |
37 } | 44 } |
38 | 45 |
39 QueueDeferredTask(from_here, task, delay, false /* is_non_nestable */); | 46 QueueDeferredTask(from_here, std::move(task), delay, |
| 47 false /* is_non_nestable */); |
40 return true; | 48 return true; |
41 } | 49 } |
42 | 50 |
43 bool DeferredSequencedTaskRunner::RunsTasksOnCurrentThread() const { | 51 bool DeferredSequencedTaskRunner::RunsTasksOnCurrentThread() const { |
44 return target_task_runner_->RunsTasksOnCurrentThread(); | 52 return target_task_runner_->RunsTasksOnCurrentThread(); |
45 } | 53 } |
46 | 54 |
47 bool DeferredSequencedTaskRunner::PostNonNestableDelayedTask( | 55 bool DeferredSequencedTaskRunner::PostNonNestableDelayedTask( |
48 const tracked_objects::Location& from_here, | 56 const tracked_objects::Location& from_here, |
49 const Closure& task, | 57 Closure task, |
50 TimeDelta delay) { | 58 TimeDelta delay) { |
51 AutoLock lock(lock_); | 59 AutoLock lock(lock_); |
52 if (started_) { | 60 if (started_) { |
53 DCHECK(deferred_tasks_queue_.empty()); | 61 DCHECK(deferred_tasks_queue_.empty()); |
54 return target_task_runner_->PostNonNestableDelayedTask(from_here, | 62 return target_task_runner_->PostNonNestableDelayedTask( |
55 task, | 63 from_here, std::move(task), delay); |
56 delay); | |
57 } | 64 } |
58 QueueDeferredTask(from_here, task, delay, true /* is_non_nestable */); | 65 QueueDeferredTask(from_here, std::move(task), delay, |
| 66 true /* is_non_nestable */); |
59 return true; | 67 return true; |
60 } | 68 } |
61 | 69 |
62 void DeferredSequencedTaskRunner::QueueDeferredTask( | 70 void DeferredSequencedTaskRunner::QueueDeferredTask( |
63 const tracked_objects::Location& from_here, | 71 const tracked_objects::Location& from_here, |
64 const Closure& task, | 72 Closure task, |
65 TimeDelta delay, | 73 TimeDelta delay, |
66 bool is_non_nestable) { | 74 bool is_non_nestable) { |
67 DeferredTask deferred_task; | 75 DeferredTask deferred_task; |
68 deferred_task.posted_from = from_here; | 76 deferred_task.posted_from = from_here; |
69 deferred_task.task = task; | 77 deferred_task.task = std::move(task); |
70 deferred_task.delay = delay; | 78 deferred_task.delay = delay; |
71 deferred_task.is_non_nestable = is_non_nestable; | 79 deferred_task.is_non_nestable = is_non_nestable; |
72 deferred_tasks_queue_.push_back(deferred_task); | 80 deferred_tasks_queue_.push_back(std::move(deferred_task)); |
73 } | 81 } |
74 | 82 |
75 | |
76 void DeferredSequencedTaskRunner::Start() { | 83 void DeferredSequencedTaskRunner::Start() { |
77 AutoLock lock(lock_); | 84 AutoLock lock(lock_); |
78 DCHECK(!started_); | 85 DCHECK(!started_); |
79 started_ = true; | 86 started_ = true; |
80 for (std::vector<DeferredTask>::iterator i = deferred_tasks_queue_.begin(); | 87 for (std::vector<DeferredTask>::iterator i = deferred_tasks_queue_.begin(); |
81 i != deferred_tasks_queue_.end(); | 88 i != deferred_tasks_queue_.end(); |
82 ++i) { | 89 ++i) { |
83 const DeferredTask& task = *i; | 90 DeferredTask& task = *i; |
84 if (task.is_non_nestable) { | 91 if (task.is_non_nestable) { |
85 target_task_runner_->PostNonNestableDelayedTask(task.posted_from, | 92 target_task_runner_->PostNonNestableDelayedTask( |
86 task.task, | 93 task.posted_from, std::move(task.task), task.delay); |
87 task.delay); | |
88 } else { | 94 } else { |
89 target_task_runner_->PostDelayedTask(task.posted_from, | 95 target_task_runner_->PostDelayedTask(task.posted_from, |
90 task.task, | 96 std::move(task.task), task.delay); |
91 task.delay); | |
92 } | 97 } |
93 // Replace the i-th element in the |deferred_tasks_queue_| with an empty | |
94 // |DelayedTask| to ensure that |task| is destroyed before the next task | |
95 // is posted. | |
96 *i = DeferredTask(); | |
97 } | 98 } |
98 deferred_tasks_queue_.clear(); | 99 deferred_tasks_queue_.clear(); |
99 } | 100 } |
100 | 101 |
101 } // namespace base | 102 } // namespace base |
OLD | NEW |