| 6 | #define BASE_TASK_POST_TASK_H_ |
| 7 |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/bind.h" |
| 13 | #include "base/callback.h" |
| 14 | #include "base/callback_helpers.h" |
| 15 | #include "base/location.h" |
| 16 | #include "base/memory/ref_counted.h" |
| 17 | #include "base/post_task_and_reply_with_result_internal.h" |
| 18 | #include "base/sequenced_task_runner.h" |
| 19 | #include "base/single_thread_task_runner.h" |
| 20 | #include "base/task/single_thread_task_runner_thread_mode.h" |
| 21 | #include "base/task/task_traits.h" |
| 22 | #include "base/task_runner.h" |
| 23 | #include "base/time/time.h" |
| 24 | #include "base/updateable_sequenced_task_runner.h" |
| 25 | #include "build/build_config.h" |
| 26 |
| 27 | namespace base { |
| 28 |
| 29 | // This is the interface to post tasks. |
| 30 | // |
| 31 | // Note: A migration is in-progress away from this API and in favor of explicit |
| 32 | // API-as-a-destination. thread_pool.h is now preferred to the |
| 33 | // base::ThreadPool() to post to the thread pool |
| 34 | // |
| 35 | // To post a simple one-off task with default traits: |
| 36 | // PostTask(FROM_HERE, BindOnce(...)); |
| 37 | // modern equivalent: |
| 38 | // ThreadPool::PostTask(FROM_HERE, BindOnce(...)); |
| 39 | // |
| 40 | // To post a high priority one-off task to respond to a user interaction: |
| 41 | // PostTask( |
| 42 | // FROM_HERE, |
| 43 | // {ThreadPool(), TaskPriority::USER_BLOCKING}, |
| 44 | // BindOnce(...)); |
| 45 | // modern equivalent: |
| 46 | // ThreadPool::PostTask( |
| 47 | // FROM_HERE, |
| 48 | // {TaskPriority::USER_BLOCKING}, |
| 49 | // BindOnce(...)); |
| 50 | // |
| 51 | // To post tasks that must run in sequence with default traits: |
| 52 | // scoped_refptr<SequencedTaskRunner> task_runner = |
| 53 | // CreateSequencedTaskRunner({ThreadPool()}); |
| 54 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 55 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 56 | // modern equivalent: |
| 57 | // scoped_refptr<SequencedTaskRunner> task_runner = |
| 58 | // ThreadPool::CreateSequencedTaskRunner({}); |
| 59 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 60 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 61 | // |
| 62 | // To post tasks that may block, must run in sequence and can be skipped on |
| 63 | // shutdown: |
| 64 | // scoped_refptr<SequencedTaskRunner> task_runner = |
| 65 | // CreateSequencedTaskRunner({ThreadPool(), MayBlock(), |
| 66 | // TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
| 67 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 68 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 69 | // modern equivalent: |
| 70 | // scoped_refptr<SequencedTaskRunner> task_runner = |
| 71 | // ThreadPool::CreateSequencedTaskRunner( |
| 72 | // {MayBlock(), TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
| 73 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 74 | // task_runner->PostTask(FROM_HERE, BindOnce(...)); |
| 75 | // |
| 76 | // The default traits apply to tasks that: |
| 77 | // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), |
| 78 | // (2) prefer inheriting the current priority to specifying their own, and |
| 79 | // (3) can either block shutdown or be skipped on shutdown |
| 80 | // (implementation is free to choose a fitting default). |
| 81 | // Explicit traits must be specified for tasks for which these loose |
| 82 | // requirements are not sufficient. |
| 83 | // |
| 84 | // Tasks posted with only traits defined in base/task/task_traits.h run on |
| 85 | // threads owned by the registered ThreadPoolInstance (i.e. not on the main |
| 86 | // thread). An embedder (e.g. Chrome) can define additional traits to make tasks |
| 87 | // run on threads of their choosing. |
| 88 | // |
| 89 | // Tasks posted with the same traits will be scheduled in the order they were |
| 90 | // posted. IMPORTANT: Please note however that, unless the traits imply a |
| 91 | // single thread or sequence, this doesn't guarantee any *execution ordering* |
| 92 | // for tasks posted in a given order (being scheduled first doesn't mean it will |
| 93 | // run first -- could run in parallel or have its physical thread preempted). |
| 94 | // |
| 95 | // Prerequisite: A ThreadPoolInstance must have been registered for the current |
| 96 | // process via ThreadPoolInstance::Set() before the functions below are |
| 97 | // valid. This is typically done during the initialization phase in each |
| 98 | // process. If your code is not running in that phase, you most likely don't |
| 99 | // have to worry about this. You will encounter DCHECKs or nullptr dereferences |
| 100 | // if this is violated. For tests, prefer base::test::TaskEnvironment. |
| 101 |
| 102 | // Equivalent to calling PostTask with default TaskTraits. |
| 103 | BASE_EXPORT bool PostTask(const Location& from_here, OnceClosure task); |
| 104 | inline bool PostTask(OnceClosure task, |
| 105 | const Location& from_here = Location::Current()) { |
| 106 | return PostTask(from_here, std::move(task)); |
| 107 | } |
| 108 |
| 109 | // Equivalent to calling PostTaskAndReply with default TaskTraits. |
| 110 | BASE_EXPORT bool PostTaskAndReply(const Location& from_here, |
| 111 | OnceClosure task, |
| 112 | OnceClosure reply); |
| 113 |
| 114 | // Equivalent to calling PostTaskAndReplyWithResult with default TaskTraits. |
| 115 | template <typename TaskReturnType, typename ReplyArgType> |
| 116 | bool PostTaskAndReplyWithResult(const Location& from_here, |
| 117 | OnceCallback<TaskReturnType()> task, |
| 118 | OnceCallback<void(ReplyArgType)> reply) { |
| 119 | return PostTaskAndReplyWithResult(from_here, {ThreadPool()}, std::move(task), |
| 120 | std::move(reply)); |
| 121 | } |
| 122 |
| 123 | // Posts |task| with specific |traits|. Returns false if the task definitely |
| 124 | // won't run because of current shutdown state. |
| 125 | BASE_EXPORT bool PostTask(const Location& from_here, |
| 126 | const TaskTraits& traits, |
| 127 | OnceClosure task); |
| 128 |
| 129 | // Posts |task| with specific |traits|. |task| will not run before |delay| |
| 130 | // expires. Returns false if the task definitely won't run because of current |
| 131 | // shutdown state. |
| 132 | // |
| 133 | // Specify a BEST_EFFORT priority via |traits| if the task doesn't have to run |
| 134 | // as soon as |delay| expires. |
| 135 | BASE_EXPORT bool PostDelayedTask(const Location& from_here, |
| 136 | const TaskTraits& traits, |
| 137 | OnceClosure task, |
| 138 | TimeDelta delay); |
| 139 |
| 140 | // Posts |task| with specific |traits| and posts |reply| on the caller's |
| 141 | // execution context (i.e. same sequence or thread and same TaskTraits if |
| 142 | // applicable) when |task| completes. Returns false if the task definitely won't |
| 143 | // run because of current shutdown state. Can only be called when |
| 144 | // SequencedTaskRunnerHandle::IsSet(). |
| 145 | BASE_EXPORT bool PostTaskAndReply(const Location& from_here, |
| 146 | const TaskTraits& traits, |
| 147 | OnceClosure task, |
| 148 | OnceClosure reply); |
| 149 |
| 150 | // Posts |task| with specific |traits| and posts |reply| with the return value |
| 151 | // of |task| as argument on the caller's execution context (i.e. same sequence |
| 152 | // or thread and same TaskTraits if applicable) when |task| completes. Returns |
| 153 | // false if the task definitely won't run because of current shutdown state. Can |
| 154 | // only be called when SequencedTaskRunnerHandle::IsSet(). |
| 155 | template <typename TaskReturnType, typename ReplyArgType> |
| 156 | bool PostTaskAndReplyWithResult(const Location& from_here, |
| 157 | const TaskTraits& traits, |
| 158 | OnceCallback<TaskReturnType()> task, |
| 159 | OnceCallback<void(ReplyArgType)> reply) { |
| 160 | auto* result = new std::unique_ptr<TaskReturnType>(); |
| 161 | return PostTaskAndReply( |
| 162 | from_here, traits, |
| 163 | BindOnce(&internal::ReturnAsParamAdapter<TaskReturnType>, std::move(task), |
| 164 | result), |
| 165 | BindOnce(&internal::ReplyAdapter<TaskReturnType, ReplyArgType>, |
| 166 | std::move(reply), Owned(result))); |
| 167 | } |
| 168 |
| 169 | // Returns a TaskRunner whose PostTask invocations result in scheduling tasks |
| 170 | // using |traits|. Tasks may run in any order and in parallel. |
| 171 | BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunner( |
| 172 | const TaskTraits& traits); |
| 173 |
| 174 | // Returns a SequencedTaskRunner whose PostTask invocations result in scheduling |
| 175 | // tasks using |traits|. Tasks run one at a time in posting order. |
| 176 | BASE_EXPORT scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunner( |
| 177 | const TaskTraits& traits); |
| 178 |
| 179 | // Returns a task runner whose PostTask invocations result in scheduling tasks |
| 180 | // using |traits|. The priority in |traits| can be updated at any time via |
| 181 | // UpdateableSequencedTaskRunner::UpdatePriority(). An update affects all tasks |
| 182 | // posted to the task runner that aren't running yet. Tasks run one at a time in |
| 183 | // posting order. |
| 184 | // |
| 185 | // |traits| requirements: |
| 186 | // - base::ThreadPool() must be specified. |
| 187 | // Note: Prefer the explicit (thread_pool.h) version of this API while we |
| 188 | // migrate this one to it. |
| 189 | // - Extension traits (e.g. BrowserThread) cannot be specified. |
| 190 | // - base::ThreadPolicy must be specified if the priority of the task runner |
| 191 | // will ever be increased from BEST_EFFORT. |
| 192 | BASE_EXPORT scoped_refptr<UpdateableSequencedTaskRunner> |
| 193 | CreateUpdateableSequencedTaskRunner(const TaskTraits& traits); |
| 194 |
| 195 | // Returns a SingleThreadTaskRunner whose PostTask invocations result in |
| 196 | // scheduling tasks using |traits| on a thread determined by |thread_mode|. See |
| 197 | // base/task/single_thread_task_runner_thread_mode.h for |thread_mode| details. |
| 198 | // If |traits| identifies an existing thread, |
| 199 | // SingleThreadTaskRunnerThreadMode::SHARED must be used. Tasks run on a single |
| 200 | // thread in posting order. |
| 201 | // |
| 202 | // If all you need is to make sure that tasks don't run concurrently (e.g. |
| 203 | // because they access a data structure which is not thread-safe), use |
| 204 | // CreateSequencedTaskRunner(). Only use this if you rely on a thread-affine API |
| 205 | // (it might be safer to assume thread-affinity when dealing with |
| 206 | // under-documented third-party APIs, e.g. other OS') or share data across tasks |
| 207 | // using thread-local storage. |
| 208 | BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunner( |
| 209 | const TaskTraits& traits, |
| 210 | SingleThreadTaskRunnerThreadMode thread_mode = |
| 211 | SingleThreadTaskRunnerThreadMode::SHARED); |
| 212 |
| 213 | #if defined(OS_WIN) |
| 214 | // Returns a SingleThreadTaskRunner whose PostTask invocations result in |
| 215 | // scheduling tasks using |traits| in a COM Single-Threaded Apartment on a |
| 216 | // thread determined by |thread_mode|. See |
| 217 | // base/task/single_thread_task_runner_thread_mode.h for |thread_mode| details. |
| 218 | // If |traits| identifies an existing thread, |
| 219 | // SingleThreadTaskRunnerThreadMode::SHARED must be used. Tasks run in the same |
| 220 | // Single-Threaded Apartment in posting order for the returned |
| 221 | // SingleThreadTaskRunner. There is not necessarily a one-to-one correspondence |
| 222 | // between SingleThreadTaskRunners and Single-Threaded Apartments. The |
| 223 | // implementation is free to share apartments or create new apartments as |
| 224 | // necessary. In either case, care should be taken to make sure COM pointers are |
| 225 | // not smuggled across apartments. |
| 226 | BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunner( |
| 227 | const TaskTraits& traits, |
| 228 | SingleThreadTaskRunnerThreadMode thread_mode = |
| 229 | SingleThreadTaskRunnerThreadMode::SHARED); |
| 230 | #endif // defined(OS_WIN) |
| 231 |
| 232 | // Helpers to send a Delete/ReleaseSoon to a new SequencedTaskRunner created |
| 233 | // from |traits|. The semantics match base::PostTask in that the deletion is |
| 234 | // guaranteed to be scheduled in order with other tasks using the same |traits|. |
| 235 | // |
| 236 | // Prefer using an existing SequencedTaskRunner's Delete/ReleaseSoon over this |
| 237 | // to encode execution order requirements when possible. |
| 238 | // |
| 239 | // Note: base::ThreadPool is not a valid destination as it'd result in a one-off |
| 240 | // parallel task which is generally ill-suited for deletion. Use an existing |
| 241 | // SequencedTaskRunner's DeleteSoon to post a safely ordered deletion. |
| 242 | template <class T> |
| 243 | bool DeleteSoon(const Location& from_here, |
| 244 | const TaskTraits& traits, |
| 245 | const T* object) { |
| 246 | DCHECK(!traits.use_thread_pool()); |
| 247 | return CreateSequencedTaskRunner(traits)->DeleteSoon(from_here, object); |
| 248 | } |
| 249 | template <class T> |
| 250 | bool DeleteSoon(const Location& from_here, |
| 251 | const TaskTraits& traits, |
| 252 | std::unique_ptr<T> object) { |
| 253 | return DeleteSoon(from_here, traits, object.release()); |
| 254 | } |
| 255 | template <class T> |
| 256 | void ReleaseSoon(const Location& from_here, |
| 257 | const TaskTraits& traits, |
| 258 | scoped_refptr<T>&& object) { |
| 259 | DCHECK(!traits.use_thread_pool()); |
| 260 | CreateSequencedTaskRunner(traits)->ReleaseSoon(from_here, std::move(object)); |
| 261 | } |
| 262 |
| 263 | } // namespace base |
| 264 |
| 265 | #endif // BASE_TASK_POST_TASK_H_ |
| 266 |