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