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

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

Issue 2678303002: Pass Callback by value on PostTaskAndReply family (Closed)
Patch Set: #include Created 3 years, 10 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 "base/base_export.h" 8 #include "base/base_export.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_forward.h" 10 #include "base/callback.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/post_task_and_reply_with_result_internal.h" 13 #include "base/post_task_and_reply_with_result_internal.h"
14 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner.h" 16 #include "base/task_runner.h"
17 #include "base/task_scheduler/task_traits.h" 17 #include "base/task_scheduler/task_traits.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 19
20 namespace base { 20 namespace base {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here, 74 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here,
75 const Closure& task, 75 const Closure& task,
76 TimeDelta delay); 76 TimeDelta delay);
77 77
78 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution 78 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution
79 // context (i.e. same sequence or thread and same TaskTraits if applicable) when 79 // context (i.e. same sequence or thread and same TaskTraits if applicable) when
80 // |task| completes. Calling this is equivalent to calling 80 // |task| completes. Calling this is equivalent to calling
81 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when 81 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when
82 // SequencedTaskRunnerHandle::IsSet(). 82 // SequencedTaskRunnerHandle::IsSet().
83 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, 83 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here,
84 const Closure& task, 84 Closure task,
85 const Closure& reply); 85 Closure reply);
86 86
87 // Posts |task| to the TaskScheduler and posts |reply| with the return value of 87 // Posts |task| to the TaskScheduler and posts |reply| with the return value of
88 // |task| as argument on the caller's execution context (i.e. same sequence or 88 // |task| as argument on the caller's execution context (i.e. same sequence or
89 // thread and same TaskTraits if applicable) when |task| completes. Calling this 89 // thread and same TaskTraits if applicable) when |task| completes. Calling this
90 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain 90 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain
91 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet(). 91 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet().
92 template <typename TaskReturnType, typename ReplyArgType> 92 template <typename TaskReturnType, typename ReplyArgType>
93 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, 93 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here,
94 const Callback<TaskReturnType(void)>& task, 94 Callback<TaskReturnType(void)> task,
95 const Callback<void(ReplyArgType)>& reply) { 95 Callback<void(ReplyArgType)> reply) {
96 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), task, reply); 96 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), std::move(task),
97 std::move(reply));
gab 2017/02/07 15:46:08 #include <utility>
tzik 2017/02/08 01:39:32 Done.
97 } 98 }
98 99
99 // Posts |task| with specific |traits| to the TaskScheduler. 100 // Posts |task| with specific |traits| to the TaskScheduler.
100 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, 101 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here,
101 const TaskTraits& traits, 102 const TaskTraits& traits,
102 const Closure& task); 103 const Closure& task);
103 104
104 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run 105 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run
105 // before |delay| expires. 106 // before |delay| expires.
106 // 107 //
107 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as 108 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as
108 // soon as |delay| expires. 109 // soon as |delay| expires.
109 BASE_EXPORT void PostDelayedTaskWithTraits( 110 BASE_EXPORT void PostDelayedTaskWithTraits(
110 const tracked_objects::Location& from_here, 111 const tracked_objects::Location& from_here,
111 const TaskTraits& traits, 112 const TaskTraits& traits,
112 const Closure& task, 113 const Closure& task,
113 TimeDelta delay); 114 TimeDelta delay);
114 115
115 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on 116 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on
116 // the caller's execution context (i.e. same sequence or thread and same 117 // the caller's execution context (i.e. same sequence or thread and same
117 // TaskTraits if applicable) when |task| completes. Can only be called when 118 // TaskTraits if applicable) when |task| completes. Can only be called when
118 // SequencedTaskRunnerHandle::IsSet(). 119 // SequencedTaskRunnerHandle::IsSet().
119 BASE_EXPORT void PostTaskWithTraitsAndReply( 120 BASE_EXPORT void PostTaskWithTraitsAndReply(
120 const tracked_objects::Location& from_here, 121 const tracked_objects::Location& from_here,
121 const TaskTraits& traits, 122 const TaskTraits& traits,
122 const Closure& task, 123 Closure task,
123 const Closure& reply); 124 Closure reply);
124 125
125 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| 126 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply|
126 // with the return value of |task| as argument on the caller's execution context 127 // with the return value of |task| as argument on the caller's execution context
127 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task| 128 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task|
128 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet(). 129 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet().
129 template <typename TaskReturnType, typename ReplyArgType> 130 template <typename TaskReturnType, typename ReplyArgType>
130 void PostTaskWithTraitsAndReplyWithResult( 131 void PostTaskWithTraitsAndReplyWithResult(
131 const tracked_objects::Location& from_here, 132 const tracked_objects::Location& from_here,
132 const TaskTraits& traits, 133 const TaskTraits& traits,
133 const Callback<TaskReturnType(void)>& task, 134 Callback<TaskReturnType()> task,
134 const Callback<void(ReplyArgType)>& reply) { 135 Callback<void(ReplyArgType)> reply) {
135 TaskReturnType* result = new TaskReturnType(); 136 TaskReturnType* result = new TaskReturnType();
136 return PostTaskWithTraitsAndReply( 137 return PostTaskWithTraitsAndReply(
137 from_here, traits, 138 from_here, traits, Bind(&internal::ReturnAsParamAdapter<TaskReturnType>,
138 Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, result), 139 std::move(task), result),
139 Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, 140 Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>,
140 Owned(result))); 141 std::move(reply), Owned(result)));
141 } 142 }
142 143
143 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks 144 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
144 // using |traits|. Tasks may run in any order and in parallel. 145 // using |traits|. Tasks may run in any order and in parallel.
145 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( 146 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
146 const TaskTraits& traits); 147 const TaskTraits& traits);
147 148
148 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling 149 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling
149 // tasks using |traits|. Tasks run one at a time in posting order. 150 // tasks using |traits|. Tasks run one at a time in posting order.
150 BASE_EXPORT scoped_refptr<SequencedTaskRunner> 151 BASE_EXPORT scoped_refptr<SequencedTaskRunner>
151 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits); 152 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits);
152 153
153 // Returns a SingleThreadTaskRunner whose PostTask invocations result in 154 // Returns a SingleThreadTaskRunner whose PostTask invocations result in
154 // scheduling tasks using |traits|. Tasks run on a single thread in posting 155 // scheduling tasks using |traits|. Tasks run on a single thread in posting
155 // order. 156 // order.
156 // 157 //
157 // If all you need is to make sure that tasks don't run concurrently (e.g. 158 // If all you need is to make sure that tasks don't run concurrently (e.g.
158 // because they access a data structure which is not thread-safe), use 159 // because they access a data structure which is not thread-safe), use
159 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread- 160 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread-
160 // affine API (it might be safer to assume thread-affinity when dealing with 161 // affine API (it might be safer to assume thread-affinity when dealing with
161 // under-documented third-party APIs, e.g. other OS') or share data across tasks 162 // under-documented third-party APIs, e.g. other OS') or share data across tasks
162 // using thread-local storage. 163 // using thread-local storage.
163 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> 164 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner>
164 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits); 165 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits);
165 166
166 } // namespace base 167 } // namespace base
167 168
168 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ 169 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698