Commit 831c60b0 authored by Sami Kyostila's avatar Sami Kyostila Committed by Commit Bot

docs: Remove "WithTraits" suffix from task posting documentation

Bug: 968047
Change-Id: Ibae113c025e4db430980c75932b8625657d096ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1728564
Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avatarAlex Clarke <alexclarke@chromium.org>
Commit-Queue: Sami Kyöstilä <skyostil@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682692}
parent 99fe1831
...@@ -179,12 +179,11 @@ base::PostTask(FROM_HERE, base::BindOnce(&Task)); ...@@ -179,12 +179,11 @@ base::PostTask(FROM_HERE, base::BindOnce(&Task));
This posts tasks with default traits. This posts tasks with default traits.
The `base::PostTask*WithTraits()` functions allow the caller to provide The `base::PostTask*()` functions allow the caller to provide additional details
additional details about the task via TaskTraits (ref. about the task via TaskTraits (ref. [Annotating Tasks with TaskTraits](#Annotating-Tasks-with-TaskTraits)).
[Annotating Tasks with TaskTraits](#Annotating-Tasks-with-TaskTraits)).
```cpp ```cpp
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {base::TaskPriority::BEST_EFFORT, MayBlock()}, FROM_HERE, {base::TaskPriority::BEST_EFFORT, MayBlock()},
base::BindOnce(&Task)); base::BindOnce(&Task));
``` ```
...@@ -214,7 +213,7 @@ class A { ...@@ -214,7 +213,7 @@ class A {
private: private:
scoped_refptr<base::TaskRunner> task_runner_ = scoped_refptr<base::TaskRunner> task_runner_ =
base::CreateTaskRunnerWithTraits({base::TaskPriority::USER_VISIBLE}); base::CreateTaskRunner({base::TaskPriority::USER_VISIBLE});
}; };
``` ```
...@@ -231,11 +230,11 @@ necessarily on the same thread). To post tasks as part of a sequence, use a ...@@ -231,11 +230,11 @@ necessarily on the same thread). To post tasks as part of a sequence, use a
### Posting to a New Sequence ### Posting to a New Sequence
A `base::SequencedTaskRunner` can be created by A `base::SequencedTaskRunner` can be created by
`base::CreateSequencedTaskRunnerWithTraits()`. `base::CreateSequencedTaskRunner()`.
```cpp ```cpp
scoped_refptr<SequencedTaskRunner> sequenced_task_runner = scoped_refptr<SequencedTaskRunner> sequenced_task_runner =
base::CreateSequencedTaskRunnerWithTraits(...); base::CreateSequencedTaskRunner(...);
// TaskB runs after TaskA completes. // TaskB runs after TaskA completes.
sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA)); sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
...@@ -337,15 +336,15 @@ posting order. ...@@ -337,15 +336,15 @@ posting order.
### Posting to the Main Thread or to the IO Thread in the Browser Process ### Posting to the Main Thread or to the IO Thread in the Browser Process
To post tasks to the main thread or to the IO thread, use To post tasks to the main thread or to the IO thread, use
`base::PostTaskWithTraits()` or get the appropriate SingleThreadTaskRunner using `base::PostTask()` or get the appropriate SingleThreadTaskRunner using
`base::CreateSingleThreadTaskRunnerWithTraits`, supplying a `BrowserThread::ID` `base::CreateSingleThreadTaskRunner`, supplying a `BrowserThread::ID`
as trait. For this, you'll also need to include as trait. For this, you'll also need to include
[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h). [`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h).
```cpp ```cpp
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI}, ...); base::PostTask(FROM_HERE, {content::BrowserThread::UI}, ...);
base::CreateSingleThreadTaskRunnerWithTraits({content::BrowserThread::IO}) base::CreateSingleThreadTaskRunner({content::BrowserThread::IO})
->PostTask(FROM_HERE, ...); ->PostTask(FROM_HERE, ...);
``` ```
...@@ -366,11 +365,11 @@ TODO ...@@ -366,11 +365,11 @@ TODO
If multiple tasks need to run on the same thread and that thread doesn’t have to If multiple tasks need to run on the same thread and that thread doesn’t have to
be the main thread or the IO thread, post them to a `base::SingleThreadTaskRunner` be the main thread or the IO thread, post them to a `base::SingleThreadTaskRunner`
created by `base::CreateSingleThreadTaskRunnerWithTraits`. created by `base::CreateSingleThreadTaskRunner`.
```cpp ```cpp
scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner = scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner =
base::CreateSingleThreadTaskRunnerWithTraits(...); base::CreateSingleThreadTaskRunner(...);
// TaskB runs after TaskA completes. Both tasks run on the same thread. // TaskB runs after TaskA completes. Both tasks run on the same thread.
single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA)); single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
...@@ -412,10 +411,9 @@ or a sequenced task. ...@@ -412,10 +411,9 @@ or a sequenced task.
Tasks that need to run on a COM Single-Thread Apartment (STA) thread must be Tasks that need to run on a COM Single-Thread Apartment (STA) thread must be
posted to a `base::SingleThreadTaskRunner` returned by posted to a `base::SingleThreadTaskRunner` returned by
`base::CreateCOMSTATaskRunnerWithTraits()`. As mentioned in [Posting Multiple `base::CreateCOMSTATaskRunner()`. As mentioned in [Posting Multiple Tasks to the
Tasks to the Same Thread](#Posting-Multiple-Tasks-to-the-Same-Thread), all tasks Same Thread](#Posting-Multiple-Tasks-to-the-Same-Thread), all tasks posted to
posted to the same `base::SingleThreadTaskRunner` run on the same thread in the same `base::SingleThreadTaskRunner` run on the same thread in posting order.
posting order.
```cpp ```cpp
// Task(A|B|C)UsingCOMSTA will run on the same COM STA thread. // Task(A|B|C)UsingCOMSTA will run on the same COM STA thread.
...@@ -433,7 +431,7 @@ void TaskAUsingCOMSTA() { ...@@ -433,7 +431,7 @@ void TaskAUsingCOMSTA() {
void TaskBUsingCOMSTA() { } void TaskBUsingCOMSTA() { }
void TaskCUsingCOMSTA() { } void TaskCUsingCOMSTA() { }
auto com_sta_task_runner = base::CreateCOMSTATaskRunnerWithTraits(...); auto com_sta_task_runner = base::CreateCOMSTATaskRunner(...);
com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskAUsingCOMSTA)); com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskAUsingCOMSTA));
com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskBUsingCOMSTA)); com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskBUsingCOMSTA));
``` ```
...@@ -472,24 +470,24 @@ base::PostTask(FROM_HERE, base::BindOnce(...)); ...@@ -472,24 +470,24 @@ base::PostTask(FROM_HERE, base::BindOnce(...));
// This task has the highest priority. The thread pool will try to // This task has the highest priority. The thread pool will try to
// run it before USER_VISIBLE and BEST_EFFORT tasks. // run it before USER_VISIBLE and BEST_EFFORT tasks.
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {base::TaskPriority::USER_BLOCKING}, FROM_HERE, {base::TaskPriority::USER_BLOCKING},
base::BindOnce(...)); base::BindOnce(...));
// This task has the lowest priority and is allowed to block (e.g. it // This task has the lowest priority and is allowed to block (e.g. it
// can read a file from disk). // can read a file from disk).
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()}, FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
base::BindOnce(...)); base::BindOnce(...));
// This task blocks shutdown. The process won't exit before its // This task blocks shutdown. The process won't exit before its
// execution is complete. // execution is complete.
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
base::BindOnce(...)); base::BindOnce(...));
// This task will run on the Browser UI thread. // This task will run on the Browser UI thread.
base::PostTaskWithTraits( base::PostTask(
FROM_HERE, {content::BrowserThread::UI}, FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(...)); base::BindOnce(...));
``` ```
...@@ -519,7 +517,7 @@ this case). The return value of the first call is automatically provided as ...@@ -519,7 +517,7 @@ this case). The return value of the first call is automatically provided as
argument to the second call. argument to the second call.
```cpp ```cpp
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()}, FROM_HERE, {base::MayBlock()},
base::BindOnce(&GetHistoryItemsFromDisk, "keyword"), base::BindOnce(&GetHistoryItemsFromDisk, "keyword"),
base::BindOnce(&AddHistoryItemsToOmniboxDropdown)); base::BindOnce(&AddHistoryItemsToOmniboxDropdown));
...@@ -533,12 +531,12 @@ To post a task that must run once after a delay expires, use ...@@ -533,12 +531,12 @@ To post a task that must run once after a delay expires, use
`base::PostDelayedTask*()` or `base::TaskRunner::PostDelayedTask()`. `base::PostDelayedTask*()` or `base::TaskRunner::PostDelayedTask()`.
```cpp ```cpp
base::PostDelayedTaskWithTraits( base::PostDelayedTask(
FROM_HERE, {base::TaskPriority::BEST_EFFORT}, base::BindOnce(&Task), FROM_HERE, {base::TaskPriority::BEST_EFFORT}, base::BindOnce(&Task),
base::TimeDelta::FromHours(1)); base::TimeDelta::FromHours(1));
scoped_refptr<base::SequencedTaskRunner> task_runner = scoped_refptr<base::SequencedTaskRunner> task_runner =
base::CreateSequencedTaskRunnerWithTraits({base::TaskPriority::BEST_EFFORT}); base::CreateSequencedTaskRunner({base::TaskPriority::BEST_EFFORT});
task_runner->PostDelayedTask( task_runner->PostDelayedTask(
FROM_HERE, base::BindOnce(&Task), base::TimeDelta::FromHours(1)); FROM_HERE, base::BindOnce(&Task), base::TimeDelta::FromHours(1));
``` ```
...@@ -617,7 +615,7 @@ tasks run. Keep in mind that `CancelableTaskTracker` cannot cancel tasks that ...@@ -617,7 +615,7 @@ tasks run. Keep in mind that `CancelableTaskTracker` cannot cancel tasks that
have already started to run. have already started to run.
```cpp ```cpp
auto task_runner = base::CreateTaskRunnerWithTraits(base::TaskTraits()); auto task_runner = base::CreateTaskRunner({base::ThreadPool()});
base::CancelableTaskTracker cancelable_task_tracker; base::CancelableTaskTracker cancelable_task_tracker;
cancelable_task_tracker.PostTask(task_runner.get(), FROM_HERE, cancelable_task_tracker.PostTask(task_runner.get(), FROM_HERE,
base::DoNothing()); base::DoNothing());
...@@ -677,16 +675,16 @@ TEST(MyTest, MyTest) { ...@@ -677,16 +675,16 @@ TEST(MyTest, MyTest) {
// D and run_loop.QuitClosure() have been executed. E is still in the queue. // D and run_loop.QuitClosure() have been executed. E is still in the queue.
// Tasks posted to thread pool run asynchronously as they are posted. // Tasks posted to thread pool run asynchronously as they are posted.
base::PostTaskWithTraits(FROM_HERE, base::TaskTraits(), base::BindOnce(&F)); base::PostTask(FROM_HERE, {base::ThreadPool()}, base::BindOnce(&F));
auto task_runner = auto task_runner =
base::CreateSequencedTaskRunnerWithTraits(base::TaskTraits()); base::CreateSequencedTaskRunner({base::ThreadPool()});
task_runner->PostTask(FROM_HERE, base::BindOnce(&G)); task_runner->PostTask(FROM_HERE, base::BindOnce(&G));
// To block until all tasks posted to thread pool are done running: // To block until all tasks posted to thread pool are done running:
base::ThreadPoolInstance::Get()->FlushForTesting(); base::ThreadPoolInstance::Get()->FlushForTesting();
// F and G have been executed. // F and G have been executed.
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskAndReplyWithResult(
FROM_HERE, base::TaskTrait(), FROM_HERE, base::TaskTrait(),
base::BindOnce(&H), base::BindOnce(&I)); base::BindOnce(&H), base::BindOnce(&I));
...@@ -762,7 +760,7 @@ class Foo { ...@@ -762,7 +760,7 @@ class Foo {
private: private:
scoped_refptr<base::SequencedTaskRunner> background_task_runner_ = scoped_refptr<base::SequencedTaskRunner> background_task_runner_ =
base::CreateSequencedTaskRunnerWithTraits( base::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT}); {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
} }
``` ```
......
...@@ -19,7 +19,7 @@ A task is posted through the `base/task/post_task.h` API with `TaskTraits`. ...@@ -19,7 +19,7 @@ A task is posted through the `base/task/post_task.h` API with `TaskTraits`.
* If `TaskTraits` don't contain `BrowserThread::UI/IO`: * If `TaskTraits` don't contain `BrowserThread::UI/IO`:
* If the task is posted through a `SingleThreadTaskRunner` obtained from * If the task is posted through a `SingleThreadTaskRunner` obtained from
`CreateSingleThreadTaskRunnerWithTraits(..., mode)`: `CreateSingleThreadTaskRunner(..., mode)`:
* Where `mode` is `SingleThreadTaskRunnerThreadMode::DEDICATED`: * Where `mode` is `SingleThreadTaskRunnerThreadMode::DEDICATED`:
* The task runs on a thread that only runs tasks from that * The task runs on a thread that only runs tasks from that
SingleThreadTaskRunner. This is not the main thread nor the IO SingleThreadTaskRunner. This is not the main thread nor the IO
...@@ -155,15 +155,15 @@ The following mappings can be useful when migrating code from a ...@@ -155,15 +155,15 @@ The following mappings can be useful when migrating code from a
* base::ThreadLocalStorage::Slot -> base::SequenceLocalStorageSlot * base::ThreadLocalStorage::Slot -> base::SequenceLocalStorageSlot
* BrowserThread::DeleteOnThread -> base::OnTaskRunnerDeleter / base::RefCountedDeleteOnSequence * BrowserThread::DeleteOnThread -> base::OnTaskRunnerDeleter / base::RefCountedDeleteOnSequence
* BrowserMessageFilter::OverrideThreadForMessage() -> BrowserMessageFilter::OverrideTaskRunnerForMessage() * BrowserMessageFilter::OverrideThreadForMessage() -> BrowserMessageFilter::OverrideTaskRunnerForMessage()
* CreateSingleThreadTaskRunnerWithTraits() -> CreateSequencedTaskRunnerWithTraits() * CreateSingleThreadTaskRunner() -> CreateSequencedTaskRunner()
* Every CreateSingleThreadTaskRunnerWithTraits() usage, outside of * Every CreateSingleThreadTaskRunner() usage, outside of
BrowserThread::UI/IO, should be accompanied with a comment and ideally a BrowserThread::UI/IO, should be accompanied with a comment and ideally a
bug to make it sequence when the sequence-unfriendly dependency is bug to make it sequence when the sequence-unfriendly dependency is
addressed. addressed.
### How to ensure mutual exclusion between tasks posted by a component? ### How to ensure mutual exclusion between tasks posted by a component?
Create a `SequencedTaskRunner` using `CreateSequencedTaskRunnerWithTraits()` and Create a `SequencedTaskRunner` using `CreateSequencedTaskRunner()` and
store it on an object that can be accessed from all the PostTask() call sites store it on an object that can be accessed from all the PostTask() call sites
that require mutual exclusion. If there isn't a shared object that can own a that require mutual exclusion. If there isn't a shared object that can own a
common `SequencedTaskRunner`, use common `SequencedTaskRunner`, use
...@@ -190,7 +190,7 @@ for all tasks to run. ...@@ -190,7 +190,7 @@ for all tasks to run.
int g_condition = false; int g_condition = false;
base::RunLoop run_loop; base::RunLoop run_loop;
base::PostTaskWithTraits(FROM_HERE, {}, base::BindOnce( base::PostTask(FROM_HERE, {}, base::BindOnce(
[] (base::OnceClosure closure) { [] (base::OnceClosure closure) {
g_condition = true; g_condition = true;
std::move(quit_closure).Run(); std::move(quit_closure).Run();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment