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> |
| 8 |
7 #include <utility> | 9 #include <utility> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
12 #include "base/location.h" | 14 #include "base/location.h" |
13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
14 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
15 #include "base/synchronization/cancellation_flag.h" | 17 #include "base/synchronization/cancellation_flag.h" |
16 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 const Closure& reply) { | 87 const Closure& reply) { |
86 DCHECK(thread_checker_.CalledOnValidThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
87 | 89 |
88 // We need a MessageLoop to run reply. | 90 // We need a MessageLoop to run reply. |
89 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | 91 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); |
90 | 92 |
91 // Owned by reply callback below. | 93 // Owned by reply callback below. |
92 CancellationFlag* flag = new CancellationFlag(); | 94 CancellationFlag* flag = new CancellationFlag(); |
93 | 95 |
94 TaskId id = next_id_; | 96 TaskId id = next_id_; |
95 next_id_++; // int64 is big enough that we ignore the potential overflow. | 97 next_id_++; // int64_t is big enough that we ignore the potential overflow. |
96 | 98 |
97 const Closure& untrack_closure = | 99 const Closure& untrack_closure = |
98 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); | 100 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); |
99 bool success = | 101 bool success = |
100 task_runner->PostTaskAndReply(from_here, | 102 task_runner->PostTaskAndReply(from_here, |
101 Bind(&RunIfNotCanceled, flag, task), | 103 Bind(&RunIfNotCanceled, flag, task), |
102 Bind(&RunIfNotCanceledThenUntrack, | 104 Bind(&RunIfNotCanceledThenUntrack, |
103 base::Owned(flag), | 105 base::Owned(flag), |
104 reply, | 106 reply, |
105 untrack_closure)); | 107 untrack_closure)); |
106 | 108 |
107 if (!success) | 109 if (!success) |
108 return kBadTaskId; | 110 return kBadTaskId; |
109 | 111 |
110 Track(id, flag); | 112 Track(id, flag); |
111 return id; | 113 return id; |
112 } | 114 } |
113 | 115 |
114 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( | 116 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( |
115 IsCanceledCallback* is_canceled_cb) { | 117 IsCanceledCallback* is_canceled_cb) { |
116 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
117 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | 119 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); |
118 | 120 |
119 TaskId id = next_id_; | 121 TaskId id = next_id_; |
120 next_id_++; // int64 is big enough that we ignore the potential overflow. | 122 next_id_++; // int64_t is big enough that we ignore the potential overflow. |
121 | 123 |
122 // Will be deleted by |untrack_and_delete_flag| after Untrack(). | 124 // Will be deleted by |untrack_and_delete_flag| after Untrack(). |
123 CancellationFlag* flag = new CancellationFlag(); | 125 CancellationFlag* flag = new CancellationFlag(); |
124 | 126 |
125 Closure untrack_and_delete_flag = Bind( | 127 Closure untrack_and_delete_flag = Bind( |
126 &RunAndDeleteFlag, | 128 &RunAndDeleteFlag, |
127 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id), | 129 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id), |
128 flag); | 130 flag); |
129 | 131 |
130 // Will always run |untrack_and_delete_flag| on current MessageLoop. | 132 // Will always run |untrack_and_delete_flag| on current MessageLoop. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 DCHECK(success); | 181 DCHECK(success); |
180 } | 182 } |
181 | 183 |
182 void CancelableTaskTracker::Untrack(TaskId id) { | 184 void CancelableTaskTracker::Untrack(TaskId id) { |
183 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
184 size_t num = task_flags_.erase(id); | 186 size_t num = task_flags_.erase(id); |
185 DCHECK_EQ(1u, num); | 187 DCHECK_EQ(1u, num); |
186 } | 188 } |
187 | 189 |
188 } // namespace base | 190 } // namespace base |
OLD | NEW |