| 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 #include "base/time/time.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 | 21 |
| 22 // This is the preferred interface to post tasks to the TaskScheduler. | 22 // This is the preferred interface to post tasks to the TaskScheduler. |
| 23 // | 23 // |
| 24 // Note: The TaskScheduler is still in an experimental phase in Chrome. Please | |
| 25 // refrain from using this API unless you know what you are doing. | |
| 26 // | |
| 27 // TaskScheduler must have been registered for the current process via | 24 // TaskScheduler must have been registered for the current process via |
| 28 // TaskScheduler::SetInstance() before the functions below are valid. | 25 // TaskScheduler::SetInstance() before the functions below are valid. |
| 29 // | 26 // |
| 30 // To post a simple one-off task: | 27 // To post a simple one-off task: |
| 31 // PostTask(FROM_HERE, Bind(...)); | 28 // PostTask(FROM_HERE, Bind(...)); |
| 32 // | 29 // |
| 33 // To post a high priority one-off task to respond to a user interaction: | 30 // To post a high priority one-off task to respond to a user interaction: |
| 34 // PostTaskWithTraits( | 31 // PostTaskWithTraits( |
| 35 // FROM_HERE, | 32 // FROM_HERE, |
| 36 // TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), | 33 // TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 37 // Bind(...)); | 34 // Bind(...)); |
| 38 // | 35 // |
| 39 // To post tasks that must run in sequence: | 36 // To post tasks that must run in sequence: |
| 40 // scoped_refptr<SequencedTaskRunner> task_runner = | 37 // scoped_refptr<SequencedTaskRunner> task_runner = |
| 41 // CreateSequencedTaskRunnerWithTraits(TaskTraits()); | 38 // CreateSequencedTaskRunnerWithTraits(TaskTraits()); |
| 42 // task_runner.PostTask(FROM_HERE, Bind(...)); | 39 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 43 // task_runner.PostTask(FROM_HERE, Bind(...)); | 40 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 44 // | 41 // |
| 45 // To post tasks that may block, must run in sequence and can be skipped on | 42 // To post tasks that may block, must run in sequence and can be skipped on |
| 46 // shutdown: | 43 // shutdown: |
| 47 // scoped_refptr<SequencedTaskRunner> task_runner = | 44 // scoped_refptr<SequencedTaskRunner> task_runner = |
| 48 // CreateSequencedTaskRunnerWithTraits( | 45 // CreateSequencedTaskRunnerWithTraits( |
| 49 // TaskTraits().MayBlock().WithShutdownBehavior( | 46 // TaskTraits().MayBlock().WithShutdownBehavior( |
| 50 // TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); | 47 // TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); |
| 51 // task_runner.PostTask(FROM_HERE, Bind(...)); | 48 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 52 // task_runner.PostTask(FROM_HERE, Bind(...)); | 49 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 53 // | 50 // |
| 54 // The default TaskTraits apply to tasks that: | 51 // The default TaskTraits apply to tasks that: |
| 55 // (1) don't need to do I/O, | 52 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), |
| 56 // (2) don't affect user interaction and/or visible elements, and | 53 // (2) prefer inheriting the current priority to specifying their own, and |
| 57 // (3) can either block shutdown or be skipped on shutdown | 54 // (3) can either block shutdown or be skipped on shutdown |
| 58 // (barring current TaskScheduler default). | 55 // (barring current TaskScheduler default). |
| 59 // If those loose requirements are sufficient for your task, use | 56 // If those loose requirements are sufficient for your task, use |
| 60 // PostTask[AndReply], otherwise override these with explicit traits via | 57 // PostTask[AndReply], otherwise override these with explicit traits via |
| 61 // PostTaskWithTraits[AndReply]. | 58 // PostTaskWithTraits[AndReply]. |
| 62 // | 59 // |
| 63 // Tasks posted to TaskScheduler with a delay may be coalesced (i.e. delays may | 60 // 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 // be adjusted to reduce the number of wakeups and hence power consumption). |
| 65 | 62 |
| 66 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling | 63 // Posts |task| to the TaskScheduler. Calling this is equivalent to calling |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread- | 159 // CreateSequencedTaskRunnerWithTraits(). Only use this if you rely on a thread- |
| 163 // affine API (it might be safer to assume thread-affinity when dealing with | 160 // affine API (it might be safer to assume thread-affinity when dealing with |
| 164 // under-documented third-party APIs, e.g. other OS') or share data across tasks | 161 // under-documented third-party APIs, e.g. other OS') or share data across tasks |
| 165 // using thread-local storage. | 162 // using thread-local storage. |
| 166 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> | 163 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> |
| 167 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits); | 164 CreateSingleThreadTaskRunnerWithTraits(const TaskTraits& traits); |
| 168 | 165 |
| 169 } // namespace base | 166 } // namespace base |
| 170 | 167 |
| 171 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ | 168 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ |
| OLD | NEW |