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 "base/task/cancelable_task_tracker.h" | 5 #include "base/task/cancelable_task_tracker.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 const tracked_objects::Location& from_here, | 67 const tracked_objects::Location& from_here, |
68 const Closure& task) { | 68 const Closure& task) { |
69 DCHECK(sequence_checker_.CalledOnValidSequence()); | 69 DCHECK(sequence_checker_.CalledOnValidSequence()); |
70 | 70 |
71 return PostTaskAndReply(task_runner, from_here, task, Bind(&base::DoNothing)); | 71 return PostTaskAndReply(task_runner, from_here, task, Bind(&base::DoNothing)); |
72 } | 72 } |
73 | 73 |
74 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( | 74 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( |
75 TaskRunner* task_runner, | 75 TaskRunner* task_runner, |
76 const tracked_objects::Location& from_here, | 76 const tracked_objects::Location& from_here, |
77 const Closure& task, | 77 Closure task, |
78 const Closure& reply) { | 78 Closure reply) { |
79 DCHECK(sequence_checker_.CalledOnValidSequence()); | 79 DCHECK(sequence_checker_.CalledOnValidSequence()); |
80 | 80 |
81 // We need a SequencedTaskRunnerHandle to run |reply|. | 81 // We need a SequencedTaskRunnerHandle to run |reply|. |
82 DCHECK(base::SequencedTaskRunnerHandle::IsSet()); | 82 DCHECK(base::SequencedTaskRunnerHandle::IsSet()); |
83 | 83 |
84 // Owned by reply callback below. | 84 // Owned by reply callback below. |
85 CancellationFlag* flag = new CancellationFlag(); | 85 CancellationFlag* flag = new CancellationFlag(); |
86 | 86 |
87 TaskId id = next_id_; | 87 TaskId id = next_id_; |
88 next_id_++; // int64_t is big enough that we ignore the potential overflow. | 88 next_id_++; // int64_t is big enough that we ignore the potential overflow. |
89 | 89 |
90 const Closure& untrack_closure = | 90 Closure untrack_closure = |
91 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); | 91 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); |
92 bool success = | 92 bool success = task_runner->PostTaskAndReply( |
93 task_runner->PostTaskAndReply(from_here, | 93 from_here, Bind(&RunIfNotCanceled, flag, std::move(task)), |
94 Bind(&RunIfNotCanceled, flag, task), | 94 Bind(&RunIfNotCanceledThenUntrack, base::Owned(flag), std::move(reply), |
95 Bind(&RunIfNotCanceledThenUntrack, | 95 std::move(untrack_closure))); |
96 base::Owned(flag), | |
97 reply, | |
98 untrack_closure)); | |
99 | 96 |
100 if (!success) | 97 if (!success) |
101 return kBadTaskId; | 98 return kBadTaskId; |
102 | 99 |
103 Track(id, flag); | 100 Track(id, flag); |
104 return id; | 101 return id; |
105 } | 102 } |
106 | 103 |
107 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( | 104 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( |
108 IsCanceledCallback* is_canceled_cb) { | 105 IsCanceledCallback* is_canceled_cb) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 DCHECK(success); | 170 DCHECK(success); |
174 } | 171 } |
175 | 172 |
176 void CancelableTaskTracker::Untrack(TaskId id) { | 173 void CancelableTaskTracker::Untrack(TaskId id) { |
177 DCHECK(sequence_checker_.CalledOnValidSequence()); | 174 DCHECK(sequence_checker_.CalledOnValidSequence()); |
178 size_t num = task_flags_.erase(id); | 175 size_t num = task_flags_.erase(id); |
179 DCHECK_EQ(1u, num); | 176 DCHECK_EQ(1u, num); |
180 } | 177 } |
181 | 178 |
182 } // namespace base | 179 } // namespace base |
OLD | NEW |