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