Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(915)

Side by Side Diff: base/task_scheduler/task_traits.h

Issue 2829083002: Add constexpr TaskTraits constructor. (Closed)
Patch Set: CR-etipdoray-gab-36-38 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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::CallFirstTag(),
142 internal::EnumArgGetter<base::TaskPriority,
143 base::TaskPriority::USER_VISIBLE>(),
144 args...)),
145 shutdown_behavior_(internal::GetValueFromArgList(
146 internal::CallFirstTag(),
147 internal::EnumArgGetter<
148 base::TaskShutdownBehavior,
149 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(),
150 args...)),
151 may_block_(internal::GetValueFromArgList(
152 internal::CallFirstTag(),
153 internal::BooleanArgGetter<base::MayBlock>(),
154 args...)),
155 with_base_sync_primitives_(internal::GetValueFromArgList(
156 internal::CallFirstTag(),
157 internal::BooleanArgGetter<base::WithBaseSyncPrimitives>(),
158 args...)) {
159 static_assert(
160 internal::ArgsAreConvertible<ValidTrait, ArgTypes...>::value,
161 "Argument of invalid type provided to the constructor of TaskTraits.");
162 }
163
164 constexpr TaskTraits(const TaskTraits& other) = default;
91 TaskTraits& operator=(const TaskTraits& other) = default; 165 TaskTraits& operator=(const TaskTraits& other) = default;
92 ~TaskTraits();
93 166
94 // Tasks with this trait may block. This includes but is not limited to tasks 167 // Deprecated. Prefer constexpr construction to builder paradigm as
95 // that wait on synchronous file I/O operations: read or write a file from 168 // documented above.
96 // disk, interact with a pipe or a socket, rename or delete a file, enumerate 169 TaskTraits& WithPriority(TaskPriority priority);
97 // files in a directory, etc. This trait isn't required for the mere use of 170 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior);
98 // locks. For tasks that block on base/ synchronization primitives, see
99 // WithBaseSyncPrimitives().
100 TaskTraits& MayBlock(); 171 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(); 172 TaskTraits& WithBaseSyncPrimitives();
132 173
133 // Applies |priority| to tasks with these traits. 174 // Returns true if the priority was set explicitly.
134 TaskTraits& WithPriority(TaskPriority priority); 175 constexpr bool priority_set_explicitly() const {
176 return priority_set_explicitly_;
177 }
135 178
136 // Applies |shutdown_behavior| to tasks with these traits. 179 // Returns the priority of tasks with these traits.
137 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); 180 constexpr TaskPriority priority() const { return priority_; }
181
182 // Returns the shutdown behavior of tasks with these traits.
183 constexpr TaskShutdownBehavior shutdown_behavior() const {
184 return shutdown_behavior_;
185 }
138 186
139 // Returns true if tasks with these traits may block. 187 // Returns true if tasks with these traits may block.
140 bool may_block() const { return may_block_; } 188 constexpr bool may_block() const { return may_block_; }
141 189
142 // Returns true if tasks with these traits may use base/ sync primitives. 190 // Returns true if tasks with these traits may use base/ sync primitives.
143 bool with_base_sync_primitives() const { return with_base_sync_primitives_; } 191 constexpr bool with_base_sync_primitives() const {
144 192 return with_base_sync_primitives_;
145 // Returns true if the priority was set explicitly. 193 }
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 194
154 private: 195 private:
155 // Do not rely on defaults hard-coded below beyond the guarantees described on 196 // The constructor only accepts types that are convertible to ValidTrait.
robliao 2017/04/26 19:45:54 // ValidTrait ensures that we only accept types ap
fdoray 2017/04/26 20:25:14 Done.
156 // the constructor; anything else is subject to change. Tasks should 197 struct ValidTrait {
157 // explicitly request defaults if the behavior is critical to the task. 198 ValidTrait(TaskPriority) {}
158 bool may_block_ = false; 199 ValidTrait(TaskShutdownBehavior) {}
159 bool with_base_sync_primitives_ = false; 200 ValidTrait(base::MayBlock) {}
160 bool priority_set_explicitly_ = false; 201 ValidTrait(base::WithBaseSyncPrimitives) {}
161 TaskPriority priority_ = TaskPriority::USER_VISIBLE; 202 };
162 TaskShutdownBehavior shutdown_behavior_ = 203
163 TaskShutdownBehavior::SKIP_ON_SHUTDOWN; 204 // TODO(fdoray): Make these const after refactoring away deprecated builder
205 // pattern.
206 bool priority_set_explicitly_;
207 TaskPriority priority_;
208 TaskShutdownBehavior shutdown_behavior_;
209 bool may_block_;
210 bool with_base_sync_primitives_;
164 }; 211 };
165 212
166 // Returns string literals for the enums defined in this file. These methods 213 // Returns string literals for the enums defined in this file. These methods
167 // should only be used for tracing and debugging. 214 // should only be used for tracing and debugging.
168 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); 215 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority);
169 BASE_EXPORT const char* TaskShutdownBehaviorToString( 216 BASE_EXPORT const char* TaskShutdownBehaviorToString(
170 TaskShutdownBehavior task_priority); 217 TaskShutdownBehavior task_priority);
171 218
172 // Stream operators so that the enums defined in this file can be used in 219 // Stream operators so that the enums defined in this file can be used in
173 // DCHECK and EXPECT statements. 220 // DCHECK and EXPECT statements.
174 BASE_EXPORT std::ostream& operator<<(std::ostream& os, 221 BASE_EXPORT std::ostream& operator<<(std::ostream& os,
175 const TaskPriority& shutdown_behavior); 222 const TaskPriority& shutdown_behavior);
176 BASE_EXPORT std::ostream& operator<<( 223 BASE_EXPORT std::ostream& operator<<(
177 std::ostream& os, 224 std::ostream& os,
178 const TaskShutdownBehavior& shutdown_behavior); 225 const TaskShutdownBehavior& shutdown_behavior);
179 226
180 } // namespace base 227 } // namespace base
181 228
182 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ 229 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698