Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 71 |
| 71 // Tasks posted with this mode before shutdown is complete will block shutdown | 72 // Tasks posted with this mode before shutdown is complete will block shutdown |
| 72 // until they're executed. Generally, this should be used only to save | 73 // until they're executed. Generally, this should be used only to save |
| 73 // critical user data. | 74 // critical user data. |
| 74 // | 75 // |
| 75 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted | 76 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted |
| 76 // to USER_VISIBLE priority during shutdown. | 77 // to USER_VISIBLE priority during shutdown. |
| 77 BLOCK_SHUTDOWN, | 78 BLOCK_SHUTDOWN, |
| 78 }; | 79 }; |
| 79 | 80 |
| 81 // Tasks with this trait may block. This includes but is not limited to tasks | |
| 82 // that wait on synchronous file I/O operations: read or write a file from disk, | |
| 83 // interact with a pipe or a socket, rename or delete a file, enumerate files in | |
| 84 // a directory, etc. This trait isn't required for the mere use of locks. For | |
| 85 // tasks that block on base/ synchronization primitives, see the | |
| 86 // WithBaseSyncPrimitives trait. | |
| 87 struct MayBlock {}; | |
| 88 | |
| 89 // Tasks with this trait will pass base::AssertWaitAllowed(), i.e. will be | |
| 90 // allowed on the following methods : | |
| 91 // - base::WaitableEvent::Wait | |
| 92 // - base::ConditionVariable::Wait | |
| 93 // - base::PlatformThread::Join | |
| 94 // - base::PlatformThread::Sleep | |
| 95 // - base::Process::WaitForExit | |
| 96 // - base::Process::WaitForExitWithTimeout | |
| 97 // | |
| 98 // Tasks should generally not use these methods. | |
| 99 // | |
| 100 // Instead of waiting on a WaitableEvent or a ConditionVariable, put the work | |
| 101 // that should happen after the wait in a callback and post that callback from | |
| 102 // where the WaitableEvent or ConditionVariable would have been signaled. If | |
| 103 // something needs to be scheduled after many tasks have executed, use | |
| 104 // base::BarrierClosure. | |
| 105 // | |
| 106 // On Windows, join processes asynchronously using base::win::ObjectWatcher. | |
| 107 // | |
| 108 // MayBlock() must be specified in conjunction with this trait if and only if | |
| 109 // removing usage of methods listed above in the labeled tasks would still | |
| 110 // result in tasks that may block (per MayBlock()'s definition). | |
| 111 // | |
| 112 // In doubt, consult with //base/task_scheduler/OWNERS. | |
| 113 struct WithBaseSyncPrimitives {}; | |
| 114 | |
| 80 // Describes metadata for a single task or a group of tasks. | 115 // Describes metadata for a single task or a group of tasks. |
| 81 class BASE_EXPORT TaskTraits { | 116 class BASE_EXPORT TaskTraits { |
| 82 public: | 117 public: |
| 83 // Constructs a default TaskTraits for tasks that | 118 // Invoking this constructor without arguments produces TaskTraits that are |
| 119 // appropriate for tasks that | |
| 84 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), | 120 // (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()), |
| 85 // (2) prefer inheriting the current priority to specifying their own, and | 121 // (2) prefer inheriting the current priority to specifying their own, and |
| 86 // (3) can either block shutdown or be skipped on shutdown | 122 // (3) can either block shutdown or be skipped on shutdown |
| 87 // (TaskScheduler implementation is free to choose a fitting default). | 123 // (TaskScheduler implementation is free to choose a fitting default). |
| 88 // Tasks that require stricter guarantees and/or know the specific | 124 // |
| 89 // TaskPriority appropriate for them should highlight those by requesting | 125 // To get TaskTraits for tasks that require stricter guarantees and/or know |
| 90 // explicit traits below. | 126 // the specific TaskPriority appropriate for them, provide arguments of type |
| 91 TaskTraits(); | 127 // TaskPriority, TaskShutdownBehavior, MayBlock and/or WithBaseSyncPrimitives |
| 92 TaskTraits(const TaskTraits& other) = default; | 128 // in any order to the constructor. |
| 129 // | |
| 130 // E.g. | |
| 131 // constexpr TaskTraits default_traits; | |
| 132 // constexpr TaskTraits user_visible_traits = {TaskPriority::USER_VISIBLE}; | |
| 133 // constexpr TaskTraits user_visible_may_block_traits = { | |
| 134 // TaskPriority::USER_VISIBLE, MayBlock()}; | |
| 135 // constexpr TaskTraits other_user_visible_may_block_traits = { | |
| 136 // MayBlock(), TaskPriority::USER_VISIBLE}; | |
|
gab
2017/04/25 17:48:14
Add base:: namespace in examples for ease of copy/
fdoray
2017/04/26 17:20:33
Done.
| |
| 137 template <class... ArgTypes> | |
| 138 constexpr TaskTraits(const ArgTypes&... args) | |
| 139 : priority_(internal::GetValueFromArgList( | |
| 140 internal::CallFirstTag(), | |
| 141 internal::EnumArgGetter<base::TaskPriority, | |
| 142 base::TaskPriority::INHERITED>(), | |
| 143 args...)), | |
| 144 shutdown_behavior_(internal::GetValueFromArgList( | |
| 145 internal::CallFirstTag(), | |
| 146 internal::EnumArgGetter< | |
| 147 base::TaskShutdownBehavior, | |
| 148 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(), | |
| 149 args...)), | |
| 150 may_block_(internal::GetValueFromArgList( | |
| 151 internal::CallFirstTag(), | |
| 152 internal::BooleanArgGetter<base::MayBlock>(), | |
| 153 args...)), | |
| 154 with_base_sync_primitives_(internal::GetValueFromArgList( | |
| 155 internal::CallFirstTag(), | |
| 156 internal::BooleanArgGetter<base::WithBaseSyncPrimitives>(), | |
| 157 args...)) {} | |
| 158 | |
| 159 constexpr TaskTraits(const TaskTraits& other) = default; | |
| 93 TaskTraits& operator=(const TaskTraits& other) = default; | 160 TaskTraits& operator=(const TaskTraits& other) = default; |
| 94 ~TaskTraits(); | |
| 95 | 161 |
| 96 // Tasks with this trait may block. This includes but is not limited to tasks | 162 // Deprecated. |
|
gab
2017/04/25 17:48:14
// Deprecated. Prefer constexpr construction to bu
fdoray
2017/04/26 17:20:33
Done.
| |
| 97 // that wait on synchronous file I/O operations: read or write a file from | 163 TaskTraits& WithPriority(TaskPriority priority); |
| 98 // disk, interact with a pipe or a socket, rename or delete a file, enumerate | 164 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); |
| 99 // files in a directory, etc. This trait isn't required for the mere use of | |
| 100 // locks. For tasks that block on base/ synchronization primitives, see | |
| 101 // WithBaseSyncPrimitives(). | |
| 102 TaskTraits& MayBlock(); | 165 TaskTraits& MayBlock(); |
| 103 | |
| 104 // Tasks with this trait will pass base::AssertWaitAllowed(), i.e. will be | |
| 105 // allowed on the following methods : | |
| 106 // - base::WaitableEvent::Wait | |
| 107 // - base::ConditionVariable::Wait | |
| 108 // - base::PlatformThread::Join | |
| 109 // - base::PlatformThread::Sleep | |
| 110 // - base::Process::WaitForExit | |
| 111 // - base::Process::WaitForExitWithTimeout | |
| 112 // | |
| 113 // Tasks should generally not use these methods. | |
| 114 // | |
| 115 // Instead of waiting on a WaitableEvent or a ConditionVariable, put the work | |
| 116 // that should happen after the wait in a callback and post that callback from | |
| 117 // where the WaitableEvent or ConditionVariable would have been signaled. If | |
| 118 // something needs to be scheduled after many tasks have executed, use | |
| 119 // base::BarrierClosure. | |
| 120 // | |
| 121 // Avoid creating threads. Instead, use | |
| 122 // base::Create(Sequenced|SingleTreaded)TaskRunnerWithTraits(). If a thread is | |
| 123 // really needed, make it non-joinable and add cleanup work at the end of the | |
| 124 // thread's main function (if using base::Thread, override Cleanup()). | |
| 125 // | |
| 126 // On Windows, join processes asynchronously using base::win::ObjectWatcher. | |
| 127 // | |
| 128 // MayBlock() must be specified in conjunction with this trait if and only if | |
| 129 // removing usage of methods listed above in the labeled tasks would still | |
| 130 // result in tasks that may block (per MayBlock()'s definition). | |
| 131 // | |
| 132 // In doubt, consult with //base/task_scheduler/OWNERS. | |
| 133 TaskTraits& WithBaseSyncPrimitives(); | 166 TaskTraits& WithBaseSyncPrimitives(); |
| 134 | 167 |
| 135 // Applies |priority| to tasks with these traits. | |
| 136 TaskTraits& WithPriority(TaskPriority priority); | |
| 137 | |
| 138 // Applies |shutdown_behavior| to tasks with these traits. | |
| 139 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); | |
| 140 | |
| 141 // Returns true if tasks with these traits may block. | |
| 142 bool may_block() const { return may_block_; } | |
| 143 | |
| 144 // Returns true if tasks with these traits may use base/ sync primitives. | |
| 145 bool with_base_sync_primitives() const { return with_base_sync_primitives_; } | |
| 146 | |
| 147 // Returns the priority of tasks with these traits. | 168 // Returns the priority of tasks with these traits. |
| 148 TaskPriority priority() const { return priority_; } | 169 TaskPriority priority() const { return priority_; } |
| 149 | 170 |
| 150 // Returns the shutdown behavior of tasks with these traits. | 171 // Returns the shutdown behavior of tasks with these traits. |
| 151 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } | 172 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } |
| 152 | 173 |
| 174 // Returns true if tasks with these traits may block. | |
| 175 bool may_block() const { return may_block_; } | |
|
etipdoray
2017/04/24 20:22:02
Accessors could be constexpr as well.
fdoray
2017/04/26 17:20:33
Done.
| |
| 176 | |
| 177 // Returns true if tasks with these traits may use base/ sync primitives. | |
| 178 bool with_base_sync_primitives() const { return with_base_sync_primitives_; } | |
| 179 | |
| 153 private: | 180 private: |
| 181 TaskPriority priority_; | |
|
gab
2017/04/25 17:48:15
// TODO(fdoray): Make these const after refactorin
fdoray
2017/04/26 17:20:33
Done.
| |
| 182 TaskShutdownBehavior shutdown_behavior_; | |
| 154 bool may_block_; | 183 bool may_block_; |
| 155 bool with_base_sync_primitives_; | 184 bool with_base_sync_primitives_; |
| 156 TaskPriority priority_; | |
| 157 TaskShutdownBehavior shutdown_behavior_; | |
| 158 }; | 185 }; |
| 159 | 186 |
| 160 // Returns string literals for the enums defined in this file. These methods | 187 // Returns string literals for the enums defined in this file. These methods |
| 161 // should only be used for tracing and debugging. | 188 // should only be used for tracing and debugging. |
| 162 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); | 189 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); |
| 163 BASE_EXPORT const char* TaskShutdownBehaviorToString( | 190 BASE_EXPORT const char* TaskShutdownBehaviorToString( |
| 164 TaskShutdownBehavior task_priority); | 191 TaskShutdownBehavior task_priority); |
| 165 | 192 |
| 166 // Stream operators so that the enums defined in this file can be used in | 193 // Stream operators so that the enums defined in this file can be used in |
| 167 // DCHECK and EXPECT statements. | 194 // DCHECK and EXPECT statements. |
| 168 BASE_EXPORT std::ostream& operator<<(std::ostream& os, | 195 BASE_EXPORT std::ostream& operator<<(std::ostream& os, |
| 169 const TaskPriority& shutdown_behavior); | 196 const TaskPriority& shutdown_behavior); |
| 170 BASE_EXPORT std::ostream& operator<<( | 197 BASE_EXPORT std::ostream& operator<<( |
| 171 std::ostream& os, | 198 std::ostream& os, |
| 172 const TaskShutdownBehavior& shutdown_behavior); | 199 const TaskShutdownBehavior& shutdown_behavior); |
| 173 | 200 |
| 174 } // namespace base | 201 } // namespace base |
| 175 | 202 |
| 176 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 203 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
| OLD | NEW |