OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_POST_TASK_H_ |
| 6 #define BASE_TASK_SCHEDULER_POST_TASK_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/callback_forward.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/task_runner.h" |
| 12 #include "base/task_scheduler/task_traits.h" |
| 13 |
| 14 namespace tracked_objects { |
| 15 class Location; |
| 16 } |
| 17 |
| 18 namespace base { |
| 19 // The functions below forward posted tasks to the task scheduler. |
| 20 // A TaskScheduler must have been initialized via |
| 21 // TaskScheduler::Initialize() before these are valid. |
| 22 // |
| 23 // To post a simple one-off task: |
| 24 // PostTask(FROM_HERE, Bind(...)); |
| 25 // |
| 26 // To post a high priority one-off task to respond to a user interaction: |
| 27 // PostTaskWithTraits( |
| 28 // FROM_HERE, |
| 29 // TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 30 // Bind(...)); |
| 31 // |
| 32 // To post tasks that must run in sequence: |
| 33 // scoped_refptr<TaskRunner> task_runner = CreateTaskRunnerWithTraits( |
| 34 // TaskTraits(), ExecutionMode::SEQUENCED); |
| 35 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 36 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 37 // |
| 38 // To post file I/O tasks that must run in sequence and can be skipped on |
| 39 // shutdown: |
| 40 // scoped_refptr<TaskRunner> task_runner = |
| 41 // CreateTaskRunnerWithTraits( |
| 42 // TaskTraits().WithFileIO().WithShutdownBehavior( |
| 43 // TaskShutdownBehavior::SKIP_ON_SHUTDOWN), |
| 44 // ExecutionMode::SEQUENCED); |
| 45 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 46 // task_runner.PostTask(FROM_HERE, Bind(...)); |
| 47 // |
| 48 // |
| 49 // The default TaskTraits apply to tasks that: |
| 50 // (1) don't need to do I/O, |
| 51 // (2) don't affect user interaction and/or visible elements, and |
| 52 // (3) can either block shutdown or be skipped on shutdown |
| 53 // (barring current TaskScheduler default). |
| 54 // If those loose requirements are sufficient for your task, use |
| 55 // PostTask[AndReply], otherwise override these with explicit traits via |
| 56 // PostTaskWithTraits[AndReply]. |
| 57 |
| 58 // Posts |task| to the TaskScheduler. Calling this is equivalent to |
| 59 // calling PostTaskWithTraits with plain TaskTraits. |
| 60 BASE_EXPORT void PostTask(const tracked_objects::Location& from_here, |
| 61 const Closure& task); |
| 62 |
| 63 // Posts |task| to the TaskScheduler and posts |reply| on the |
| 64 // caller's execution context (i.e. same Sequence or MessageLoop) |
| 65 // when |task| completes. Calling this is equivalent to calling |
| 66 // PostTaskWithTraitsAndReply with plain TaskTraits. |
| 67 BASE_EXPORT void PostTaskAndReply(const tracked_objects::Location& from_here, |
| 68 const Closure& task, |
| 69 const Closure& reply); |
| 70 |
| 71 // Posts |task| with specific |traits| to the TaskScheduler. |
| 72 BASE_EXPORT void PostTaskWithTraits(const tracked_objects::Location& from_here, |
| 73 TaskTraits traits, |
| 74 const Closure& task); |
| 75 |
| 76 // Posts |task| with specific |traits| to the TaskScheduler and posts |
| 77 // |reply| on the caller's execution context (i.e. same Sequence or |
| 78 // MessageLoop). |
| 79 BASE_EXPORT void PostTaskWithTraitsAndReply( |
| 80 const tracked_objects::Location& from_here, |
| 81 TaskTraits traits, |
| 82 const Closure& task, |
| 83 const Closure& reply); |
| 84 |
| 85 // Returns a TaskRunner whose PostTask invocations will result in scheduling |
| 86 // tasks using |traits| which will be executed according to |execution_mode|. |
| 87 BASE_EXPORT scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| 88 TaskTraits traits, |
| 89 ExecutionMode execution_mode); |
| 90 |
| 91 // Returns a TaskRunner akin to one created through CreateTaskRunnerWithTraits() |
| 92 // except that it will inherit |parent_task_runner|'s sequence instead of |
| 93 // creating its own. Useful for posting tasks with different traits to the same |
| 94 // sequence. |parent_task_runner| must be a SEQUENCED TaskRunner returned from |
| 95 // CreateTaskRunnerWithTraits. |
| 96 BASE_EXPORT scoped_refptr<TaskRunner> CreateChildTaskRunnerWithTraits( |
| 97 TaskRunner* parent_task_runner, |
| 98 TaskTraits traits); |
| 99 |
| 100 } // namespace base |
| 101 |
| 102 #endif // BASE_TASK_SCHEDULER_POST_TASK_H_ |
OLD | NEW |