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_TASK_TRAITS_H_ |
| 6 #define BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "build/build_config.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 using TaskPriorityUnderlyingType = char; |
| 14 |
| 15 // Valid priorities supported by the task scheduler. |
| 16 enum class TaskPriority : TaskPriorityUnderlyingType { |
| 17 // This task affects UI immediately after a user interaction. |
| 18 // Example: Generating data shown in the UI immediately after a click. |
| 19 USER_BLOCKING = 2, |
| 20 // This task affects UI or responsiveness of future user interactions. It is |
| 21 // not an immediate response to a user interaction. |
| 22 // Examples: |
| 23 // - Updating the UI to reflect progress on a long task. |
| 24 // - Loading data that might be shown in the UI after a future user |
| 25 // interaction. |
| 26 USER_VISIBLE = 1, |
| 27 // Everything else (user won't notice if this takes an arbitrarily long time |
| 28 // to complete). |
| 29 BACKGROUND = 0, |
| 30 }; |
| 31 |
| 32 const TaskPriorityUnderlyingType kNumTaskPriorities = 3; |
| 33 |
| 34 // Valid shutdown behaviors supported by the task scheduler. |
| 35 enum class TaskShutdownBehavior { |
| 36 // Tasks posted with this mode which have not started executing before |
| 37 // shutdown is initiated will never run. Tasks with this mode running at |
| 38 // shutdown will be ignored (the worker thread will not be joined). |
| 39 // |
| 40 // This option provides a nice way to post stuff you don't want blocking |
| 41 // shutdown. For example, you might be doing a slow DNS lookup and if it's |
| 42 // blocked on the OS, you may not want to stop shutdown, since the result |
| 43 // doesn't really matter at that point. |
| 44 // |
| 45 // However, you need to be very careful what you do in your callback when you |
| 46 // use this option. Since the thread will continue to run until the OS |
| 47 // terminates the process, the app can be in the process of tearing down when |
| 48 // you're running. This means any singletons or global objects you use may |
| 49 // suddenly become invalid out from under you. For this reason, it's best to |
| 50 // use this only for slow but simple operations like the DNS example. |
| 51 CONTINUE_ON_SHUTDOWN, |
| 52 |
| 53 // Tasks posted with this mode that have not started executing at |
| 54 // shutdown will never run. However, any task that has already begun |
| 55 // executing when shutdown is invoked will be allowed to continue and |
| 56 // will block shutdown until completion. |
| 57 // |
| 58 // Note: Because TaskScheduler::Shutdown() may block while these tasks are |
| 59 // executing, care must be taken to ensure that they do not block on the |
| 60 // thread that called TaskScheduler::Shutdown(), as this may lead to deadlock. |
| 61 SKIP_ON_SHUTDOWN, |
| 62 |
| 63 // Tasks posted with this mode before shutdown is complete will block shutdown |
| 64 // until they're executed. Generally, this should be used only to save |
| 65 // critical user data. |
| 66 // |
| 67 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted |
| 68 // to USER_VISIBLE priority during shutdown. |
| 69 BLOCK_SHUTDOWN, |
| 70 }; |
| 71 |
| 72 // Describes metadata for a single task or a group of tasks. |
| 73 struct BASE_EXPORT TaskTraits { |
| 74 // Constructs a default TaskTraits for tasks with |
| 75 // (1) no I/O, |
| 76 // (2) low priority, and |
| 77 // (3) may block shutdown or be skipped on shutdown. |
| 78 // Tasks that require stricter guarantees should highlight those by requesting |
| 79 // explicit traits below. |
| 80 TaskTraits(); |
| 81 ~TaskTraits(); |
| 82 |
| 83 // Allows tasks with these traits to do file I/O. |
| 84 TaskTraits& WithFileIO(); |
| 85 |
| 86 // Applies |priority| to tasks with these traits. |
| 87 TaskTraits& WithPriority(TaskPriority priority); |
| 88 |
| 89 // Applies |shutdown_behavior| to tasks with these traits. |
| 90 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); |
| 91 |
| 92 // Returns true if file I/O is allowed by these traits. |
| 93 bool with_file_io() const { return with_file_io_; } |
| 94 |
| 95 // Returns the priority of tasks with these traits. |
| 96 TaskPriority priority() const { return priority_; } |
| 97 |
| 98 // Returns the shutdown behavior of tasks with these traits. |
| 99 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } |
| 100 |
| 101 private: |
| 102 bool with_file_io_; |
| 103 TaskPriority priority_; |
| 104 TaskShutdownBehavior shutdown_behavior_; |
| 105 }; |
| 106 |
| 107 // Describes how tasks are executed by a task runner. |
| 108 enum class ExecutionMode { |
| 109 // Can execute multiple tasks at a time in any order. |
| 110 PARALLEL, |
| 111 |
| 112 // Executes one task at a time in posting order. The sequence’s priority is |
| 113 // equivalent to the highest priority pending task in the sequence. |
| 114 SEQUENCED, |
| 115 |
| 116 // Executes one task at a time on a single thread in posting order. |
| 117 SINGLE_THREADED, |
| 118 }; |
| 119 |
| 120 } // namespace base |
| 121 |
| 122 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
OLD | NEW |