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

Side by Side Diff: base/task_runner.h

Issue 9416060: Move PostTaskAndReplyWithStatus into task_runner_helpers.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with ToT Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_proxy.cc ('k') | base/task_runner_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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef BASE_TASK_RUNNER_H_ 5 #ifndef BASE_TASK_RUNNER_H_
6 #define BASE_TASK_RUNNER_H_ 6 #define BASE_TASK_RUNNER_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/base_export.h" 9 #include "base/base_export.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/callback_forward.h" 11 #include "base/bind.h"
willchan no longer on Chromium 2012/04/04 17:35:49 I have a problem with all these new headers. I see
battre 2012/04/04 19:47:41 Either way is fine with me. I'll follow your decis
willchan no longer on Chromium 2012/04/05 16:53:03 darin/akalin, feel free to chime in, but otherwise
battre 2012/04/12 06:46:33 Done.
12 #include "base/bind_helpers.h"
13 #include "base/callback.h"
14 #include "base/logging.h"
12 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
13 #include "base/time.h" 16 #include "base/time.h"
14 17
15 namespace tracked_objects { 18 namespace tracked_objects {
16 class Location; 19 class Location;
17 } // namespace tracked_objects 20 } // namespace tracked_objects
18 21
19 namespace base { 22 namespace base {
20 23
24 namespace internal {
25
26 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
27 template <typename ReturnType>
28 void ReturnAsParamAdapter(const Callback<ReturnType(void)>& func,
29 ReturnType* result) {
30 if (!func.is_null())
31 *result = func.Run();
32 }
33
34 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
35 template <typename ReturnType>
36 Closure ReturnAsParam(const Callback<ReturnType(void)>& func,
37 ReturnType* result) {
38 DCHECK(result);
39 return Bind(&ReturnAsParamAdapter<ReturnType>, func, result);
40 }
41
42 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
43 template <typename ReturnType>
44 void ReplyAdapter(const Callback<void(ReturnType)>& callback,
45 ReturnType* result) {
46 DCHECK(result);
47 if(!callback.is_null())
48 callback.Run(*result);
49 }
50
51 // Helper class for TaskRunner::PostTaskAndReplyWithResult.
52 template <typename ReturnType, typename OWNED>
willchan no longer on Chromium 2012/04/04 17:35:49 s/OWNED/OwnedType/
battre 2012/04/12 06:46:33 Done.
53 Closure ReplyHelper(const Callback<void(ReturnType)>& callback, OWNED result) {
54 return Bind(&ReplyAdapter<ReturnType>, callback, result);
55 }
56
57 } // namespace internal
58
21 struct TaskRunnerTraits; 59 struct TaskRunnerTraits;
22 60
23 // A TaskRunner is an object that runs posted tasks (in the form of 61 // A TaskRunner is an object that runs posted tasks (in the form of
24 // Closure objects). The TaskRunner interface provides a way of 62 // Closure objects). The TaskRunner interface provides a way of
25 // decoupling task posting from the mechanics of how each task will be 63 // decoupling task posting from the mechanics of how each task will be
26 // run. TaskRunner provides very weak guarantees as to how posted 64 // run. TaskRunner provides very weak guarantees as to how posted
27 // tasks are run (or if they're run at all). In particular, it only 65 // tasks are run (or if they're run at all). In particular, it only
28 // guarantees: 66 // guarantees:
29 // 67 //
30 // - Posting a task will not run it synchronously. That is, no 68 // - Posting a task will not run it synchronously. That is, no
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 // * Results of |task| are shared with |reply| by binding a shared argument 165 // * Results of |task| are shared with |reply| by binding a shared argument
128 // (a DataBuffer instance). 166 // (a DataBuffer instance).
129 // * The DataLoader object has no special thread safety. 167 // * The DataLoader object has no special thread safety.
130 // * The DataLoader object can be deleted while |task| is still running, 168 // * The DataLoader object can be deleted while |task| is still running,
131 // and the reply will cancel itself safely because it is bound to a 169 // and the reply will cancel itself safely because it is bound to a
132 // WeakPtr<>. 170 // WeakPtr<>.
133 bool PostTaskAndReply(const tracked_objects::Location& from_here, 171 bool PostTaskAndReply(const tracked_objects::Location& from_here,
134 const Closure& task, 172 const Closure& task,
135 const Closure& reply); 173 const Closure& reply);
136 174
175 // When you have these methods
176 //
177 // R DoWorkAndReturn();
178 // void Callback(const R& result);
179 //
180 // and want to call them in a PostTaskAndReply kind of fashion where the
181 // result of DoWorkAndReturn is passed to the Callback, you can use
182 // PostTaskAndReplyWithResult as in this example:
183 //
184 // target_thread_.message_loop_proxy()->PostTaskAndReplyWithResult(
185 // FROM_HERE,
186 // Bind(&DoWorkAndReturn),
187 // Bind(&Callback));
188 template <typename ReturnType>
189 bool PostTaskAndReplyWithResult(
190 const tracked_objects::Location& from_here,
191 const Callback<ReturnType(void)>& task,
192 const Callback<void(ReturnType)>& reply) {
193 ReturnType* result = new ReturnType;
194 return PostTaskAndReply(
195 from_here,
196 internal::ReturnAsParam<ReturnType>(task, result),
197 internal::ReplyHelper(reply, Owned(result)));
198 }
199
137 protected: 200 protected:
138 friend struct TaskRunnerTraits; 201 friend struct TaskRunnerTraits;
139 202
140 // Only the Windows debug build seems to need this: see 203 // Only the Windows debug build seems to need this: see
141 // http://crbug.com/112250. 204 // http://crbug.com/112250.
142 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>; 205 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>;
143 206
144 TaskRunner(); 207 TaskRunner();
145 virtual ~TaskRunner(); 208 virtual ~TaskRunner();
146 209
147 // Called when this object should be destroyed. By default simply 210 // Called when this object should be destroyed. By default simply
148 // deletes |this|, but can be overridden to do something else, like 211 // deletes |this|, but can be overridden to do something else, like
149 // delete on a certain thread. 212 // delete on a certain thread.
150 virtual void OnDestruct() const; 213 virtual void OnDestruct() const;
151 }; 214 };
152 215
153 struct BASE_EXPORT TaskRunnerTraits { 216 struct BASE_EXPORT TaskRunnerTraits {
154 static void Destruct(const TaskRunner* task_runner); 217 static void Destruct(const TaskRunner* task_runner);
155 }; 218 };
156 219
157 } // namespace base 220 } // namespace base
158 221
159 #endif // BASE_TASK_RUNNER_H_ 222 #endif // BASE_TASK_RUNNER_H_
OLDNEW
« no previous file with comments | « base/file_util_proxy.cc ('k') | base/task_runner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698