xref: /aosp_15_r20/external/cronet/base/task/task_traits.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2016 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_TASK_TASK_TRAITS_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_TASK_TASK_TRAITS_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <iosfwd>
11*6777b538SAndroid Build Coastguard Worker #include <tuple>
12*6777b538SAndroid Build Coastguard Worker #include <type_traits>
13*6777b538SAndroid Build Coastguard Worker #include <utility>
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
16*6777b538SAndroid Build Coastguard Worker #include "base/check.h"
17*6777b538SAndroid Build Coastguard Worker #include "base/check_op.h"
18*6777b538SAndroid Build Coastguard Worker #include "base/traits_bag.h"
19*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker namespace base {
22*6777b538SAndroid Build Coastguard Worker 
23*6777b538SAndroid Build Coastguard Worker // Valid priorities supported by the task scheduling infrastructure.
24*6777b538SAndroid Build Coastguard Worker //
25*6777b538SAndroid Build Coastguard Worker // Note: internal algorithms depend on priorities being expressed as a
26*6777b538SAndroid Build Coastguard Worker // continuous zero-based list from lowest to highest priority. Users of this API
27*6777b538SAndroid Build Coastguard Worker // shouldn't otherwise care about nor use the underlying values.
28*6777b538SAndroid Build Coastguard Worker //
29*6777b538SAndroid Build Coastguard Worker // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base.task
30*6777b538SAndroid Build Coastguard Worker enum class TaskPriority : uint8_t {
31*6777b538SAndroid Build Coastguard Worker   // This will always be equal to the lowest priority available.
32*6777b538SAndroid Build Coastguard Worker   LOWEST = 0,
33*6777b538SAndroid Build Coastguard Worker   // Best effort tasks will only start running when machine resources are
34*6777b538SAndroid Build Coastguard Worker   // available. The application may preempt best effort tasks if it expects that
35*6777b538SAndroid Build Coastguard Worker   // resources will soon be needed by work of higher priority. Dependending on
36*6777b538SAndroid Build Coastguard Worker   // the ThreadPolicy, best effort tasks may run on a thread that is likely to
37*6777b538SAndroid Build Coastguard Worker   // be descheduled when higher priority work arrives (in this process or
38*6777b538SAndroid Build Coastguard Worker   // another).
39*6777b538SAndroid Build Coastguard Worker   //
40*6777b538SAndroid Build Coastguard Worker   // Examples:
41*6777b538SAndroid Build Coastguard Worker   // - Reporting metrics.
42*6777b538SAndroid Build Coastguard Worker   // - Persisting data to disk.
43*6777b538SAndroid Build Coastguard Worker   // - Loading data that is required for a potential future user interaction
44*6777b538SAndroid Build Coastguard Worker   //   (Note: Use CreateUpdateableSequencedTaskRunner() to increase the priority
45*6777b538SAndroid Build Coastguard Worker   //   when that user interactions happens).
46*6777b538SAndroid Build Coastguard Worker   BEST_EFFORT = LOWEST,
47*6777b538SAndroid Build Coastguard Worker 
48*6777b538SAndroid Build Coastguard Worker   // The result of user visible tasks is visible to the user (in the UI or as a
49*6777b538SAndroid Build Coastguard Worker   // side-effect on the system) but it is not an immediate response to a user
50*6777b538SAndroid Build Coastguard Worker   // interaction.
51*6777b538SAndroid Build Coastguard Worker   //
52*6777b538SAndroid Build Coastguard Worker   // Examples:
53*6777b538SAndroid Build Coastguard Worker   // - Updating the UI to reflect progress on a long task.
54*6777b538SAndroid Build Coastguard Worker   // - Downloading a file requested by the user.
55*6777b538SAndroid Build Coastguard Worker   // - Loading an image that is displayed in the UI but is non-critical.
56*6777b538SAndroid Build Coastguard Worker   USER_VISIBLE,
57*6777b538SAndroid Build Coastguard Worker 
58*6777b538SAndroid Build Coastguard Worker   // User blocking tasks affects UI immediately after a user interaction.
59*6777b538SAndroid Build Coastguard Worker   //
60*6777b538SAndroid Build Coastguard Worker   // Example:
61*6777b538SAndroid Build Coastguard Worker   // - Loading and rendering a web page after the user clicks a link.
62*6777b538SAndroid Build Coastguard Worker   // - Sorting suggestions after the user types a character in the omnibox.
63*6777b538SAndroid Build Coastguard Worker   //
64*6777b538SAndroid Build Coastguard Worker   // This is the default TaskPriority in order for tasks to run in order by
65*6777b538SAndroid Build Coastguard Worker   // default and avoid unintended consequences. The only way to get a task to
66*6777b538SAndroid Build Coastguard Worker   // run at a higher priority than USER_BLOCKING is to coordinate with a
67*6777b538SAndroid Build Coastguard Worker   // higher-level scheduler (contact [email protected] for such use
68*6777b538SAndroid Build Coastguard Worker   // cases).
69*6777b538SAndroid Build Coastguard Worker   USER_BLOCKING,
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker   // This will always be equal to the highest priority available.
72*6777b538SAndroid Build Coastguard Worker   HIGHEST = USER_BLOCKING
73*6777b538SAndroid Build Coastguard Worker };
74*6777b538SAndroid Build Coastguard Worker 
75*6777b538SAndroid Build Coastguard Worker // Valid shutdown behaviors supported by the thread pool.
76*6777b538SAndroid Build Coastguard Worker enum class TaskShutdownBehavior : uint8_t {
77*6777b538SAndroid Build Coastguard Worker   // Tasks posted with this mode which have not started executing before
78*6777b538SAndroid Build Coastguard Worker   // shutdown is initiated will never run. Tasks with this mode running at
79*6777b538SAndroid Build Coastguard Worker   // shutdown will be ignored (the worker will not be joined).
80*6777b538SAndroid Build Coastguard Worker   //
81*6777b538SAndroid Build Coastguard Worker   // This option provides a nice way to post stuff you don't want blocking
82*6777b538SAndroid Build Coastguard Worker   // shutdown. For example, you might be doing a slow DNS lookup and if it's
83*6777b538SAndroid Build Coastguard Worker   // blocked on the OS, you may not want to stop shutdown, since the result
84*6777b538SAndroid Build Coastguard Worker   // doesn't really matter at that point.
85*6777b538SAndroid Build Coastguard Worker   //
86*6777b538SAndroid Build Coastguard Worker   // However, you need to be very careful what you do in your callback when you
87*6777b538SAndroid Build Coastguard Worker   // use this option. Since the thread will continue to run until the OS
88*6777b538SAndroid Build Coastguard Worker   // terminates the process, the app can be in the process of tearing down when
89*6777b538SAndroid Build Coastguard Worker   // you're running. This means any singletons or global objects you use may
90*6777b538SAndroid Build Coastguard Worker   // suddenly become invalid out from under you. For this reason, it's best to
91*6777b538SAndroid Build Coastguard Worker   // use this only for slow but simple operations like the DNS example.
92*6777b538SAndroid Build Coastguard Worker   CONTINUE_ON_SHUTDOWN,
93*6777b538SAndroid Build Coastguard Worker 
94*6777b538SAndroid Build Coastguard Worker   // Tasks posted with this mode that have not started executing at
95*6777b538SAndroid Build Coastguard Worker   // shutdown will never run. However, any task that has already begun
96*6777b538SAndroid Build Coastguard Worker   // executing when shutdown is invoked will be allowed to continue and
97*6777b538SAndroid Build Coastguard Worker   // will block shutdown until completion.
98*6777b538SAndroid Build Coastguard Worker   //
99*6777b538SAndroid Build Coastguard Worker   // Note: Because ThreadPoolInstance::Shutdown() may block while these tasks
100*6777b538SAndroid Build Coastguard Worker   // are executing, care must be taken to ensure that they do not block on the
101*6777b538SAndroid Build Coastguard Worker   // thread that called ThreadPoolInstance::Shutdown(), as this may lead to
102*6777b538SAndroid Build Coastguard Worker   // deadlock.
103*6777b538SAndroid Build Coastguard Worker   SKIP_ON_SHUTDOWN,
104*6777b538SAndroid Build Coastguard Worker 
105*6777b538SAndroid Build Coastguard Worker   // Tasks posted with this mode before shutdown is complete will block shutdown
106*6777b538SAndroid Build Coastguard Worker   // until they're executed. Generally, this should be used only to save
107*6777b538SAndroid Build Coastguard Worker   // critical user data.
108*6777b538SAndroid Build Coastguard Worker   //
109*6777b538SAndroid Build Coastguard Worker   // Note 1: Delayed tasks cannot block shutdown. Delayed tasks posted as part
110*6777b538SAndroid Build Coastguard Worker   // of a BLOCK_SHUTDOWN sequence will behave like SKIP_ON_SHUTDOWN tasks.
111*6777b538SAndroid Build Coastguard Worker   //
112*6777b538SAndroid Build Coastguard Worker   // Note 2: Background threads will be promoted to normal threads at shutdown
113*6777b538SAndroid Build Coastguard Worker   // (i.e. TaskPriority::BEST_EFFORT + TaskShutdownBehavior::BLOCK_SHUTDOWN will
114*6777b538SAndroid Build Coastguard Worker   // resolve without a priority inversion).
115*6777b538SAndroid Build Coastguard Worker   BLOCK_SHUTDOWN,
116*6777b538SAndroid Build Coastguard Worker };
117*6777b538SAndroid Build Coastguard Worker 
118*6777b538SAndroid Build Coastguard Worker // Determines at which thread priority a task may run.
119*6777b538SAndroid Build Coastguard Worker //
120*6777b538SAndroid Build Coastguard Worker // ThreadPolicy and priority updates
121*6777b538SAndroid Build Coastguard Worker // ---------------------------------
122*6777b538SAndroid Build Coastguard Worker //
123*6777b538SAndroid Build Coastguard Worker //   If the TaskPriority of an UpdateableSequencedTaskRunner is increased while
124*6777b538SAndroid Build Coastguard Worker //   one of its tasks is running at background thread priority, the task's
125*6777b538SAndroid Build Coastguard Worker //   execution will have to complete at background thread priority (may take a
126*6777b538SAndroid Build Coastguard Worker //   long time) before the next task can be scheduled with the new TaskPriority.
127*6777b538SAndroid Build Coastguard Worker //   If it is important that priority increases take effect quickly,
128*6777b538SAndroid Build Coastguard Worker //   MUST_USE_FOREGROUND should be used to prevent the tasks from running at
129*6777b538SAndroid Build Coastguard Worker //   background thread priority. If it is important to minimize impact on the
130*6777b538SAndroid Build Coastguard Worker //   rest on the system when the TaskPriority is BEST_EFFORT, PREFER_BACKGROUND
131*6777b538SAndroid Build Coastguard Worker //   should be used.
132*6777b538SAndroid Build Coastguard Worker //
133*6777b538SAndroid Build Coastguard Worker // ThreadPolicy and priority inversions
134*6777b538SAndroid Build Coastguard Worker // ------------------------------------
135*6777b538SAndroid Build Coastguard Worker //
136*6777b538SAndroid Build Coastguard Worker //   A priority inversion occurs when a task running at background thread
137*6777b538SAndroid Build Coastguard Worker //   priority is descheduled while holding a resource needed by a thread of
138*6777b538SAndroid Build Coastguard Worker //   higher priority. MUST_USE_FOREGROUND can be combined with BEST_EFFORT to
139*6777b538SAndroid Build Coastguard Worker //   indicate that a task has a low priority, but shouldn't run at background
140*6777b538SAndroid Build Coastguard Worker //   thread priority in order to avoid priority inversions. Please consult with
141*6777b538SAndroid Build Coastguard Worker //   //base/task/OWNERS if you suspect a priority inversion.
142*6777b538SAndroid Build Coastguard Worker enum class ThreadPolicy : uint8_t {
143*6777b538SAndroid Build Coastguard Worker   // The task runs on a background priority thread if:
144*6777b538SAndroid Build Coastguard Worker   // - The TaskPriority is BEST_EFFORT.
145*6777b538SAndroid Build Coastguard Worker   // - Background thread priority is supported by the platform (see
146*6777b538SAndroid Build Coastguard Worker   //   environment_config_unittest.cc).
147*6777b538SAndroid Build Coastguard Worker   // - ThreadPoolInstance::Shutdown() hadn't been called when the task started
148*6777b538SAndroid Build Coastguard Worker   // running.
149*6777b538SAndroid Build Coastguard Worker   //       (Remaining TaskShutdownBehavior::BLOCK_SHUTDOWN tasks use foreground
150*6777b538SAndroid Build Coastguard Worker   //        threads during shutdown regardless of TaskPriority)
151*6777b538SAndroid Build Coastguard Worker   // Otherwise, it runs on a normal priority thread.
152*6777b538SAndroid Build Coastguard Worker   // This is the default.
153*6777b538SAndroid Build Coastguard Worker   PREFER_BACKGROUND,
154*6777b538SAndroid Build Coastguard Worker 
155*6777b538SAndroid Build Coastguard Worker   // The task runs at normal thread priority, irrespective of its TaskPriority.
156*6777b538SAndroid Build Coastguard Worker   MUST_USE_FOREGROUND
157*6777b538SAndroid Build Coastguard Worker };
158*6777b538SAndroid Build Coastguard Worker 
159*6777b538SAndroid Build Coastguard Worker // Tasks with this trait may block. This includes but is not limited to tasks
160*6777b538SAndroid Build Coastguard Worker // that wait on synchronous file I/O operations: read or write a file from disk,
161*6777b538SAndroid Build Coastguard Worker // interact with a pipe or a socket, rename or delete a file, enumerate files in
162*6777b538SAndroid Build Coastguard Worker // a directory, etc. This trait isn't required for the mere use of locks. For
163*6777b538SAndroid Build Coastguard Worker // tasks that block on base/ synchronization primitives, see the
164*6777b538SAndroid Build Coastguard Worker // WithBaseSyncPrimitives trait.
165*6777b538SAndroid Build Coastguard Worker struct MayBlock {};
166*6777b538SAndroid Build Coastguard Worker 
167*6777b538SAndroid Build Coastguard Worker // DEPRECATED. Use base::ScopedAllowBaseSyncPrimitives(ForTesting) instead.
168*6777b538SAndroid Build Coastguard Worker //
169*6777b538SAndroid Build Coastguard Worker // Tasks with this trait will pass base::AssertBaseSyncPrimitivesAllowed(), i.e.
170*6777b538SAndroid Build Coastguard Worker // will be allowed on the following methods :
171*6777b538SAndroid Build Coastguard Worker // - base::WaitableEvent::Wait
172*6777b538SAndroid Build Coastguard Worker // - base::ConditionVariable::Wait
173*6777b538SAndroid Build Coastguard Worker // - base::PlatformThread::Join
174*6777b538SAndroid Build Coastguard Worker // - base::PlatformThread::Sleep
175*6777b538SAndroid Build Coastguard Worker // - base::Process::WaitForExit
176*6777b538SAndroid Build Coastguard Worker // - base::Process::WaitForExitWithTimeout
177*6777b538SAndroid Build Coastguard Worker //
178*6777b538SAndroid Build Coastguard Worker // Tasks should generally not use these methods.
179*6777b538SAndroid Build Coastguard Worker //
180*6777b538SAndroid Build Coastguard Worker // Instead of waiting on a WaitableEvent or a ConditionVariable, put the work
181*6777b538SAndroid Build Coastguard Worker // that should happen after the wait in a callback and post that callback from
182*6777b538SAndroid Build Coastguard Worker // where the WaitableEvent or ConditionVariable would have been signaled. If
183*6777b538SAndroid Build Coastguard Worker // something needs to be scheduled after many tasks have executed, use
184*6777b538SAndroid Build Coastguard Worker // base::BarrierClosure.
185*6777b538SAndroid Build Coastguard Worker //
186*6777b538SAndroid Build Coastguard Worker // On Windows, join processes asynchronously using base::win::ObjectWatcher.
187*6777b538SAndroid Build Coastguard Worker //
188*6777b538SAndroid Build Coastguard Worker // MayBlock() must be specified in conjunction with this trait if and only if
189*6777b538SAndroid Build Coastguard Worker // removing usage of methods listed above in the labeled tasks would still
190*6777b538SAndroid Build Coastguard Worker // result in tasks that may block (per MayBlock()'s definition).
191*6777b538SAndroid Build Coastguard Worker //
192*6777b538SAndroid Build Coastguard Worker // In doubt, consult with //base/task/OWNERS.
193*6777b538SAndroid Build Coastguard Worker struct WithBaseSyncPrimitives {};
194*6777b538SAndroid Build Coastguard Worker 
195*6777b538SAndroid Build Coastguard Worker // Describes metadata for a single task or a group of tasks.
196*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT TaskTraits {
197*6777b538SAndroid Build Coastguard Worker  public:
198*6777b538SAndroid Build Coastguard Worker   // ValidTrait ensures TaskTraits' constructor only accepts appropriate types.
199*6777b538SAndroid Build Coastguard Worker   struct ValidTrait {
200*6777b538SAndroid Build Coastguard Worker     ValidTrait(TaskPriority);
201*6777b538SAndroid Build Coastguard Worker     ValidTrait(TaskShutdownBehavior);
202*6777b538SAndroid Build Coastguard Worker     ValidTrait(ThreadPolicy);
203*6777b538SAndroid Build Coastguard Worker     ValidTrait(MayBlock);
204*6777b538SAndroid Build Coastguard Worker     ValidTrait(WithBaseSyncPrimitives);
205*6777b538SAndroid Build Coastguard Worker   };
206*6777b538SAndroid Build Coastguard Worker 
207*6777b538SAndroid Build Coastguard Worker   // Invoking this constructor without arguments produces default TaskTraits
208*6777b538SAndroid Build Coastguard Worker   // that are appropriate for tasks that
209*6777b538SAndroid Build Coastguard Worker   //     (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()),
210*6777b538SAndroid Build Coastguard Worker   //     (2) pertain to user-blocking activity,
211*6777b538SAndroid Build Coastguard Worker   //         (explicitly or implicitly by having an ordering dependency with a
212*6777b538SAndroid Build Coastguard Worker   //          component that does)
213*6777b538SAndroid Build Coastguard Worker   //     (3) can either block shutdown or be skipped on shutdown
214*6777b538SAndroid Build Coastguard Worker   //         (the task recipient is free to choose a fitting default).
215*6777b538SAndroid Build Coastguard Worker   //
216*6777b538SAndroid Build Coastguard Worker   // To get TaskTraits for tasks that have more precise traits: provide any
217*6777b538SAndroid Build Coastguard Worker   // combination of ValidTrait's as arguments to this constructor.
218*6777b538SAndroid Build Coastguard Worker   //
219*6777b538SAndroid Build Coastguard Worker   // Note: When posting to well-known threads (e.g. UI/IO), default traits are
220*6777b538SAndroid Build Coastguard Worker   // almost always what you want unless you know for sure the task being posted
221*6777b538SAndroid Build Coastguard Worker   // has no explicit/implicit ordering dependency with anything else running at
222*6777b538SAndroid Build Coastguard Worker   // default (USER_BLOCKING) priority.
223*6777b538SAndroid Build Coastguard Worker   //
224*6777b538SAndroid Build Coastguard Worker   // E.g.
225*6777b538SAndroid Build Coastguard Worker   // constexpr base::TaskTraits default_traits = {};
226*6777b538SAndroid Build Coastguard Worker   // constexpr base::TaskTraits user_visible_traits = {
227*6777b538SAndroid Build Coastguard Worker   //     base::TaskPriority::USER_VISIBLE};
228*6777b538SAndroid Build Coastguard Worker   // constexpr base::TaskTraits user_visible_may_block_traits = {
229*6777b538SAndroid Build Coastguard Worker   //     base::TaskPriority::USER_VISIBLE, base::MayBlock()
230*6777b538SAndroid Build Coastguard Worker   // };
231*6777b538SAndroid Build Coastguard Worker   // constexpr base::TaskTraits other_user_visible_may_block_traits = {
232*6777b538SAndroid Build Coastguard Worker   //     base::MayBlock(), base::TaskPriority::USER_VISIBLE
233*6777b538SAndroid Build Coastguard Worker   // };
234*6777b538SAndroid Build Coastguard Worker   template <class... ArgTypes>
235*6777b538SAndroid Build Coastguard Worker     requires trait_helpers::AreValidTraits<ValidTrait, ArgTypes...>
236*6777b538SAndroid Build Coastguard Worker   // TaskTraits are intended to be implicitly-constructable (eg {}).
237*6777b538SAndroid Build Coastguard Worker   // NOLINTNEXTLINE(google-explicit-constructor)
TaskTraits(ArgTypes...args)238*6777b538SAndroid Build Coastguard Worker   constexpr TaskTraits(ArgTypes... args)
239*6777b538SAndroid Build Coastguard Worker       : priority_(
240*6777b538SAndroid Build Coastguard Worker             trait_helpers::GetEnum<TaskPriority, TaskPriority::USER_BLOCKING>(
241*6777b538SAndroid Build Coastguard Worker                 args...)),
242*6777b538SAndroid Build Coastguard Worker         shutdown_behavior_(
243*6777b538SAndroid Build Coastguard Worker             static_cast<uint8_t>(
244*6777b538SAndroid Build Coastguard Worker                 trait_helpers::GetEnum<TaskShutdownBehavior,
245*6777b538SAndroid Build Coastguard Worker                                        TaskShutdownBehavior::SKIP_ON_SHUTDOWN>(
246*6777b538SAndroid Build Coastguard Worker                     args...)) |
247*6777b538SAndroid Build Coastguard Worker             (trait_helpers::HasTrait<TaskShutdownBehavior, ArgTypes...>()
248*6777b538SAndroid Build Coastguard Worker                  ? kIsExplicitFlag
249*6777b538SAndroid Build Coastguard Worker                  : 0)),
250*6777b538SAndroid Build Coastguard Worker         thread_policy_(
251*6777b538SAndroid Build Coastguard Worker             static_cast<uint8_t>(
252*6777b538SAndroid Build Coastguard Worker                 trait_helpers::GetEnum<ThreadPolicy,
253*6777b538SAndroid Build Coastguard Worker                                        ThreadPolicy::PREFER_BACKGROUND>(
254*6777b538SAndroid Build Coastguard Worker                     args...)) |
255*6777b538SAndroid Build Coastguard Worker             (trait_helpers::HasTrait<ThreadPolicy, ArgTypes...>()
256*6777b538SAndroid Build Coastguard Worker                  ? kIsExplicitFlag
257*6777b538SAndroid Build Coastguard Worker                  : 0)),
258*6777b538SAndroid Build Coastguard Worker         may_block_(trait_helpers::HasTrait<MayBlock, ArgTypes...>()),
259*6777b538SAndroid Build Coastguard Worker         with_base_sync_primitives_(
260*6777b538SAndroid Build Coastguard Worker             trait_helpers::HasTrait<WithBaseSyncPrimitives, ArgTypes...>()) {}
261*6777b538SAndroid Build Coastguard Worker 
262*6777b538SAndroid Build Coastguard Worker   constexpr TaskTraits(const TaskTraits& other) = default;
263*6777b538SAndroid Build Coastguard Worker   TaskTraits& operator=(const TaskTraits& other) = default;
264*6777b538SAndroid Build Coastguard Worker 
265*6777b538SAndroid Build Coastguard Worker   friend bool operator==(const TaskTraits&, const TaskTraits&) = default;
266*6777b538SAndroid Build Coastguard Worker 
267*6777b538SAndroid Build Coastguard Worker   // Sets the priority of tasks with these traits to |priority|.
UpdatePriority(TaskPriority priority)268*6777b538SAndroid Build Coastguard Worker   void UpdatePriority(TaskPriority priority) { priority_ = priority; }
269*6777b538SAndroid Build Coastguard Worker 
270*6777b538SAndroid Build Coastguard Worker   // Returns the priority of tasks with these traits.
priority()271*6777b538SAndroid Build Coastguard Worker   constexpr TaskPriority priority() const { return priority_; }
272*6777b538SAndroid Build Coastguard Worker 
273*6777b538SAndroid Build Coastguard Worker   // Returns true if the shutdown behavior was set explicitly.
shutdown_behavior_set_explicitly()274*6777b538SAndroid Build Coastguard Worker   constexpr bool shutdown_behavior_set_explicitly() const {
275*6777b538SAndroid Build Coastguard Worker     return shutdown_behavior_ & kIsExplicitFlag;
276*6777b538SAndroid Build Coastguard Worker   }
277*6777b538SAndroid Build Coastguard Worker 
278*6777b538SAndroid Build Coastguard Worker   // Returns the shutdown behavior of tasks with these traits.
shutdown_behavior()279*6777b538SAndroid Build Coastguard Worker   constexpr TaskShutdownBehavior shutdown_behavior() const {
280*6777b538SAndroid Build Coastguard Worker     return static_cast<TaskShutdownBehavior>(shutdown_behavior_ &
281*6777b538SAndroid Build Coastguard Worker                                              ~kIsExplicitFlag);
282*6777b538SAndroid Build Coastguard Worker   }
283*6777b538SAndroid Build Coastguard Worker 
284*6777b538SAndroid Build Coastguard Worker   // Returns true if the thread policy was set explicitly.
thread_policy_set_explicitly()285*6777b538SAndroid Build Coastguard Worker   constexpr bool thread_policy_set_explicitly() const {
286*6777b538SAndroid Build Coastguard Worker     return thread_policy_ & kIsExplicitFlag;
287*6777b538SAndroid Build Coastguard Worker   }
288*6777b538SAndroid Build Coastguard Worker 
289*6777b538SAndroid Build Coastguard Worker   // Returns the thread policy of tasks with these traits.
thread_policy()290*6777b538SAndroid Build Coastguard Worker   constexpr ThreadPolicy thread_policy() const {
291*6777b538SAndroid Build Coastguard Worker     return static_cast<ThreadPolicy>(thread_policy_ & ~kIsExplicitFlag);
292*6777b538SAndroid Build Coastguard Worker   }
293*6777b538SAndroid Build Coastguard Worker 
294*6777b538SAndroid Build Coastguard Worker   // Returns true if tasks with these traits may block.
may_block()295*6777b538SAndroid Build Coastguard Worker   constexpr bool may_block() const { return may_block_; }
296*6777b538SAndroid Build Coastguard Worker 
297*6777b538SAndroid Build Coastguard Worker   // Returns true if tasks with these traits may use base/ sync primitives.
with_base_sync_primitives()298*6777b538SAndroid Build Coastguard Worker   constexpr bool with_base_sync_primitives() const {
299*6777b538SAndroid Build Coastguard Worker     return with_base_sync_primitives_;
300*6777b538SAndroid Build Coastguard Worker   }
301*6777b538SAndroid Build Coastguard Worker 
302*6777b538SAndroid Build Coastguard Worker  private:
303*6777b538SAndroid Build Coastguard Worker   // This bit is set in |priority_|, |shutdown_behavior_| and |thread_policy_|
304*6777b538SAndroid Build Coastguard Worker   // when the value was set explicitly.
305*6777b538SAndroid Build Coastguard Worker   static constexpr uint8_t kIsExplicitFlag = 0x80;
306*6777b538SAndroid Build Coastguard Worker 
307*6777b538SAndroid Build Coastguard Worker   // Ordered for packing.
308*6777b538SAndroid Build Coastguard Worker   TaskPriority priority_;
309*6777b538SAndroid Build Coastguard Worker   uint8_t shutdown_behavior_;
310*6777b538SAndroid Build Coastguard Worker   uint8_t thread_policy_;
311*6777b538SAndroid Build Coastguard Worker   bool may_block_;
312*6777b538SAndroid Build Coastguard Worker   bool with_base_sync_primitives_;
313*6777b538SAndroid Build Coastguard Worker };
314*6777b538SAndroid Build Coastguard Worker 
315*6777b538SAndroid Build Coastguard Worker // Returns string literals for the enums defined in this file. These methods
316*6777b538SAndroid Build Coastguard Worker // should only be used for tracing and debugging.
317*6777b538SAndroid Build Coastguard Worker BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority);
318*6777b538SAndroid Build Coastguard Worker BASE_EXPORT const char* TaskShutdownBehaviorToString(
319*6777b538SAndroid Build Coastguard Worker     TaskShutdownBehavior task_priority);
320*6777b538SAndroid Build Coastguard Worker 
321*6777b538SAndroid Build Coastguard Worker // Stream operators so that the enums defined in this file can be used in
322*6777b538SAndroid Build Coastguard Worker // DCHECK and EXPECT statements.
323*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::ostream& operator<<(std::ostream& os,
324*6777b538SAndroid Build Coastguard Worker                                      const TaskPriority& shutdown_behavior);
325*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::ostream& operator<<(
326*6777b538SAndroid Build Coastguard Worker     std::ostream& os,
327*6777b538SAndroid Build Coastguard Worker     const TaskShutdownBehavior& shutdown_behavior);
328*6777b538SAndroid Build Coastguard Worker 
329*6777b538SAndroid Build Coastguard Worker }  // namespace base
330*6777b538SAndroid Build Coastguard Worker 
331*6777b538SAndroid Build Coastguard Worker #endif  // BASE_TASK_TASK_TRAITS_H_
332