Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 enum class TaskPriority : TaskPriorityUnderlyingType { | |
| 16 // This task affects UI immediately after a user interaction. | |
| 17 // Example: Generating data shown in the UI immediately after a click. | |
| 18 USER_BLOCKING = 2, | |
| 19 // This task affects UI or responsiveness of future user interactions. It is | |
| 20 // not an immediate response to a user interaction. | |
| 21 // Examples: | |
| 22 // - Updating the UI to reflect that a long task was completed. | |
| 23 // - Loading data that might be shown in the UI after a future user | |
| 24 // interaction. | |
| 25 USER_VISIBLE = 1, | |
| 26 // Everything else (user won't notice if this takes an arbitrarily long time | |
| 27 // to complete). | |
| 28 BACKGROUND = 0, | |
| 29 }; | |
| 30 | |
| 31 const TaskPriorityUnderlyingType kNumTaskPriorities = 3; | |
| 32 | |
| 33 enum class TaskShutdownBehavior { | |
| 34 // Tasks posted with this mode which have not started executing before | |
| 35 // shutdown is initiated will never run. Tasks with this mode running at | |
| 36 // shutdown will be ignored (the worker thread will not be joined). | |
| 37 // | |
| 38 // This option provides a nice way to post stuff you don't want blocking | |
| 39 // shutdown. For example, you might be doing a slow DNS lookup and if it's | |
| 40 // blocked on the OS, you may not want to stop shutdown, since the result | |
| 41 // doesn't really matter at that point. | |
| 42 // | |
| 43 // However, you need to be very careful what you do in your callback when you | |
| 44 // use this option. Since the thread will continue to run until the OS | |
| 45 // terminates the process, the app can be in the process of tearing down when | |
| 46 // you're running. This means any singletons or global objects you use may | |
| 47 // suddenly become invalid out from under you. For this reason, it's best to | |
| 48 // use this only for slow but simple operations like the DNS example. | |
| 49 CONTINUE_ON_SHUTDOWN, | |
| 50 | |
| 51 // Tasks posted with this mode that have not started executing at | |
| 52 // shutdown will never run. However, any task that has already begun | |
| 53 // executing when shutdown is invoked will be allowed to continue and | |
| 54 // will block shutdown until completion. | |
| 55 // | |
| 56 // Note: Because TaskScheduler::Shutdown() may block while these tasks are | |
| 57 // executing, care must be taken to ensure that they do not block on the | |
| 58 // thread that called TaskScheduler::Shutdown(), as this may lead to deadlock. | |
| 59 SKIP_ON_SHUTDOWN, | |
| 60 | |
| 61 // Tasks posted with this mode before shutdown is complete will block shutdown | |
| 62 // until they're executed. Generally, this should be used only to save | |
| 63 // critical user data. | |
| 64 // | |
| 65 // Note: Tasks with BACKGROUND priority that block shutdown will be promoted | |
| 66 // to USER_VISIBLE priority during shutdown. | |
| 67 BLOCK_SHUTDOWN, | |
| 68 }; | |
| 69 | |
| 70 // TaskTraits holds metadata about a task and prevents the combinatorial | |
| 71 // explosion of PostTaskWith*() call sites. | |
| 72 // Usage example: | |
| 73 // PostTaskWithTraits( | |
| 74 // FROM_HERE, TaskTraits().WithFileIO().WithPriority(USER_VISIBLE), | |
| 75 // Bind(...)); | |
| 76 struct BASE_EXPORT TaskTraits { | |
| 77 // Constructs a default TaskTraits for tasks with | |
| 78 // (1) no I/O, | |
| 79 // (2) low priority, and | |
| 80 // (3) may block shutdown or be skipped on shutdown. | |
| 81 // Tasks that require stricter guarantees should highlight those by requesting | |
| 82 // explicit traits below. | |
| 83 TaskTraits(); | |
| 84 ~TaskTraits(); | |
| 85 | |
| 86 // Allows tasks with these traits to do file I/O. | |
| 87 TaskTraits& WithFileIO(); | |
| 88 | |
| 89 // Applies a priority to tasks with these traits. | |
| 90 TaskTraits& WithPriority(TaskPriority priority); | |
| 91 | |
| 92 // Applies a shutdown behavior to tasks with these traits. | |
| 93 TaskTraits& WithShutdownBehavior(TaskShutdownBehavior behavior); | |
| 94 | |
| 95 // Returns true if file I/O is allowed by these traits. | |
| 96 bool with_file_io() const { return with_file_io_; } | |
| 97 | |
| 98 // Returns the priority of tasks with these traits. | |
| 99 TaskPriority priority() const { return priority_; } | |
| 100 | |
| 101 // Returns the shutdown behavior of tasks with these traits. | |
| 102 TaskShutdownBehavior shutdown_behavior() const { return shutdown_behavior_; } | |
| 103 | |
| 104 private: | |
| 105 bool with_file_io_; | |
| 106 TaskPriority priority_; | |
| 107 TaskShutdownBehavior shutdown_behavior_; | |
| 108 }; | |
| 109 | |
| 110 enum class ExecutionMode { | |
|
fdoray
2016/02/11 17:30:33
Add a comment explaining that this is used to crea
fdoray
2016/02/12 04:16:20
Done.
| |
| 111 // Can execute multiple tasks at a time. High priority tasks will execute | |
| 112 // first under contention. | |
| 113 PARALLEL, | |
| 114 | |
| 115 // Executes one task at a time. Tasks are guaranteed to run in posting order. | |
| 116 // The sequence’s priority will be that of its pending task with the highest | |
| 117 // priority. | |
| 118 SEQUENCED, | |
| 119 | |
| 120 // Executes one task at a time on a single thread. Tasks are guaranteed to run | |
| 121 // in posting order. | |
| 122 SINGLE_THREADED, | |
| 123 | |
| 124 #if defined(OS_WIN) | |
| 125 // Executes one task at a time on a single thread that has initialized the COM | |
| 126 // library with the single-threaded apartment (STA) concurrency model. Tasks | |
| 127 // are guaranteed to run in posting order and the assigned thread will also | |
| 128 // process COM messages so long as the associated TaskRunner is kept alive. | |
| 129 SINGLE_THREADED_COM_STA, | |
| 130 #endif // defined(OS_WIN) | |
| 131 }; | |
| 132 | |
| 133 } // namespace base | |
| 134 | |
| 135 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_H_ | |
| OLD | NEW |