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