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