Commit f62ff03c authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[base] Make LazyTaskRunner ThreadPool specific

Users with BrowserThread traits can just use the getter to get a new
pointer to the existing task runner each time (the BrowserThread API
will soon move to content::GetUIThreadTaskRunner() and this call will
make even more sense then the current one using "create" semantics).

This is necessary in order to rename LazyTaskRunner to
LazyThreadPoolTaskRunner and having it automatically call into the
upcoming base::ThreadPool:: APIs (destination as an API rather than as
a trait).

See Task Posting APIs v3 for details:
https://docs.google.com/document/d/1tssusPykvx3g0gvbvU4HxGyn3MjJlIylnsH13-Tv6s4/edit

R=fdoray@chromium.org

Bug: 1026641
Change-Id: I03e84ad5f607c93c4e2d649820954f0a4b08df56
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1988682
Auto-Submit: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarPatrick Monette <pmonette@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#729940}
parent 2adc4f76
...@@ -22,12 +22,17 @@ ...@@ -22,12 +22,17 @@
// //
// Lazy(Sequenced|SingleThread|COMSTA)TaskRunner is meant to be instantiated in // Lazy(Sequenced|SingleThread|COMSTA)TaskRunner is meant to be instantiated in
// an anonymous namespace (no static initializer is generated) and used to post // an anonymous namespace (no static initializer is generated) and used to post
// tasks to the same sequence/thread from pieces of code that don't have a // tasks to the same thread-pool-bound sequence/thread from pieces of code that
// better way of sharing a TaskRunner. It is important to use this class // don't have a better way of sharing a TaskRunner. It is important to use this
// instead of a self-managed global variable or LazyInstance so that the // class instead of a self-managed global variable or LazyInstance so that the
// TaskRunners do not outlive the scope of the TaskEnvironment in unit // TaskRunners do not outlive the scope of the TaskEnvironment in unit tests
// tests (otherwise the next test in the same process will die in use-after- // (otherwise the next test in the same process will die in use-after-frees).
// frees). //
// Note: This is only meant for base::ThreadPool bound task runners. Task
// runners bound to other destination which share an existing sequence (like
// BrowserThreads) should just use the appropriate getter each time (e.g.
// base::Create*TaskRunner({BrowserThread::UI})).
// TODO(1026641): Rename this API to LazyThreadPoolTaskRunner.
// //
// IMPORTANT: Only use this API as a last resort. Prefer storing a // IMPORTANT: Only use this API as a last resort. Prefer storing a
// (Sequenced|SingleThread)TaskRunner returned by // (Sequenced|SingleThread)TaskRunner returned by
...@@ -96,6 +101,7 @@ using LazyCOMSTATaskRunner = ...@@ -96,6 +101,7 @@ using LazyCOMSTATaskRunner =
// |traits| are TaskTraits used when creating the SequencedTaskRunner. // |traits| are TaskTraits used when creating the SequencedTaskRunner.
#define LAZY_SEQUENCED_TASK_RUNNER_INITIALIZER(traits) \ #define LAZY_SEQUENCED_TASK_RUNNER_INITIALIZER(traits) \
base::LazySequencedTaskRunner::CreateInternal(traits); \ base::LazySequencedTaskRunner::CreateInternal(traits); \
static_assert(traits.use_thread_pool(), ""); \
ALLOW_UNUSED_TYPE constexpr base::TaskTraits \ ALLOW_UNUSED_TYPE constexpr base::TaskTraits \
LAZY_TASK_RUNNER_CONCATENATE_INTERNAL(kVerifyTraitsAreConstexpr, \ LAZY_TASK_RUNNER_CONCATENATE_INTERNAL(kVerifyTraitsAreConstexpr, \
__LINE__) = traits __LINE__) = traits
...@@ -105,6 +111,7 @@ using LazyCOMSTATaskRunner = ...@@ -105,6 +111,7 @@ using LazyCOMSTATaskRunner =
// thread with other SingleThreadTaskRunners. // thread with other SingleThreadTaskRunners.
#define LAZY_SINGLE_THREAD_TASK_RUNNER_INITIALIZER(traits, thread_mode) \ #define LAZY_SINGLE_THREAD_TASK_RUNNER_INITIALIZER(traits, thread_mode) \
base::LazySingleThreadTaskRunner::CreateInternal(traits, thread_mode); \ base::LazySingleThreadTaskRunner::CreateInternal(traits, thread_mode); \
static_assert(traits.use_thread_pool(), ""); \
ALLOW_UNUSED_TYPE constexpr base::TaskTraits \ ALLOW_UNUSED_TYPE constexpr base::TaskTraits \
LAZY_TASK_RUNNER_CONCATENATE_INTERNAL(kVerifyTraitsAreConstexpr, \ LAZY_TASK_RUNNER_CONCATENATE_INTERNAL(kVerifyTraitsAreConstexpr, \
__LINE__) = traits; \ __LINE__) = traits; \
...@@ -118,6 +125,7 @@ using LazyCOMSTATaskRunner = ...@@ -118,6 +125,7 @@ using LazyCOMSTATaskRunner =
// SingleThreadTaskRunners. // SingleThreadTaskRunners.
#define LAZY_COM_STA_TASK_RUNNER_INITIALIZER(traits, thread_mode) \ #define LAZY_COM_STA_TASK_RUNNER_INITIALIZER(traits, thread_mode) \
base::LazyCOMSTATaskRunner::CreateInternal(traits, thread_mode); \ base::LazyCOMSTATaskRunner::CreateInternal(traits, thread_mode); \
static_assert(traits.use_thread_pool(), ""); \
ALLOW_UNUSED_TYPE constexpr base::TaskTraits \ ALLOW_UNUSED_TYPE constexpr base::TaskTraits \
LAZY_TASK_RUNNER_CONCATENATE_INTERNAL(kVerifyTraitsAreConstexpr, \ LAZY_TASK_RUNNER_CONCATENATE_INTERNAL(kVerifyTraitsAreConstexpr, \
__LINE__) = traits; \ __LINE__) = traits; \
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "chrome/browser/win/conflicts/module_database_observer.h" #include "chrome/browser/win/conflicts/module_database_observer.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#if defined(GOOGLE_CHROME_BUILD) #if defined(GOOGLE_CHROME_BUILD)
#include "base/feature_list.h" #include "base/feature_list.h"
...@@ -111,17 +112,17 @@ scoped_refptr<base::SequencedTaskRunner> ModuleDatabase::GetTaskRunner() { ...@@ -111,17 +112,17 @@ scoped_refptr<base::SequencedTaskRunner> ModuleDatabase::GetTaskRunner() {
static constexpr base::Feature kDistinctModuleDatabaseSequence{ static constexpr base::Feature kDistinctModuleDatabaseSequence{
"DistinctModuleDatabaseSequence", base::FEATURE_ENABLED_BY_DEFAULT}; "DistinctModuleDatabaseSequence", base::FEATURE_ENABLED_BY_DEFAULT};
static base::LazySequencedTaskRunner g_ui_task_runner =
LAZY_SEQUENCED_TASK_RUNNER_INITIALIZER(
base::TaskTraits(content::BrowserThread::UI));
static base::LazySequencedTaskRunner g_distinct_task_runner = static base::LazySequencedTaskRunner g_distinct_task_runner =
LAZY_SEQUENCED_TASK_RUNNER_INITIALIZER( LAZY_SEQUENCED_TASK_RUNNER_INITIALIZER(
base::TaskTraits(base::ThreadPool(), base::TaskPriority::BEST_EFFORT, base::TaskTraits(base::ThreadPool(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN));
// A new task runner to the UI thread can be "created" every time in the
// disabled arm, in practice it's always the same task runner (it doesn't need
// a lazy instance to a privately owned sequence).
return base::FeatureList::IsEnabled(kDistinctModuleDatabaseSequence) return base::FeatureList::IsEnabled(kDistinctModuleDatabaseSequence)
? g_distinct_task_runner.Get() ? g_distinct_task_runner.Get()
: g_ui_task_runner.Get(); : base::CreateSequencedTaskRunner({content::BrowserThread::UI});
} }
// static // static
......
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