OLD | NEW |
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // Prerequisite: A TaskScheduler must have been registered for the current | 64 // Prerequisite: A TaskScheduler must have been registered for the current |
65 // process via TaskScheduler::SetInstance() before the functions below are | 65 // process via TaskScheduler::SetInstance() before the functions below are |
66 // valid. This is typically done during the initialization phase in each | 66 // valid. This is typically done during the initialization phase in each |
67 // process. If your code is not running in that phase, you most likely don't | 67 // process. If your code is not running in that phase, you most likely don't |
68 // have to worry about this. You will encounter DCHECKs or nullptr dereferences | 68 // have to worry about this. You will encounter DCHECKs or nullptr dereferences |
69 // if this is violated. For tests, prefer base::test::ScopedTaskScheduler. | 69 // if this is violated. For tests, prefer base::test::ScopedTaskScheduler. |
70 | 70 |
71 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling | 71 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling |
72 // PostTaskWithTraits with plain TaskTraits. | 72 // PostTaskWithTraits with plain TaskTraits. |
73 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here, | 73 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here, |
74 Closure task); | 74 OnceClosure task); |
75 | 75 |
76 // Posts |task| to the TaskScheduler. |task| will not run before |delay| | 76 // Posts |task| to the TaskScheduler. |task| will not run before |delay| |
77 // expires. Calling this is equivalent to calling PostDelayedTaskWithTraits with | 77 // expires. Calling this is equivalent to calling PostDelayedTaskWithTraits with |
78 // plain TaskTraits. | 78 // plain TaskTraits. |
79 // | 79 // |
80 // Use PostDelayedTaskWithTraits to specify a BACKGROUND priority if the task | 80 // Use PostDelayedTaskWithTraits to specify a BACKGROUND priority if the task |
81 // doesn't have to run as soon as |delay| expires. | 81 // doesn't have to run as soon as |delay| expires. |
82 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here, | 82 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here, |
83 Closure task, | 83 OnceClosure task, |
84 TimeDelta delay); | 84 TimeDelta delay); |
85 | 85 |
86 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution | 86 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution |
87 // context (i.e. same sequence or thread and same TaskTraits if applicable) when | 87 // context (i.e. same sequence or thread and same TaskTraits if applicable) when |
88 // |task| completes. Calling this is equivalent to calling | 88 // |task| completes. Calling this is equivalent to calling |
89 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when | 89 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when |
90 // SequencedTaskRunnerHandle::IsSet(). | 90 // SequencedTaskRunnerHandle::IsSet(). |
91 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, | 91 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, |
92 Closure task, | 92 OnceClosure task, |
93 Closure reply); | 93 OnceClosure reply); |
94 | 94 |
95 // Posts |task| to the TaskScheduler and posts |reply| with the return value of | 95 // Posts |task| to the TaskScheduler and posts |reply| with the return value of |
96 // |task| as argument on the caller's execution context (i.e. same sequence or | 96 // |task| as argument on the caller's execution context (i.e. same sequence or |
97 // thread and same TaskTraits if applicable) when |task| completes. Calling this | 97 // thread and same TaskTraits if applicable) when |task| completes. Calling this |
98 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain | 98 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain |
99 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet(). | 99 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet(). |
100 template <typename TaskReturnType, typename ReplyArgType> | 100 template <typename TaskReturnType, typename ReplyArgType> |
101 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, | 101 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, |
102 Callback<TaskReturnType(void)> task, | 102 OnceCallback<TaskReturnType()> task, |
103 Callback<void(ReplyArgType)> reply) { | 103 OnceCallback<void(ReplyArgType)> reply) { |
104 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), std::move(task), | 104 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), std::move(task), |
105 std::move(reply)); | 105 std::move(reply)); |
106 } | 106 } |
107 | 107 |
| 108 // Callback version of PostTaskAndReplyWithResult above. |
| 109 // Though RepeatingCallback is convertible to OnceCallback, we need this since |
| 110 // we can not use template deduction and object conversion at once on the |
| 111 // overload resolution. |
| 112 // TODO(tzik): Update all callers of the Callback version to use OnceCallback. |
| 113 template <typename TaskReturnType, typename ReplyArgType> |
| 114 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, |
| 115 Callback<TaskReturnType()> task, |
| 116 Callback<void(ReplyArgType)> reply) { |
| 117 PostTaskAndReplyWithResult( |
| 118 from_here, OnceCallback<TaskReturnType()>(std::move(task)), |
| 119 OnceCallback<void(ReplyArgType)>(std::move(reply))); |
| 120 } |
| 121 |
108 // Posts |task| with specific |traits| to the TaskScheduler. | 122 // Posts |task| with specific |traits| to the TaskScheduler. |
109 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, | 123 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, |
110 const TaskTraits& traits, | 124 const TaskTraits& traits, |
111 Closure task); | 125 OnceClosure task); |
112 | 126 |
113 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run | 127 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run |
114 // before |delay| expires. | 128 // before |delay| expires. |
115 // | 129 // |
116 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as | 130 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as |
117 // soon as |delay| expires. | 131 // soon as |delay| expires. |
118 BASE_EXPORT void PostDelayedTaskWithTraits( | 132 BASE_EXPORT void PostDelayedTaskWithTraits( |
119 const tracked_objects::Location& from_here, | 133 const tracked_objects::Location& from_here, |
120 const TaskTraits& traits, | 134 const TaskTraits& traits, |
121 Closure task, | 135 OnceClosure task, |
122 TimeDelta delay); | 136 TimeDelta delay); |
123 | 137 |
124 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on | 138 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on |
125 // the caller's execution context (i.e. same sequence or thread and same | 139 // the caller's execution context (i.e. same sequence or thread and same |
126 // TaskTraits if applicable) when |task| completes. Can only be called when | 140 // TaskTraits if applicable) when |task| completes. Can only be called when |
127 // SequencedTaskRunnerHandle::IsSet(). | 141 // SequencedTaskRunnerHandle::IsSet(). |
128 BASE_EXPORT void PostTaskWithTraitsAndReply( | 142 BASE_EXPORT void PostTaskWithTraitsAndReply( |
129 const tracked_objects::Location& from_here, | 143 const tracked_objects::Location& from_here, |
130 const TaskTraits& traits, | 144 const TaskTraits& traits, |
131 Closure task, | 145 OnceClosure task, |
132 Closure reply); | 146 OnceClosure reply); |
133 | 147 |
134 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| | 148 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| |
135 // with the return value of |task| as argument on the caller's execution context | 149 // with the return value of |task| as argument on the caller's execution context |
136 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task| | 150 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task| |
137 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet(). | 151 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet(). |
138 template <typename TaskReturnType, typename ReplyArgType> | 152 template <typename TaskReturnType, typename ReplyArgType> |
139 void PostTaskWithTraitsAndReplyWithResult( | 153 void PostTaskWithTraitsAndReplyWithResult( |
140 const tracked_objects::Location& from_here, | 154 const tracked_objects::Location& from_here, |
141 const TaskTraits& traits, | 155 const TaskTraits& traits, |
| 156 OnceCallback<TaskReturnType()> task, |
| 157 OnceCallback<void(ReplyArgType)> reply) { |
| 158 TaskReturnType* result = new TaskReturnType(); |
| 159 return PostTaskWithTraitsAndReply( |
| 160 from_here, traits, |
| 161 BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task), |
| 162 result), |
| 163 BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, |
| 164 std::move(reply), Owned(result))); |
| 165 } |
| 166 |
| 167 // Callback version of PostTaskWithTraitsAndReplyWithResult above. |
| 168 // Though RepeatingCallback is convertible to OnceCallback, we need this since |
| 169 // we can not use template deduction and object conversion at once on the |
| 170 // overload resolution. |
| 171 // TODO(tzik): Update all callers of the Callback version to use OnceCallback. |
| 172 template <typename TaskReturnType, typename ReplyArgType> |
| 173 void PostTaskWithTraitsAndReplyWithResult( |
| 174 const tracked_objects::Location& from_here, |
| 175 const TaskTraits& traits, |
142 Callback<TaskReturnType()> task, | 176 Callback<TaskReturnType()> task, |
143 Callback<void(ReplyArgType)> reply) { | 177 Callback<void(ReplyArgType)> reply) { |
144 TaskReturnType* result = new TaskReturnType(); | 178 PostTaskWithTraitsAndReplyWithResult( |
145 return PostTaskWithTraitsAndReply( | 179 from_here, traits, OnceCallback<TaskReturnType()>(std::move(task)), |
146 from_here, traits, Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, | 180 OnceCallback<void(ReplyArgType)>(std::move(reply))); |
147 std::move(task), result), | |
148 Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, | |
149 std::move(reply), Owned(result))); | |
150 } | 181 } |
151 | 182 |
152 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks | 183 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks |
153 // using |traits|. Tasks may run in any order and in parallel. | 184 // using |traits|. Tasks may run in any order and in parallel. |
154 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 185 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
155 const TaskTraits& traits); | 186 const TaskTraits& traits); |
156 | 187 |
157 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling | 188 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling |
158 // 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. |
159 BASE_EXPORT scoped_refptr<SequencedTaskRunner> | 190 BASE_EXPORT scoped_refptr<SequencedTaskRunner> |
(...skipping 21 matching lines...) Expand all Loading... |
181 // implementation is free to share apartments or create new apartments as | 212 // implementation is free to share apartments or create new apartments as |
182 // necessary. In either case, care should be taken to make sure COM pointers are | 213 // necessary. In either case, care should be taken to make sure COM pointers are |
183 // not smuggled across apartments. | 214 // not smuggled across apartments. |
184 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> | 215 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> |
185 CreateCOMSTATaskRunnerWithTraits(const TaskTraits& traits); | 216 CreateCOMSTATaskRunnerWithTraits(const TaskTraits& traits); |
186 #endif // defined(OS_WIN) | 217 #endif // defined(OS_WIN) |
187 | 218 |
188 } // namespace base | 219 } // namespace base |
189 | 220 |
190 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ | 221 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ |
OLD | NEW |