PSA : Sequencification and PostTask paradigm update

138 views
Skip to first unread message

Gabriel Charette

unread,
Jun 6, 2017, 1:02:12 PM6/6/17
to Chromium-dev, Francois Pierre Doray, Robert Liao
Hello all,

tl;dr; you should now prefer SequencedTaskRunners obtained from base/task_scheduler/post_task.h to run tasks off the UI/IO threads in a thread-safe fashion.

Chromium has evolved from an initial dedicated thread (BrowserThread::FILE, etc.). As such our codebase has many single-thread constructs (SingleThreadTaskRunner, ThreadTaskRunnerHandle, ThreadChecker, etc.) in classes that are merely thread-unsafe but not thread-affine per se.

SequencedTaskRunners provide thread-safety as well and are much prefered to SingleThreadTaskRunners as they provide more flexibility for scheduling.

Over the last few months we made many core APIs sequence-friendly.

We just made a scripted pass over the codebase to migrate away from base::NonThreadSafe (gone gone!) to SequenceChecker. It turned out that < 5% of the code truly needed thread-affinity (ThreadChecker).

As you start using SequencedTaskRunner, either in new components or replacing existing SingleThreadTaskRunners, if you run into DCHECKs in any leaf dependency (outside of your component) per it using ThreadChecker/ThreadTaskRunnerHandle/etc. : you may ideally fix those components (they're not typically thread-affine unless using TLS or third-party APIs that are affine) or, alternatively, you can temporarily use base::CreateSingleThreadTaskRunnerWithTraits(...) with a TODO tagged with a bug # marked as a blocker for https://crbug.com/675631 (one of us will take a look at it and flip your code to use a SequencedTaskRunner when fixed).

PS: We automated much of the migration to TaskScheduler away from old APIs but are getting to the harder cases (requiring manual steps). We will soon reach out to individual owners with precise migration docs to complete this on a component-by-component basis.

Thanks!
Gab on behalf of the TaskScheduler team.

Gabriel Charette

unread,
Jun 6, 2017, 1:06:48 PM6/6/17
to Gabriel Charette, Chromium-dev, Francois Pierre Doray, Robert Liao
Oh and I forgot the most important part... we have brand new documentation for threading/tasks in Chromium :) : https://chromium.googlesource.com/chromium/src/+/master/docs/threading_and_tasks.md

Marijn Kruisselbrink

unread,
Jun 6, 2017, 1:35:06 PM6/6/17
to Gabriel Charette, Chromium-dev, Francois Pierre Doray, Robert Liao
Keep in mind that if you're using mojo in any way (either implementing a mojo interface, or connection/communication to a mojo interface) you still won't be able to use SequencedTaskRunners until http://crbug.com/678155 is fixed. Which I imagine excludes a large part of the code base, especially as more and more code is migrated to mojo...

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAJTZ7LLy9OakNny0TK5bSJe9MPecB5ijhwOnHqgBSArtsWTrSg%40mail.gmail.com.

Gabriel Charette

unread,
Jun 6, 2017, 1:50:03 PM6/6/17
to Marijn Kruisselbrink, Gabriel Charette, Chromium-dev, Francois Pierre Doray, Robert Liao
On Tue, Jun 6, 2017 at 1:33 PM Marijn Kruisselbrink <m...@chromium.org> wrote:
Keep in mind that if you're using mojo in any way (either implementing a mojo interface, or connection/communication to a mojo interface) you still won't be able to use SequencedTaskRunners until http://crbug.com/678155 is fixed. Which I imagine excludes a large part of the code base, especially as more and more code is migrated to mojo...

True but the same rules apply (except in this case you have an existing bug to use to tag your use of base::CreateSingleThreadTaskRunnerWithTraits()'s TODO).

PS: We are in the process of implementing SequenceLocalStorage as of this writing. It's the last requirement to allow Mojo (and most Chromium code that uses TLS) to become sequence-friendly :).

James Cook

unread,
Jun 7, 2017, 12:59:02 PM6/7/17
to Gabriel Charette, Marijn Kruisselbrink, Chromium-dev, Francois Pierre Doray, Robert Liao
These docs are awesome!

One request: Could you add a sentence or two about *why* we are doing all this thread / sequence stuff? I presume it's so we can scale the number of threads based on CPU hardware and OS, and so the scheduler can be more clever about keeping all of them busy.

James

Gabriel Charette

unread,
Jun 8, 2017, 1:20:44 PM6/8/17
to James Cook, Gabriel Charette, Marijn Kruisselbrink, Chromium-dev, Francois Pierre Doray, Robert Liao
On Wed, Jun 7, 2017 at 12:58 PM James Cook <jame...@chromium.org> wrote:
These docs are awesome!

One request: Could you add a sentence or two about *why* we are doing all this thread / sequence stuff? I presume it's so we can scale the number of threads based on CPU hardware and OS, and so the scheduler can be more clever about keeping all of them busy.

Reply all
Reply to author
Forward
0 new messages