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> |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // | 104 // |
105 // On Windows, join processes asynchronously using base::win::ObjectWatcher. | 105 // On Windows, join processes asynchronously using base::win::ObjectWatcher. |
106 // | 106 // |
107 // MayBlock() must be specified in conjunction with this trait if and only if | 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 | 108 // removing usage of methods listed above in the labeled tasks would still |
109 // result in tasks that may block (per MayBlock()'s definition). | 109 // result in tasks that may block (per MayBlock()'s definition). |
110 // | 110 // |
111 // In doubt, consult with //base/task_scheduler/OWNERS. | 111 // In doubt, consult with //base/task_scheduler/OWNERS. |
112 struct WithBaseSyncPrimitives {}; | 112 struct WithBaseSyncPrimitives {}; |
113 | 113 |
114 // Describes metadata for a single task or a group of tasks. | 114 // Describes immutable metadata for a single task or a group of tasks. |
115 class BASE_EXPORT TaskTraits { | 115 class BASE_EXPORT TaskTraits { |
116 private: | 116 private: |
117 // ValidTrait ensures TaskTraits' constructor only accepts appropriate types. | 117 // ValidTrait ensures TaskTraits' constructor only accepts appropriate types. |
118 // | |
119 // TODO(fdoray): Remove base:: prefixes once the TaskTraits::MayBlock() and | |
120 // TaskTraits::WithBaseSyncPrimitives() methods are gone. | |
121 // https://crbug.com/713683 | |
122 struct ValidTrait { | 118 struct ValidTrait { |
123 ValidTrait(TaskPriority) {} | 119 ValidTrait(TaskPriority) {} |
124 ValidTrait(TaskShutdownBehavior) {} | 120 ValidTrait(TaskShutdownBehavior) {} |
125 ValidTrait(base::MayBlock) {} | 121 ValidTrait(MayBlock) {} |
126 ValidTrait(base::WithBaseSyncPrimitives) {} | 122 ValidTrait(WithBaseSyncPrimitives) {} |
127 }; | 123 }; |
128 | 124 |
129 public: | 125 public: |
130 // Invoking this constructor without arguments produces TaskTraits that are | 126 // Invoking this constructor without arguments produces TaskTraits that are |
131 // appropriate for tasks that | 127 // appropriate for tasks that |
132 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), | 128 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), |
133 // (2) prefer inheriting the current priority to specifying their own, and | 129 // (2) prefer inheriting the current priority to specifying their own, and |
134 // (3) can either block shutdown or be skipped on shutdown | 130 // (3) can either block shutdown or be skipped on shutdown |
135 // (TaskScheduler implementation is free to choose a fitting default). | 131 // (TaskScheduler implementation is free to choose a fitting default). |
136 // | 132 // |
137 // To get TaskTraits for tasks that require stricter guarantees and/or know | 133 // To get TaskTraits for tasks that require stricter guarantees and/or know |
138 // the specific TaskPriority appropriate for them, provide arguments of type | 134 // the specific TaskPriority appropriate for them, provide arguments of type |
139 // TaskPriority, TaskShutdownBehavior, MayBlock, and/or WithBaseSyncPrimitives | 135 // TaskPriority, TaskShutdownBehavior, MayBlock, and/or WithBaseSyncPrimitives |
140 // in any order to the constructor. | 136 // in any order to the constructor. |
141 // | 137 // |
142 // E.g. | 138 // E.g. |
143 // constexpr base::TaskTraits default_traits = {}; | 139 // constexpr base::TaskTraits default_traits = {}; |
144 // constexpr base::TaskTraits user_visible_traits = | 140 // constexpr base::TaskTraits user_visible_traits = |
145 // {base::TaskPriority::USER_VISIBLE}; | 141 // {base::TaskPriority::USER_VISIBLE}; |
146 // constexpr base::TaskTraits user_visible_may_block_traits = { | 142 // constexpr base::TaskTraits user_visible_may_block_traits = { |
147 // base::TaskPriority::USER_VISIBLE, base::MayBlock()}; | 143 // base::TaskPriority::USER_VISIBLE, base::MayBlock()}; |
148 // constexpr base::TaskTraits other_user_visible_may_block_traits = { | 144 // constexpr base::TaskTraits other_user_visible_may_block_traits = { |
149 // base::MayBlock(), base::TaskPriority::USER_VISIBLE}; | 145 // base::MayBlock(), base::TaskPriority::USER_VISIBLE}; |
150 template <class... ArgTypes, | 146 template <class... ArgTypes, |
151 class CheckArgumentsAreValid = internal::InitTypes< | 147 class CheckArgumentsAreValid = internal::InitTypes< |
152 decltype(ValidTrait(std::declval<ArgTypes>()))...>> | 148 decltype(ValidTrait(std::declval<ArgTypes>()))...>> |
153 constexpr TaskTraits(ArgTypes... args) | 149 constexpr TaskTraits(ArgTypes... args) |
154 : priority_set_explicitly_( | 150 : priority_set_explicitly_( |
155 internal::HasArgOfType<base::TaskPriority, ArgTypes...>::value), | 151 internal::HasArgOfType<TaskPriority, ArgTypes...>::value), |
156 priority_(internal::GetValueFromArgList( | 152 priority_(internal::GetValueFromArgList( |
157 internal::EnumArgGetter<base::TaskPriority, | 153 internal::EnumArgGetter<TaskPriority, TaskPriority::USER_VISIBLE>(), |
158 base::TaskPriority::USER_VISIBLE>(), | |
159 args...)), | 154 args...)), |
160 shutdown_behavior_set_explicitly_( | 155 shutdown_behavior_set_explicitly_( |
161 internal::HasArgOfType<base::TaskShutdownBehavior, | 156 internal::HasArgOfType<TaskShutdownBehavior, ArgTypes...>::value), |
162 ArgTypes...>::value), | |
163 shutdown_behavior_(internal::GetValueFromArgList( | 157 shutdown_behavior_(internal::GetValueFromArgList( |
164 internal::EnumArgGetter< | 158 internal::EnumArgGetter<TaskShutdownBehavior, |
165 base::TaskShutdownBehavior, | 159 TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(), |
166 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(), | |
167 args...)), | 160 args...)), |
168 may_block_(internal::GetValueFromArgList( | 161 may_block_(internal::GetValueFromArgList( |
169 internal::BooleanArgGetter<base::MayBlock>(), | 162 internal::BooleanArgGetter<MayBlock>(), |
170 args...)), | 163 args...)), |
171 with_base_sync_primitives_(internal::GetValueFromArgList( | 164 with_base_sync_primitives_(internal::GetValueFromArgList( |
172 internal::BooleanArgGetter<base::WithBaseSyncPrimitives>(), | 165 internal::BooleanArgGetter<WithBaseSyncPrimitives>(), |
173 args...)) {} | 166 args...)) {} |
174 | 167 |
175 constexpr TaskTraits(const TaskTraits& other) = default; | 168 constexpr TaskTraits(const TaskTraits& other) = default; |
176 TaskTraits& operator=(const TaskTraits& other) = default; | 169 TaskTraits& operator=(const TaskTraits& other) = default; |
177 | 170 |
178 // Returns TaskTraits constructed by combining |left| and |right|. If a trait | 171 // Returns TaskTraits constructed by combining |left| and |right|. If a trait |
179 // is specified in both |left| and |right|, the returned TaskTraits will have | 172 // is specified in both |left| and |right|, the returned TaskTraits will have |
180 // the value from |right|. | 173 // the value from |right|. |
181 static constexpr TaskTraits Override(const TaskTraits& left, | 174 static constexpr TaskTraits Override(const TaskTraits& left, |
182 const TaskTraits& right) { | 175 const TaskTraits& right) { |
183 return TaskTraits(left, right); | 176 return TaskTraits(left, right); |
184 } | 177 } |
185 | 178 |
186 // Deprecated. Prefer constexpr construction to builder paradigm as | |
187 // documented above. | |
188 // TODO(fdoray): Remove these methods. https://crbug.com/713683 | |
189 TaskTraits& WithPriority(TaskPriority priority); | |
190 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); | |
191 TaskTraits& MayBlock(); | |
192 TaskTraits& WithBaseSyncPrimitives(); | |
193 | |
194 // Returns true if the priority was set explicitly. | 179 // Returns true if the priority was set explicitly. |
195 constexpr bool priority_set_explicitly() const { | 180 constexpr bool priority_set_explicitly() const { |
196 return priority_set_explicitly_; | 181 return priority_set_explicitly_; |
197 } | 182 } |
198 | 183 |
199 // Returns the priority of tasks with these traits. | 184 // Returns the priority of tasks with these traits. |
200 constexpr TaskPriority priority() const { return priority_; } | 185 constexpr TaskPriority priority() const { return priority_; } |
201 | 186 |
202 // Returns true if the shutdown behavior was set explicitly. | 187 // Returns true if the shutdown behavior was set explicitly. |
203 constexpr bool shutdown_behavior_set_explicitly() const { | 188 constexpr bool shutdown_behavior_set_explicitly() const { |
(...skipping 22 matching lines...) Expand all Loading... |
226 shutdown_behavior_set_explicitly_( | 211 shutdown_behavior_set_explicitly_( |
227 left.shutdown_behavior_set_explicitly_ || | 212 left.shutdown_behavior_set_explicitly_ || |
228 right.shutdown_behavior_set_explicitly_), | 213 right.shutdown_behavior_set_explicitly_), |
229 shutdown_behavior_(right.shutdown_behavior_set_explicitly_ | 214 shutdown_behavior_(right.shutdown_behavior_set_explicitly_ |
230 ? right.shutdown_behavior_ | 215 ? right.shutdown_behavior_ |
231 : left.shutdown_behavior_), | 216 : left.shutdown_behavior_), |
232 may_block_(left.may_block_ || right.may_block_), | 217 may_block_(left.may_block_ || right.may_block_), |
233 with_base_sync_primitives_(left.with_base_sync_primitives_ || | 218 with_base_sync_primitives_(left.with_base_sync_primitives_ || |
234 right.with_base_sync_primitives_) {} | 219 right.with_base_sync_primitives_) {} |
235 | 220 |
236 // TODO(fdoray): Make these const after refactoring away deprecated builder | |
237 // pattern. | |
238 bool priority_set_explicitly_; | 221 bool priority_set_explicitly_; |
239 TaskPriority priority_; | 222 TaskPriority priority_; |
240 bool shutdown_behavior_set_explicitly_; | 223 bool shutdown_behavior_set_explicitly_; |
241 TaskShutdownBehavior shutdown_behavior_; | 224 TaskShutdownBehavior shutdown_behavior_; |
242 bool may_block_; | 225 bool may_block_; |
243 bool with_base_sync_primitives_; | 226 bool with_base_sync_primitives_; |
244 }; | 227 }; |
245 | 228 |
246 // Returns string literals for the enums defined in this file. These methods | 229 // Returns string literals for the enums defined in this file. These methods |
247 // should only be used for tracing and debugging. | 230 // should only be used for tracing and debugging. |
248 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); | 231 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); |
249 BASE_EXPORT const char* TaskShutdownBehaviorToString( | 232 BASE_EXPORT const char* TaskShutdownBehaviorToString( |
250 TaskShutdownBehavior task_priority); | 233 TaskShutdownBehavior task_priority); |
251 | 234 |
252 // Stream operators so that the enums defined in this file can be used in | 235 // Stream operators so that the enums defined in this file can be used in |
253 // DCHECK and EXPECT statements. | 236 // DCHECK and EXPECT statements. |
254 BASE_EXPORT std::ostream& operator<<(std::ostream& os, | 237 BASE_EXPORT std::ostream& operator<<(std::ostream& os, |
255 const TaskPriority& shutdown_behavior); | 238 const TaskPriority& shutdown_behavior); |
256 BASE_EXPORT std::ostream& operator<<( | 239 BASE_EXPORT std::ostream& operator<<( |
257 std::ostream& os, | 240 std::ostream& os, |
258 const TaskShutdownBehavior& shutdown_behavior); | 241 const TaskShutdownBehavior& shutdown_behavior); |
259 | 242 |
260 } // namespace base | 243 } // namespace base |
261 | 244 |
262 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 245 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
OLD | NEW |