Commit d710e152 authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[TaskScheduler]: Split section on blocking tasks in faq

Question on tasks blocking forever contains answer that applies to the general
case. This CL adds a new question that answer the general case:
How to make a blocking call without affecting other tasks?

The question:
How to make a blocking call that may never return without affecting other tasks?
references the general case and confirms that it also works in that case.

Change-Id: I2237853301b946c661fcdc623188e26877633404
Reviewed-on: https://chromium-review.googlesource.com/c/1300033
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603109}
parent 16775a20
...@@ -36,12 +36,9 @@ dedicated thread. ...@@ -36,12 +36,9 @@ dedicated thread.
## Blocking off-CPU ## Blocking off-CPU
### How to make a call that may block off-CPU and never return? ### How to make a blocking call without preventing other tasks from being scheduled?
If you can't avoid making a call to a third-party library that may block off-CPU The steps depend on where the task runs (see [Where will a task run?](#On-what-thread-will-a-task-run_)).
and never return, you must take some steps to ensure that it doesn't prevent
other tasks from running. The steps depend on where the task runs (see [Where
will a task run?](#On-what-thread-will-a-task-run_)).
If the task runs in a thread pool: If the task runs in a thread pool:
...@@ -50,10 +47,7 @@ If the task runs in a thread pool: ...@@ -50,10 +47,7 @@ If the task runs in a thread pool:
after the annotated scope is entered, the capacity of the thread pool is after the annotated scope is entered, the capacity of the thread pool is
incremented. This ensures that your task doesn't reduce the number of tasks incremented. This ensures that your task doesn't reduce the number of tasks
that can run concurrently on the CPU. If the scope exits, the thread pool that can run concurrently on the CPU. If the scope exits, the thread pool
capacity goes back to normal. Since tasks posted to the same sequence can't capacity goes back to normal.
run concurrently, it is advisable to run tasks that may block indefinitely in
[parallel](threading_and_tasks.md#posting-a-parallel-task) rather than in
[sequence](threading_and_tasks.md#posting-a-sequenced-task).
If the task runs on the main thread, the IO thread or a `SHARED If the task runs on the main thread, the IO thread or a `SHARED
SingleThreadTaskRunner`: SingleThreadTaskRunner`:
...@@ -74,6 +68,19 @@ If the task runs on a `DEDICATED SingleThreadTaskRunner`: ...@@ -74,6 +68,19 @@ If the task runs on a `DEDICATED SingleThreadTaskRunner`:
explains the difference between `MAY_BLOCK ` and `WILL_BLOCK` and gives explains the difference between `MAY_BLOCK ` and `WILL_BLOCK` and gives
examples of off-CPU blocking operations. examples of off-CPU blocking operations.
### How to make a blocking call that may never return without preventing other tasks from being scheduled?
If you can't avoid making a call to a third-party library that may block off-
CPU, follow recommendations in [How to make a blocking call without affecting
other tasks?](#How-to-make-a-blocking-call-without-affecting-other-tasks_).
This ensures that a current task doesn't prevent other tasks from running even
if it never returns.
Since tasks posted to the same sequence can't run concurrently, it is advisable
to run tasks that may block indefinitely in
[parallel](threading_and_tasks.md#posting-a-parallel-task) rather than in
[sequence](threading_and_tasks.md#posting-a-sequenced-task).
### Do calls to blocking //base APIs need to be annotated with ScopedBlockingCall? ### Do calls to blocking //base APIs need to be annotated with ScopedBlockingCall?
No. All blocking //base APIs (e.g. base::ReadFileToString, base::File::Read, No. All blocking //base APIs (e.g. base::ReadFileToString, base::File::Read,
......
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