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, interact with a pipe or a socket, rename or delete a file, enumerate |
| 96 // files in a directory, etc. This trait isn't required for the mere use of |
| 97 // locks. For tasks that block on synchronization primitives (thread or |
| 98 // process handles, waitable events, condition variables), see |
| 99 // WithSyncPrimitives(). |
| 100 TaskTraits& MayBlock(); |
| 101 |
| 102 // Tasks with this trait are allowed to wait on waitable events and condition |
| 103 // variables as well as to join threads and processes. This trait implies |
| 104 // MayBlock(). |
| 105 // |
| 106 // This trait should generally not be used. |
| 107 // |
| 108 // Instead of waiting on a waitable event or a condition variable, put the |
| 109 // work that should happen after the wait in a callback and post that callback |
| 110 // from where the waitable event or condition variable would have been |
| 111 // signaled. If something needs to be scheduled after many tasks have |
| 112 // executed, use base::BarrierClosure. |
| 113 // |
| 114 // On Windows, join processes asynchronously using base::win::ObjectWatcher. |
| 115 // |
| 116 // Avoid creating threads. Instead, use |
| 117 // base::Create(Sequenced|SingleTreaded)TaskRunnerWithTraits(). If a thread is |
| 118 // really needed, make it non-joinable and add cleanup work at the end of the |
| 119 // thread's main function (if using base::Thread, override Cleanup()). |
| 120 TaskTraits& WithSyncPrimitives(); |
| 121 |
| 122 // DEPRECATED |
| 123 // TODO(fdoray): Remove this as part of crbug.com/675660 |
94 TaskTraits& WithFileIO(); | 124 TaskTraits& WithFileIO(); |
95 | 125 |
96 // Allows tasks with these traits to wait on things other than file I/O. In | 126 // DEPRECATED |
97 // particular, they may wait on a WaitableEvent or a ConditionVariable, join a | 127 // TODO(fdoray): Remove this as part of 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(); | 128 TaskTraits& WithWait(); |
101 | 129 |
102 // Applies |priority| to tasks with these traits. | 130 // Applies |priority| to tasks with these traits. |
103 TaskTraits& WithPriority(TaskPriority priority); | 131 TaskTraits& WithPriority(TaskPriority priority); |
104 | 132 |
105 // Applies |shutdown_behavior| to tasks with these traits. | 133 // Applies |shutdown_behavior| to tasks with these traits. |
106 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); | 134 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior shutdown_behavior); |
107 | 135 |
108 // Returns true if waiting on synchronous file I/O is allowed by these traits. | 136 // Returns true if tasks with these traits may block. |
109 bool with_file_io() const { return with_file_io_; } | 137 bool may_block() const { return may_block_; } |
110 | 138 |
111 // Returns true if waiting on things other than file I/O is allowed by these | 139 // Returns true if tasks with these traits may wait on sync primitives. |
112 // traits. | 140 bool with_sync_primitives() const { return with_sync_primitives_; } |
113 bool with_wait() const { return with_wait_; } | 141 |
| 142 // DEPRECATED |
| 143 // TODO(fdoray): Remove this as part of crbug.com/675660 |
| 144 bool with_file_io() const { return may_block(); } |
114 | 145 |
115 // Returns the priority of tasks with these traits. | 146 // Returns the priority of tasks with these traits. |
116 TaskPriority priority() const { return priority_; } | 147 TaskPriority priority() const { return priority_; } |
117 | 148 |
118 // Returns the shutdown behavior of tasks with these traits. | 149 // Returns the shutdown behavior of tasks with these traits. |
119 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } | 150 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } |
120 | 151 |
121 private: | 152 private: |
122 bool with_file_io_; | 153 bool may_block_; |
123 bool with_wait_; | 154 bool with_sync_primitives_; |
124 TaskPriority priority_; | 155 TaskPriority priority_; |
125 TaskShutdownBehavior shutdown_behavior_; | 156 TaskShutdownBehavior shutdown_behavior_; |
126 }; | 157 }; |
127 | 158 |
128 // Returns string literals for the enums defined in this file. These methods | 159 // Returns string literals for the enums defined in this file. These methods |
129 // should only be used for tracing and debugging. | 160 // should only be used for tracing and debugging. |
130 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); | 161 BASE_EXPORT const char* TaskPriorityToString(TaskPriority task_priority); |
131 BASE_EXPORT const char* TaskShutdownBehaviorToString( | 162 BASE_EXPORT const char* TaskShutdownBehaviorToString( |
132 TaskShutdownBehavior task_priority); | 163 TaskShutdownBehavior task_priority); |
133 | 164 |
134 // Stream operators so that the enums defined in this file can be used in | 165 // Stream operators so that the enums defined in this file can be used in |
135 // DCHECK and EXPECT statements. | 166 // DCHECK and EXPECT statements. |
136 BASE_EXPORT std::ostream& operator<<(std::ostream& os, | 167 BASE_EXPORT std::ostream& operator<<(std::ostream& os, |
137 const TaskPriority& shutdown_behavior); | 168 const TaskPriority& shutdown_behavior); |
138 BASE_EXPORT std::ostream& operator<<( | 169 BASE_EXPORT std::ostream& operator<<( |
139 std::ostream& os, | 170 std::ostream& os, |
140 const TaskShutdownBehavior& shutdown_behavior); | 171 const TaskShutdownBehavior& shutdown_behavior); |
141 | 172 |
142 } // namespace base | 173 } // namespace base |
143 | 174 |
144 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | 175 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ |
OLD | NEW |