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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/synchronization/cancellation_flag.h" | 15 #include "base/synchronization/cancellation_flag.h" |
16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | |
17 | 18 |
18 using base::Bind; | 19 using base::Bind; |
19 using base::CancellationFlag; | 20 using base::CancellationFlag; |
20 using base::Closure; | 21 using base::Closure; |
21 using base::hash_map; | 22 using base::hash_map; |
22 using base::TaskRunner; | 23 using base::TaskRunner; |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 void RunIfNotCanceled(const CancellationFlag* flag, const Closure& task) { | 27 void RunIfNotCanceled(const CancellationFlag* flag, const Closure& task) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 } | 79 } |
79 | 80 |
80 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( | 81 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( |
81 TaskRunner* task_runner, | 82 TaskRunner* task_runner, |
82 const tracked_objects::Location& from_here, | 83 const tracked_objects::Location& from_here, |
83 const Closure& task, | 84 const Closure& task, |
84 const Closure& reply) { | 85 const Closure& reply) { |
85 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
86 | 87 |
87 // We need a MessageLoop to run reply. | 88 // We need a MessageLoop to run reply. |
88 DCHECK(base::MessageLoopProxy::current().get()); | 89 DCHECK(base::ThreadTaskRunnerHandle::Get()); |
danakj
2015/04/21 20:16:30
Get() already DCHECKs. Do you want to IsSet()?
Sami
2015/04/23 17:48:25
Yes, that makes more sense (checked for any other
| |
89 | 90 |
90 // Owned by reply callback below. | 91 // Owned by reply callback below. |
91 CancellationFlag* flag = new CancellationFlag(); | 92 CancellationFlag* flag = new CancellationFlag(); |
92 | 93 |
93 TaskId id = next_id_; | 94 TaskId id = next_id_; |
94 next_id_++; // int64 is big enough that we ignore the potential overflow. | 95 next_id_++; // int64 is big enough that we ignore the potential overflow. |
95 | 96 |
96 const Closure& untrack_closure = | 97 const Closure& untrack_closure = |
97 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); | 98 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id); |
98 bool success = | 99 bool success = |
99 task_runner->PostTaskAndReply(from_here, | 100 task_runner->PostTaskAndReply(from_here, |
100 Bind(&RunIfNotCanceled, flag, task), | 101 Bind(&RunIfNotCanceled, flag, task), |
101 Bind(&RunIfNotCanceledThenUntrack, | 102 Bind(&RunIfNotCanceledThenUntrack, |
102 base::Owned(flag), | 103 base::Owned(flag), |
103 reply, | 104 reply, |
104 untrack_closure)); | 105 untrack_closure)); |
105 | 106 |
106 if (!success) | 107 if (!success) |
107 return kBadTaskId; | 108 return kBadTaskId; |
108 | 109 |
109 Track(id, flag); | 110 Track(id, flag); |
110 return id; | 111 return id; |
111 } | 112 } |
112 | 113 |
113 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( | 114 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( |
114 IsCanceledCallback* is_canceled_cb) { | 115 IsCanceledCallback* is_canceled_cb) { |
115 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
116 DCHECK(base::MessageLoopProxy::current().get()); | 117 DCHECK(base::ThreadTaskRunnerHandle::Get()); |
danakj
2015/04/21 20:16:30
dittos
Sami
2015/04/23 17:48:25
Done.
| |
117 | 118 |
118 TaskId id = next_id_; | 119 TaskId id = next_id_; |
119 next_id_++; // int64 is big enough that we ignore the potential overflow. | 120 next_id_++; // int64 is big enough that we ignore the potential overflow. |
120 | 121 |
121 // Will be deleted by |untrack_and_delete_flag| after Untrack(). | 122 // Will be deleted by |untrack_and_delete_flag| after Untrack(). |
122 CancellationFlag* flag = new CancellationFlag(); | 123 CancellationFlag* flag = new CancellationFlag(); |
123 | 124 |
124 Closure untrack_and_delete_flag = Bind( | 125 Closure untrack_and_delete_flag = Bind( |
125 &RunAndDeleteFlag, | 126 &RunAndDeleteFlag, |
126 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id), | 127 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id), |
127 flag); | 128 flag); |
128 | 129 |
129 // Will always run |untrack_and_delete_flag| on current MessageLoop. | 130 // Will always run |untrack_and_delete_flag| on current MessageLoop. |
130 base::ScopedClosureRunner* untrack_and_delete_flag_runner = | 131 base::ScopedClosureRunner* untrack_and_delete_flag_runner = |
131 new base::ScopedClosureRunner(Bind(&RunOrPostToTaskRunner, | 132 new base::ScopedClosureRunner(Bind(&RunOrPostToTaskRunner, |
132 base::MessageLoopProxy::current(), | 133 base::ThreadTaskRunnerHandle::Get(), |
133 untrack_and_delete_flag)); | 134 untrack_and_delete_flag)); |
134 | 135 |
135 *is_canceled_cb = | 136 *is_canceled_cb = |
136 Bind(&IsCanceled, flag, base::Owned(untrack_and_delete_flag_runner)); | 137 Bind(&IsCanceled, flag, base::Owned(untrack_and_delete_flag_runner)); |
137 | 138 |
138 Track(id, flag); | 139 Track(id, flag); |
139 return id; | 140 return id; |
140 } | 141 } |
141 | 142 |
142 void CancelableTaskTracker::TryCancel(TaskId id) { | 143 void CancelableTaskTracker::TryCancel(TaskId id) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 DCHECK(success); | 179 DCHECK(success); |
179 } | 180 } |
180 | 181 |
181 void CancelableTaskTracker::Untrack(TaskId id) { | 182 void CancelableTaskTracker::Untrack(TaskId id) { |
182 DCHECK(thread_checker_.CalledOnValidThread()); | 183 DCHECK(thread_checker_.CalledOnValidThread()); |
183 size_t num = task_flags_.erase(id); | 184 size_t num = task_flags_.erase(id); |
184 DCHECK_EQ(1u, num); | 185 DCHECK_EQ(1u, num); |
185 } | 186 } |
186 | 187 |
187 } // namespace base | 188 } // namespace base |
OLD | NEW |