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
3 // found in the LICENSE file.
5 #include "base/task/cancelable_task_tracker.h"
10 #include "base/callback_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/synchronization/cancellation_flag.h"
16 #include "base/task_runner.h"
19 using base::CancellationFlag
;
22 using base::TaskRunner
;
26 void RunIfNotCanceled(const CancellationFlag
* flag
, const Closure
& task
) {
31 void RunIfNotCanceledThenUntrack(const CancellationFlag
* flag
,
33 const Closure
& untrack
) {
34 RunIfNotCanceled(flag
, task
);
38 bool IsCanceled(const CancellationFlag
* flag
,
39 base::ScopedClosureRunner
* cleanup_runner
) {
43 void RunAndDeleteFlag(const Closure
& closure
, const CancellationFlag
* flag
) {
48 void RunOrPostToTaskRunner(TaskRunner
* task_runner
, const Closure
& closure
) {
49 if (task_runner
->RunsTasksOnCurrentThread())
52 task_runner
->PostTask(FROM_HERE
, closure
);
60 const CancelableTaskTracker::TaskId
CancelableTaskTracker::kBadTaskId
= 0;
62 CancelableTaskTracker::CancelableTaskTracker()
63 : weak_factory_(this), next_id_(1) {}
65 CancelableTaskTracker::~CancelableTaskTracker() {
66 DCHECK(thread_checker_
.CalledOnValidThread());
71 CancelableTaskTracker::TaskId
CancelableTaskTracker::PostTask(
72 TaskRunner
* task_runner
,
73 const tracked_objects::Location
& from_here
,
74 const Closure
& task
) {
75 DCHECK(thread_checker_
.CalledOnValidThread());
77 return PostTaskAndReply(task_runner
, from_here
, task
, Bind(&base::DoNothing
));
80 CancelableTaskTracker::TaskId
CancelableTaskTracker::PostTaskAndReply(
81 TaskRunner
* task_runner
,
82 const tracked_objects::Location
& from_here
,
84 const Closure
& reply
) {
85 DCHECK(thread_checker_
.CalledOnValidThread());
87 // We need a MessageLoop to run reply.
88 DCHECK(base::MessageLoopProxy::current().get());
90 // Owned by reply callback below.
91 CancellationFlag
* flag
= new CancellationFlag();
94 next_id_
++; // int64 is big enough that we ignore the potential overflow.
96 const Closure
& untrack_closure
=
97 Bind(&CancelableTaskTracker::Untrack
, weak_factory_
.GetWeakPtr(), id
);
99 task_runner
->PostTaskAndReply(from_here
,
100 Bind(&RunIfNotCanceled
, flag
, task
),
101 Bind(&RunIfNotCanceledThenUntrack
,
113 CancelableTaskTracker::TaskId
CancelableTaskTracker::NewTrackedTaskId(
114 IsCanceledCallback
* is_canceled_cb
) {
115 DCHECK(thread_checker_
.CalledOnValidThread());
116 DCHECK(base::MessageLoopProxy::current().get());
118 TaskId id
= next_id_
;
119 next_id_
++; // int64 is big enough that we ignore the potential overflow.
121 // Will be deleted by |untrack_and_delete_flag| after Untrack().
122 CancellationFlag
* flag
= new CancellationFlag();
124 Closure untrack_and_delete_flag
= Bind(
126 Bind(&CancelableTaskTracker::Untrack
, weak_factory_
.GetWeakPtr(), id
),
129 // Will always run |untrack_and_delete_flag| on current MessageLoop.
130 base::ScopedClosureRunner
* untrack_and_delete_flag_runner
=
131 new base::ScopedClosureRunner(Bind(&RunOrPostToTaskRunner
,
132 base::MessageLoopProxy::current(),
133 untrack_and_delete_flag
));
136 Bind(&IsCanceled
, flag
, base::Owned(untrack_and_delete_flag_runner
));
142 void CancelableTaskTracker::TryCancel(TaskId id
) {
143 DCHECK(thread_checker_
.CalledOnValidThread());
145 hash_map
<TaskId
, CancellationFlag
*>::const_iterator it
= task_flags_
.find(id
);
146 if (it
== task_flags_
.end()) {
147 // Two possibilities:
149 // 1. The task has already been untracked.
150 // 2. The TaskId is bad or unknown.
152 // Since this function is best-effort, it's OK to ignore these.
158 void CancelableTaskTracker::TryCancelAll() {
159 DCHECK(thread_checker_
.CalledOnValidThread());
161 for (hash_map
<TaskId
, CancellationFlag
*>::const_iterator it
=
163 it
!= task_flags_
.end();
169 bool CancelableTaskTracker::HasTrackedTasks() const {
170 DCHECK(thread_checker_
.CalledOnValidThread());
171 return !task_flags_
.empty();
174 void CancelableTaskTracker::Track(TaskId id
, CancellationFlag
* flag
) {
175 DCHECK(thread_checker_
.CalledOnValidThread());
177 bool success
= task_flags_
.insert(std::make_pair(id
, flag
)).second
;
181 void CancelableTaskTracker::Untrack(TaskId id
) {
182 DCHECK(thread_checker_
.CalledOnValidThread());
183 size_t num
= task_flags_
.erase(id
);