Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / base / task / cancelable_task_tracker.cc
bloba2e4799f43854485f9d077c27155683ab66ccf47
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"
7 #include <utility>
9 #include "base/bind.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"
19 using base::Bind;
20 using base::CancellationFlag;
21 using base::Closure;
22 using base::hash_map;
23 using base::TaskRunner;
25 namespace {
27 void RunIfNotCanceled(const CancellationFlag* flag, const Closure& task) {
28 if (!flag->IsSet())
29 task.Run();
32 void RunIfNotCanceledThenUntrack(const CancellationFlag* flag,
33 const Closure& task,
34 const Closure& untrack) {
35 RunIfNotCanceled(flag, task);
36 untrack.Run();
39 bool IsCanceled(const CancellationFlag* flag,
40 base::ScopedClosureRunner* cleanup_runner) {
41 return flag->IsSet();
44 void RunAndDeleteFlag(const Closure& closure, const CancellationFlag* flag) {
45 closure.Run();
46 delete flag;
49 void RunOrPostToTaskRunner(TaskRunner* task_runner, const Closure& closure) {
50 if (task_runner->RunsTasksOnCurrentThread())
51 closure.Run();
52 else
53 task_runner->PostTask(FROM_HERE, closure);
56 } // namespace
58 namespace base {
60 // static
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());
69 TryCancelAll();
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,
84 const Closure& task,
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();
94 TaskId id = next_id_;
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);
99 bool success =
100 task_runner->PostTaskAndReply(from_here,
101 Bind(&RunIfNotCanceled, flag, task),
102 Bind(&RunIfNotCanceledThenUntrack,
103 base::Owned(flag),
104 reply,
105 untrack_closure));
107 if (!success)
108 return kBadTaskId;
110 Track(id, flag);
111 return id;
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(
126 &RunAndDeleteFlag,
127 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id),
128 flag);
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));
136 *is_canceled_cb =
137 Bind(&IsCanceled, flag, base::Owned(untrack_and_delete_flag_runner));
139 Track(id, flag);
140 return id;
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.
154 return;
156 it->second->Set();
159 void CancelableTaskTracker::TryCancelAll() {
160 DCHECK(thread_checker_.CalledOnValidThread());
162 for (hash_map<TaskId, CancellationFlag*>::const_iterator it =
163 task_flags_.begin();
164 it != task_flags_.end();
165 ++it) {
166 it->second->Set();
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;
179 DCHECK(success);
182 void CancelableTaskTracker::Untrack(TaskId id) {
183 DCHECK(thread_checker_.CalledOnValidThread());
184 size_t num = task_flags_.erase(id);
185 DCHECK_EQ(1u, num);
188 } // namespace base