OLD | NEW |
1 # Threading and Tasks in Chrome | 1 # Threading and Tasks in Chrome |
2 | 2 |
3 [TOC] | 3 [TOC] |
4 | 4 |
5 ## Overview | 5 ## Overview |
6 | 6 |
7 ### Threads | 7 ### Threads |
8 | 8 |
9 Every Chrome process has | 9 Every Chrome process has |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 * [Parallel](#Posting-a-Parallel-Task): No task execution ordering, possibly all | 42 * [Parallel](#Posting-a-Parallel-Task): No task execution ordering, possibly all |
43 at once on any thread | 43 at once on any thread |
44 * [Sequenced](#Posting-a-Sequenced-Task): Tasks executed in posting order, one | 44 * [Sequenced](#Posting-a-Sequenced-Task): Tasks executed in posting order, one |
45 at a time on any thread. | 45 at a time on any thread. |
46 * [Single Threaded](#Posting-Multiple-Tasks-to-the-Same-Thread): Tasks executed | 46 * [Single Threaded](#Posting-Multiple-Tasks-to-the-Same-Thread): Tasks executed |
47 in posting order, one at a time on a single thread. | 47 in posting order, one at a time on a single thread. |
48 * [COM Single Threaded](#Posting-Tasks-to-a-COM-Single-Thread-Apartment-STA_T
hread-Windows_): | 48 * [COM Single Threaded](#Posting-Tasks-to-a-COM-Single-Thread-Apartment-STA_T
hread-Windows_): |
49 A variant of single threaded with COM initialized. | 49 A variant of single threaded with COM initialized. |
50 | 50 |
51 The discussion below covers all of these ways to execute tasks. | 51 ### Prefer Sequences to Threads |
| 52 |
| 53 **Sequenced execution mode is far prefered to Single Threaded** in scenarios |
| 54 that require mere thread-safety as it opens up scheduling paradigms that |
| 55 wouldn't be possible otherwise (sequences can hop threads instead of being stuck |
| 56 behind unrelated work on a dedicated thread). Ability to hop threads also means |
| 57 the thread count can dynamically adapt to the machine's true resource |
| 58 availability (faster on bigger machines, avoids trashing on slower machines). |
| 59 |
| 60 Many core APIs were recently made sequence-friendly (classes are rarely |
| 61 thread-affine -- i.e. only when using thread-local-storage or third-party APIs |
| 62 that do). But the codebase has long evolved assuming single-threaded contexts... |
| 63 If your class could run on a sequence but is blocked by an overzealous use of |
| 64 ThreadChecker/ThreadTaskRunnerHandle/SingleThreadTaskRunner in a leaf |
| 65 dependency, consider fixing that dependency for everyone's benefit (or at the |
| 66 very least file a blocking bug against https://crbug.com/675631 and flag your |
| 67 use of base::CreateSingleThreadTaskRunnerWithTraits() with a TODO against your |
| 68 bug to use base::CreateSequencedTaskRunnerWithTraits() when fixed). |
| 69 |
| 70 The discussion below covers all of these ways to execute tasks in details. |
52 | 71 |
53 ## Posting a Parallel Task | 72 ## Posting a Parallel Task |
54 | 73 |
55 ### Direct Posting to the Task Scheduler | 74 ### Direct Posting to the Task Scheduler |
56 | 75 |
57 A task that can run on any thread and doesn’t have ordering or mutual exclusion | 76 A task that can run on any thread and doesn’t have ordering or mutual exclusion |
58 requirements with other tasks should be posted using one of the | 77 requirements with other tasks should be posted using one of the |
59 `base::PostTask*()` functions defined in | 78 `base::PostTask*()` functions defined in |
60 [`base/task_scheduler/post_task.h`](https://cs.chromium.org/chromium/src/base/ta
sk_scheduler/post_task.h). | 79 [`base/task_scheduler/post_task.h`](https://cs.chromium.org/chromium/src/base/ta
sk_scheduler/post_task.h). |
61 | 80 |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 ## TaskRunner ownership | 636 ## TaskRunner ownership |
618 | 637 |
619 TaskRunners shouldn't be passed through several components. Instead, the | 638 TaskRunners shouldn't be passed through several components. Instead, the |
620 components that uses a TaskRunner should be the one that creates it. | 639 components that uses a TaskRunner should be the one that creates it. |
621 | 640 |
622 See [this example](https://codereview.chromium.org/2885173002/) of a | 641 See [this example](https://codereview.chromium.org/2885173002/) of a |
623 refactoring where a TaskRunner was passed through a lot of components only to be | 642 refactoring where a TaskRunner was passed through a lot of components only to be |
624 used in an eventual leaf. The leaf can and should now obtain its TaskRunner | 643 used in an eventual leaf. The leaf can and should now obtain its TaskRunner |
625 directly from | 644 directly from |
626 [`base/task_scheduler/post_task.h`](https://cs.chromium.org/chromium/src/base/ta
sk_scheduler/post_task.h). | 645 [`base/task_scheduler/post_task.h`](https://cs.chromium.org/chromium/src/base/ta
sk_scheduler/post_task.h). |
OLD | NEW |