fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 1 | # Threading and Tasks in Chrome |
| 2 | |
| 3 | [TOC] |
| 4 | |
Gabriel Charette | 8917f4c | 2018-11-22 15:50:28 | [diff] [blame] | 5 | Note: See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more |
| 6 | examples. |
| 7 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 8 | ## Overview |
| 9 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 10 | Chrome has a [multi-process |
| 11 | architecture](https://www.chromium.org/developers/design-documents/multi-process-architecture) |
| 12 | and each process is heavily multi-threaded. In this document we will go over the |
Charlie Harrison | bda5465 | 2023-11-17 21:20:06 | [diff] [blame^] | 13 | basic threading system shared by each process. Our primary goal is to keep the |
| 14 | browser highly responsive. Absent external requirements about latency or |
| 15 | workload, Chrome attempts to be a [highly concurrent, but not necessarily |
Matt Falkenhagen | 72a2dfc | 2021-08-05 22:36:13 | [diff] [blame] | 16 | parallel](https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism#:~:text=Concurrency%20is%20when%20two%20or,e.g.%2C%20on%20a%20multicore%20processor.), |
Jared Saul | ea867ab | 2021-07-15 17:39:01 | [diff] [blame] | 17 | system. |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 18 | |
cfredric | ff6d86c | 2022-02-15 16:26:11 | [diff] [blame] | 19 | A basic intro to the way Chromium does concurrency (especially Sequences) can be |
| 20 | found |
| 21 | [here](https://docs.google.com/presentation/d/1ujV8LjIUyPBmULzdT2aT9Izte8PDwbJi). |
| 22 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 23 | This documentation assumes familiarity with computer science |
| 24 | [threading concepts](https://en.wikipedia.org/wiki/Thread_(computing)). |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 25 | |
Charlie Harrison | bda5465 | 2023-11-17 21:20:06 | [diff] [blame^] | 26 | ### Quick start guide |
| 27 | |
| 28 | * Do not perform expensive computation or blocking IO on the main thread |
| 29 | (a.k.a. “UI” thread in the browser process) or IO thread (each |
| 30 | process's thread for receiving IPC). A busy UI / IO thread can cause |
| 31 | user-visible latency, so prefer running that work on the |
| 32 | [thread pool](#direct-posting-to-the-thread-pool). |
| 33 | * Always avoid reading/writing to the same place in memory from separate |
| 34 | threads or sequences. This will lead to |
| 35 | [data races](https://en.wikipedia.org/wiki/Race_condition#Data_race)! |
| 36 | Prefer passing messages across sequences instead. Alternatives to message |
| 37 | passing like using locks is discouraged. |
| 38 | * To prevent accidental data races, prefer for most classes to be used |
| 39 | exclusively on a single sequence. You should use utilities like |
| 40 | [SEQUENCE_CHECKER](https://source.chromium.org/chromium/chromium/src/+/main:base/sequence_checker.h) |
| 41 | or [base::SequenceBound](https://source.chromium.org/chromium/chromium/src/+/main:base/threading/sequence_bound.h) |
| 42 | to help enforce this constraint. |
| 43 | * If you need to orchestrate multiple objects that live on different |
| 44 | sequences, be careful about object lifetimes. For example, |
| 45 | using [base::Unretained](https://source.chromium.org/chromium/chromium/src/+/main:base/functional/bind.h;l=169;drc=ef1375f2c9fffa0d9cd664b43b0035c09fb70e99) |
| 46 | in posted tasks can often lead to use-after-free bugs without careful |
| 47 | analysis of object lifetimes. Consider whether you need to use |
| 48 | [weak pointers](https://source.chromium.org/chromium/chromium/src/+/main:base/memory/weak_ptr.h) |
| 49 | or [scoped refptrs](https://source.chromium.org/chromium/chromium/src/+/main:base/memory/scoped_refptr.h) |
| 50 | instead. |
| 51 | |
Gabriel Charette | 364a16a | 2019-02-06 21:12:15 | [diff] [blame] | 52 | ### Nomenclature |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 53 | |
| 54 | ## Core Concepts |
| 55 | * **Task**: A unit of work to be processed. Effectively a function pointer with |
Alex St-Onge | 490a97a | 2021-02-04 02:47:19 | [diff] [blame] | 56 | optionally associated state. In Chrome this is `base::OnceCallback` and |
| 57 | `base::RepeatingCallback` created via `base::BindOnce` and |
| 58 | `base::BindRepeating`, respectively. |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 59 | ([documentation](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md)). |
| 60 | * **Task queue**: A queue of tasks to be processed. |
| 61 | * **Physical thread**: An operating system provided thread (e.g. pthread on |
| 62 | POSIX or CreateThread() on Windows). The Chrome cross-platform abstraction |
| 63 | is `base::PlatformThread`. You should pretty much never use this directly. |
| 64 | * **`base::Thread`**: A physical thread forever processing messages from a |
| 65 | dedicated task queue until Quit(). You should pretty much never be creating |
| 66 | your own `base::Thread`'s. |
| 67 | * **Thread pool**: A pool of physical threads with a shared task queue. In |
Gabriel Charette | 0b20ee6 | 2019-09-18 14:06:12 | [diff] [blame] | 68 | Chrome, this is `base::ThreadPoolInstance`. There's exactly one instance per |
| 69 | Chrome process, it serves tasks posted through |
Gabriel Charette | 9b6c0407 | 2022-04-01 23:22:46 | [diff] [blame] | 70 | [`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h) |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 71 | and as such you should rarely need to use the `base::ThreadPoolInstance` API |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 72 | directly (more on posting tasks later). |
| 73 | * **Sequence** or **Virtual thread**: A chrome-managed thread of execution. |
| 74 | Like a physical thread, only one task can run on a given sequence / virtual |
| 75 | thread at any given moment and each task sees the side-effects of the |
| 76 | preceding tasks. Tasks are executed sequentially but may hop physical |
| 77 | threads between each one. |
| 78 | * **Task runner**: An interface through which tasks can be posted. In Chrome |
| 79 | this is `base::TaskRunner`. |
| 80 | * **Sequenced task runner**: A task runner which guarantees that tasks posted |
| 81 | to it will run sequentially, in posted order. Each such task is guaranteed to |
| 82 | see the side-effects of the task preceding it. Tasks posted to a sequenced |
| 83 | task runner are typically processed by a single thread (virtual or physical). |
| 84 | In Chrome this is `base::SequencedTaskRunner` which is-a |
| 85 | `base::TaskRunner`. |
| 86 | * **Single-thread task runner**: A sequenced task runner which guarantees that |
| 87 | all tasks will be processed by the same physical thread. In Chrome this is |
| 88 | `base::SingleThreadTaskRunner` which is-a `base::SequencedTaskRunner`. We |
| 89 | [prefer sequences to threads](#prefer-sequences-to-physical-threads) whenever |
| 90 | possible. |
| 91 | |
| 92 | ## Threading Lexicon |
| 93 | Note to the reader: the following terms are an attempt to bridge the gap between |
| 94 | common threading nomenclature and the way we use them in Chrome. It might be a |
| 95 | bit heavy if you're just getting started. Should this be hard to parse, consider |
| 96 | skipping to the more detailed sections below and referring back to this as |
| 97 | necessary. |
| 98 | |
| 99 | * **Thread-unsafe**: The vast majority of types in Chrome are thread-unsafe |
| 100 | (by design). Access to such types/methods must be externally synchronized. |
| 101 | Typically thread-unsafe types require that all tasks accessing their state be |
| 102 | posted to the same `base::SequencedTaskRunner` and they verify this in debug |
| 103 | builds with a `SEQUENCE_CHECKER` member. Locks are also an option to |
| 104 | synchronize access but in Chrome we strongly |
| 105 | [prefer sequences to locks](#Using-Sequences-Instead-of-Locks). |
Gabriel Charette | 364a16a | 2019-02-06 21:12:15 | [diff] [blame] | 106 | * **Thread-affine**: Such types/methods need to be always accessed from the |
Gabriel Charette | b984d67 | 2019-02-12 21:53:27 | [diff] [blame] | 107 | same physical thread (i.e. from the same `base::SingleThreadTaskRunner`) and |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 108 | typically have a `THREAD_CHECKER` member to verify that they are. Short of |
| 109 | using a third-party API or having a leaf dependency which is thread-affine: |
| 110 | there's pretty much no reason for a type to be thread-affine in Chrome. |
| 111 | Note that `base::SingleThreadTaskRunner` is-a `base::SequencedTaskRunner` so |
Gabriel Charette | b984d67 | 2019-02-12 21:53:27 | [diff] [blame] | 112 | thread-affine is a subset of thread-unsafe. Thread-affine is also sometimes |
| 113 | referred to as **thread-hostile**. |
Albert J. Wong | f06ff500 | 2021-07-08 20:37:00 | [diff] [blame] | 114 | * **Thread-safe**: Such types/methods can be safely accessed in parallel. |
| 115 | * **Thread-compatible**: Such types provide safe parallel access to const |
Gabriel Charette | b984d67 | 2019-02-12 21:53:27 | [diff] [blame] | 116 | methods but require synchronization for non-const (or mixed const/non-const |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 117 | access). Chrome doesn't expose reader-writer locks; as such, the only use |
Gabriel Charette | b984d67 | 2019-02-12 21:53:27 | [diff] [blame] | 118 | case for this is objects (typically globals) which are initialized once in a |
Gabriel Charette | 364a16a | 2019-02-06 21:12:15 | [diff] [blame] | 119 | thread-safe manner (either in the single-threaded phase of startup or lazily |
| 120 | through a thread-safe static-local-initialization paradigm a la |
Gabriel Charette | b984d67 | 2019-02-12 21:53:27 | [diff] [blame] | 121 | `base::NoDestructor`) and forever after immutable. |
| 122 | * **Immutable**: A subset of thread-compatible types which cannot be modified |
| 123 | after construction. |
Gabriel Charette | 364a16a | 2019-02-06 21:12:15 | [diff] [blame] | 124 | * **Sequence-friendly**: Such types/methods are thread-unsafe types which |
| 125 | support being invoked from a `base::SequencedTaskRunner`. Ideally this would |
| 126 | be the case for all thread-unsafe types but legacy code sometimes has |
| 127 | overzealous checks that enforce thread-affinity in mere thread-unsafe |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 128 | scenarios. See [Prefer Sequences to |
| 129 | Threads](#prefer-sequences-to-physical-threads) below for more details. |
Gabriel Charette | 364a16a | 2019-02-06 21:12:15 | [diff] [blame] | 130 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 131 | ### Threads |
| 132 | |
| 133 | Every Chrome process has |
| 134 | |
| 135 | * a main thread |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 136 | * in the browser process (BrowserThread::UI): updates the UI |
| 137 | * in renderer processes (Blink main thread): runs most of Blink |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 138 | * an IO thread |
Matt Falkenhagen | 72a2dfc | 2021-08-05 22:36:13 | [diff] [blame] | 139 | * in all processes: all IPC messages arrive on this thread. The application |
| 140 | logic to handle the message may be in a different thread (i.e., the IO |
| 141 | thread may route the message to a [Mojo |
| 142 | interface](/docs/README.md#Mojo-Services) which is bound to a |
| 143 | different thread). |
| 144 | * more generally most async I/O happens on this thread (e.g., through |
| 145 | base::FileDescriptorWatcher). |
| 146 | * in the browser process: this is called BrowserThread::IO. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 147 | * a few more special-purpose threads |
| 148 | * and a pool of general-purpose threads |
| 149 | |
| 150 | Most threads have a loop that gets tasks from a queue and runs them (the queue |
| 151 | may be shared between multiple threads). |
| 152 | |
| 153 | ### Tasks |
| 154 | |
| 155 | A task is a `base::OnceClosure` added to a queue for asynchronous execution. |
| 156 | |
| 157 | A `base::OnceClosure` stores a function pointer and arguments. It has a `Run()` |
| 158 | method that invokes the function pointer using the bound arguments. It is |
| 159 | created using `base::BindOnce`. (ref. [Callback<> and Bind() |
| 160 | documentation](callback.md)). |
| 161 | |
| 162 | ``` |
| 163 | void TaskA() {} |
| 164 | void TaskB(int v) {} |
| 165 | |
| 166 | auto task_a = base::BindOnce(&TaskA); |
| 167 | auto task_b = base::BindOnce(&TaskB, 42); |
| 168 | ``` |
| 169 | |
| 170 | A group of tasks can be executed in one of the following ways: |
| 171 | |
| 172 | * [Parallel](#Posting-a-Parallel-Task): No task execution ordering, possibly all |
| 173 | at once on any thread |
| 174 | * [Sequenced](#Posting-a-Sequenced-Task): Tasks executed in posting order, one |
| 175 | at a time on any thread. |
| 176 | * [Single Threaded](#Posting-Multiple-Tasks-to-the-Same-Thread): Tasks executed |
| 177 | in posting order, one at a time on a single thread. |
Drew Stonebraker | 653a3ba | 2019-07-02 19:24:23 | [diff] [blame] | 178 | * [COM Single Threaded](#Posting-Tasks-to-a-COM-Single_Thread-Apartment-STA_Thread-Windows): |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 179 | A variant of single threaded with COM initialized. |
| 180 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 181 | ### Prefer Sequences to Physical Threads |
gab | 2a457605 | 2017-06-07 23:36:12 | [diff] [blame] | 182 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 183 | Sequenced execution (on virtual threads) is strongly preferred to |
| 184 | single-threaded execution (on physical threads). Except for types/methods bound |
| 185 | to the main thread (UI) or IO threads: thread-safety is better achieved via |
| 186 | `base::SequencedTaskRunner` than through managing your own physical threads |
| 187 | (ref. [Posting a Sequenced Task](#posting-a-sequenced-task) below). |
gab | 2a457605 | 2017-06-07 23:36:12 | [diff] [blame] | 188 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 189 | All APIs which are exposed for "current physical thread" have an equivalent for |
| 190 | "current sequence" |
| 191 | ([mapping](threading_and_tasks_faq.md#How-to-migrate-from-SingleThreadTaskRunner-to-SequencedTaskRunner)). |
gab | 2a457605 | 2017-06-07 23:36:12 | [diff] [blame] | 192 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 193 | If you find yourself writing a sequence-friendly type and it fails |
| 194 | thread-affinity checks (e.g., `THREAD_CHECKER`) in a leaf dependency: consider |
| 195 | making that dependency sequence-friendly as well. Most core APIs in Chrome are |
| 196 | sequence-friendly, but some legacy types may still over-zealously use |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 197 | ThreadChecker/SingleThreadTaskRunner when they could instead rely on the |
| 198 | "current sequence" and no longer be thread-affine. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 199 | |
| 200 | ## Posting a Parallel Task |
| 201 | |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 202 | ### Direct Posting to the Thread Pool |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 203 | |
| 204 | A task that can run on any thread and doesn’t have ordering or mutual exclusion |
| 205 | requirements with other tasks should be posted using one of the |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 206 | `base::ThreadPool::PostTask*()` functions defined in |
| 207 | [`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h). |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 208 | |
| 209 | ```cpp |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 210 | base::ThreadPool::PostTask(FROM_HERE, base::BindOnce(&Task)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 211 | ``` |
| 212 | |
| 213 | This posts tasks with default traits. |
| 214 | |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 215 | The `base::ThreadPool::PostTask*()` functions allow the caller to provide |
| 216 | additional details about the task via TaskTraits (ref. [Annotating Tasks with |
| 217 | TaskTraits](#Annotating-Tasks-with-TaskTraits)). |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 218 | |
| 219 | ```cpp |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 220 | base::ThreadPool::PostTask( |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 221 | FROM_HERE, {base::TaskPriority::BEST_EFFORT, MayBlock()}, |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 222 | base::BindOnce(&Task)); |
| 223 | ``` |
| 224 | |
fdoray | 52bf555 | 2017-05-11 12:43:59 | [diff] [blame] | 225 | ### Posting via a TaskRunner |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 226 | |
| 227 | A parallel |
Patrick Monette | 2d93ad90 | 2021-11-01 19:20:22 | [diff] [blame] | 228 | [`base::TaskRunner`](https://cs.chromium.org/chromium/src/base/task/task_runner.h) is |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 229 | an alternative to calling `base::ThreadPool::PostTask*()` directly. This is |
| 230 | mainly useful when it isn’t known in advance whether tasks will be posted in |
| 231 | parallel, in sequence, or to a single-thread (ref. [Posting a Sequenced |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 232 | Task](#Posting-a-Sequenced-Task), [Posting Multiple Tasks to the Same |
| 233 | Thread](#Posting-Multiple-Tasks-to-the-Same-Thread)). Since `base::TaskRunner` |
| 234 | is the base class of `base::SequencedTaskRunner` and |
| 235 | `base::SingleThreadTaskRunner`, a `scoped_refptr<TaskRunner>` member can hold a |
| 236 | `base::TaskRunner`, a `base::SequencedTaskRunner` or a |
| 237 | `base::SingleThreadTaskRunner`. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 238 | |
| 239 | ```cpp |
| 240 | class A { |
| 241 | public: |
| 242 | A() = default; |
| 243 | |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 244 | void PostSomething() { |
| 245 | task_runner_->PostTask(FROM_HERE, base::BindOnce(&A, &DoSomething)); |
| 246 | } |
| 247 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 248 | void DoSomething() { |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | private: |
| 252 | scoped_refptr<base::TaskRunner> task_runner_ = |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 253 | base::ThreadPool::CreateTaskRunner({base::TaskPriority::USER_VISIBLE}); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 254 | }; |
| 255 | ``` |
| 256 | |
| 257 | Unless a test needs to control precisely how tasks are executed, it is preferred |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 258 | to call `base::ThreadPool::PostTask*()` directly (ref. [Testing](#Testing) for |
| 259 | less invasive ways of controlling tasks in tests). |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 260 | |
| 261 | ## Posting a Sequenced Task |
| 262 | |
| 263 | A sequence is a set of tasks that run one at a time in posting order (not |
| 264 | necessarily on the same thread). To post tasks as part of a sequence, use a |
Patrick Monette | 2d93ad90 | 2021-11-01 19:20:22 | [diff] [blame] | 265 | [`base::SequencedTaskRunner`](https://cs.chromium.org/chromium/src/base/task/sequenced_task_runner.h). |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 266 | |
| 267 | ### Posting to a New Sequence |
| 268 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 269 | A `base::SequencedTaskRunner` can be created by |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 270 | `base::ThreadPool::CreateSequencedTaskRunner()`. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 271 | |
| 272 | ```cpp |
| 273 | scoped_refptr<SequencedTaskRunner> sequenced_task_runner = |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 274 | base::ThreadPool::CreateSequencedTaskRunner(...); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 275 | |
| 276 | // TaskB runs after TaskA completes. |
| 277 | sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA)); |
| 278 | sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB)); |
| 279 | ``` |
| 280 | |
Alex Clarke | 0dd49956 | 2019-10-18 19:45:09 | [diff] [blame] | 281 | ### Posting to the Current (Virtual) Thread |
| 282 | |
Gabriel Charette | fee5566 | 2019-11-20 21:06:28 | [diff] [blame] | 283 | The preferred way of posting to the current (virtual) thread is via |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 284 | `base::SequencedTaskRunner::GetCurrentDefault()`. |
Alex Clarke | 0dd49956 | 2019-10-18 19:45:09 | [diff] [blame] | 285 | |
| 286 | ```cpp |
| 287 | // The task will run on the current (virtual) thread's default task queue. |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 288 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
Gabriel Charette | fee5566 | 2019-11-20 21:06:28 | [diff] [blame] | 289 | FROM_HERE, base::BindOnce(&Task); |
Alex Clarke | 0dd49956 | 2019-10-18 19:45:09 | [diff] [blame] | 290 | ``` |
| 291 | |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 292 | Note that `SequencedTaskRunner::GetCurrentDefault()` returns the default queue for the |
Gabriel Charette | fee5566 | 2019-11-20 21:06:28 | [diff] [blame] | 293 | current virtual thread. On threads with multiple task queues (e.g. |
| 294 | BrowserThread::UI) this can be a different queue than the one the current task |
| 295 | belongs to. The "current" task runner is intentionally not exposed via a static |
| 296 | getter. Either you know it already and can post to it directly or you don't and |
Francois Doray | a06ee17 | 2022-11-24 21:09:18 | [diff] [blame] | 297 | the only sensible destination is the default queue. See https://bit.ly/3JvCLsX |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 298 | for detailed discussion. |
Alex Clarke | 0dd49956 | 2019-10-18 19:45:09 | [diff] [blame] | 299 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 300 | ## Using Sequences Instead of Locks |
| 301 | |
| 302 | Usage of locks is discouraged in Chrome. Sequences inherently provide |
Gabriel Charette | a3ccc97 | 2018-11-13 14:43:12 | [diff] [blame] | 303 | thread-safety. Prefer classes that are always accessed from the same |
| 304 | sequence to managing your own thread-safety with locks. |
| 305 | |
| 306 | **Thread-safe but not thread-affine; how so?** Tasks posted to the same sequence |
| 307 | will run in sequential order. After a sequenced task completes, the next task |
| 308 | may be picked up by a different worker thread, but that task is guaranteed to |
| 309 | see any side-effects caused by the previous one(s) on its sequence. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 310 | |
| 311 | ```cpp |
| 312 | class A { |
| 313 | public: |
| 314 | A() { |
| 315 | // Do not require accesses to be on the creation sequence. |
isherman | 8c33b8a | 2017-06-27 19:18:30 | [diff] [blame] | 316 | DETACH_FROM_SEQUENCE(sequence_checker_); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void AddValue(int v) { |
| 320 | // Check that all accesses are on the same sequence. |
isherman | 8c33b8a | 2017-06-27 19:18:30 | [diff] [blame] | 321 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 322 | values_.push_back(v); |
| 323 | } |
| 324 | |
| 325 | private: |
isherman | 8c33b8a | 2017-06-27 19:18:30 | [diff] [blame] | 326 | SEQUENCE_CHECKER(sequence_checker_); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 327 | |
| 328 | // No lock required, because all accesses are on the |
| 329 | // same sequence. |
| 330 | std::vector<int> values_; |
| 331 | }; |
| 332 | |
| 333 | A a; |
| 334 | scoped_refptr<SequencedTaskRunner> task_runner_for_a = ...; |
Mike Bjorge | d3a0984 | 2018-05-15 18:37:28 | [diff] [blame] | 335 | task_runner_for_a->PostTask(FROM_HERE, |
| 336 | base::BindOnce(&A::AddValue, base::Unretained(&a), 42)); |
| 337 | task_runner_for_a->PostTask(FROM_HERE, |
| 338 | base::BindOnce(&A::AddValue, base::Unretained(&a), 27)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 339 | |
| 340 | // Access from a different sequence causes a DCHECK failure. |
| 341 | scoped_refptr<SequencedTaskRunner> other_task_runner = ...; |
| 342 | other_task_runner->PostTask(FROM_HERE, |
Mike Bjorge | d3a0984 | 2018-05-15 18:37:28 | [diff] [blame] | 343 | base::BindOnce(&A::AddValue, base::Unretained(&a), 1)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 344 | ``` |
| 345 | |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 346 | Locks should only be used to swap in a shared data structure that can be |
| 347 | accessed on multiple threads. If one thread updates it based on expensive |
| 348 | computation or through disk access, then that slow work should be done without |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 349 | holding the lock. Only when the result is available should the lock be used to |
| 350 | swap in the new data. An example of this is in PluginList::LoadPlugins |
| 351 | ([`content/browser/plugin_list.cc`](https://cs.chromium.org/chromium/src/content/browser/plugin_list.cc). |
| 352 | If you must use locks, |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 353 | [here](https://www.chromium.org/developers/lock-and-condition-variable) are some |
| 354 | best practices and pitfalls to avoid. |
| 355 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 356 | In order to write non-blocking code, many APIs in Chrome are asynchronous. |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 357 | Usually this means that they either need to be executed on a particular |
| 358 | thread/sequence and will return results via a custom delegate interface, or they |
Alex St-Onge | 490a97a | 2021-02-04 02:47:19 | [diff] [blame] | 359 | take a `base::OnceCallback<>` (or `base::RepeatingCallback<>`) object that is |
| 360 | called when the requested operation is completed. Executing work on a specific |
| 361 | thread/sequence is covered in the PostTask sections above. |
Gabriel Charette | 9048031 | 2018-02-16 15:10:05 | [diff] [blame] | 362 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 363 | ## Posting Multiple Tasks to the Same Thread |
| 364 | |
| 365 | If multiple tasks need to run on the same thread, post them to a |
Patrick Monette | 2d93ad90 | 2021-11-01 19:20:22 | [diff] [blame] | 366 | [`base::SingleThreadTaskRunner`](https://cs.chromium.org/chromium/src/base/task/single_thread_task_runner.h). |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 367 | All tasks posted to the same `base::SingleThreadTaskRunner` run on the same thread in |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 368 | posting order. |
| 369 | |
| 370 | ### Posting to the Main Thread or to the IO Thread in the Browser Process |
| 371 | |
Eric Seckler | 6cf08db8 | 2018-08-30 12:01:55 | [diff] [blame] | 372 | To post tasks to the main thread or to the IO thread, use |
Olivier Li | 56b99d4e | 2020-02-11 13:51:41 | [diff] [blame] | 373 | `content::GetUIThreadTaskRunner({})` or `content::GetIOThreadTaskRunner({})` |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 374 | from |
| 375 | [`content/public/browser/browser_thread.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_thread.h) |
| 376 | |
| 377 | You may provide additional BrowserTaskTraits as a parameter to those methods |
| 378 | though this is generally still uncommon in BrowserThreads and should be reserved |
| 379 | for advanced use cases. |
| 380 | |
| 381 | There's an ongoing migration ([task APIs v3]) away from the previous |
| 382 | base-API-with-traits which you may still find throughout the codebase (it's |
| 383 | equivalent): |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 384 | |
| 385 | ```cpp |
Sami Kyostila | 831c60b | 2019-07-31 13:31:23 | [diff] [blame] | 386 | base::PostTask(FROM_HERE, {content::BrowserThread::UI}, ...); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 387 | |
Sami Kyostila | 831c60b | 2019-07-31 13:31:23 | [diff] [blame] | 388 | base::CreateSingleThreadTaskRunner({content::BrowserThread::IO}) |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 389 | ->PostTask(FROM_HERE, ...); |
| 390 | ``` |
| 391 | |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 392 | Note: For the duration of the migration, you'll unfortunately need to continue |
| 393 | manually including |
| 394 | [`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h). |
| 395 | to use the browser_thread.h API. |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 396 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 397 | The main thread and the IO thread are already super busy. Therefore, prefer |
fdoray | 52bf555 | 2017-05-11 12:43:59 | [diff] [blame] | 398 | posting to a general purpose thread when possible (ref. |
| 399 | [Posting a Parallel Task](#Posting-a-Parallel-Task), |
| 400 | [Posting a Sequenced task](#Posting-a-Sequenced-Task)). |
| 401 | Good reasons to post to the main thread are to update the UI or access objects |
| 402 | that are bound to it (e.g. `Profile`). A good reason to post to the IO thread is |
| 403 | to access the internals of components that are bound to it (e.g. IPCs, network). |
| 404 | Note: It is not necessary to have an explicit post task to the IO thread to |
| 405 | send/receive an IPC or send/receive data on the network. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 406 | |
| 407 | ### Posting to the Main Thread in a Renderer Process |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 408 | TODO(blink-dev) |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 409 | |
| 410 | ### Posting to a Custom SingleThreadTaskRunner |
| 411 | |
| 412 | If multiple tasks need to run on the same thread and that thread doesn’t have to |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 413 | be the main thread or the IO thread, post them to a |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 414 | `base::SingleThreadTaskRunner` created by |
| 415 | `base::Threadpool::CreateSingleThreadTaskRunner`. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 416 | |
| 417 | ```cpp |
Dominic Farolino | dbe9769b | 2019-05-31 04:06:03 | [diff] [blame] | 418 | scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner = |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 419 | base::Threadpool::CreateSingleThreadTaskRunner(...); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 420 | |
| 421 | // TaskB runs after TaskA completes. Both tasks run on the same thread. |
| 422 | single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA)); |
| 423 | single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB)); |
| 424 | ``` |
| 425 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 426 | Remember that we [prefer sequences to physical |
| 427 | threads](#prefer-sequences-to-physical-threads) and that this thus should rarely |
| 428 | be necessary. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 429 | |
Alexander Timin | e653dfc | 2020-01-07 17:55:06 | [diff] [blame] | 430 | ### Posting to the Current Thread |
| 431 | |
| 432 | *** note |
| 433 | **IMPORTANT:** To post a task that needs mutual exclusion with the current |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 434 | sequence of tasks but doesn’t absolutely need to run on the current physical |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 435 | thread, use `base::SequencedTaskRunner::GetCurrentDefault()` instead of |
| 436 | `base::SingleThreadTaskRunner::GetCurrentDefault()` (ref. [Posting to the Current |
Gabriel Charette | 49e3cd0 | 2020-01-28 03:45:27 | [diff] [blame] | 437 | Sequence](#Posting-to-the-Current-Virtual_Thread)). That will better document |
| 438 | the requirements of the posted task and will avoid unnecessarily making your API |
| 439 | physical thread-affine. In a single-thread task, |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 440 | `base::SequencedTaskRunner::GetCurrentDefault()` is equivalent to |
| 441 | `base::SingleThreadTaskRunner::GetCurrentDefault()`. |
Alexander Timin | e653dfc | 2020-01-07 17:55:06 | [diff] [blame] | 442 | *** |
| 443 | |
| 444 | If you must post a task to the current physical thread nonetheless, use |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 445 | [`base::SingleThreadTaskRunner::CurrentDefaultHandle`](https://source.chromium.org/chromium/chromium/src/+/main:base/task/single_thread_task_runner.h). |
Alexander Timin | e653dfc | 2020-01-07 17:55:06 | [diff] [blame] | 446 | |
| 447 | ```cpp |
| 448 | // The task will run on the current thread in the future. |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 449 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Alexander Timin | e653dfc | 2020-01-07 17:55:06 | [diff] [blame] | 450 | FROM_HERE, base::BindOnce(&Task)); |
| 451 | ``` |
| 452 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 453 | ## Posting Tasks to a COM Single-Thread Apartment (STA) Thread (Windows) |
| 454 | |
| 455 | Tasks that need to run on a COM Single-Thread Apartment (STA) thread must be |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 456 | posted to a `base::SingleThreadTaskRunner` returned by |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 457 | `base::ThreadPool::CreateCOMSTATaskRunner()`. As mentioned in [Posting Multiple |
| 458 | Tasks to the Same Thread](#Posting-Multiple-Tasks-to-the-Same-Thread), all tasks |
| 459 | posted to the same `base::SingleThreadTaskRunner` run on the same thread in |
| 460 | posting order. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 461 | |
| 462 | ```cpp |
| 463 | // Task(A|B|C)UsingCOMSTA will run on the same COM STA thread. |
| 464 | |
| 465 | void TaskAUsingCOMSTA() { |
| 466 | // [ This runs on a COM STA thread. ] |
| 467 | |
| 468 | // Make COM STA calls. |
| 469 | // ... |
| 470 | |
| 471 | // Post another task to the current COM STA thread. |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 472 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 473 | FROM_HERE, base::BindOnce(&TaskCUsingCOMSTA)); |
| 474 | } |
| 475 | void TaskBUsingCOMSTA() { } |
| 476 | void TaskCUsingCOMSTA() { } |
| 477 | |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 478 | auto com_sta_task_runner = base::ThreadPool::CreateCOMSTATaskRunner(...); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 479 | com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskAUsingCOMSTA)); |
| 480 | com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskBUsingCOMSTA)); |
| 481 | ``` |
| 482 | |
Egor Pasko | 7f58c33b | 2023-02-17 13:19:56 | [diff] [blame] | 483 | ## Memory ordering guarantees for posted Tasks |
| 484 | |
| 485 | This task system guarantees that all the memory effects of sequential execution |
Egor Pasko | 049e8d35 | 2023-02-23 17:09:50 | [diff] [blame] | 486 | before posting a task are _visible_ to the task when it starts running. More |
| 487 | formally, a call to `PostTask()` and the execution of the posted task are in the |
| 488 | [happens-before |
Egor Pasko | 7f58c33b | 2023-02-17 13:19:56 | [diff] [blame] | 489 | relationship](https://preshing.com/20130702/the-happens-before-relation/) with |
Egor Pasko | 049e8d35 | 2023-02-23 17:09:50 | [diff] [blame] | 490 | each other. This is true for all variants of posting a task in `::base`, |
| 491 | including `PostTaskAndReply()`. Similarly the happens-before relationship is |
| 492 | present for tasks running in a sequence as part of the same SequencedTaskRunner. |
Egor Pasko | 7f58c33b | 2023-02-17 13:19:56 | [diff] [blame] | 493 | |
Egor Pasko | 049e8d35 | 2023-02-23 17:09:50 | [diff] [blame] | 494 | This guarantee is important to know about because Chrome tasks commonly access |
| 495 | memory beyond the immediate data copied into the `base::OnceCallback`, and this |
| 496 | happens-before relationship allows to avoid additional synchronization within |
| 497 | the tasks themselves. As a very specific example, consider a callback that binds |
| 498 | a pointer to memory which was just initialized in the thread posting the task. |
Egor Pasko | 7f58c33b | 2023-02-17 13:19:56 | [diff] [blame] | 499 | |
Egor Pasko | 049e8d35 | 2023-02-23 17:09:50 | [diff] [blame] | 500 | A more constrained model is also worth noting. Execution can be split into tasks |
| 501 | running on different task runners, where each task _exclusively_ accesses |
| 502 | certain objects in memory without explicit synchronization. Posting another task |
| 503 | transfers this 'ownership' (of the objects) to the next task. With this the |
| 504 | notion of object ownership can often be extended to the level of task runners, |
| 505 | which provides useful invariants to reason about. This model allows to avoid |
| 506 | race conditions while also avoiding locks and atomic operations. Because of its |
| 507 | simplicity this model is commonly used in Chrome. |
Egor Pasko | 7f58c33b | 2023-02-17 13:19:56 | [diff] [blame] | 508 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 509 | ## Annotating Tasks with TaskTraits |
| 510 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 511 | [`base::TaskTraits`](https://cs.chromium.org/chromium/src/base/task/task_traits.h) |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 512 | encapsulate information about a task that helps the thread pool make better |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 513 | scheduling decisions. |
| 514 | |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 515 | Methods that take `base::TaskTraits` can be be passed `{}` when default traits |
| 516 | are sufficient. Default traits are appropriate for tasks that: |
Nigel Tao | 26ad185 | 2023-03-23 01:14:56 | [diff] [blame] | 517 | |
Gabriel Charette | de41cad | 2020-03-03 18:05:06 | [diff] [blame] | 518 | - Don’t block (ref. MayBlock and WithBaseSyncPrimitives); |
| 519 | - Pertain to user-blocking activity; |
| 520 | (explicitly or implicitly by having an ordering dependency with a component |
| 521 | that does) |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 522 | - Can either block shutdown or be skipped on shutdown (thread pool is free to |
| 523 | choose a fitting default). |
Nigel Tao | 26ad185 | 2023-03-23 01:14:56 | [diff] [blame] | 524 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 525 | Tasks that don’t match this description must be posted with explicit TaskTraits. |
| 526 | |
Gabriel Charette | 04b138f | 2018-08-06 00:03:22 | [diff] [blame] | 527 | [`base/task/task_traits.h`](https://cs.chromium.org/chromium/src/base/task/task_traits.h) |
Eric Seckler | 6cf08db8 | 2018-08-30 12:01:55 | [diff] [blame] | 528 | provides exhaustive documentation of available traits. The content layer also |
| 529 | provides additional traits in |
| 530 | [`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h) |
| 531 | to facilitate posting a task onto a BrowserThread. |
| 532 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 533 | Below are some examples of how to specify `base::TaskTraits`. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 534 | |
| 535 | ```cpp |
Gabriel Charette | de41cad | 2020-03-03 18:05:06 | [diff] [blame] | 536 | // This task has no explicit TaskTraits. It cannot block. Its priority is |
| 537 | // USER_BLOCKING. It will either block shutdown or be skipped on shutdown. |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 538 | base::ThreadPool::PostTask(FROM_HERE, base::BindOnce(...)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 539 | |
Gabriel Charette | de41cad | 2020-03-03 18:05:06 | [diff] [blame] | 540 | // This task has the highest priority. The thread pool will schedule it before |
| 541 | // USER_VISIBLE and BEST_EFFORT tasks. |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 542 | base::ThreadPool::PostTask( |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 543 | FROM_HERE, {base::TaskPriority::USER_BLOCKING}, |
| 544 | base::BindOnce(...)); |
| 545 | |
| 546 | // This task has the lowest priority and is allowed to block (e.g. it |
| 547 | // can read a file from disk). |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 548 | base::ThreadPool::PostTask( |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 549 | FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()}, |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 550 | base::BindOnce(...)); |
| 551 | |
| 552 | // This task blocks shutdown. The process won't exit before its |
| 553 | // execution is complete. |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 554 | base::ThreadPool::PostTask( |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 555 | FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, |
| 556 | base::BindOnce(...)); |
| 557 | ``` |
| 558 | |
| 559 | ## Keeping the Browser Responsive |
| 560 | |
| 561 | Do not perform expensive work on the main thread, the IO thread or any sequence |
| 562 | that is expected to run tasks with a low latency. Instead, perform expensive |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 563 | work asynchronously using `base::ThreadPool::PostTaskAndReply*()` or |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 564 | `base::SequencedTaskRunner::PostTaskAndReply()`. Note that |
| 565 | asynchronous/overlapped I/O on the IO thread are fine. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 566 | |
| 567 | Example: Running the code below on the main thread will prevent the browser from |
| 568 | responding to user input for a long time. |
| 569 | |
| 570 | ```cpp |
| 571 | // GetHistoryItemsFromDisk() may block for a long time. |
| 572 | // AddHistoryItemsToOmniboxDropDown() updates the UI and therefore must |
| 573 | // be called on the main thread. |
| 574 | AddHistoryItemsToOmniboxDropdown(GetHistoryItemsFromDisk("keyword")); |
| 575 | ``` |
| 576 | |
| 577 | The code below solves the problem by scheduling a call to |
| 578 | `GetHistoryItemsFromDisk()` in a thread pool followed by a call to |
| 579 | `AddHistoryItemsToOmniboxDropdown()` on the origin sequence (the main thread in |
| 580 | this case). The return value of the first call is automatically provided as |
| 581 | argument to the second call. |
| 582 | |
| 583 | ```cpp |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 584 | base::ThreadPool::PostTaskAndReplyWithResult( |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 585 | FROM_HERE, {base::MayBlock()}, |
| 586 | base::BindOnce(&GetHistoryItemsFromDisk, "keyword"), |
| 587 | base::BindOnce(&AddHistoryItemsToOmniboxDropdown)); |
| 588 | ``` |
| 589 | |
| 590 | ## Posting a Task with a Delay |
| 591 | |
| 592 | ### Posting a One-Off Task with a Delay |
| 593 | |
| 594 | To post a task that must run once after a delay expires, use |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 595 | `base::ThreadPool::PostDelayedTask*()` or `base::TaskRunner::PostDelayedTask()`. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 596 | |
| 597 | ```cpp |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 598 | base::ThreadPool::PostDelayedTask( |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 599 | FROM_HERE, {base::TaskPriority::BEST_EFFORT}, base::BindOnce(&Task), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 600 | base::Hours(1)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 601 | |
| 602 | scoped_refptr<base::SequencedTaskRunner> task_runner = |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 603 | base::ThreadPool::CreateSequencedTaskRunner( |
| 604 | {base::TaskPriority::BEST_EFFORT}); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 605 | task_runner->PostDelayedTask( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 606 | FROM_HERE, base::BindOnce(&Task), base::Hours(1)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 607 | ``` |
| 608 | |
| 609 | *** note |
| 610 | **NOTE:** A task that has a 1-hour delay probably doesn’t have to run right away |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 611 | when its delay expires. Specify `base::TaskPriority::BEST_EFFORT` to prevent it |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 612 | from slowing down the browser when its delay expires. |
| 613 | *** |
| 614 | |
| 615 | ### Posting a Repeating Task with a Delay |
| 616 | To post a task that must run at regular intervals, |
| 617 | use [`base::RepeatingTimer`](https://cs.chromium.org/chromium/src/base/timer/timer.h). |
| 618 | |
| 619 | ```cpp |
| 620 | class A { |
| 621 | public: |
| 622 | ~A() { |
| 623 | // The timer is stopped automatically when it is deleted. |
| 624 | } |
| 625 | void StartDoingStuff() { |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 626 | timer_.Start(FROM_HERE, Seconds(1), |
Erik Chen | 0ee26a3 | 2021-07-14 20:04:47 | [diff] [blame] | 627 | this, &A::DoStuff); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 628 | } |
| 629 | void StopDoingStuff() { |
| 630 | timer_.Stop(); |
| 631 | } |
| 632 | private: |
| 633 | void DoStuff() { |
| 634 | // This method is called every second on the sequence that invoked |
| 635 | // StartDoingStuff(). |
| 636 | } |
| 637 | base::RepeatingTimer timer_; |
| 638 | }; |
| 639 | ``` |
| 640 | |
| 641 | ## Cancelling a Task |
| 642 | |
| 643 | ### Using base::WeakPtr |
| 644 | |
| 645 | [`base::WeakPtr`](https://cs.chromium.org/chromium/src/base/memory/weak_ptr.h) |
| 646 | can be used to ensure that any callback bound to an object is canceled when that |
| 647 | object is destroyed. |
| 648 | |
| 649 | ```cpp |
| 650 | int Compute() { … } |
| 651 | |
| 652 | class A { |
| 653 | public: |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 654 | void ComputeAndStore() { |
| 655 | // Schedule a call to Compute() in a thread pool followed by |
| 656 | // a call to A::Store() on the current sequence. The call to |
| 657 | // A::Store() is canceled when |weak_ptr_factory_| is destroyed. |
| 658 | // (guarantees that |this| will not be used-after-free). |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 659 | base::ThreadPool::PostTaskAndReplyWithResult( |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 660 | FROM_HERE, base::BindOnce(&Compute), |
| 661 | base::BindOnce(&A::Store, weak_ptr_factory_.GetWeakPtr())); |
| 662 | } |
| 663 | |
| 664 | private: |
| 665 | void Store(int value) { value_ = value; } |
| 666 | |
| 667 | int value_; |
Jeremy Roman | 0dd0b2f | 2019-07-16 21:00:43 | [diff] [blame] | 668 | base::WeakPtrFactory<A> weak_ptr_factory_{this}; |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 669 | }; |
| 670 | ``` |
| 671 | |
Eugene Zemtsov | 48b30d3 | 2022-03-18 23:56:11 | [diff] [blame] | 672 | Note: `WeakPtr` is not thread-safe: `~WeakPtrFactory()` and |
Francois Doray | f652a9d0 | 2021-07-06 13:07:52 | [diff] [blame] | 673 | `Store()` (bound to a `WeakPtr`) must all run on the same sequence. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 674 | |
| 675 | ### Using base::CancelableTaskTracker |
| 676 | |
| 677 | [`base::CancelableTaskTracker`](https://cs.chromium.org/chromium/src/base/task/cancelable_task_tracker.h) |
| 678 | allows cancellation to happen on a different sequence than the one on which |
| 679 | tasks run. Keep in mind that `CancelableTaskTracker` cannot cancel tasks that |
| 680 | have already started to run. |
| 681 | |
| 682 | ```cpp |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 683 | auto task_runner = base::ThreadPool::CreateTaskRunner({}); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 684 | base::CancelableTaskTracker cancelable_task_tracker; |
| 685 | cancelable_task_tracker.PostTask(task_runner.get(), FROM_HERE, |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 686 | base::DoNothing()); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 687 | // Cancels Task(), only if it hasn't already started running. |
| 688 | cancelable_task_tracker.TryCancelAll(); |
| 689 | ``` |
| 690 | |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 691 | ## Posting a Job to run in parallel |
| 692 | |
| 693 | The [`base::PostJob`](https://cs.chromium.org/chromium/src/base/task/post_job.h) |
| 694 | is a power user API to be able to schedule a single base::RepeatingCallback |
Albert J. Wong | f06ff500 | 2021-07-08 20:37:00 | [diff] [blame] | 695 | worker task and request that ThreadPool workers invoke it in parallel. |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 696 | This avoids degenerate cases: |
| 697 | * Calling `PostTask()` for each work item, causing significant overhead. |
| 698 | * Fixed number of `PostTask()` calls that split the work and might run for a |
| 699 | long time. This is problematic when many components post “num cores” tasks and |
| 700 | all expect to use all the cores. In these cases, the scheduler lacks context |
| 701 | to be fair to multiple same-priority requests and/or ability to request lower |
| 702 | priority work to yield when high priority work comes in. |
| 703 | |
Etienne Pierre-doray | 6d3cd919 | 2020-04-06 21:10:37 | [diff] [blame] | 704 | See [`base/task/job_perftest.cc`](https://cs.chromium.org/chromium/src/base/task/job_perftest.cc) |
| 705 | for a complete example. |
| 706 | |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 707 | ```cpp |
| 708 | // A canonical implementation of |worker_task|. |
| 709 | void WorkerTask(base::JobDelegate* job_delegate) { |
| 710 | while (!job_delegate->ShouldYield()) { |
| 711 | auto work_item = TakeWorkItem(); // Smallest unit of work. |
| 712 | if (!work_item) |
| 713 | return: |
| 714 | ProcessWork(work_item); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // Returns the latest thread-safe number of incomplete work items. |
Etienne Pierre-Doray | f91d7a0 | 2020-09-11 15:53:27 | [diff] [blame] | 719 | void NumIncompleteWorkItems(size_t worker_count) { |
| 720 | // NumIncompleteWorkItems() may use |worker_count| if it needs to account for |
| 721 | // local work lists, which is easier than doing its own accounting, keeping in |
| 722 | // mind that the actual number of items may be racily overestimated and thus |
| 723 | // WorkerTask() may be called when there's no available work. |
| 724 | return GlobalQueueSize() + worker_count; |
| 725 | } |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 726 | |
Gabriel Charette | 1138d60 | 2020-01-29 08:51:52 | [diff] [blame] | 727 | base::PostJob(FROM_HERE, {}, |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 728 | base::BindRepeating(&WorkerTask), |
| 729 | base::BindRepeating(&NumIncompleteWorkItems)); |
| 730 | ``` |
| 731 | |
| 732 | By doing as much work as possible in a loop when invoked, the worker task avoids |
| 733 | scheduling overhead. Meanwhile `base::JobDelegate::ShouldYield()` is |
| 734 | periodically invoked to conditionally exit and let the scheduler prioritize |
| 735 | other work. This yield-semantic allows, for example, a user-visible job to use |
| 736 | all cores but get out of the way when a user-blocking task comes in. |
| 737 | |
Jared Saul | ea867ab | 2021-07-15 17:39:01 | [diff] [blame] | 738 | ### Adding additional work to a running job |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 739 | |
| 740 | When new work items are added and the API user wants additional threads to |
Albert J. Wong | f06ff500 | 2021-07-08 20:37:00 | [diff] [blame] | 741 | invoke the worker task in parallel, |
Etienne Pierre-doray | d388299 | 2020-01-14 20:34:11 | [diff] [blame] | 742 | `JobHandle/JobDelegate::NotifyConcurrencyIncrease()` *must* be invoked shortly |
| 743 | after max concurrency increases. |
| 744 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 745 | ## Testing |
| 746 | |
Gabriel Charette | 0b20ee6 | 2019-09-18 14:06:12 | [diff] [blame] | 747 | For more details see [Testing Components Which Post |
| 748 | Tasks](threading_and_tasks_testing.md). |
| 749 | |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 750 | To test code that uses `base::SingleThreadTaskRunner::CurrentDefaultHandle`, |
| 751 | `base::SequencedTaskRunner::CurrentDefaultHandle` or a function in |
Gabriel Charette | 9b6c0407 | 2022-04-01 23:22:46 | [diff] [blame] | 752 | [`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h), |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 753 | instantiate a |
Gabriel Charette | 0b20ee6 | 2019-09-18 14:06:12 | [diff] [blame] | 754 | [`base::test::TaskEnvironment`](https://cs.chromium.org/chromium/src/base/test/task_environment.h) |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 755 | for the scope of the test. If you need BrowserThreads, use |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 756 | `content::BrowserTaskEnvironment` instead of |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 757 | `base::test::TaskEnvironment`. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 758 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 759 | Tests can run the `base::test::TaskEnvironment`'s message pump using a |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 760 | `base::RunLoop`, which can be made to run until `Quit()` (explicitly or via |
| 761 | `RunLoop::QuitClosure()`), or to `RunUntilIdle()` ready-to-run tasks and |
| 762 | immediately return. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 763 | |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 764 | TaskEnvironment configures RunLoop::Run() to GTEST_FAIL() if it hasn't been |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 765 | explicitly quit after TestTimeouts::action_timeout(). This is preferable to |
| 766 | having the test hang if the code under test fails to trigger the RunLoop to |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 767 | quit. The timeout can be overridden with base::test::ScopedRunLoopTimeout. |
Wez | d9e4cb77 | 2019-01-09 03:07:03 | [diff] [blame] | 768 | |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 769 | ```cpp |
| 770 | class MyTest : public testing::Test { |
| 771 | public: |
| 772 | // ... |
| 773 | protected: |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 774 | base::test::TaskEnvironment task_environment_; |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 775 | }; |
| 776 | |
Xiaohan Wang | 5279ee0 | 2022-10-19 22:21:06 | [diff] [blame] | 777 | TEST_F(MyTest, FirstTest) { |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 778 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, base::BindOnce(&A)); |
| 779 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 780 | base::BindOnce(&B)); |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 781 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 782 | FROM_HERE, base::BindOnce(&C), base::TimeDelta::Max()); |
| 783 | |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 784 | // This runs the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue until it is empty. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 785 | // Delayed tasks are not added to the queue until they are ripe for execution. |
Gabriel Charette | bd126bc3 | 2022-02-01 18:19:19 | [diff] [blame] | 786 | // Prefer explicit exit conditions to RunUntilIdle when possible: |
| 787 | // bit.ly/run-until-idle-with-care2. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 788 | base::RunLoop().RunUntilIdle(); |
| 789 | // A and B have been executed. C is not ripe for execution yet. |
| 790 | |
| 791 | base::RunLoop run_loop; |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 792 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, base::BindOnce(&D)); |
| 793 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 794 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, base::BindOnce(&E)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 795 | |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 796 | // This runs the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue until QuitClosure is |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 797 | // invoked. |
| 798 | run_loop.Run(); |
| 799 | // D and run_loop.QuitClosure() have been executed. E is still in the queue. |
| 800 | |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 801 | // Tasks posted to thread pool run asynchronously as they are posted. |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 802 | base::ThreadPool::PostTask(FROM_HERE, {}, base::BindOnce(&F)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 803 | auto task_runner = |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 804 | base::ThreadPool::CreateSequencedTaskRunner({}); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 805 | task_runner->PostTask(FROM_HERE, base::BindOnce(&G)); |
| 806 | |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 807 | // To block until all tasks posted to thread pool are done running: |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 808 | base::ThreadPoolInstance::Get()->FlushForTesting(); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 809 | // F and G have been executed. |
| 810 | |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 811 | base::ThreadPool::PostTaskAndReplyWithResult( |
| 812 | FROM_HERE, {}, base::BindOnce(&H), base::BindOnce(&I)); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 813 | |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 814 | // This runs the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue until both the |
| 815 | // (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue and the ThreadPool queue are |
Gabriel Charette | bd126bc3 | 2022-02-01 18:19:19 | [diff] [blame] | 816 | // empty. Prefer explicit exit conditions to RunUntilIdle when possible: |
| 817 | // bit.ly/run-until-idle-with-care2. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 818 | task_environment_.RunUntilIdle(); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 819 | // E, H, I have been executed. |
| 820 | } |
| 821 | ``` |
| 822 | |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 823 | ## Using ThreadPool in a New Process |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 824 | |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 825 | ThreadPoolInstance needs to be initialized in a process before the functions in |
Gabriel Charette | 9b6c0407 | 2022-04-01 23:22:46 | [diff] [blame] | 826 | [`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h) |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 827 | can be used. Initialization of ThreadPoolInstance in the Chrome browser process |
| 828 | and child processes (renderer, GPU, utility) has already been taken care of. To |
| 829 | use ThreadPoolInstance in another process, initialize ThreadPoolInstance early |
| 830 | in the main function: |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 831 | |
| 832 | ```cpp |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 833 | // This initializes and starts ThreadPoolInstance with default params. |
| 834 | base::ThreadPoolInstance::CreateAndStartWithDefaultParams(“process_name”); |
Gabriel Charette | 9b6c0407 | 2022-04-01 23:22:46 | [diff] [blame] | 835 | // The base/task/thread_pool.h API can now be used with base::ThreadPool trait. |
Jared Saul | ea867ab | 2021-07-15 17:39:01 | [diff] [blame] | 836 | // Tasks will be scheduled as they are posted. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 837 | |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 838 | // This initializes ThreadPoolInstance. |
| 839 | base::ThreadPoolInstance::Create(“process_name”); |
Gabriel Charette | 9b6c0407 | 2022-04-01 23:22:46 | [diff] [blame] | 840 | // The base/task/thread_pool.h API can now be used with base::ThreadPool trait. No |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 841 | // threads will be created and no tasks will be scheduled until after Start() is |
| 842 | // called. |
| 843 | base::ThreadPoolInstance::Get()->Start(params); |
Gabriel Charette | 52fa3ae | 2019-04-15 21:44:37 | [diff] [blame] | 844 | // ThreadPool can now create threads and schedule tasks. |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 845 | ``` |
| 846 | |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 847 | And shutdown ThreadPoolInstance late in the main function: |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 848 | |
| 849 | ```cpp |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 850 | base::ThreadPoolInstance::Get()->Shutdown(); |
fdoray | bacba4a2 | 2017-05-10 21:10:00 | [diff] [blame] | 851 | // Tasks posted with TaskShutdownBehavior::BLOCK_SHUTDOWN and |
| 852 | // tasks posted with TaskShutdownBehavior::SKIP_ON_SHUTDOWN that |
| 853 | // have started to run before the Shutdown() call have now completed their |
| 854 | // execution. Tasks posted with |
| 855 | // TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN may still be |
| 856 | // running. |
| 857 | ``` |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 858 | ## TaskRunner ownership (encourage no dependency injection) |
Sebastien Marchand | c95489b | 2017-05-25 16:39:34 | [diff] [blame] | 859 | |
| 860 | TaskRunners shouldn't be passed through several components. Instead, the |
Jared Saul | ea867ab | 2021-07-15 17:39:01 | [diff] [blame] | 861 | component that uses a TaskRunner should be the one that creates it. |
Sebastien Marchand | c95489b | 2017-05-25 16:39:34 | [diff] [blame] | 862 | |
| 863 | See [this example](https://codereview.chromium.org/2885173002/) of a |
| 864 | refactoring where a TaskRunner was passed through a lot of components only to be |
| 865 | used in an eventual leaf. The leaf can and should now obtain its TaskRunner |
| 866 | directly from |
Gabriel Charette | 9b6c0407 | 2022-04-01 23:22:46 | [diff] [blame] | 867 | [`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h). |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 868 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 869 | As mentioned above, `base::test::TaskEnvironment` allows unit tests to |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 870 | control tasks posted from underlying TaskRunners. In rare cases where a test |
| 871 | needs to more precisely control task ordering: dependency injection of |
| 872 | TaskRunners can be useful. For such cases the preferred approach is the |
| 873 | following: |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 874 | |
| 875 | ```cpp |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 876 | class Foo { |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 877 | public: |
| 878 | |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 879 | // Overrides |background_task_runner_| in tests. |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 880 | void SetBackgroundTaskRunnerForTesting( |
Gabriel Charette | 39db4c6 | 2019-04-29 19:52:38 | [diff] [blame] | 881 | scoped_refptr<base::SequencedTaskRunner> background_task_runner) { |
| 882 | background_task_runner_ = std::move(background_task_runner); |
| 883 | } |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 884 | |
| 885 | private: |
michaelpg | 12c0457 | 2017-06-26 23:25:06 | [diff] [blame] | 886 | scoped_refptr<base::SequencedTaskRunner> background_task_runner_ = |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 887 | base::ThreadPool::CreateSequencedTaskRunner( |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 888 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT}); |
Gabriel Charette | b86e5fe6 | 2017-06-08 19:39:28 | [diff] [blame] | 889 | } |
| 890 | ``` |
| 891 | |
| 892 | Note that this still allows removing all layers of plumbing between //chrome and |
| 893 | that component since unit tests will use the leaf layer directly. |
Gabriel Charette | 8917f4c | 2018-11-22 15:50:28 | [diff] [blame] | 894 | |
| 895 | ## FAQ |
| 896 | See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more examples. |
Gabriel Charette | 43de5c4 | 2020-01-27 22:44:45 | [diff] [blame] | 897 | |
| 898 | [task APIs v3]: https://docs.google.com/document/d/1tssusPykvx3g0gvbvU4HxGyn3MjJlIylnsH13-Tv6s4/edit?ts=5de99a52#heading=h.ss4tw38hvh3s |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 899 | |
| 900 | ## Internals |
| 901 | |
| 902 | ### SequenceManager |
| 903 | |
| 904 | [SequenceManager](https://cs.chromium.org/chromium/src/base/task/sequence_manager/sequence_manager.h) |
| 905 | manages TaskQueues which have different properties (e.g. priority, common task |
| 906 | type) multiplexing all posted tasks into a single backing sequence. This will |
| 907 | usually be a MessagePump. Depending on the type of message pump used other |
| 908 | events such as UI messages may be processed as well. On Windows APC calls (as |
| 909 | time permits) and signals sent to a registered set of HANDLEs may also be |
| 910 | processed. |
| 911 | |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 912 | ### MessagePump |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 913 | |
| 914 | [MessagePumps](https://cs.chromium.org/chromium/src/base/message_loop/message_pump.h) |
| 915 | are responsible for processing native messages as well as for giving cycles to |
| 916 | their delegate (SequenceManager) periodically. MessagePumps take care to mixing |
| 917 | delegate callbacks with native message processing so neither type of event |
| 918 | starves the other of cycles. |
| 919 | |
| 920 | There are different [MessagePumpTypes](https://cs.chromium.org/chromium/src/base/message_loop/message_pump_type.h), |
| 921 | most common are: |
| 922 | |
| 923 | * DEFAULT: Supports tasks and timers only |
| 924 | |
| 925 | * UI: Supports native UI events (e.g. Windows messages) |
| 926 | |
| 927 | * IO: Supports asynchronous IO (not file I/O!) |
| 928 | |
| 929 | * CUSTOM: User provided implementation of MessagePump interface |
| 930 | |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 931 | ### RunLoop |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 932 | |
Jared Saul | ea867ab | 2021-07-15 17:39:01 | [diff] [blame] | 933 | RunLoop is a helper class to run the RunLoop::Delegate associated with the |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 934 | current thread (usually a SequenceManager). Create a RunLoop on the stack and |
| 935 | call Run/Quit to run a nested RunLoop but please avoid nested loops in |
| 936 | production code! |
| 937 | |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 938 | ### Task Reentrancy |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 939 | |
| 940 | SequenceManager has task reentrancy protection. This means that if a |
| 941 | task is being processed, a second task cannot start until the first task is |
| 942 | finished. Reentrancy can happen when processing a task, and an inner |
| 943 | message pump is created. That inner pump then processes native messages |
| 944 | which could implicitly start an inner task. Inner message pumps are created |
| 945 | with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
| 946 | (DoDragDrop), printer functions (StartDoc) and *many* others. |
| 947 | |
| 948 | ```cpp |
| 949 | Sample workaround when inner task processing is needed: |
| 950 | HRESULT hr; |
| 951 | { |
Francois Doray | a06ee17 | 2022-11-24 21:09:18 | [diff] [blame] | 952 | CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow; |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 953 | hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
| 954 | } |
| 955 | // Process |hr| (the result returned by DoDragDrop()). |
| 956 | ``` |
| 957 | |
| 958 | Please be SURE your task is reentrant (nestable) and all global variables |
| 959 | are stable and accessible before before using |
Francois Doray | a06ee17 | 2022-11-24 21:09:18 | [diff] [blame] | 960 | CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop. |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 961 | |
| 962 | ## APIs for general use |
| 963 | |
| 964 | User code should hardly ever need to access SequenceManager APIs directly as |
| 965 | these are meant for code that deals with scheduling. Instead you should use the |
| 966 | following: |
| 967 | |
| 968 | * base::RunLoop: Drive the SequenceManager from the thread it's bound to. |
| 969 | |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 970 | * base::Thread/SequencedTaskRunner::CurrentDefaultHandle: Post back to the SequenceManager TaskQueues from a task running on it. |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 971 | |
| 972 | * SequenceLocalStorageSlot : Bind external state to a sequence. |
| 973 | |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 974 | * base::CurrentThread : Proxy to a subset of Task related APIs bound to the current thread |
Carlos Caballero | 40b6d04 | 2020-06-16 06:50:25 | [diff] [blame] | 975 | |
| 976 | * Embedders may provide their own static accessors to post tasks on specific loops (e.g. content::BrowserThreads). |
| 977 | |
| 978 | ### SingleThreadTaskExecutor and TaskEnvironment |
| 979 | |
| 980 | Instead of having to deal with SequenceManager and TaskQueues code that needs a |
| 981 | simple task posting environment (one default task queue) can use a |
| 982 | [SingleThreadTaskExecutor](https://cs.chromium.org/chromium/src/base/task/single_thread_task_executor.h). |
| 983 | |
| 984 | Unit tests can use [TaskEnvironment](https://cs.chromium.org/chromium/src/base/test/task_environment.h) |
| 985 | which is highly configurable. |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 986 | |
Wen Fan | e09439ca | 2021-03-09 16:50:41 | [diff] [blame] | 987 | ## MessageLoop and MessageLoopCurrent |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 988 | |
Wen Fan | e09439ca | 2021-03-09 16:50:41 | [diff] [blame] | 989 | You might come across references to MessageLoop or MessageLoopCurrent in the |
Carlos Caballero | 4a05092 | 2020-07-02 11:43:38 | [diff] [blame] | 990 | code or documentation. These classes no longer exist and we are in the process |
Jared Saul | ea867ab | 2021-07-15 17:39:01 | [diff] [blame] | 991 | or getting rid of all references to them. `base::MessageLoopCurrent` was |
| 992 | replaced by `base::CurrentThread` and the drop in replacements for |
| 993 | `base::MessageLoop` are `base::SingleThreadTaskExecutor` and |
| 994 | `base::Test::TaskEnvironment`. |