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> | 8 #include <stdint.h> |
9 | 9 |
10 #include <iosfwd> | 10 #include <iosfwd> |
| 11 #include <type_traits> |
11 | 12 |
12 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/task_scheduler/task_traits_details.h" |
13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
14 | 16 |
15 namespace base { | 17 namespace base { |
16 | 18 |
17 // Valid priorities supported by the task scheduler. Note: internal algorithms | 19 // Valid priorities supported by the task scheduler. Note: internal algorithms |
18 // depend on priorities being expressed as a continuous zero-based list from | 20 // depend on priorities being expressed as a continuous zero-based list from |
19 // lowest to highest priority. Users of this API shouldn't otherwise care about | 21 // lowest to highest priority. Users of this API shouldn't otherwise care about |
20 // nor use the underlying values. | 22 // nor use the underlying values. |
21 enum class TaskPriority { | 23 enum class TaskPriority { |
22 // This will always be equal to the lowest priority available. | 24 // This will always be equal to the lowest priority available. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 70 |
69 // Tasks posted with this mode before shutdown is complete will block shutdown | 71 // Tasks posted with this mode before shutdown is complete will block shutdown |
70 // until they're executed. Generally, this should be used only to save | 72 // until they're executed. Generally, this should be used only to save |
71 // critical user data. | 73 // critical user data. |
72 // | 74 // |
73 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted | 75 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted |
74 // to USER_VISIBLE priority during shutdown. | 76 // to USER_VISIBLE priority during shutdown. |
75 BLOCK_SHUTDOWN, | 77 BLOCK_SHUTDOWN, |
76 }; | 78 }; |
77 | 79 |
| 80 // Tasks with this trait may block. This includes but is not limited to tasks |
| 81 // that wait on synchronous file I/O operations: read or write a file from disk, |
| 82 // interact with a pipe or a socket, rename or delete a file, enumerate files in |
| 83 // a directory, etc. This trait isn't required for the mere use of locks. For |
| 84 // tasks that block on base/ synchronization primitives, see the |
| 85 // WithBaseSyncPrimitives trait. |
| 86 struct MayBlock {}; |
| 87 |
| 88 // Tasks with this trait will pass base::AssertWaitAllowed(), i.e. will be |
| 89 // allowed on the following methods : |
| 90 // - base::WaitableEvent::Wait |
| 91 // - base::ConditionVariable::Wait |
| 92 // - base::PlatformThread::Join |
| 93 // - base::PlatformThread::Sleep |
| 94 // - base::Process::WaitForExit |
| 95 // - base::Process::WaitForExitWithTimeout |
| 96 // |
| 97 // Tasks should generally not use these methods. |
| 98 // |
| 99 // Instead of waiting on a WaitableEvent or a ConditionVariable, put the work |
| 100 // that should happen after the wait in a callback and post that callback from |
| 101 // where the WaitableEvent or ConditionVariable would have been signaled. If |
| 102 // something needs to be scheduled after many tasks have executed, use |
| 103 // base::BarrierClosure. |
| 104 // |
| 105 // On Windows, join processes asynchronously using base::win::ObjectWatcher. |
| 106 // |
| 107 // MayBlock() must be specified in conjunction with this trait if and only if |
| 108 // removing usage of methods listed above in the labeled tasks would still |
| 109 // result in tasks that may block (per MayBlock()'s definition). |
| 110 // |
| 111 // In doubt, consult with //base/task_scheduler/OWNERS. |
| 112 struct WithBaseSyncPrimitives {}; |
| 113 |
78 // Describes metadata for a single task or a group of tasks. | 114 // Describes metadata for a single task or a group of tasks. |
79 class BASE_EXPORT TaskTraits { | 115 class BASE_EXPORT TaskTraits { |
80 public: | 116 public: |
81 // Constructs a default TaskTraits for tasks that | 117 // Invoking this constructor without arguments produces TaskTraits that are |
| 118 // appropriate for tasks that |
82 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), | 119 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), |
83 // (2) prefer inheriting the current priority to specifying their own, and | 120 // (2) prefer inheriting the current priority to specifying their own, and |
84 // (3) can either block shutdown or be skipped on shutdown | 121 // (3) can either block shutdown or be skipped on shutdown |
85 // (TaskScheduler implementation is free to choose a fitting default). | 122 // (TaskScheduler implementation is free to choose a fitting default). |
86 // Tasks that require stricter guarantees and/or know the specific | 123 // |
87 // TaskPriority appropriate for them should highlight those by requesting | 124 // To get TaskTraits for tasks that require stricter guarantees and/or know |
88 // explicit traits below. | 125 // the specific TaskPriority appropriate for them, provide arguments of type |
89 TaskTraits(); | 126 // TaskPriority, TaskShutdownBehavior, MayBlock and/or WithBaseSyncPrimitives |
90 TaskTraits(const TaskTraits& other) = default; | 127 // in any order to the constructor. |
| 128 // |
| 129 // E.g. |
| 130 // constexpr base::TaskTraits default_traits = {}; |
| 131 // constexpr base::TaskTraits user_visible_traits = |
| 132 // {base::TaskPriority::USER_VISIBLE}; |
| 133 // constexpr base::TaskTraits user_visible_may_block_traits = { |
| 134 // base::TaskPriority::USER_VISIBLE, base::MayBlock()}; |
| 135 // constexpr base::TaskTraits other_user_visible_may_block_traits = { |
| 136 // base::MayBlock(), base::TaskPriority::USER_VISIBLE}; |
| 137 template <class... ArgTypes, |
| 138 class CheckArgumentsAreValid = internal::InitTypes< |
| 139 decltype(ValidTrait(std::declval<ArgTypes>()))...>> |
| 140 constexpr TaskTraits(ArgTypes... args) |
| 141 : priority_set_explicitly_( |
| 142 internal::HasArgOfType<base::TaskPriority, ArgTypes...>::value), |
| 143 priority_(internal::GetValueFromArgList( |
| 144 internal::EnumArgGetter<base::TaskPriority, |
| 145 base::TaskPriority::USER_VISIBLE>(), |
| 146 args...)), |
| 147 shutdown_behavior_(internal::GetValueFromArgList( |
| 148 internal::EnumArgGetter< |
| 149 base::TaskShutdownBehavior, |
| 150 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(), |
| 151 args...)), |
| 152 may_block_(internal::GetValueFromArgList( |
| 153 internal::BooleanArgGetter<base::MayBlock>(), |
| 154 args...)), |
| 155 with_base_sync_primitives_(internal::GetValueFromArgList( |
| 156 internal::BooleanArgGetter<base::WithBaseSyncPrimitives>(), |
| 157 args...)) {} |
| 158 |
| 159 constexpr TaskTraits(const TaskTraits& other) = default; |
91 TaskTraits& operator=(const TaskTraits& other) = default; | 160 TaskTraits& operator=(const TaskTraits& other) = default; |
92 ~TaskTraits(); | |
93 | 161 |
94 // Tasks with this trait may block. This includes but is not limited to tasks | 162 // Deprecated. Prefer constexpr construction to builder paradigm as |
95 // that wait on synchronous file I/O operations: read or write a file from | 163 // documented above. |
96 // disk, interact with a pipe or a socket, rename or delete a file, enumerate | 164 TaskTraits& WithPriority(TaskPriority priority); |
97 // files in a directory, etc. This trait isn't required for the mere use of | 165 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); |
98 // locks. For tasks that block on base/ synchronization primitives, see | |
99 // WithBaseSyncPrimitives(). | |
100 TaskTraits& MayBlock(); | 166 TaskTraits& MayBlock(); |
101 | |
102 // Tasks with this trait will pass base::AssertWaitAllowed(), i.e. will be | |
103 // allowed on the following methods : | |
104 // - base::WaitableEvent::Wait | |
105 // - base::ConditionVariable::Wait | |
106 // - base::PlatformThread::Join | |
107 // - base::PlatformThread::Sleep | |
108 // - base::Process::WaitForExit | |
109 // - base::Process::WaitForExitWithTimeout | |
110 // | |
111 // Tasks should generally not use these methods. | |
112 // | |
113 // Instead of waiting on a WaitableEvent or a ConditionVariable, put the work | |
114 // that should happen after the wait in a callback and post that callback from | |
115 // where the WaitableEvent or ConditionVariable would have been signaled. If | |
116 // something needs to be scheduled after many tasks have executed, use | |
117 // base::BarrierClosure. | |
118 // | |
119 // Avoid creating threads. Instead, use | |
120 // base::Create(Sequenced|SingleTreaded)TaskRunnerWithTraits(). If a thread is | |
121 // really needed, make it non-joinable and add cleanup work at the end of the | |
122 // thread's main function (if using base::Thread, override Cleanup()). | |
123 // | |
124 // On Windows, join processes asynchronously using base::win::ObjectWatcher. | |
125 // | |
126 // MayBlock() must be specified in conjunction with this trait if and only if | |
127 // removing usage of methods listed above in the labeled tasks would still | |
128 // result in tasks that may block (per MayBlock()'s definition). | |
129 // | |
130 // In doubt, consult with //base/task_scheduler/OWNERS. | |
131 TaskTraits& WithBaseSyncPrimitives(); | 167 TaskTraits& WithBaseSyncPrimitives(); |
132 | 168 |
133 // Applies |priority| to tasks with these traits. | 169 // Returns true if the priority was set explicitly. |
134 TaskTraits& WithPriority(TaskPriority priority); | 170 constexpr bool priority_set_explicitly() const { |
| 171 return priority_set_explicitly_; |
| 172 } |
135 | 173 |
136 // Applies |shutdown_behavior| to tasks with these traits. | 174 // Returns the priority of tasks with these traits. |
137 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); | 175 constexpr TaskPriority priority() const { return priority_; } |
| 176 |
| 177 // Returns the shutdown behavior of tasks with these traits. |
| 178 constexpr TaskShutdownBehavior shutdown_behavior() const { |
| 179 return shutdown_behavior_; |
| 180 } |
138 | 181 |
139 // Returns true if tasks with these traits may block. | 182 // Returns true if tasks with these traits may block. |
140 bool may_block() const { return may_block_; } | 183 constexpr bool may_block() const { return may_block_; } |
141 | 184 |
142 // Returns true if tasks with these traits may use base/ sync primitives. | 185 // Returns true if tasks with these traits may use base/ sync primitives. |
143 bool with_base_sync_primitives() const { return with_base_sync_primitives_; } | 186 constexpr bool with_base_sync_primitives() const { |
144 | 187 return with_base_sync_primitives_; |
145 // Returns true if the priority was set explicitly. | 188 } |
146 bool priority_set_explicitly() const { return priority_set_explicitly_; } | |
147 | |
148 // Returns the priority of tasks with these traits. | |
149 TaskPriority priority() const { return priority_; } | |
150 | |
151 // Returns the shutdown behavior of tasks with these traits. | |
152 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } | |
153 | 189 |
154 private: | 190 private: |
155 // Do not rely on defaults hard-coded below beyond the guarantees described on | 191 // ValidTrait ensures that we only accept types appropriate for TaskTraits. |
156 // the constructor; anything else is subject to change. Tasks should | 192 struct ValidTrait { |
157 // explicitly request defaults if the behavior is critical to the task. | 193 ValidTrait(TaskPriority) {} |
158 bool may_block_ = false; | 194 ValidTrait(TaskShutdownBehavior) {} |
159 bool with_base_sync_primitives_ = false; | 195 ValidTrait(base::MayBlock) {} |
160 bool priority_set_explicitly_ = false; | 196 ValidTrait(base::WithBaseSyncPrimitives) {} |
161 TaskPriority priority_ = TaskPriority::USER_VISIBLE; | 197 }; |
162 TaskShutdownBehavior shutdown_behavior_ = | 198 |
163 TaskShutdownBehavior::SKIP_ON_SHUTDOWN; | 199 // TODO(fdoray): Make these const after refactoring away deprecated builder |
| 200 // pattern. |
| 201 bool priority_set_explicitly_; |
| 202 TaskPriority priority_; |
| 203 TaskShutdownBehavior shutdown_behavior_; |
| 204 bool may_block_; |
| 205 bool with_base_sync_primitives_; |
164 }; | 206 }; |
165 | 207 |
166 // Returns string literals for the enums defined in this file. These methods | 208 // Returns string literals for the enums defined in this file. These methods |
167 // should only be used for tracing and debugging. | 209 // should only be used for tracing and debugging. |
168 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); | 210 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); |
169 BASE_EXPORT const char* TaskShutdownBehaviorToString( | 211 BASE_EXPORT const char* TaskShutdownBehaviorToString( |
170 TaskShutdownBehavior task_priority); | 212 TaskShutdownBehavior task_priority); |
171 | 213 |
172 // Stream operators so that the enums defined in this file can be used in | 214 // Stream operators so that the enums defined in this file can be used in |
173 // DCHECK and EXPECT statements. | 215 // DCHECK and EXPECT statements. |
174 BASE_EXPORT std::ostream& operator<<(std::ostream& os, | 216 BASE_EXPORT std::ostream& operator<<(std::ostream& os, |
175 const TaskPriority& shutdown_behavior); | 217 const TaskPriority& shutdown_behavior); |
176 BASE_EXPORT std::ostream& operator<<( | 218 BASE_EXPORT std::ostream& operator<<( |
177 std::ostream& os, | 219 std::ostream& os, |
178 const TaskShutdownBehavior& shutdown_behavior); | 220 const TaskShutdownBehavior& shutdown_behavior); |
179 | 221 |
180 } // namespace base | 222 } // namespace base |
181 | 223 |
182 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 224 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
OLD | NEW |