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_TASK_TRAITS_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
6 #define BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 6 #define BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <iosfwd> |
| 11 |
8 #include "base/base_export.h" | 12 #include "base/base_export.h" |
9 #include "build/build_config.h" | 13 #include "build/build_config.h" |
10 | 14 |
11 namespace base { | 15 namespace base { |
12 | 16 |
13 using TaskPriorityUnderlyingType = char; | 17 // Valid priorities supported by the task scheduler. Note: internal algorithms |
14 | 18 // depend on priorities being expressed as a continuous zero-based list from |
15 // Valid priorities supported by the task scheduler. | 19 // lowest to highest priority. Users of this API shouldn't otherwise care about |
16 // A higher value means a higher priority in the scheduler. | 20 // nor use the underlying values. |
17 enum class TaskPriority : TaskPriorityUnderlyingType { | 21 enum class TaskPriority { |
18 // This task affects UI immediately after a user interaction. | 22 // This will always be equal to the lowest priority available. |
19 // Example: Generating data shown in the UI immediately after a click. | 23 LOWEST = 0, |
20 USER_BLOCKING = 2, | 24 // User won't notice if this task takes an arbitrarily long time to complete. |
| 25 BACKGROUND = LOWEST, |
21 // This task affects UI or responsiveness of future user interactions. It is | 26 // This task affects UI or responsiveness of future user interactions. It is |
22 // not an immediate response to a user interaction. | 27 // not an immediate response to a user interaction. |
23 // Examples: | 28 // Examples: |
24 // - Updating the UI to reflect progress on a long task. | 29 // - Updating the UI to reflect progress on a long task. |
25 // - Loading data that might be shown in the UI after a future user | 30 // - Loading data that might be shown in the UI after a future user |
26 // interaction. | 31 // interaction. |
27 USER_VISIBLE = 1, | 32 USER_VISIBLE, |
28 // Everything else (user won't notice if this takes an arbitrarily long time | 33 // This task affects UI immediately after a user interaction. |
29 // to complete). | 34 // Example: Generating data shown in the UI immediately after a click. |
30 BACKGROUND = 0, | 35 USER_BLOCKING, |
| 36 // This will always be equal to the highest priority available. |
| 37 HIGHEST = USER_BLOCKING, |
31 }; | 38 }; |
32 | 39 |
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 // Valid shutdown behaviors supported by the task scheduler. |
40 enum class TaskShutdownBehavior { | 41 enum class TaskShutdownBehavior { |
41 // Tasks posted with this mode which have not started executing before | 42 // 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 is initiated will never run. Tasks with this mode running at |
43 // shutdown will be ignored (the worker thread will not be joined). | 44 // shutdown will be ignored (the worker thread will not be joined). |
44 // | 45 // |
45 // This option provides a nice way to post stuff you don't want blocking | 46 // 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 // 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 // blocked on the OS, you may not want to stop shutdown, since the result |
48 // doesn't really matter at that point. | 49 // doesn't really matter at that point. |
(...skipping 20 matching lines...) Expand all Loading... |
69 // until they're executed. Generally, this should be used only to save | 70 // until they're executed. Generally, this should be used only to save |
70 // critical user data. | 71 // critical user data. |
71 // | 72 // |
72 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted | 73 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted |
73 // to USER_VISIBLE priority during shutdown. | 74 // to USER_VISIBLE priority during shutdown. |
74 BLOCK_SHUTDOWN, | 75 BLOCK_SHUTDOWN, |
75 }; | 76 }; |
76 | 77 |
77 // Describes metadata for a single task or a group of tasks. | 78 // Describes metadata for a single task or a group of tasks. |
78 class BASE_EXPORT TaskTraits { | 79 class BASE_EXPORT TaskTraits { |
| 80 public: |
79 // Constructs a default TaskTraits for tasks with | 81 // Constructs a default TaskTraits for tasks with |
80 // (1) no I/O, | 82 // (1) no I/O, |
81 // (2) low priority, and | 83 // (2) low priority, and |
82 // (3) may block shutdown or be skipped on shutdown. | 84 // (3) may block shutdown or be skipped on shutdown. |
83 // Tasks that require stricter guarantees should highlight those by requesting | 85 // Tasks that require stricter guarantees should highlight those by requesting |
84 // explicit traits below. | 86 // explicit traits below. |
85 TaskTraits(); | 87 TaskTraits(); |
| 88 TaskTraits(const TaskTraits& other) = default; |
| 89 TaskTraits& operator=(const TaskTraits& other) = default; |
86 ~TaskTraits(); | 90 ~TaskTraits(); |
87 | 91 |
88 // Allows tasks with these traits to do file I/O. | 92 // Allows tasks with these traits to do file I/O. |
89 TaskTraits& WithFileIO(); | 93 TaskTraits& WithFileIO(); |
90 | 94 |
91 // Applies |priority| to tasks with these traits. | 95 // Applies |priority| to tasks with these traits. |
92 TaskTraits& WithPriority(TaskPriority priority); | 96 TaskTraits& WithPriority(TaskPriority priority); |
93 | 97 |
94 // Applies |shutdown_behavior| to tasks with these traits. | 98 // Applies |shutdown_behavior| to tasks with these traits. |
95 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); | 99 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); |
(...skipping 19 matching lines...) Expand all Loading... |
115 PARALLEL, | 119 PARALLEL, |
116 | 120 |
117 // Executes one task at a time in posting order. The sequence’s priority is | 121 // 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. | 122 // equivalent to the highest priority pending task in the sequence. |
119 SEQUENCED, | 123 SEQUENCED, |
120 | 124 |
121 // Executes one task at a time on a single thread in posting order. | 125 // Executes one task at a time on a single thread in posting order. |
122 SINGLE_THREADED, | 126 SINGLE_THREADED, |
123 }; | 127 }; |
124 | 128 |
| 129 // Pretty Printer for Google Test. |
| 130 void BASE_EXPORT PrintTo(const TaskPriority& task_priority, std::ostream* os); |
| 131 |
125 } // namespace base | 132 } // namespace base |
126 | 133 |
127 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 134 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
OLD | NEW |