Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: base/task/cancelable_task_tracker.cc

Issue 1048373003: CancelableTaskTracker::PostDelayedTask() support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove unnecessary base:: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/task/cancelable_task_tracker.h ('k') | base/task/cancelable_task_tracker_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/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"
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 CancelableTaskTracker::~CancelableTaskTracker() { 66 CancelableTaskTracker::~CancelableTaskTracker() {
66 DCHECK(thread_checker_.CalledOnValidThread()); 67 DCHECK(thread_checker_.CalledOnValidThread());
67 68
68 TryCancelAll(); 69 TryCancelAll();
69 } 70 }
70 71
71 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTask( 72 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTask(
72 TaskRunner* task_runner, 73 TaskRunner* task_runner,
73 const tracked_objects::Location& from_here, 74 const tracked_objects::Location& from_here,
74 const Closure& task) { 75 const Closure& task) {
75 DCHECK(thread_checker_.CalledOnValidThread()); 76 return PostDelayedTaskAndReply(task_runner, from_here, task,
77 Bind(&base::DoNothing), TimeDelta());
78 }
76 79
77 return PostTaskAndReply(task_runner, from_here, task, Bind(&base::DoNothing)); 80 CancelableTaskTracker::TaskId CancelableTaskTracker::PostDelayedTask(
81 TaskRunner* task_runner,
82 const tracked_objects::Location& from_here,
83 const Closure& task,
84 const TimeDelta& delay) {
85 return PostDelayedTaskAndReply(task_runner, from_here, task,
86 Bind(&base::DoNothing), delay);
78 } 87 }
79 88
80 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply( 89 CancelableTaskTracker::TaskId CancelableTaskTracker::PostTaskAndReply(
81 TaskRunner* task_runner, 90 TaskRunner* task_runner,
82 const tracked_objects::Location& from_here, 91 const tracked_objects::Location& from_here,
83 const Closure& task, 92 const Closure& task,
84 const Closure& reply) { 93 const Closure& reply) {
85 DCHECK(thread_checker_.CalledOnValidThread()); 94 return PostDelayedTaskAndReply(task_runner, from_here, task, reply,
86 95 TimeDelta());
87 // We need a MessageLoop to run reply.
88 DCHECK(base::MessageLoopProxy::current().get());
89
90 // Owned by reply callback below.
91 CancellationFlag* flag = new CancellationFlag();
92
93 TaskId id = next_id_;
94 next_id_++; // int64 is big enough that we ignore the potential overflow.
95
96 const Closure& untrack_closure =
97 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id);
98 bool success =
99 task_runner->PostTaskAndReply(from_here,
100 Bind(&RunIfNotCanceled, flag, task),
101 Bind(&RunIfNotCanceledThenUntrack,
102 base::Owned(flag),
103 reply,
104 untrack_closure));
105
106 if (!success)
107 return kBadTaskId;
108
109 Track(id, flag);
110 return id;
111 } 96 }
112 97
113 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId( 98 CancelableTaskTracker::TaskId CancelableTaskTracker::NewTrackedTaskId(
114 IsCanceledCallback* is_canceled_cb) { 99 IsCanceledCallback* is_canceled_cb) {
115 DCHECK(thread_checker_.CalledOnValidThread()); 100 DCHECK(thread_checker_.CalledOnValidThread());
116 DCHECK(base::MessageLoopProxy::current().get()); 101 DCHECK(base::MessageLoopProxy::current().get());
117 102
118 TaskId id = next_id_; 103 TaskId id = next_id_;
119 next_id_++; // int64 is big enough that we ignore the potential overflow. 104 next_id_++; // int64 is big enough that we ignore the potential overflow.
120 105
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 bool success = task_flags_.insert(std::make_pair(id, flag)).second; 162 bool success = task_flags_.insert(std::make_pair(id, flag)).second;
178 DCHECK(success); 163 DCHECK(success);
179 } 164 }
180 165
181 void CancelableTaskTracker::Untrack(TaskId id) { 166 void CancelableTaskTracker::Untrack(TaskId id) {
182 DCHECK(thread_checker_.CalledOnValidThread()); 167 DCHECK(thread_checker_.CalledOnValidThread());
183 size_t num = task_flags_.erase(id); 168 size_t num = task_flags_.erase(id);
184 DCHECK_EQ(1u, num); 169 DCHECK_EQ(1u, num);
185 } 170 }
186 171
172 CancelableTaskTracker::TaskId CancelableTaskTracker::PostDelayedTaskAndReply(
173 TaskRunner* task_runner,
174 const tracked_objects::Location& from_here,
175 const Closure& task,
176 const Closure& reply,
177 const TimeDelta& delay) {
178 DCHECK(thread_checker_.CalledOnValidThread());
179
180 // We need a MessageLoop to run reply.
181 DCHECK(ThreadTaskRunnerHandle::Get());
182
183 // Owned by reply callback below.
184 CancellationFlag* flag = new CancellationFlag();
185
186 TaskId id = next_id_;
187 next_id_++; // int64 is big enough that we ignore the potential overflow.
188
189 const Closure& untrack_closure =
190 Bind(&CancelableTaskTracker::Untrack, weak_factory_.GetWeakPtr(), id);
191 bool success = task_runner->PostDelayedTaskAndReply(
192 from_here,
193 Bind(&RunIfNotCanceled, flag, task),
194 Bind(&RunIfNotCanceledThenUntrack,
195 Owned(flag),
196 reply,
197 untrack_closure),
198 delay);
199
200 if (!success)
201 return kBadTaskId;
202
203 Track(id, flag);
204 return id;
205 }
206
187 } // namespace base 207 } // namespace base
OLDNEW
« no previous file with comments | « base/task/cancelable_task_tracker.h ('k') | base/task/cancelable_task_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698