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