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> |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 // critical user data. | 71 // critical user data. |
72 // | 72 // |
73 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted | 73 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted |
74 // to USER_VISIBLE priority during shutdown. | 74 // to USER_VISIBLE priority during shutdown. |
75 BLOCK_SHUTDOWN, | 75 BLOCK_SHUTDOWN, |
76 }; | 76 }; |
77 | 77 |
78 // Describes metadata for a single task or a group of tasks. | 78 // Describes metadata for a single task or a group of tasks. |
79 class BASE_EXPORT TaskTraits { | 79 class BASE_EXPORT TaskTraits { |
80 public: | 80 public: |
81 // Constructs a default TaskTraits for tasks with | 81 // Constructs a default TaskTraits for tasks that |
82 // (1) no I/O, | 82 // (1) do not make blocking calls |
83 // (2) priority inherited from the calling context, and | 83 // (2) can inherit their priority from the calling context, and |
84 // (3) may block shutdown or be skipped on shutdown. | 84 // (3) may block shutdown or be skipped on shutdown. |
85 // Tasks that require stricter guarantees and/or know the specific | 85 // Tasks that require stricter guarantees and/or know the specific |
86 // TaskPriority appropriate for them should highlight those by requesting | 86 // TaskPriority appropriate for them should highlight those by requesting |
87 // explicit traits below. | 87 // explicit traits below. |
88 TaskTraits(); | 88 TaskTraits(); |
89 TaskTraits(const TaskTraits& other) = default; | 89 TaskTraits(const TaskTraits& other) = default; |
90 TaskTraits& operator=(const TaskTraits& other) = default; | 90 TaskTraits& operator=(const TaskTraits& other) = default; |
91 ~TaskTraits(); | 91 ~TaskTraits(); |
92 | 92 |
93 // Allows tasks with these traits to wait on synchronous file I/O. | 93 // Tasks with this trait may block. This includes but is not limited to tasks |
94 // that wait on synchronous file I/O operations: read or write a file from | |
95 // disk, a pipe or a socket, rename or delete a file, enumerate files in a | |
96 // directory, etc. It is not necessary to use this trait to use locks. For | |
97 // tasks that join threads or processes or wait on a waitable event or | |
98 // condition variable, see WithSyncPrimitives(). | |
99 TaskTraits& MayBlock(); | |
100 | |
101 // Tasks with this trait are allowed to wait on waitable events and condition | |
102 // variables and to join threads and processes. This trait implies MayBlock(). | |
gab
2016/12/19 20:20:14
s/and to/as well as/
fdoray
2016/12/19 21:22:43
Done.
| |
103 // | |
104 // In general, you should not use this trait. | |
gab
2016/12/19 20:20:14
// This trait should generally not be used.
(to
fdoray
2016/12/19 21:22:43
Done.
| |
105 // | |
gab
2016/12/19 20:20:14
Check with jam@ whether we should make this trait
fdoray
2016/12/19 21:22:43
jam@: wdyt?
Note that today, you only need to be
jam
2016/12/19 23:17:34
If it's only used on non UI/IO threads that are me
| |
106 // Instead of waiting on a waitable event or a condition variable, put the | |
107 // work that you would have done after the wait in a callback and post this | |
gab
2016/12/19 20:20:14
s/this callback/that callback/
gab
2016/12/19 20:20:14
s/that you would have done/that should happen/
fdoray
2016/12/19 21:22:43
Done.
fdoray
2016/12/19 21:22:43
Done.
| |
108 // callback from where you would have signaled the waitable event or condition | |
gab
2016/12/19 20:20:14
s/you would have signaled the WE or CV/from where
fdoray
2016/12/19 21:22:43
Done.
| |
109 // variable. If you need to do something after a bunch of tasks have run, use | |
110 // base::BarrierClosure. | |
gab
2016/12/19 20:20:14
// If something needs to be scheduled after many t
fdoray
2016/12/19 21:22:43
Done.
| |
111 // | |
112 // On Windows, join processes asynchronously using base::win::ObjectWatcher. | |
113 // | |
114 // Avoid creating threads. Instead, use | |
115 // base::Create(Sequenced|SingleTreaded)TaskRunnerWithTraits(). If you really | |
116 // need a thread, make it non-joinable and add cleanup work at the end of the | |
gab
2016/12/19 20:20:14
If a thread is really needed,
fdoray
2016/12/19 21:22:43
Done.
| |
117 // thread's main function (if you use base::Thread, override Cleanup()). | |
gab
2016/12/19 20:20:14
if using base::Thread, ...
fdoray
2016/12/19 21:22:43
Done.
| |
118 TaskTraits& WithSyncPrimitives(); | |
119 | |
120 // DEPRECATED | |
121 // TODO(fdoray): Remove this crbug.com/675660 | |
gab
2016/12/19 20:20:14
Removes this as part of crbug...
^^^^
fdoray
2016/12/19 21:22:43
Done.
| |
94 TaskTraits& WithFileIO(); | 122 TaskTraits& WithFileIO(); |
95 | 123 |
96 // Allows tasks with these traits to wait on things other than file I/O. In | 124 // DEPRECATED |
97 // particular, they may wait on a WaitableEvent or a ConditionVariable, join a | 125 // TODO(fdoray): Remove this crbug.com/675660 |
98 // thread or a process, or make a blocking system call that doesn't involve | |
99 // interactions with the file system. | |
100 TaskTraits& WithWait(); | 126 TaskTraits& WithWait(); |
101 | 127 |
102 // Applies |priority| to tasks with these traits. | 128 // Applies |priority| to tasks with these traits. |
103 TaskTraits& WithPriority(TaskPriority priority); | 129 TaskTraits& WithPriority(TaskPriority priority); |
104 | 130 |
105 // Applies |shutdown_behavior| to tasks with these traits. | 131 // Applies |shutdown_behavior| to tasks with these traits. |
106 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); | 132 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); |
107 | 133 |
108 // Returns true if waiting on synchronous file I/O is allowed by these traits. | 134 // Returns true if tasks with these traits may block. |
109 bool with_file_io() const { return with_file_io_; } | 135 bool may_block() const { return may_block_; } |
110 | 136 |
111 // Returns true if waiting on things other than file I/O is allowed by these | 137 // Returns true if tasks with these traits may wait on sync primitives. |
112 // traits. | 138 bool with_sync_primitives() const { return with_sync_primitives_; } |
113 bool with_wait() const { return with_wait_; } | 139 |
140 // DEPRECATED | |
141 // TODO(fdoray): Remove this crbug.com/675660 | |
142 bool with_file_io() const { return may_block(); } | |
114 | 143 |
115 // Returns the priority of tasks with these traits. | 144 // Returns the priority of tasks with these traits. |
116 TaskPriority priority() const { return priority_; } | 145 TaskPriority priority() const { return priority_; } |
117 | 146 |
118 // Returns the shutdown behavior of tasks with these traits. | 147 // Returns the shutdown behavior of tasks with these traits. |
119 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } | 148 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } |
120 | 149 |
121 private: | 150 private: |
122 bool with_file_io_; | 151 bool may_block_; |
123 bool with_wait_; | 152 bool with_sync_primitives_; |
124 TaskPriority priority_; | 153 TaskPriority priority_; |
125 TaskShutdownBehavior shutdown_behavior_; | 154 TaskShutdownBehavior shutdown_behavior_; |
126 }; | 155 }; |
127 | 156 |
128 // Returns string literals for the enums defined in this file. These methods | 157 // Returns string literals for the enums defined in this file. These methods |
129 // should only be used for tracing and debugging. | 158 // should only be used for tracing and debugging. |
130 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); | 159 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); |
131 BASE_EXPORT const char* TaskShutdownBehaviorToString( | 160 BASE_EXPORT const char* TaskShutdownBehaviorToString( |
132 TaskShutdownBehavior task_priority); | 161 TaskShutdownBehavior task_priority); |
133 | 162 |
134 // Stream operators so that the enums defined in this file can be used in | 163 // Stream operators so that the enums defined in this file can be used in |
135 // DCHECK and EXPECT statements. | 164 // DCHECK and EXPECT statements. |
136 BASE_EXPORT std::ostream& operator<<(std::ostream& os, | 165 BASE_EXPORT std::ostream& operator<<(std::ostream& os, |
137 const TaskPriority& shutdown_behavior); | 166 const TaskPriority& shutdown_behavior); |
138 BASE_EXPORT std::ostream& operator<<( | 167 BASE_EXPORT std::ostream& operator<<( |
139 std::ostream& os, | 168 std::ostream& os, |
140 const TaskShutdownBehavior& shutdown_behavior); | 169 const TaskShutdownBehavior& shutdown_behavior); |
141 | 170 |
142 } // namespace base | 171 } // namespace base |
143 | 172 |
144 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 173 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
OLD | NEW |