| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 182 |
| 183 Usage of locks is discouraged in Chrome. Sequences inherently provide | 183 Usage of locks is discouraged in Chrome. Sequences inherently provide |
| 184 thread-safety. Prefer classes that are always accessed from the same sequence to | 184 thread-safety. Prefer classes that are always accessed from the same sequence to |
| 185 managing your own thread-safety with locks. | 185 managing your own thread-safety with locks. |
| 186 | 186 |
| 187 ```cpp | 187 ```cpp |
| 188 class A { | 188 class A { |
| 189 public: | 189 public: |
| 190 A() { | 190 A() { |
| 191 // Do not require accesses to be on the creation sequence. | 191 // Do not require accesses to be on the creation sequence. |
| 192 sequence_checker_.DetachFromSequence(); | 192 DETACH_FROM_SEQUENCE(sequence_checker_); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void AddValue(int v) { | 195 void AddValue(int v) { |
| 196 // Check that all accesses are on the same sequence. | 196 // Check that all accesses are on the same sequence. |
| 197 DCHECK(sequence_checker_.CalledOnValidSequence()); | 197 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 198 values_.push_back(v); | 198 values_.push_back(v); |
| 199 } | 199 } |
| 200 | 200 |
| 201 private: | 201 private: |
| 202 base::SequenceChecker sequence_checker_; | 202 SEQUENCE_CHECKER(sequence_checker_); |
| 203 | 203 |
| 204 // No lock required, because all accesses are on the | 204 // No lock required, because all accesses are on the |
| 205 // same sequence. | 205 // same sequence. |
| 206 std::vector<int> values_; | 206 std::vector<int> values_; |
| 207 }; | 207 }; |
| 208 | 208 |
| 209 A a; | 209 A a; |
| 210 scoped_refptr<SequencedTaskRunner> task_runner_for_a = ...; | 210 scoped_refptr<SequencedTaskRunner> task_runner_for_a = ...; |
| 211 task_runner->PostTask(FROM_HERE, | 211 task_runner->PostTask(FROM_HERE, |
| 212 base::BindOnce(&A::AddValue, base::Unretained(&a))); | 212 base::BindOnce(&A::AddValue, base::Unretained(&a))); |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 | 660 |
| 661 private: | 661 private: |
| 662 scoped_refptr<base::SequenceTaskRunner> background_task_runner_ = | 662 scoped_refptr<base::SequenceTaskRunner> background_task_runner_ = |
| 663 base::CreateSequenceTaskRunnerWithTraits( | 663 base::CreateSequenceTaskRunnerWithTraits( |
| 664 {base::MayBlock(), base::TaskPriority::BACKGROUND}); | 664 {base::MayBlock(), base::TaskPriority::BACKGROUND}); |
| 665 } | 665 } |
| 666 ``` | 666 ``` |
| 667 | 667 |
| 668 Note that this still allows removing all layers of plumbing between //chrome and | 668 Note that this still allows removing all layers of plumbing between //chrome and |
| 669 that component since unit tests will use the leaf layer directly. | 669 that component since unit tests will use the leaf layer directly. |
| OLD | NEW |