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 "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_forward.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 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 // This is the preferred interface to post tasks to the TaskScheduler. | 22 // This is the preferred interface to post tasks to the TaskScheduler. |
22 // | 23 // |
23 // Note: The TaskScheduler is still in an experimental phase in Chrome. Please | 24 // Note: The TaskScheduler is still in an experimental phase in Chrome. Please |
24 // refrain from using this API unless you know what you are doing. | 25 // refrain from using this API unless you know what you are doing. |
25 // | 26 // |
26 // TaskScheduler must have been registered for the current process via | 27 // TaskScheduler must have been registered for the current process via |
27 // TaskScheduler::SetInstance() before the functions below are valid. | 28 // TaskScheduler::SetInstance() before the functions below are valid. |
(...skipping 23 matching lines...) Expand all Loading... |
51 // task_runner.PostTask(FROM_HERE, Bind(...)); | 52 // task_runner.PostTask(FROM_HERE, Bind(...)); |
52 // | 53 // |
53 // The default TaskTraits apply to tasks that: | 54 // The default TaskTraits apply to tasks that: |
54 // (1) don't need to do I/O, | 55 // (1) don't need to do I/O, |
55 // (2) don't affect user interaction and/or visible elements, and | 56 // (2) don't affect user interaction and/or visible elements, and |
56 // (3) can either block shutdown or be skipped on shutdown | 57 // (3) can either block shutdown or be skipped on shutdown |
57 // (barring current TaskScheduler default). | 58 // (barring current TaskScheduler default). |
58 // If those loose requirements are sufficient for your task, use | 59 // If those loose requirements are sufficient for your task, use |
59 // PostTask[AndReply], otherwise override these with explicit traits via | 60 // PostTask[AndReply], otherwise override these with explicit traits via |
60 // PostTaskWithTraits[AndReply]. | 61 // PostTaskWithTraits[AndReply]. |
| 62 // |
| 63 // Tasks posted to TaskScheduler with a delay may be coalesced (i.e. delays may |
| 64 // be adjusted to reduce the number of wakeups and hence power consumption). |
61 | 65 |
62 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling | 66 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling |
63 // PostTaskWithTraits with plain TaskTraits. | 67 // PostTaskWithTraits with plain TaskTraits. |
64 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here, | 68 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here, |
65 const Closure& task); | 69 const Closure& task); |
66 | 70 |
| 71 // Posts |task| to the TaskScheduler. |task| will not run before |delay| |
| 72 // expires. Calling this is equivalent to calling PostDelayedTaskWithTraits with |
| 73 // plain TaskTraits. |
| 74 // |
| 75 // Use PostDelayedTaskWithTraits to specify a BACKGROUND priority if the task |
| 76 // doesn't have to run as soon as |delay| expires. |
| 77 BASE_EXPORT void PostDelayedTask(const tracked_objects::Location& from_here, |
| 78 const Closure& task, |
| 79 TimeDelta delay); |
| 80 |
67 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution | 81 // Posts |task| to the TaskScheduler and posts |reply| on the caller's execution |
68 // context (i.e. same sequence or thread and same TaskTraits if applicable) when | 82 // context (i.e. same sequence or thread and same TaskTraits if applicable) when |
69 // |task| completes. Calling this is equivalent to calling | 83 // |task| completes. Calling this is equivalent to calling |
70 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when | 84 // PostTaskWithTraitsAndReply with plain TaskTraits. Can only be called when |
71 // SequencedTaskRunnerHandle::IsSet(). | 85 // SequencedTaskRunnerHandle::IsSet(). |
72 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, | 86 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, |
73 const Closure& task, | 87 const Closure& task, |
74 const Closure& reply); | 88 const Closure& reply); |
75 | 89 |
76 // Posts |task| to the TaskScheduler and posts |reply| with the return value of | 90 // Posts |task| to the TaskScheduler and posts |reply| with the return value of |
77 // |task| as argument on the caller's execution context (i.e. same sequence or | 91 // |task| as argument on the caller's execution context (i.e. same sequence or |
78 // thread and same TaskTraits if applicable) when |task| completes. Calling this | 92 // thread and same TaskTraits if applicable) when |task| completes. Calling this |
79 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain | 93 // is equivalent to calling PostTaskWithTraitsAndReplyWithResult with plain |
80 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet(). | 94 // TaskTraits. Can only be called when SequencedTaskRunnerHandle::IsSet(). |
81 template <typename TaskReturnType, typename ReplyArgType> | 95 template <typename TaskReturnType, typename ReplyArgType> |
82 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, | 96 void PostTaskAndReplyWithResult(const tracked_objects::Location& from_here, |
83 const Callback<TaskReturnType(void)>& task, | 97 const Callback<TaskReturnType(void)>& task, |
84 const Callback<void(ReplyArgType)>& reply) { | 98 const Callback<void(ReplyArgType)>& reply) { |
85 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), task, reply); | 99 PostTaskWithTraitsAndReplyWithResult(from_here, TaskTraits(), task, reply); |
86 } | 100 } |
87 | 101 |
88 // Posts |task| with specific |traits| to the TaskScheduler. | 102 // Posts |task| with specific |traits| to the TaskScheduler. |
89 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, | 103 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, |
90 const TaskTraits& traits, | 104 const TaskTraits& traits, |
91 const Closure& task); | 105 const Closure& task); |
92 | 106 |
| 107 // Posts |task| with specific |traits| to the TaskScheduler. |task| will not run |
| 108 // before |delay| expires. |
| 109 // |
| 110 // Specify a BACKGROUND priority via |traits| if the task doesn't have to run as |
| 111 // soon as |delay| expires. |
| 112 BASE_EXPORT void PostDelayedTaskWithTraits( |
| 113 const tracked_objects::Location& from_here, |
| 114 const TaskTraits& traits, |
| 115 const Closure& task, |
| 116 TimeDelta delay); |
| 117 |
93 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on | 118 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| on |
94 // the caller's execution context (i.e. same sequence or thread and same | 119 // the caller's execution context (i.e. same sequence or thread and same |
95 // TaskTraits if applicable) when |task| completes. Can only be called when | 120 // TaskTraits if applicable) when |task| completes. Can only be called when |
96 // SequencedTaskRunnerHandle::IsSet(). | 121 // SequencedTaskRunnerHandle::IsSet(). |
97 BASE_EXPORT void PostTaskWithTraitsAndReply( | 122 BASE_EXPORT void PostTaskWithTraitsAndReply( |
98 const tracked_objects::Location& from_here, | 123 const tracked_objects::Location& from_here, |
99 const TaskTraits& traits, | 124 const TaskTraits& traits, |
100 const Closure& task, | 125 const Closure& task, |
101 const Closure& reply); | 126 const Closure& reply); |
102 | 127 |
103 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| | 128 // Posts |task| with specific |traits| to the TaskScheduler and posts |reply| |
104 // with the return value of |task| as argument on the caller's execution context | 129 // with the return value of |task| as argument on the caller's execution context |
105 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task| | 130 // (i.e. same sequence or thread and same TaskTraits if applicable) when |task| |
106 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet(). | 131 // completes. Can only be called when SequencedTaskRunnerHandle::IsSet(). |
107 template <typename TaskReturnType, typename ReplyArgType> | 132 template <typename TaskReturnType, typename ReplyArgType> |
108 void PostTaskWithTraitsAndReplyWithResult( | 133 void PostTaskWithTraitsAndReplyWithResult( |
109 const tracked_objects::Location& from_here, | 134 const tracked_objects::Location& from_here, |
110 const TaskTraits& traits, | 135 const TaskTraits& traits, |
111 const Callback<TaskReturnType(void)>& task, | 136 const Callback<TaskReturnType(void)>& task, |
112 const Callback<void(ReplyArgType)>& reply) { | 137 const Callback<void(ReplyArgType)>& reply) { |
113 TaskReturnType* result = new TaskReturnType(); | 138 TaskReturnType* result = new TaskReturnType(); |
114 return PostTaskWithTraitsAndReply( | 139 return PostTaskWithTraitsAndReply( |
115 from_here, traits, | 140 from_here, traits, |
116 Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, result), | 141 Bind(&internal::ReturnAsParamAdapter<TaskReturnType>, task, result), |
117 Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, | 142 Bind(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, reply, |
118 Owned(result))); | 143 Owned(result))); |
119 } | 144 } |
120 | 145 |
121 // Delayed tasks posted to TaskRunners returned by the functions below may be | |
122 // coalesced (i.e. delays may be adjusted to reduce the number of wakeups and | |
123 // hence power consumption). | |
124 | |
125 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks | 146 // Returns a TaskRunner whose PostTask invocations result in scheduling tasks |
126 // using |traits|. Tasks may run in any order and in parallel. | 147 // using |traits|. Tasks may run in any order and in parallel. |
127 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 148 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
128 const TaskTraits& traits); | 149 const TaskTraits& traits); |
129 | 150 |
130 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling | 151 // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling |
131 // tasks using |traits|. Tasks run one at a time in posting order. | 152 // tasks using |traits|. Tasks run one at a time in posting order. |
132 BASE_EXPORT scoped_refptr<SequencedTaskRunner> | 153 BASE_EXPORT scoped_refptr<SequencedTaskRunner> |
133 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits); | 154 CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits); |
134 | 155 |
135 // Returns a SingleThreadTaskRunner whose PostTask invocations result in | 156 // Returns a SingleThreadTaskRunner whose PostTask invocations result in |
136 // scheduling tasks using |traits|. Tasks run on a single thread in posting | 157 // scheduling tasks using |traits|. Tasks run on a single thread in posting |
137 // order. | 158 // order. |
138 // | 159 // |
139 // If all you need is to make sure that tasks don't run concurrently (e.g. | 160 // If all you need is to make sure that tasks don't run concurrently (e.g. |
140 // because they access a data structure which is not thread-safe), use | 161 // because they access a data structure which is not thread-safe), use |
141 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread- | 162 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread- |
142 // affine API (it might be safer to assume thread-affinity when dealing with | 163 // affine API (it might be safer to assume thread-affinity when dealing with |
143 // under-documented third-party APIs, e.g. other OS') or share data across tasks | 164 // under-documented third-party APIs, e.g. other OS') or share data across tasks |
144 // using thread-local storage. | 165 // using thread-local storage. |
145 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> | 166 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> |
146 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits); | 167 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits); |
147 | 168 |
148 } // namespace base | 169 } // namespace base |
149 | 170 |
150 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ | 171 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ |
OLD | NEW |