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

Side by Side Diff: base/task_scheduler/post_task.h

Issue 2637843002: Migrate base::TaskRunner from Closure to OnceClosure (Closed)
Patch Set: rebase without dcheck_in_ref_count Created 3 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_SCHEDULER_POST_TASK_H_ 5 #ifndef BASE_TASK_SCHEDULER_POST_TASK_H_
6 #define BASE_TASK_SCHEDULER_POST_TASK_H_ 6 #define BASE_TASK_SCHEDULER_POST_TASK_H_
7 7
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/base_export.h" 10 #include "base/base_export.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // Prerequisite: A TaskScheduler must have been registered for the current 63 // Prerequisite: A TaskScheduler must have been registered for the current
64 // process via TaskScheduler::SetInstance() before the functions below are 64 // process via TaskScheduler::SetInstance() before the functions below are
65 // valid. This is typically done during the initialization phase in each 65 // valid. This is typically done during the initialization phase in each
66 // process. If your code is not running in that phase, you most likely don't 66 // process. If your code is not running in that phase, you most likely don't
67 // have to worry about this. You will encounter DCHECKs or nullptr dereferences 67 // have to worry about this. You will encounter DCHECKs or nullptr dereferences
68 // if this is violated. For tests, prefer base::test::ScopedTaskScheduler. 68 // if this is violated. For tests, prefer base::test::ScopedTaskScheduler.
69 69
70 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling 70 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling
71 // PostTaskWithTraits with plain TaskTraits. 71 // PostTaskWithTraits with plain TaskTraits.
72 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here, 72 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here,
73 Closure task); 73 OnceClosure task);
74 74
75 // Posts |task| to the TaskScheduler. |task| will not run before |delay| 75 // Posts |task| to the TaskScheduler. |task| will not run before |delay|
76 // expires. Calling this is equivalent to calling PostDelayedTaskWithTraits with 76 // expires. Calling this is equivalent to calling PostDelayedTaskWithTraits with
77 // plain TaskTraits. 77 // plain TaskTraits.
78 // 78 //
79 // Use PostDelayedTaskWithTraits to specify a BACKGROUND priority if the task 79 // Use PostDelayedTaskWithTraits to specify a BACKGROUND priority if the task
80 // doesn't have to run as soon as |delay| expires. 80 // doesn't have to run as soon as |delay| expires.
81 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here, 81 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here,
82 Closure task, 82 OnceClosure task,
83 TimeDelta delay); 83 TimeDelta delay);
84 84
85 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution 85 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution
86 // context (i.e. same sequence or thread and same TaskTraits if applicable) when 86 // context (i.e. same sequence or thread and same TaskTraits if applicable) when
87 // |task| completes. Calling this is equivalent to calling 87 // |task| completes. Calling this is equivalent to calling
88 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when 88 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when
89 // SequencedTaskRunnerHandle::IsSet(). 89 // SequencedTaskRunnerHandle::IsSet().
90 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, 90 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here,
91 Closure task, 91 OnceClosure task,
92 Closure reply); 92 OnceClosure reply);
93 93
94 // Posts |task| to the TaskScheduler and posts |reply| with the return value of 94 // Posts |task| to the TaskScheduler and posts |reply| with the return value of
95 // |task| as argument on the caller's execution context (i.e. same sequence or 95 // |task| as argument on the caller's execution context (i.e. same sequence or
96 // thread and same TaskTraits if applicable) when |task| completes. Calling this 96 // thread and same TaskTraits if applicable) when |task| completes. Calling this
97 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain 97 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain
98 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet(). 98 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet().
99 template <typename TaskReturnType, typename ReplyArgType> 99 template <typename TaskReturnType, typename ReplyArgType>
100 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, 100 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here,
101 Callback<TaskReturnType(void)> task, 101 OnceCallback<TaskReturnType()> task,
102 Callback<void(ReplyArgType)> reply) { 102 OnceCallback<void(ReplyArgType)> reply) {
103 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), std::move(task), 103 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), std::move(task),
104 std::move(reply)); 104 std::move(reply));
105 } 105 }
106 106
107 // Callback version of PostTaskAndReplyWithResult above.
108 // Though RepeatingCallback is convertible to OnceCallback, we need this since
109 // we can not use template deduction and object conversion at once on the
110 // overload resolution.
111 // TODO(tzik): Update all callers of the Callback version to use OnceCallback.
112 template <typename TaskReturnType, typename ReplyArgType>
113 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here,
114 Callback<TaskReturnType()> task,
115 Callback<void(ReplyArgType)> reply) {
116 PostTaskAndReplyWithResult(
117 from_here, static_cast<OnceCallback<TaskReturnType()>>(task),
gab 2017/03/29 16:57:35 std::move(task), std::move(reply)?
tzik 2017/03/29 18:17:43 Ah, right. I forgot to do it. Updated.
118 static_cast<OnceCallback<void(ReplyArgType)>>(reply));
gab 2017/03/29 16:57:34 These casts are effectively doing OnceCallback(tas
tzik 2017/03/29 18:17:42 Done.
119 }
120
107 // Posts |task| with specific |traits| to the TaskScheduler. 121 // Posts |task| with specific |traits| to the TaskScheduler.
108 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, 122 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here,
109 const TaskTraits& traits, 123 const TaskTraits& traits,
110 Closure task); 124 OnceClosure task);
111 125
112 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run 126 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run
113 // before |delay| expires. 127 // before |delay| expires.
114 // 128 //
115 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as 129 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as
116 // soon as |delay| expires. 130 // soon as |delay| expires.
117 BASE_EXPORT void PostDelayedTaskWithTraits( 131 BASE_EXPORT void PostDelayedTaskWithTraits(
118 const tracked_objects::Location& from_here, 132 const tracked_objects::Location& from_here,
119 const TaskTraits& traits, 133 const TaskTraits& traits,
120 Closure task, 134 OnceClosure task,
121 TimeDelta delay); 135 TimeDelta delay);
122 136
123 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on 137 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on
124 // the caller's execution context (i.e. same sequence or thread and same 138 // the caller's execution context (i.e. same sequence or thread and same
125 // TaskTraits if applicable) when |task| completes. Can only be called when 139 // TaskTraits if applicable) when |task| completes. Can only be called when
126 // SequencedTaskRunnerHandle::IsSet(). 140 // SequencedTaskRunnerHandle::IsSet().
127 BASE_EXPORT void PostTaskWithTraitsAndReply( 141 BASE_EXPORT void PostTaskWithTraitsAndReply(
128 const tracked_objects::Location& from_here, 142 const tracked_objects::Location& from_here,
129 const TaskTraits& traits, 143 const TaskTraits& traits,
130 Closure task, 144 OnceClosure task,
131 Closure reply); 145 OnceClosure reply);
132 146
133 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| 147 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply|
134 // with the return value of |task| as argument on the caller's execution context 148 // with the return value of |task| as argument on the caller's execution context
135 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task| 149 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task|
136 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet(). 150 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet().
137 template <typename TaskReturnType, typename ReplyArgType> 151 template <typename TaskReturnType, typename ReplyArgType>
138 void PostTaskWithTraitsAndReplyWithResult( 152 void PostTaskWithTraitsAndReplyWithResult(
139 const tracked_objects::Location& from_here, 153 const tracked_objects::Location& from_here,
140 const TaskTraits& traits, 154 const TaskTraits& traits,
155 OnceCallback<TaskReturnType()> task,
156 OnceCallback<void(ReplyArgType)> reply) {
157 TaskReturnType* result = new TaskReturnType();
158 return PostTaskWithTraitsAndReply(
159 from_here, traits,
160 BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task),
161 result),
162 BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
163 std::move(reply), Owned(result)));
164 }
165
166 // Callback version of PostTaskWithTraitsAndReplyWithResult above.
167 // Though RepeatingCallback is convertible to OnceCallback, we need this since
168 // we can not use template deduction and object conversion at once on the
169 // overload resolution.
170 // TODO(tzik): Update all callers of the Callback version to use OnceCallback.
171 template <typename TaskReturnType, typename ReplyArgType>
172 void PostTaskWithTraitsAndReplyWithResult(
173 const tracked_objects::Location& from_here,
174 const TaskTraits& traits,
141 Callback<TaskReturnType()> task, 175 Callback<TaskReturnType()> task,
142 Callback<void(ReplyArgType)> reply) { 176 Callback<void(ReplyArgType)> reply) {
143 TaskReturnType* result = new TaskReturnType(); 177 PostTaskWithTraitsAndReplyWithResult(
144 return PostTaskWithTraitsAndReply( 178 from_here, traits,
145 from_here, traits, Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, 179 static_cast<OnceCallback<TaskReturnType()>>(std::move(task)),
146 std::move(task), result), 180 static_cast<OnceCallback<void(ReplyArgType)>>(std::move(reply)));
gab 2017/03/29 16:57:35 ditto
tzik 2017/03/29 18:17:43 Done.
147 Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
148 std::move(reply), Owned(result)));
149 } 181 }
150 182
151 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks 183 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
152 // using |traits|. Tasks may run in any order and in parallel. 184 // using |traits|. Tasks may run in any order and in parallel.
153 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( 185 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
154 const TaskTraits& traits); 186 const TaskTraits& traits);
155 187
156 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling 188 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling
157 // tasks using |traits|. Tasks run one at a time in posting order. 189 // tasks using |traits|. Tasks run one at a time in posting order.
158 BASE_EXPORT scoped_refptr<SequencedTaskRunner> 190 BASE_EXPORT scoped_refptr<SequencedTaskRunner>
159 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits); 191 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits);
160 192
161 // Returns a SingleThreadTaskRunner whose PostTask invocations result in 193 // Returns a SingleThreadTaskRunner whose PostTask invocations result in
162 // scheduling tasks using |traits|. Tasks run on a single thread in posting 194 // scheduling tasks using |traits|. Tasks run on a single thread in posting
163 // order. 195 // order.
164 // 196 //
165 // If all you need is to make sure that tasks don't run concurrently (e.g. 197 // If all you need is to make sure that tasks don't run concurrently (e.g.
166 // because they access a data structure which is not thread-safe), use 198 // because they access a data structure which is not thread-safe), use
167 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread- 199 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread-
168 // affine API (it might be safer to assume thread-affinity when dealing with 200 // affine API (it might be safer to assume thread-affinity when dealing with
169 // under-documented third-party APIs, e.g. other OS') or share data across tasks 201 // under-documented third-party APIs, e.g. other OS') or share data across tasks
170 // using thread-local storage. 202 // using thread-local storage.
171 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> 203 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner>
172 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits); 204 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits);
173 205
174 } // namespace base 206 } // namespace base
175 207
176 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ 208 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698