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/single_thread_task_runner.h"
15 #include "base/synchronization/cancellation_flag.h"
16 #include "base/task_runner.h"
17 #include "base/thread_task_runner_handle.h"
20 using base::CancellationFlag
;
23 using base::TaskRunner
;
27 void RunIfNotCanceled(const CancellationFlag
* flag
, const Closure
& task
) {
32 void RunIfNotCanceledThenUntrack(const CancellationFlag
* flag
,
34 const Closure
& untrack
) {
35 RunIfNotCanceled(flag
, task
);
39 bool IsCanceled(const CancellationFlag
* flag
,
40 base::ScopedClosureRunner
* cleanup_runner
) {
44 void RunAndDeleteFlag(const Closure
& closure
, const CancellationFlag
* flag
) {
49 void RunOrPostToTaskRunner(TaskRunner
* task_runner
, const Closure
& closure
) {
50 if (task_runner
->RunsTasksOnCurrentThread())
53 task_runner
->PostTask(FROM_HERE
, closure
);
61 const CancelableTaskTracker::TaskId
CancelableTaskTracker::kBadTaskId
= 0;
63 CancelableTaskTracker::CancelableTaskTracker()
64 : next_id_(1),weak_factory_(this) {}
66 CancelableTaskTracker::~CancelableTaskTracker() {
67 DCHECK(thread_checker_
.CalledOnValidThread());
72 CancelableTaskTracker::TaskId
CancelableTaskTracker::PostTask(
73 TaskRunner
* task_runner
,
74 const tracked_objects::Location
& from_here
,
75 const Closure
& task
) {
76 DCHECK(thread_checker_
.CalledOnValidThread());
78 return PostTaskAndReply(task_runner
, from_here
, task
, Bind(&base::DoNothing
));
81 CancelableTaskTracker::TaskId
CancelableTaskTracker::PostTaskAndReply(
82 TaskRunner
* task_runner
,
83 const tracked_objects::Location
& from_here
,
85 const Closure
& reply
) {
86 DCHECK(thread_checker_
.CalledOnValidThread());
88 // We need a MessageLoop to run reply.
89 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
91 // Owned by reply callback below.
92 CancellationFlag
* flag
= new CancellationFlag();
95 next_id_
++; // int64 is big enough that we ignore the potential overflow.
97 const Closure
& untrack_closure
=
98 Bind(&CancelableTaskTracker::Untrack
, weak_factory_
.GetWeakPtr(), id
);
100 task_runner
->PostTaskAndReply(from_here
,
101 Bind(&RunIfNotCanceled
, flag
, task
),
102 Bind(&RunIfNotCanceledThenUntrack
,
114 CancelableTaskTracker::TaskId
CancelableTaskTracker::NewTrackedTaskId(
115 IsCanceledCallback
* is_canceled_cb
) {
116 DCHECK(thread_checker_
.CalledOnValidThread());
117 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
119 TaskId id
= next_id_
;
120 next_id_
++; // int64 is big enough that we ignore the potential overflow.
122 // Will be deleted by |untrack_and_delete_flag| after Untrack().
123 CancellationFlag
* flag
= new CancellationFlag();
125 Closure untrack_and_delete_flag
= Bind(
127 Bind(&CancelableTaskTracker::Untrack
, weak_factory_
.GetWeakPtr(), id
),
130 // Will always run |untrack_and_delete_flag| on current MessageLoop.
131 base::ScopedClosureRunner
* untrack_and_delete_flag_runner
=
132 new base::ScopedClosureRunner(Bind(&RunOrPostToTaskRunner
,
133 base::ThreadTaskRunnerHandle::Get(),
134 untrack_and_delete_flag
));
137 Bind(&IsCanceled
, flag
, base::Owned(untrack_and_delete_flag_runner
));
143 void CancelableTaskTracker::TryCancel(TaskId id
) {
144 DCHECK(thread_checker_
.CalledOnValidThread());
146 hash_map
<TaskId
, CancellationFlag
*>::const_iterator it
= task_flags_
.find(id
);
147 if (it
== task_flags_
.end()) {
148 // Two possibilities:
150 // 1. The task has already been untracked.
151 // 2. The TaskId is bad or unknown.
153 // Since this function is best-effort, it's OK to ignore these.
159 void CancelableTaskTracker::TryCancelAll() {
160 DCHECK(thread_checker_
.CalledOnValidThread());
162 for (hash_map
<TaskId
, CancellationFlag
*>::const_iterator it
=
164 it
!= task_flags_
.end();
170 bool CancelableTaskTracker::HasTrackedTasks() const {
171 DCHECK(thread_checker_
.CalledOnValidThread());
172 return !task_flags_
.empty();
175 void CancelableTaskTracker::Track(TaskId id
, CancellationFlag
* flag
) {
176 DCHECK(thread_checker_
.CalledOnValidThread());
178 bool success
= task_flags_
.insert(std::make_pair(id
, flag
)).second
;
182 void CancelableTaskTracker::Untrack(TaskId id
) {
183 DCHECK(thread_checker_
.CalledOnValidThread());
184 size_t num
= task_flags_
.erase(id
);