blob: be606b35e854b620cbf860bcf42853159fe1073f [file] [log] [blame] [view]
fdoraybacba4a22017-05-10 21:10:001# Threading and Tasks in Chrome
2
3[TOC]
4
Gabriel Charette8917f4c2018-11-22 15:50:285Note: See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more
6examples.
7
fdoraybacba4a22017-05-10 21:10:008## Overview
9
Gabriel Charette39db4c62019-04-29 19:52:3810Chrome has a [multi-process
11architecture](https://www.chromium.org/developers/design-documents/multi-process-architecture)
12and each process is heavily multi-threaded. In this document we will go over the
13basic threading system shared by each process. The main goal is to keep the main
Matt Falkenhagen72a2dfc2021-08-05 22:36:1314thread (a.k.a. "UI" thread in the browser process) and IO thread (each process's
15thread for receiving
16[IPC](https://en.wikipedia.org/wiki/Inter-process_communication))
17responsive. This means offloading any blocking I/O or other expensive
18operations to other threads. Our approach is to use message passing as the way
19of communicating between threads. We discourage locking and thread-safe objects.
20Instead, objects live on only one (often virtual -- we'll get to that later!)
21thread and we pass messages between those threads for communication. Absent
22external requirements about latency or workload, Chrome attempts to be a [highly
23concurrent, but not necessarily
24parallel](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 Saulea867ab2021-07-15 17:39:0125system.
Gabriel Charette39db4c62019-04-29 19:52:3826
27This documentation assumes familiarity with computer science
28[threading concepts](https://en.wikipedia.org/wiki/Thread_(computing)).
Gabriel Charette90480312018-02-16 15:10:0529
Gabriel Charette364a16a2019-02-06 21:12:1530### Nomenclature
Gabriel Charette39db4c62019-04-29 19:52:3831
32## Core Concepts
33 * **Task**: A unit of work to be processed. Effectively a function pointer with
Alex St-Onge490a97a2021-02-04 02:47:1934 optionally associated state. In Chrome this is `base::OnceCallback` and
35 `base::RepeatingCallback` created via `base::BindOnce` and
36 `base::BindRepeating`, respectively.
Gabriel Charette39db4c62019-04-29 19:52:3837 ([documentation](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md)).
38 * **Task queue**: A queue of tasks to be processed.
39 * **Physical thread**: An operating system provided thread (e.g. pthread on
40 POSIX or CreateThread() on Windows). The Chrome cross-platform abstraction
41 is `base::PlatformThread`. You should pretty much never use this directly.
42 * **`base::Thread`**: A physical thread forever processing messages from a
43 dedicated task queue until Quit(). You should pretty much never be creating
44 your own `base::Thread`'s.
45 * **Thread pool**: A pool of physical threads with a shared task queue. In
Gabriel Charette0b20ee6c2019-09-18 14:06:1246 Chrome, this is `base::ThreadPoolInstance`. There's exactly one instance per
47 Chrome process, it serves tasks posted through
Gabriel Charette39db4c62019-04-29 19:52:3848 [`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h)
Gabriel Charette43fd3702019-05-29 16:36:5149 and as such you should rarely need to use the `base::ThreadPoolInstance` API
Gabriel Charette39db4c62019-04-29 19:52:3850 directly (more on posting tasks later).
51 * **Sequence** or **Virtual thread**: A chrome-managed thread of execution.
52 Like a physical thread, only one task can run on a given sequence / virtual
53 thread at any given moment and each task sees the side-effects of the
54 preceding tasks. Tasks are executed sequentially but may hop physical
55 threads between each one.
56 * **Task runner**: An interface through which tasks can be posted. In Chrome
57 this is `base::TaskRunner`.
58 * **Sequenced task runner**: A task runner which guarantees that tasks posted
59 to it will run sequentially, in posted order. Each such task is guaranteed to
60 see the side-effects of the task preceding it. Tasks posted to a sequenced
61 task runner are typically processed by a single thread (virtual or physical).
62 In Chrome this is `base::SequencedTaskRunner` which is-a
63 `base::TaskRunner`.
64 * **Single-thread task runner**: A sequenced task runner which guarantees that
65 all tasks will be processed by the same physical thread. In Chrome this is
66 `base::SingleThreadTaskRunner` which is-a `base::SequencedTaskRunner`. We
67 [prefer sequences to threads](#prefer-sequences-to-physical-threads) whenever
68 possible.
69
70## Threading Lexicon
71Note to the reader: the following terms are an attempt to bridge the gap between
72common threading nomenclature and the way we use them in Chrome. It might be a
73bit heavy if you're just getting started. Should this be hard to parse, consider
74skipping to the more detailed sections below and referring back to this as
75necessary.
76
77 * **Thread-unsafe**: The vast majority of types in Chrome are thread-unsafe
78 (by design). Access to such types/methods must be externally synchronized.
79 Typically thread-unsafe types require that all tasks accessing their state be
80 posted to the same `base::SequencedTaskRunner` and they verify this in debug
81 builds with a `SEQUENCE_CHECKER` member. Locks are also an option to
82 synchronize access but in Chrome we strongly
83 [prefer sequences to locks](#Using-Sequences-Instead-of-Locks).
Gabriel Charette364a16a2019-02-06 21:12:1584 * **Thread-affine**: Such types/methods need to be always accessed from the
Gabriel Charetteb984d672019-02-12 21:53:2785 same physical thread (i.e. from the same `base::SingleThreadTaskRunner`) and
Gabriel Charette39db4c62019-04-29 19:52:3886 typically have a `THREAD_CHECKER` member to verify that they are. Short of
87 using a third-party API or having a leaf dependency which is thread-affine:
88 there's pretty much no reason for a type to be thread-affine in Chrome.
89 Note that `base::SingleThreadTaskRunner` is-a `base::SequencedTaskRunner` so
Gabriel Charetteb984d672019-02-12 21:53:2790 thread-affine is a subset of thread-unsafe. Thread-affine is also sometimes
91 referred to as **thread-hostile**.
Albert J. Wongf06ff5002021-07-08 20:37:0092 * **Thread-safe**: Such types/methods can be safely accessed in parallel.
93 * **Thread-compatible**: Such types provide safe parallel access to const
Gabriel Charetteb984d672019-02-12 21:53:2794 methods but require synchronization for non-const (or mixed const/non-const
Gabriel Charette39db4c62019-04-29 19:52:3895 access). Chrome doesn't expose reader-writer locks; as such, the only use
Gabriel Charetteb984d672019-02-12 21:53:2796 case for this is objects (typically globals) which are initialized once in a
Gabriel Charette364a16a2019-02-06 21:12:1597 thread-safe manner (either in the single-threaded phase of startup or lazily
98 through a thread-safe static-local-initialization paradigm a la
Gabriel Charetteb984d672019-02-12 21:53:2799 `base::NoDestructor`) and forever after immutable.
100 * **Immutable**: A subset of thread-compatible types which cannot be modified
101 after construction.
Gabriel Charette364a16a2019-02-06 21:12:15102 * **Sequence-friendly**: Such types/methods are thread-unsafe types which
103 support being invoked from a `base::SequencedTaskRunner`. Ideally this would
104 be the case for all thread-unsafe types but legacy code sometimes has
105 overzealous checks that enforce thread-affinity in mere thread-unsafe
Gabriel Charette39db4c62019-04-29 19:52:38106 scenarios. See [Prefer Sequences to
107 Threads](#prefer-sequences-to-physical-threads) below for more details.
Gabriel Charette364a16a2019-02-06 21:12:15108
fdoraybacba4a22017-05-10 21:10:00109### Threads
110
111Every Chrome process has
112
113* a main thread
Gabriel Charette39db4c62019-04-29 19:52:38114 * in the browser process (BrowserThread::UI): updates the UI
115 * in renderer processes (Blink main thread): runs most of Blink
fdoraybacba4a22017-05-10 21:10:00116* an IO thread
Matt Falkenhagen72a2dfc2021-08-05 22:36:13117 * in all processes: all IPC messages arrive on this thread. The application
118 logic to handle the message may be in a different thread (i.e., the IO
119 thread may route the message to a [Mojo
120 interface](/docs/README.md#Mojo-Services) which is bound to a
121 different thread).
122 * more generally most async I/O happens on this thread (e.g., through
123 base::FileDescriptorWatcher).
124 * in the browser process: this is called BrowserThread::IO.
fdoraybacba4a22017-05-10 21:10:00125* a few more special-purpose threads
126* and a pool of general-purpose threads
127
128Most threads have a loop that gets tasks from a queue and runs them (the queue
129may be shared between multiple threads).
130
131### Tasks
132
133A task is a `base::OnceClosure` added to a queue for asynchronous execution.
134
135A `base::OnceClosure` stores a function pointer and arguments. It has a `Run()`
136method that invokes the function pointer using the bound arguments. It is
137created using `base::BindOnce`. (ref. [Callback<> and Bind()
138documentation](callback.md)).
139
140```
141void TaskA() {}
142void TaskB(int v) {}
143
144auto task_a = base::BindOnce(&TaskA);
145auto task_b = base::BindOnce(&TaskB, 42);
146```
147
148A group of tasks can be executed in one of the following ways:
149
150* [Parallel](#Posting-a-Parallel-Task): No task execution ordering, possibly all
151 at once on any thread
152* [Sequenced](#Posting-a-Sequenced-Task): Tasks executed in posting order, one
153 at a time on any thread.
154* [Single Threaded](#Posting-Multiple-Tasks-to-the-Same-Thread): Tasks executed
155 in posting order, one at a time on a single thread.
Drew Stonebraker653a3ba2019-07-02 19:24:23156 * [COM Single Threaded](#Posting-Tasks-to-a-COM-Single_Thread-Apartment-STA_Thread-Windows):
fdoraybacba4a22017-05-10 21:10:00157 A variant of single threaded with COM initialized.
158
Gabriel Charette39db4c62019-04-29 19:52:38159### Prefer Sequences to Physical Threads
gab2a4576052017-06-07 23:36:12160
Gabriel Charette39db4c62019-04-29 19:52:38161Sequenced execution (on virtual threads) is strongly preferred to
162single-threaded execution (on physical threads). Except for types/methods bound
163to the main thread (UI) or IO threads: thread-safety is better achieved via
164`base::SequencedTaskRunner` than through managing your own physical threads
165(ref. [Posting a Sequenced Task](#posting-a-sequenced-task) below).
gab2a4576052017-06-07 23:36:12166
Gabriel Charette39db4c62019-04-29 19:52:38167All APIs which are exposed for "current physical thread" have an equivalent for
168"current sequence"
169([mapping](threading_and_tasks_faq.md#How-to-migrate-from-SingleThreadTaskRunner-to-SequencedTaskRunner)).
gab2a4576052017-06-07 23:36:12170
Gabriel Charette39db4c62019-04-29 19:52:38171If you find yourself writing a sequence-friendly type and it fails
172thread-affinity checks (e.g., `THREAD_CHECKER`) in a leaf dependency: consider
173making that dependency sequence-friendly as well. Most core APIs in Chrome are
174sequence-friendly, but some legacy types may still over-zealously use
175ThreadChecker/ThreadTaskRunnerHandle/SingleThreadTaskRunner when they could
176instead rely on the "current sequence" and no longer be thread-affine.
fdoraybacba4a22017-05-10 21:10:00177
178## Posting a Parallel Task
179
Gabriel Charette52fa3ae2019-04-15 21:44:37180### Direct Posting to the Thread Pool
fdoraybacba4a22017-05-10 21:10:00181
182A task that can run on any thread and doesn’t have ordering or mutual exclusion
183requirements with other tasks should be posted using one of the
Gabriel Charette43de5c42020-01-27 22:44:45184`base::ThreadPool::PostTask*()` functions defined in
185[`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h).
fdoraybacba4a22017-05-10 21:10:00186
187```cpp
Gabriel Charette43de5c42020-01-27 22:44:45188base::ThreadPool::PostTask(FROM_HERE, base::BindOnce(&Task));
fdoraybacba4a22017-05-10 21:10:00189```
190
191This posts tasks with default traits.
192
Gabriel Charette43de5c42020-01-27 22:44:45193The `base::ThreadPool::PostTask*()` functions allow the caller to provide
194additional details about the task via TaskTraits (ref. [Annotating Tasks with
195TaskTraits](#Annotating-Tasks-with-TaskTraits)).
fdoraybacba4a22017-05-10 21:10:00196
197```cpp
Gabriel Charette43de5c42020-01-27 22:44:45198base::ThreadPool::PostTask(
Gabriel Charetteb10aeebc2018-07-26 20:15:00199 FROM_HERE, {base::TaskPriority::BEST_EFFORT, MayBlock()},
fdoraybacba4a22017-05-10 21:10:00200 base::BindOnce(&Task));
201```
202
fdoray52bf5552017-05-11 12:43:59203### Posting via a TaskRunner
fdoraybacba4a22017-05-10 21:10:00204
205A parallel
Gabriel Charette39db4c62019-04-29 19:52:38206[`base::TaskRunner`](https://cs.chromium.org/chromium/src/base/task_runner.h) is
Gabriel Charette43de5c42020-01-27 22:44:45207an alternative to calling `base::ThreadPool::PostTask*()` directly. This is
208mainly useful when it isn’t known in advance whether tasks will be posted in
209parallel, in sequence, or to a single-thread (ref. [Posting a Sequenced
Gabriel Charette39db4c62019-04-29 19:52:38210Task](#Posting-a-Sequenced-Task), [Posting Multiple Tasks to the Same
211Thread](#Posting-Multiple-Tasks-to-the-Same-Thread)). Since `base::TaskRunner`
212is the base class of `base::SequencedTaskRunner` and
213`base::SingleThreadTaskRunner`, a `scoped_refptr<TaskRunner>` member can hold a
214`base::TaskRunner`, a `base::SequencedTaskRunner` or a
215`base::SingleThreadTaskRunner`.
fdoraybacba4a22017-05-10 21:10:00216
217```cpp
218class A {
219 public:
220 A() = default;
221
Gabriel Charette43de5c42020-01-27 22:44:45222 void PostSomething() {
223 task_runner_->PostTask(FROM_HERE, base::BindOnce(&A, &DoSomething));
224 }
225
fdoraybacba4a22017-05-10 21:10:00226 void DoSomething() {
fdoraybacba4a22017-05-10 21:10:00227 }
228
229 private:
230 scoped_refptr<base::TaskRunner> task_runner_ =
Gabriel Charette43de5c42020-01-27 22:44:45231 base::ThreadPool::CreateTaskRunner({base::TaskPriority::USER_VISIBLE});
fdoraybacba4a22017-05-10 21:10:00232};
233```
234
235Unless a test needs to control precisely how tasks are executed, it is preferred
Gabriel Charette49e3cd02020-01-28 03:45:27236to call `base::ThreadPool::PostTask*()` directly (ref. [Testing](#Testing) for
237less invasive ways of controlling tasks in tests).
fdoraybacba4a22017-05-10 21:10:00238
239## Posting a Sequenced Task
240
241A sequence is a set of tasks that run one at a time in posting order (not
242necessarily on the same thread). To post tasks as part of a sequence, use a
Gabriel Charette39db4c62019-04-29 19:52:38243[`base::SequencedTaskRunner`](https://cs.chromium.org/chromium/src/base/sequenced_task_runner.h).
fdoraybacba4a22017-05-10 21:10:00244
245### Posting to a New Sequence
246
Gabriel Charette39db4c62019-04-29 19:52:38247A `base::SequencedTaskRunner` can be created by
Gabriel Charette43de5c42020-01-27 22:44:45248`base::ThreadPool::CreateSequencedTaskRunner()`.
fdoraybacba4a22017-05-10 21:10:00249
250```cpp
251scoped_refptr<SequencedTaskRunner> sequenced_task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45252 base::ThreadPool::CreateSequencedTaskRunner(...);
fdoraybacba4a22017-05-10 21:10:00253
254// TaskB runs after TaskA completes.
255sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
256sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB));
257```
258
Alex Clarke0dd499562019-10-18 19:45:09259### Posting to the Current (Virtual) Thread
260
Gabriel Charettefee55662019-11-20 21:06:28261The preferred way of posting to the current (virtual) thread is via
262`base::SequencedTaskRunnerHandle::Get()`.
Alex Clarke0dd499562019-10-18 19:45:09263
264```cpp
265// The task will run on the current (virtual) thread's default task queue.
Gabriel Charettefee55662019-11-20 21:06:28266base::SequencedTaskRunnerHandle::Get()->PostTask(
267 FROM_HERE, base::BindOnce(&Task);
Alex Clarke0dd499562019-10-18 19:45:09268```
269
Jared Saulea867ab2021-07-15 17:39:01270Note that `SequencedTaskRunnerHandle::Get()` returns the default queue for the
Gabriel Charettefee55662019-11-20 21:06:28271current virtual thread. On threads with multiple task queues (e.g.
272BrowserThread::UI) this can be a different queue than the one the current task
273belongs to. The "current" task runner is intentionally not exposed via a static
274getter. Either you know it already and can post to it directly or you don't and
275the only sensible destination is the default queue.
Alex Clarke0dd499562019-10-18 19:45:09276
fdoraybacba4a22017-05-10 21:10:00277## Using Sequences Instead of Locks
278
279Usage of locks is discouraged in Chrome. Sequences inherently provide
Gabriel Charettea3ccc972018-11-13 14:43:12280thread-safety. Prefer classes that are always accessed from the same
281sequence to managing your own thread-safety with locks.
282
283**Thread-safe but not thread-affine; how so?** Tasks posted to the same sequence
284will run in sequential order. After a sequenced task completes, the next task
285may be picked up by a different worker thread, but that task is guaranteed to
286see any side-effects caused by the previous one(s) on its sequence.
fdoraybacba4a22017-05-10 21:10:00287
288```cpp
289class A {
290 public:
291 A() {
292 // Do not require accesses to be on the creation sequence.
isherman8c33b8a2017-06-27 19:18:30293 DETACH_FROM_SEQUENCE(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00294 }
295
296 void AddValue(int v) {
297 // Check that all accesses are on the same sequence.
isherman8c33b8a2017-06-27 19:18:30298 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00299 values_.push_back(v);
300}
301
302 private:
isherman8c33b8a2017-06-27 19:18:30303 SEQUENCE_CHECKER(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00304
305 // No lock required, because all accesses are on the
306 // same sequence.
307 std::vector<int> values_;
308};
309
310A a;
311scoped_refptr<SequencedTaskRunner> task_runner_for_a = ...;
Mike Bjorged3a09842018-05-15 18:37:28312task_runner_for_a->PostTask(FROM_HERE,
313 base::BindOnce(&A::AddValue, base::Unretained(&a), 42));
314task_runner_for_a->PostTask(FROM_HERE,
315 base::BindOnce(&A::AddValue, base::Unretained(&a), 27));
fdoraybacba4a22017-05-10 21:10:00316
317// Access from a different sequence causes a DCHECK failure.
318scoped_refptr<SequencedTaskRunner> other_task_runner = ...;
319other_task_runner->PostTask(FROM_HERE,
Mike Bjorged3a09842018-05-15 18:37:28320 base::BindOnce(&A::AddValue, base::Unretained(&a), 1));
fdoraybacba4a22017-05-10 21:10:00321```
322
Gabriel Charette90480312018-02-16 15:10:05323Locks should only be used to swap in a shared data structure that can be
324accessed on multiple threads. If one thread updates it based on expensive
325computation or through disk access, then that slow work should be done without
Gabriel Charette39db4c62019-04-29 19:52:38326holding the lock. Only when the result is available should the lock be used to
327swap in the new data. An example of this is in PluginList::LoadPlugins
328([`content/browser/plugin_list.cc`](https://cs.chromium.org/chromium/src/content/browser/plugin_list.cc).
329If you must use locks,
Gabriel Charette90480312018-02-16 15:10:05330[here](https://www.chromium.org/developers/lock-and-condition-variable) are some
331best practices and pitfalls to avoid.
332
Gabriel Charette39db4c62019-04-29 19:52:38333In order to write non-blocking code, many APIs in Chrome are asynchronous.
Gabriel Charette90480312018-02-16 15:10:05334Usually this means that they either need to be executed on a particular
335thread/sequence and will return results via a custom delegate interface, or they
Alex St-Onge490a97a2021-02-04 02:47:19336take a `base::OnceCallback<>` (or `base::RepeatingCallback<>`) object that is
337called when the requested operation is completed. Executing work on a specific
338thread/sequence is covered in the PostTask sections above.
Gabriel Charette90480312018-02-16 15:10:05339
fdoraybacba4a22017-05-10 21:10:00340## Posting Multiple Tasks to the Same Thread
341
342If multiple tasks need to run on the same thread, post them to a
Gabriel Charette39db4c62019-04-29 19:52:38343[`base::SingleThreadTaskRunner`](https://cs.chromium.org/chromium/src/base/single_thread_task_runner.h).
344All tasks posted to the same `base::SingleThreadTaskRunner` run on the same thread in
fdoraybacba4a22017-05-10 21:10:00345posting order.
346
347### Posting to the Main Thread or to the IO Thread in the Browser Process
348
Eric Seckler6cf08db82018-08-30 12:01:55349To post tasks to the main thread or to the IO thread, use
Olivier Li56b99d4e2020-02-11 13:51:41350`content::GetUIThreadTaskRunner({})` or `content::GetIOThreadTaskRunner({})`
Gabriel Charette49e3cd02020-01-28 03:45:27351from
352[`content/public/browser/browser_thread.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_thread.h)
353
354You may provide additional BrowserTaskTraits as a parameter to those methods
355though this is generally still uncommon in BrowserThreads and should be reserved
356for advanced use cases.
357
358There's an ongoing migration ([task APIs v3]) away from the previous
359base-API-with-traits which you may still find throughout the codebase (it's
360equivalent):
fdoraybacba4a22017-05-10 21:10:00361
362```cpp
Sami Kyostila831c60b2019-07-31 13:31:23363base::PostTask(FROM_HERE, {content::BrowserThread::UI}, ...);
fdoraybacba4a22017-05-10 21:10:00364
Sami Kyostila831c60b2019-07-31 13:31:23365base::CreateSingleThreadTaskRunner({content::BrowserThread::IO})
fdoraybacba4a22017-05-10 21:10:00366 ->PostTask(FROM_HERE, ...);
367```
368
Gabriel Charette49e3cd02020-01-28 03:45:27369Note: For the duration of the migration, you'll unfortunately need to continue
370manually including
371[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h).
372to use the browser_thread.h API.
Gabriel Charette43de5c42020-01-27 22:44:45373
fdoraybacba4a22017-05-10 21:10:00374The main thread and the IO thread are already super busy. Therefore, prefer
fdoray52bf5552017-05-11 12:43:59375posting to a general purpose thread when possible (ref.
376[Posting a Parallel Task](#Posting-a-Parallel-Task),
377[Posting a Sequenced task](#Posting-a-Sequenced-Task)).
378Good reasons to post to the main thread are to update the UI or access objects
379that are bound to it (e.g. `Profile`). A good reason to post to the IO thread is
380to access the internals of components that are bound to it (e.g. IPCs, network).
381Note: It is not necessary to have an explicit post task to the IO thread to
382send/receive an IPC or send/receive data on the network.
fdoraybacba4a22017-05-10 21:10:00383
384### Posting to the Main Thread in a Renderer Process
Gabriel Charette49e3cd02020-01-28 03:45:27385TODO(blink-dev)
fdoraybacba4a22017-05-10 21:10:00386
387### Posting to a Custom SingleThreadTaskRunner
388
389If multiple tasks need to run on the same thread and that thread doesn’t have to
Gabriel Charette43de5c42020-01-27 22:44:45390be the main thread or the IO thread, post them to a
Gabriel Charette49e3cd02020-01-28 03:45:27391`base::SingleThreadTaskRunner` created by
392`base::Threadpool::CreateSingleThreadTaskRunner`.
fdoraybacba4a22017-05-10 21:10:00393
394```cpp
Dominic Farolinodbe9769b2019-05-31 04:06:03395scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45396 base::Threadpool::CreateSingleThreadTaskRunner(...);
fdoraybacba4a22017-05-10 21:10:00397
398// TaskB runs after TaskA completes. Both tasks run on the same thread.
399single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
400single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB));
401```
402
Gabriel Charette39db4c62019-04-29 19:52:38403Remember that we [prefer sequences to physical
404threads](#prefer-sequences-to-physical-threads) and that this thus should rarely
405be necessary.
fdoraybacba4a22017-05-10 21:10:00406
Alexander Timine653dfc2020-01-07 17:55:06407### Posting to the Current Thread
408
409*** note
410**IMPORTANT:** To post a task that needs mutual exclusion with the current
Gabriel Charette49e3cd02020-01-28 03:45:27411sequence of tasks but doesn’t absolutely need to run on the current physical
412thread, use `base::SequencedTaskRunnerHandle::Get()` instead of
Alexander Timine653dfc2020-01-07 17:55:06413`base::ThreadTaskRunnerHandle::Get()` (ref. [Posting to the Current
Gabriel Charette49e3cd02020-01-28 03:45:27414Sequence](#Posting-to-the-Current-Virtual_Thread)). That will better document
415the requirements of the posted task and will avoid unnecessarily making your API
416physical thread-affine. In a single-thread task,
417`base::SequencedTaskRunnerHandle::Get()` is equivalent to
418`base::ThreadTaskRunnerHandle::Get()`.
Alexander Timine653dfc2020-01-07 17:55:06419***
420
421If you must post a task to the current physical thread nonetheless, use
422[`base::ThreadTaskRunnerHandle`](https://cs.chromium.org/chromium/src/base/threading/thread_task_runner_handle.h).
423
424```cpp
425// The task will run on the current thread in the future.
426base::ThreadTaskRunnerHandle::Get()->PostTask(
427 FROM_HERE, base::BindOnce(&Task));
428```
429
fdoraybacba4a22017-05-10 21:10:00430## Posting Tasks to a COM Single-Thread Apartment (STA) Thread (Windows)
431
432Tasks that need to run on a COM Single-Thread Apartment (STA) thread must be
Gabriel Charette39db4c62019-04-29 19:52:38433posted to a `base::SingleThreadTaskRunner` returned by
Gabriel Charette43de5c42020-01-27 22:44:45434`base::ThreadPool::CreateCOMSTATaskRunner()`. As mentioned in [Posting Multiple
435Tasks to the Same Thread](#Posting-Multiple-Tasks-to-the-Same-Thread), all tasks
436posted to the same `base::SingleThreadTaskRunner` run on the same thread in
437posting order.
fdoraybacba4a22017-05-10 21:10:00438
439```cpp
440// Task(A|B|C)UsingCOMSTA will run on the same COM STA thread.
441
442void TaskAUsingCOMSTA() {
443 // [ This runs on a COM STA thread. ]
444
445 // Make COM STA calls.
446 // ...
447
448 // Post another task to the current COM STA thread.
449 base::ThreadTaskRunnerHandle::Get()->PostTask(
450 FROM_HERE, base::BindOnce(&TaskCUsingCOMSTA));
451}
452void TaskBUsingCOMSTA() { }
453void TaskCUsingCOMSTA() { }
454
Gabriel Charette43de5c42020-01-27 22:44:45455auto com_sta_task_runner = base::ThreadPool::CreateCOMSTATaskRunner(...);
fdoraybacba4a22017-05-10 21:10:00456com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskAUsingCOMSTA));
457com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskBUsingCOMSTA));
458```
459
460## Annotating Tasks with TaskTraits
461
Gabriel Charette39db4c62019-04-29 19:52:38462[`base::TaskTraits`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
Gabriel Charette52fa3ae2019-04-15 21:44:37463encapsulate information about a task that helps the thread pool make better
fdoraybacba4a22017-05-10 21:10:00464scheduling decisions.
465
Gabriel Charette43de5c42020-01-27 22:44:45466Methods that take `base::TaskTraits` can be be passed `{}` when default traits
467are sufficient. Default traits are appropriate for tasks that:
Gabriel Charettede41cad2020-03-03 18:05:06468- Don’t block (ref. MayBlock and WithBaseSyncPrimitives);
469- Pertain to user-blocking activity;
470 (explicitly or implicitly by having an ordering dependency with a component
471 that does)
Gabriel Charette52fa3ae2019-04-15 21:44:37472- Can either block shutdown or be skipped on shutdown (thread pool is free to
473 choose a fitting default).
fdoraybacba4a22017-05-10 21:10:00474Tasks that don’t match this description must be posted with explicit TaskTraits.
475
Gabriel Charette04b138f2018-08-06 00:03:22476[`base/task/task_traits.h`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
Eric Seckler6cf08db82018-08-30 12:01:55477provides exhaustive documentation of available traits. The content layer also
478provides additional traits in
479[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h)
480to facilitate posting a task onto a BrowserThread.
481
Gabriel Charette39db4c62019-04-29 19:52:38482Below are some examples of how to specify `base::TaskTraits`.
fdoraybacba4a22017-05-10 21:10:00483
484```cpp
Gabriel Charettede41cad2020-03-03 18:05:06485// This task has no explicit TaskTraits. It cannot block. Its priority is
486// USER_BLOCKING. It will either block shutdown or be skipped on shutdown.
Gabriel Charette43de5c42020-01-27 22:44:45487base::ThreadPool::PostTask(FROM_HERE, base::BindOnce(...));
fdoraybacba4a22017-05-10 21:10:00488
Gabriel Charettede41cad2020-03-03 18:05:06489// This task has the highest priority. The thread pool will schedule it before
490// USER_VISIBLE and BEST_EFFORT tasks.
Gabriel Charette43de5c42020-01-27 22:44:45491base::ThreadPool::PostTask(
fdoraybacba4a22017-05-10 21:10:00492 FROM_HERE, {base::TaskPriority::USER_BLOCKING},
493 base::BindOnce(...));
494
495// This task has the lowest priority and is allowed to block (e.g. it
496// can read a file from disk).
Gabriel Charette43de5c42020-01-27 22:44:45497base::ThreadPool::PostTask(
Gabriel Charetteb10aeebc2018-07-26 20:15:00498 FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
fdoraybacba4a22017-05-10 21:10:00499 base::BindOnce(...));
500
501// This task blocks shutdown. The process won't exit before its
502// execution is complete.
Gabriel Charette43de5c42020-01-27 22:44:45503base::ThreadPool::PostTask(
fdoraybacba4a22017-05-10 21:10:00504 FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
505 base::BindOnce(...));
506```
507
508## Keeping the Browser Responsive
509
510Do not perform expensive work on the main thread, the IO thread or any sequence
511that is expected to run tasks with a low latency. Instead, perform expensive
Gabriel Charette43de5c42020-01-27 22:44:45512work asynchronously using `base::ThreadPool::PostTaskAndReply*()` or
Gabriel Charette39db4c62019-04-29 19:52:38513`base::SequencedTaskRunner::PostTaskAndReply()`. Note that
514asynchronous/overlapped I/O on the IO thread are fine.
fdoraybacba4a22017-05-10 21:10:00515
516Example: Running the code below on the main thread will prevent the browser from
517responding to user input for a long time.
518
519```cpp
520// GetHistoryItemsFromDisk() may block for a long time.
521// AddHistoryItemsToOmniboxDropDown() updates the UI and therefore must
522// be called on the main thread.
523AddHistoryItemsToOmniboxDropdown(GetHistoryItemsFromDisk("keyword"));
524```
525
526The code below solves the problem by scheduling a call to
527`GetHistoryItemsFromDisk()` in a thread pool followed by a call to
528`AddHistoryItemsToOmniboxDropdown()` on the origin sequence (the main thread in
529this case). The return value of the first call is automatically provided as
530argument to the second call.
531
532```cpp
Gabriel Charette43de5c42020-01-27 22:44:45533base::ThreadPool::PostTaskAndReplyWithResult(
fdoraybacba4a22017-05-10 21:10:00534 FROM_HERE, {base::MayBlock()},
535 base::BindOnce(&GetHistoryItemsFromDisk, "keyword"),
536 base::BindOnce(&AddHistoryItemsToOmniboxDropdown));
537```
538
539## Posting a Task with a Delay
540
541### Posting a One-Off Task with a Delay
542
543To post a task that must run once after a delay expires, use
Gabriel Charette43de5c42020-01-27 22:44:45544`base::ThreadPool::PostDelayedTask*()` or `base::TaskRunner::PostDelayedTask()`.
fdoraybacba4a22017-05-10 21:10:00545
546```cpp
Gabriel Charette43de5c42020-01-27 22:44:45547base::ThreadPool::PostDelayedTask(
Gabriel Charetteb10aeebc2018-07-26 20:15:00548 FROM_HERE, {base::TaskPriority::BEST_EFFORT}, base::BindOnce(&Task),
Peter Kastinge5a38ed2021-10-02 03:06:35549 base::Hours(1));
fdoraybacba4a22017-05-10 21:10:00550
551scoped_refptr<base::SequencedTaskRunner> task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45552 base::ThreadPool::CreateSequencedTaskRunner(
553 {base::TaskPriority::BEST_EFFORT});
fdoraybacba4a22017-05-10 21:10:00554task_runner->PostDelayedTask(
Peter Kastinge5a38ed2021-10-02 03:06:35555 FROM_HERE, base::BindOnce(&Task), base::Hours(1));
fdoraybacba4a22017-05-10 21:10:00556```
557
558*** note
559**NOTE:** A task that has a 1-hour delay probably doesn’t have to run right away
Gabriel Charetteb10aeebc2018-07-26 20:15:00560when its delay expires. Specify `base::TaskPriority::BEST_EFFORT` to prevent it
fdoraybacba4a22017-05-10 21:10:00561from slowing down the browser when its delay expires.
562***
563
564### Posting a Repeating Task with a Delay
565To post a task that must run at regular intervals,
566use [`base::RepeatingTimer`](https://cs.chromium.org/chromium/src/base/timer/timer.h).
567
568```cpp
569class A {
570 public:
571 ~A() {
572 // The timer is stopped automatically when it is deleted.
573 }
574 void StartDoingStuff() {
Peter Kasting53fd6ee2021-10-05 20:40:48575 timer_.Start(FROM_HERE, Seconds(1),
Erik Chen0ee26a32021-07-14 20:04:47576 this, &A::DoStuff);
fdoraybacba4a22017-05-10 21:10:00577 }
578 void StopDoingStuff() {
579 timer_.Stop();
580 }
581 private:
582 void DoStuff() {
583 // This method is called every second on the sequence that invoked
584 // StartDoingStuff().
585 }
586 base::RepeatingTimer timer_;
587};
588```
589
590## Cancelling a Task
591
592### Using base::WeakPtr
593
594[`base::WeakPtr`](https://cs.chromium.org/chromium/src/base/memory/weak_ptr.h)
595can be used to ensure that any callback bound to an object is canceled when that
596object is destroyed.
597
598```cpp
599int Compute() { … }
600
601class A {
602 public:
fdoraybacba4a22017-05-10 21:10:00603 void ComputeAndStore() {
604 // Schedule a call to Compute() in a thread pool followed by
605 // a call to A::Store() on the current sequence. The call to
606 // A::Store() is canceled when |weak_ptr_factory_| is destroyed.
607 // (guarantees that |this| will not be used-after-free).
Gabriel Charette43de5c42020-01-27 22:44:45608 base::ThreadPool::PostTaskAndReplyWithResult(
fdoraybacba4a22017-05-10 21:10:00609 FROM_HERE, base::BindOnce(&Compute),
610 base::BindOnce(&A::Store, weak_ptr_factory_.GetWeakPtr()));
611 }
612
613 private:
614 void Store(int value) { value_ = value; }
615
616 int value_;
Jeremy Roman0dd0b2f2019-07-16 21:00:43617 base::WeakPtrFactory<A> weak_ptr_factory_{this};
fdoraybacba4a22017-05-10 21:10:00618};
619```
620
621Note: `WeakPtr` is not thread-safe: `GetWeakPtr()`, `~WeakPtrFactory()`, and
Francois Dorayf652a9d02021-07-06 13:07:52622`Store()` (bound to a `WeakPtr`) must all run on the same sequence.
fdoraybacba4a22017-05-10 21:10:00623
624### Using base::CancelableTaskTracker
625
626[`base::CancelableTaskTracker`](https://cs.chromium.org/chromium/src/base/task/cancelable_task_tracker.h)
627allows cancellation to happen on a different sequence than the one on which
628tasks run. Keep in mind that `CancelableTaskTracker` cannot cancel tasks that
629have already started to run.
630
631```cpp
Gabriel Charette43de5c42020-01-27 22:44:45632auto task_runner = base::ThreadPool::CreateTaskRunner({});
fdoraybacba4a22017-05-10 21:10:00633base::CancelableTaskTracker cancelable_task_tracker;
634cancelable_task_tracker.PostTask(task_runner.get(), FROM_HERE,
Peter Kasting341e1fb2018-02-24 00:03:01635 base::DoNothing());
fdoraybacba4a22017-05-10 21:10:00636// Cancels Task(), only if it hasn't already started running.
637cancelable_task_tracker.TryCancelAll();
638```
639
Etienne Pierre-dorayd3882992020-01-14 20:34:11640## Posting a Job to run in parallel
641
642The [`base::PostJob`](https://cs.chromium.org/chromium/src/base/task/post_job.h)
643is a power user API to be able to schedule a single base::RepeatingCallback
Albert J. Wongf06ff5002021-07-08 20:37:00644worker task and request that ThreadPool workers invoke it in parallel.
Etienne Pierre-dorayd3882992020-01-14 20:34:11645This avoids degenerate cases:
646* Calling `PostTask()` for each work item, causing significant overhead.
647* Fixed number of `PostTask()` calls that split the work and might run for a
648 long time. This is problematic when many components post “num cores” tasks and
649 all expect to use all the cores. In these cases, the scheduler lacks context
650 to be fair to multiple same-priority requests and/or ability to request lower
651 priority work to yield when high priority work comes in.
652
Etienne Pierre-doray6d3cd9192020-04-06 21:10:37653See [`base/task/job_perftest.cc`](https://cs.chromium.org/chromium/src/base/task/job_perftest.cc)
654for a complete example.
655
Etienne Pierre-dorayd3882992020-01-14 20:34:11656```cpp
657// A canonical implementation of |worker_task|.
658void WorkerTask(base::JobDelegate* job_delegate) {
659 while (!job_delegate->ShouldYield()) {
660 auto work_item = TakeWorkItem(); // Smallest unit of work.
661 if (!work_item)
662 return:
663 ProcessWork(work_item);
664 }
665}
666
667// Returns the latest thread-safe number of incomplete work items.
Etienne Pierre-Dorayf91d7a02020-09-11 15:53:27668void NumIncompleteWorkItems(size_t worker_count) {
669 // NumIncompleteWorkItems() may use |worker_count| if it needs to account for
670 // local work lists, which is easier than doing its own accounting, keeping in
671 // mind that the actual number of items may be racily overestimated and thus
672 // WorkerTask() may be called when there's no available work.
673 return GlobalQueueSize() + worker_count;
674}
Etienne Pierre-dorayd3882992020-01-14 20:34:11675
Gabriel Charette1138d602020-01-29 08:51:52676base::PostJob(FROM_HERE, {},
Etienne Pierre-dorayd3882992020-01-14 20:34:11677 base::BindRepeating(&WorkerTask),
678 base::BindRepeating(&NumIncompleteWorkItems));
679```
680
681By doing as much work as possible in a loop when invoked, the worker task avoids
682scheduling overhead. Meanwhile `base::JobDelegate::ShouldYield()` is
683periodically invoked to conditionally exit and let the scheduler prioritize
684other work. This yield-semantic allows, for example, a user-visible job to use
685all cores but get out of the way when a user-blocking task comes in.
686
Jared Saulea867ab2021-07-15 17:39:01687### Adding additional work to a running job
Etienne Pierre-dorayd3882992020-01-14 20:34:11688
689When new work items are added and the API user wants additional threads to
Albert J. Wongf06ff5002021-07-08 20:37:00690invoke the worker task in parallel,
Etienne Pierre-dorayd3882992020-01-14 20:34:11691`JobHandle/JobDelegate::NotifyConcurrencyIncrease()` *must* be invoked shortly
692after max concurrency increases.
693
fdoraybacba4a22017-05-10 21:10:00694## Testing
695
Gabriel Charette0b20ee6c2019-09-18 14:06:12696For more details see [Testing Components Which Post
697Tasks](threading_and_tasks_testing.md).
698
fdoraybacba4a22017-05-10 21:10:00699To test code that uses `base::ThreadTaskRunnerHandle`,
700`base::SequencedTaskRunnerHandle` or a function in
Gabriel Charette39db4c62019-04-29 19:52:38701[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h),
702instantiate a
Gabriel Charette0b20ee6c2019-09-18 14:06:12703[`base::test::TaskEnvironment`](https://cs.chromium.org/chromium/src/base/test/task_environment.h)
Gabriel Charette39db4c62019-04-29 19:52:38704for the scope of the test. If you need BrowserThreads, use
Gabriel Charette798fde72019-08-20 22:24:04705`content::BrowserTaskEnvironment` instead of
Gabriel Charette694c3c332019-08-19 14:53:05706`base::test::TaskEnvironment`.
fdoraybacba4a22017-05-10 21:10:00707
Gabriel Charette694c3c332019-08-19 14:53:05708Tests can run the `base::test::TaskEnvironment`'s message pump using a
Gabriel Charette39db4c62019-04-29 19:52:38709`base::RunLoop`, which can be made to run until `Quit()` (explicitly or via
710`RunLoop::QuitClosure()`), or to `RunUntilIdle()` ready-to-run tasks and
711immediately return.
Wezd9e4cb772019-01-09 03:07:03712
Wez9d5dd282020-02-10 17:21:22713TaskEnvironment configures RunLoop::Run() to GTEST_FAIL() if it hasn't been
Wezd9e4cb772019-01-09 03:07:03714explicitly quit after TestTimeouts::action_timeout(). This is preferable to
715having the test hang if the code under test fails to trigger the RunLoop to
Wez9d5dd282020-02-10 17:21:22716quit. The timeout can be overridden with base::test::ScopedRunLoopTimeout.
Wezd9e4cb772019-01-09 03:07:03717
fdoraybacba4a22017-05-10 21:10:00718```cpp
719class MyTest : public testing::Test {
720 public:
721 // ...
722 protected:
Gabriel Charette694c3c332019-08-19 14:53:05723 base::test::TaskEnvironment task_environment_;
fdoraybacba4a22017-05-10 21:10:00724};
725
726TEST(MyTest, MyTest) {
727 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindOnce(&A));
728 base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE,
729 base::BindOnce(&B));
730 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
731 FROM_HERE, base::BindOnce(&C), base::TimeDelta::Max());
732
733 // This runs the (Thread|Sequenced)TaskRunnerHandle queue until it is empty.
734 // Delayed tasks are not added to the queue until they are ripe for execution.
735 base::RunLoop().RunUntilIdle();
736 // A and B have been executed. C is not ripe for execution yet.
737
738 base::RunLoop run_loop;
739 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindOnce(&D));
740 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop.QuitClosure());
741 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindOnce(&E));
742
743 // This runs the (Thread|Sequenced)TaskRunnerHandle queue until QuitClosure is
744 // invoked.
745 run_loop.Run();
746 // D and run_loop.QuitClosure() have been executed. E is still in the queue.
747
Gabriel Charette52fa3ae2019-04-15 21:44:37748 // Tasks posted to thread pool run asynchronously as they are posted.
Gabriel Charette43de5c42020-01-27 22:44:45749 base::ThreadPool::PostTask(FROM_HERE, {}, base::BindOnce(&F));
fdoraybacba4a22017-05-10 21:10:00750 auto task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45751 base::ThreadPool::CreateSequencedTaskRunner({});
fdoraybacba4a22017-05-10 21:10:00752 task_runner->PostTask(FROM_HERE, base::BindOnce(&G));
753
Gabriel Charette52fa3ae2019-04-15 21:44:37754 // To block until all tasks posted to thread pool are done running:
Gabriel Charette43fd3702019-05-29 16:36:51755 base::ThreadPoolInstance::Get()->FlushForTesting();
fdoraybacba4a22017-05-10 21:10:00756 // F and G have been executed.
757
Gabriel Charette43de5c42020-01-27 22:44:45758 base::ThreadPool::PostTaskAndReplyWithResult(
759 FROM_HERE, {}, base::BindOnce(&H), base::BindOnce(&I));
fdoraybacba4a22017-05-10 21:10:00760
761 // This runs the (Thread|Sequenced)TaskRunnerHandle queue until both the
762 // (Thread|Sequenced)TaskRunnerHandle queue and the TaskSchedule queue are
763 // empty:
Gabriel Charette694c3c332019-08-19 14:53:05764 task_environment_.RunUntilIdle();
fdoraybacba4a22017-05-10 21:10:00765 // E, H, I have been executed.
766}
767```
768
Gabriel Charette52fa3ae2019-04-15 21:44:37769## Using ThreadPool in a New Process
fdoraybacba4a22017-05-10 21:10:00770
Gabriel Charette43fd3702019-05-29 16:36:51771ThreadPoolInstance needs to be initialized in a process before the functions in
Gabriel Charette04b138f2018-08-06 00:03:22772[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h)
Gabriel Charette43fd3702019-05-29 16:36:51773can be used. Initialization of ThreadPoolInstance in the Chrome browser process
774and child processes (renderer, GPU, utility) has already been taken care of. To
775use ThreadPoolInstance in another process, initialize ThreadPoolInstance early
776in the main function:
fdoraybacba4a22017-05-10 21:10:00777
778```cpp
Gabriel Charette43fd3702019-05-29 16:36:51779// This initializes and starts ThreadPoolInstance with default params.
780base::ThreadPoolInstance::CreateAndStartWithDefaultParams(“process_name”);
781// The base/task/post_task.h API can now be used with base::ThreadPool trait.
Jared Saulea867ab2021-07-15 17:39:01782// Tasks will be scheduled as they are posted.
fdoraybacba4a22017-05-10 21:10:00783
Gabriel Charette43fd3702019-05-29 16:36:51784// This initializes ThreadPoolInstance.
785base::ThreadPoolInstance::Create(“process_name”);
786// The base/task/post_task.h API can now be used with base::ThreadPool trait. No
787// threads will be created and no tasks will be scheduled until after Start() is
788// called.
789base::ThreadPoolInstance::Get()->Start(params);
Gabriel Charette52fa3ae2019-04-15 21:44:37790// ThreadPool can now create threads and schedule tasks.
fdoraybacba4a22017-05-10 21:10:00791```
792
Gabriel Charette43fd3702019-05-29 16:36:51793And shutdown ThreadPoolInstance late in the main function:
fdoraybacba4a22017-05-10 21:10:00794
795```cpp
Gabriel Charette43fd3702019-05-29 16:36:51796base::ThreadPoolInstance::Get()->Shutdown();
fdoraybacba4a22017-05-10 21:10:00797// Tasks posted with TaskShutdownBehavior::BLOCK_SHUTDOWN and
798// tasks posted with TaskShutdownBehavior::SKIP_ON_SHUTDOWN that
799// have started to run before the Shutdown() call have now completed their
800// execution. Tasks posted with
801// TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN may still be
802// running.
803```
Gabriel Charetteb86e5fe62017-06-08 19:39:28804## TaskRunner ownership (encourage no dependency injection)
Sebastien Marchandc95489b2017-05-25 16:39:34805
806TaskRunners shouldn't be passed through several components. Instead, the
Jared Saulea867ab2021-07-15 17:39:01807component that uses a TaskRunner should be the one that creates it.
Sebastien Marchandc95489b2017-05-25 16:39:34808
809See [this example](https://codereview.chromium.org/2885173002/) of a
810refactoring where a TaskRunner was passed through a lot of components only to be
811used in an eventual leaf. The leaf can and should now obtain its TaskRunner
812directly from
Gabriel Charette04b138f2018-08-06 00:03:22813[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h).
Gabriel Charetteb86e5fe62017-06-08 19:39:28814
Gabriel Charette694c3c332019-08-19 14:53:05815As mentioned above, `base::test::TaskEnvironment` allows unit tests to
Gabriel Charette39db4c62019-04-29 19:52:38816control tasks posted from underlying TaskRunners. In rare cases where a test
817needs to more precisely control task ordering: dependency injection of
818TaskRunners can be useful. For such cases the preferred approach is the
819following:
Gabriel Charetteb86e5fe62017-06-08 19:39:28820
821```cpp
Gabriel Charette39db4c62019-04-29 19:52:38822class Foo {
Gabriel Charetteb86e5fe62017-06-08 19:39:28823 public:
824
Gabriel Charette39db4c62019-04-29 19:52:38825 // Overrides |background_task_runner_| in tests.
Gabriel Charetteb86e5fe62017-06-08 19:39:28826 void SetBackgroundTaskRunnerForTesting(
Gabriel Charette39db4c62019-04-29 19:52:38827 scoped_refptr<base::SequencedTaskRunner> background_task_runner) {
828 background_task_runner_ = std::move(background_task_runner);
829 }
Gabriel Charetteb86e5fe62017-06-08 19:39:28830
831 private:
michaelpg12c04572017-06-26 23:25:06832 scoped_refptr<base::SequencedTaskRunner> background_task_runner_ =
Gabriel Charette43de5c42020-01-27 22:44:45833 base::ThreadPool::CreateSequencedTaskRunner(
Gabriel Charetteb10aeebc2018-07-26 20:15:00834 {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
Gabriel Charetteb86e5fe62017-06-08 19:39:28835}
836```
837
838Note that this still allows removing all layers of plumbing between //chrome and
839that component since unit tests will use the leaf layer directly.
Gabriel Charette8917f4c2018-11-22 15:50:28840
841## FAQ
842See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more examples.
Gabriel Charette43de5c42020-01-27 22:44:45843
844[task APIs v3]: https://docs.google.com/document/d/1tssusPykvx3g0gvbvU4HxGyn3MjJlIylnsH13-Tv6s4/edit?ts=5de99a52#heading=h.ss4tw38hvh3s
Carlos Caballero40b6d042020-06-16 06:50:25845
846## Internals
847
848### SequenceManager
849
850[SequenceManager](https://cs.chromium.org/chromium/src/base/task/sequence_manager/sequence_manager.h)
851manages TaskQueues which have different properties (e.g. priority, common task
852type) multiplexing all posted tasks into a single backing sequence. This will
853usually be a MessagePump. Depending on the type of message pump used other
854events such as UI messages may be processed as well. On Windows APC calls (as
855time permits) and signals sent to a registered set of HANDLEs may also be
856processed.
857
Carlos Caballero4a050922020-07-02 11:43:38858### MessagePump
Carlos Caballero40b6d042020-06-16 06:50:25859
860[MessagePumps](https://cs.chromium.org/chromium/src/base/message_loop/message_pump.h)
861are responsible for processing native messages as well as for giving cycles to
862their delegate (SequenceManager) periodically. MessagePumps take care to mixing
863delegate callbacks with native message processing so neither type of event
864starves the other of cycles.
865
866There are different [MessagePumpTypes](https://cs.chromium.org/chromium/src/base/message_loop/message_pump_type.h),
867most common are:
868
869* DEFAULT: Supports tasks and timers only
870
871* UI: Supports native UI events (e.g. Windows messages)
872
873* IO: Supports asynchronous IO (not file I/O!)
874
875* CUSTOM: User provided implementation of MessagePump interface
876
Carlos Caballero4a050922020-07-02 11:43:38877### RunLoop
Carlos Caballero40b6d042020-06-16 06:50:25878
Jared Saulea867ab2021-07-15 17:39:01879RunLoop is a helper class to run the RunLoop::Delegate associated with the
Carlos Caballero40b6d042020-06-16 06:50:25880current thread (usually a SequenceManager). Create a RunLoop on the stack and
881call Run/Quit to run a nested RunLoop but please avoid nested loops in
882production code!
883
Carlos Caballero4a050922020-07-02 11:43:38884### Task Reentrancy
Carlos Caballero40b6d042020-06-16 06:50:25885
886SequenceManager has task reentrancy protection. This means that if a
887task is being processed, a second task cannot start until the first task is
888finished. Reentrancy can happen when processing a task, and an inner
889message pump is created. That inner pump then processes native messages
890which could implicitly start an inner task. Inner message pumps are created
891with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
892(DoDragDrop), printer functions (StartDoc) and *many* others.
893
894```cpp
895Sample workaround when inner task processing is needed:
896 HRESULT hr;
897 {
Carlos Caballerob25fe8472020-07-17 10:27:17898 CurrentThread::ScopedNestableTaskAllower allow;
Carlos Caballero40b6d042020-06-16 06:50:25899 hr = DoDragDrop(...); // Implicitly runs a modal message loop.
900 }
901 // Process |hr| (the result returned by DoDragDrop()).
902```
903
904Please be SURE your task is reentrant (nestable) and all global variables
905are stable and accessible before before using
Carlos Caballerob25fe8472020-07-17 10:27:17906CurrentThread::ScopedNestableTaskAllower.
Carlos Caballero40b6d042020-06-16 06:50:25907
908## APIs for general use
909
910User code should hardly ever need to access SequenceManager APIs directly as
911these are meant for code that deals with scheduling. Instead you should use the
912following:
913
914* base::RunLoop: Drive the SequenceManager from the thread it's bound to.
915
916* base::Thread/SequencedTaskRunnerHandle: Post back to the SequenceManager TaskQueues from a task running on it.
917
918* SequenceLocalStorageSlot : Bind external state to a sequence.
919
Carlos Caballero4a050922020-07-02 11:43:38920* base::CurrentThread : Proxy to a subset of Task related APIs bound to the current thread
Carlos Caballero40b6d042020-06-16 06:50:25921
922* Embedders may provide their own static accessors to post tasks on specific loops (e.g. content::BrowserThreads).
923
924### SingleThreadTaskExecutor and TaskEnvironment
925
926Instead of having to deal with SequenceManager and TaskQueues code that needs a
927simple task posting environment (one default task queue) can use a
928[SingleThreadTaskExecutor](https://cs.chromium.org/chromium/src/base/task/single_thread_task_executor.h).
929
930Unit tests can use [TaskEnvironment](https://cs.chromium.org/chromium/src/base/test/task_environment.h)
931which is highly configurable.
Carlos Caballero4a050922020-07-02 11:43:38932
Wen Fane09439ca2021-03-09 16:50:41933## MessageLoop and MessageLoopCurrent
Carlos Caballero4a050922020-07-02 11:43:38934
Wen Fane09439ca2021-03-09 16:50:41935You might come across references to MessageLoop or MessageLoopCurrent in the
Carlos Caballero4a050922020-07-02 11:43:38936code or documentation. These classes no longer exist and we are in the process
Jared Saulea867ab2021-07-15 17:39:01937or getting rid of all references to them. `base::MessageLoopCurrent` was
938replaced by `base::CurrentThread` and the drop in replacements for
939`base::MessageLoop` are `base::SingleThreadTaskExecutor` and
940`base::Test::TaskEnvironment`.