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

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

Issue 1702813002: Task Scheduler [1/8] Task Traits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « base/task_scheduler/OWNERS ('k') | base/task_scheduler/task_traits.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_TASK_SCHEDULER_TASK_TRAITS_H_
6 #define BASE_TASK_SCHEDULER_TASK_TRAITS_H_
7
8 #include "base/base_export.h"
9 #include "build/build_config.h"
10
11 namespace base {
12
13 using TaskPriorityUnderlyingType = char;
14
15 // Valid priorities supported by the task scheduler.
gab 2016/02/17 19:27:55 Please add to this comment that values for each en
robliao 2016/02/17 20:41:09 Done.
16 enum class TaskPriority : TaskPriorityUnderlyingType {
17 // This task affects UI immediately after a user interaction.
18 // Example: Generating data shown in the UI immediately after a click.
19 USER_BLOCKING = 2,
jam 2016/02/17 16:50:08 nit: just wondering why you're starting this with
robliao 2016/02/17 19:06:00 It's currently an implementation detail that highe
gab 2016/02/17 19:27:55 I think keeping 0,1,2 for now is fine. We don't pe
robliao 2016/02/17 20:41:09 After given it some more thought, 0, 1, and 2 and
jam 2016/02/17 21:48:26 sgtm to change later as needed. i didn't know the
robliao 2016/02/17 23:12:56 Done. Added a static_assert.
20 // This task affects UI or responsiveness of future user interactions. It is
21 // not an immediate response to a user interaction.
22 // Examples:
23 // - Updating the UI to reflect progress on a long task.
24 // - Loading data that might be shown in the UI after a future user
25 // interaction.
26 USER_VISIBLE = 1,
27 // Everything else (user won't notice if this takes an arbitrarily long time
28 // to complete).
29 BACKGROUND = 0,
30 };
31
32 const TaskPriorityUnderlyingType kNumTaskPriorities = 3;
33
34 // Valid shutdown behaviors supported by the task scheduler.
35 enum class TaskShutdownBehavior {
36 // Tasks posted with this mode which have not started executing before
37 // shutdown is initiated will never run. Tasks with this mode running at
38 // shutdown will be ignored (the worker thread will not be joined).
39 //
40 // This option provides a nice way to post stuff you don't want blocking
41 // shutdown. For example, you might be doing a slow DNS lookup and if it's
42 // blocked on the OS, you may not want to stop shutdown, since the result
43 // doesn't really matter at that point.
44 //
45 // However, you need to be very careful what you do in your callback when you
46 // use this option. Since the thread will continue to run until the OS
47 // terminates the process, the app can be in the process of tearing down when
48 // you're running. This means any singletons or global objects you use may
49 // suddenly become invalid out from under you. For this reason, it's best to
50 // use this only for slow but simple operations like the DNS example.
51 CONTINUE_ON_SHUTDOWN,
52
53 // Tasks posted with this mode that have not started executing at
54 // shutdown will never run. However, any task that has already begun
55 // executing when shutdown is invoked will be allowed to continue and
56 // will block shutdown until completion.
57 //
58 // Note: Because TaskScheduler::Shutdown() may block while these tasks are
59 // executing, care must be taken to ensure that they do not block on the
60 // thread that called TaskScheduler::Shutdown(), as this may lead to deadlock.
61 SKIP_ON_SHUTDOWN,
62
63 // Tasks posted with this mode before shutdown is complete will block shutdown
64 // until they're executed. Generally, this should be used only to save
65 // critical user data.
66 //
67 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted
68 // to USER_VISIBLE priority during shutdown.
69 BLOCK_SHUTDOWN,
70 };
71
72 // Describes metadata for a single task or a group of tasks.
73 struct BASE_EXPORT TaskTraits {
74 // Constructs a default TaskTraits for tasks with
75 // (1) no I/O,
76 // (2) low priority, and
77 // (3) may block shutdown or be skipped on shutdown.
78 // Tasks that require stricter guarantees should highlight those by requesting
79 // explicit traits below.
80 TaskTraits();
81 ~TaskTraits();
82
83 // Allows tasks with these traits to do file I/O.
84 TaskTraits& WithFileIO();
jam 2016/02/17 16:50:08 i'm curious, why do these WithFoo methods modify t
robliao 2016/02/17 19:06:00 There were a few goals behind this setup: 1) We wa
jam 2016/02/17 21:48:26 sure, I was just wondering about returning another
85
86 // Applies |priority| to tasks with these traits.
87 TaskTraits& WithPriority(TaskPriority priority);
88
89 // Applies |shutdown_behavior| to tasks with these traits.
90 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior);
91
92 // Returns true if file I/O is allowed by these traits.
93 bool with_file_io() const { return with_file_io_; }
94
95 // Returns the priority of tasks with these traits.
96 TaskPriority priority() const { return priority_; }
97
98 // Returns the shutdown behavior of tasks with these traits.
99 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; }
100
101 private:
jam 2016/02/17 16:50:08 per style guide, if this has private section shoul
robliao 2016/02/17 19:06:00 This can likely be a class. Changed.
gab 2016/02/17 19:27:55 I think what jam meant is that per style it should
robliao 2016/02/17 20:41:09 There are two reasons why we think these should re
jam 2016/02/17 21:48:26 ok, up to you. in that case this should be a class
robliao 2016/02/17 23:12:56 Done.
102 bool with_file_io_;
103 TaskPriority priority_;
104 TaskShutdownBehavior shutdown_behavior_;
105 };
106
107 // Describes how tasks are executed by a task runner.
108 enum class ExecutionMode {
109 // Can execute multiple tasks at a time in any order.
110 PARALLEL,
111
112 // Executes one task at a time in posting order. The sequence’s priority is
113 // equivalent to the highest priority pending task in the sequence.
114 SEQUENCED,
115
116 // Executes one task at a time on a single thread in posting order.
117 SINGLE_THREADED,
118 };
119
120 } // namespace base
121
122 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_
OLDNEW
« no previous file with comments | « base/task_scheduler/OWNERS ('k') | base/task_scheduler/task_traits.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698