Chromium Code Reviews| Index: base/task_scheduler/task_scheduler_impl.cc |
| diff --git a/base/task_scheduler/task_scheduler_impl.cc b/base/task_scheduler/task_scheduler_impl.cc |
| index e5c8abf716306f8602b0cde8786e10dc292f1739..64da1b77e045188a07ed1ece0c7504395310db97 100644 |
| --- a/base/task_scheduler/task_scheduler_impl.cc |
| +++ b/base/task_scheduler/task_scheduler_impl.cc |
| @@ -24,6 +24,52 @@ |
| namespace base { |
| namespace internal { |
| +namespace { |
| + |
| +enum EnvironmentType { |
| + BACKGROUND = 0, |
| + BACKGROUND_BLOCKING, |
| + FOREGROUND, |
| + FOREGROUND_BLOCKING, |
| + ENVIRONMENT_COUNT // Always last. |
| +}; |
| + |
| +// Order must match the EnvironmentType enum. |
| +constexpr struct { |
| + // The threads and histograms of this environment will be labeled with |
| + // task scheduler name + |name_suffix|. |
|
robliao
2017/03/23 18:00:52
Might be clearer to say the name from InitParams +
fdoray
2017/03/24 14:00:38
We don't use the name from InitParams, we use the
|
| + const char* name_suffix; |
| + |
| + // Preferred priority for threads in this environment; the actual thread |
| + // priority depends on shutdown state and platform capabilities. |
| + ThreadPriority priority_hint; |
| + |
| + // Offset of the SchedulerWorkerPoolParams corresponding to this environement |
| + // in TaskSchedulerInitParams. |
|
robliao
2017/03/23 18:00:52
Worth discussing that this allows us to easily use
fdoray
2017/03/24 14:00:38
Done.
|
| + size_t offset; |
| +} kEnvironmentParams[] = { |
| + {"Background", base::ThreadPriority::BACKGROUND, |
| + offsetof(TaskScheduler::InitParams, background_worker_pool_params)}, |
| + {"BackgroundBlocking", base::ThreadPriority::BACKGROUND, |
| + offsetof(TaskScheduler::InitParams, |
| + background_blocking_worker_pool_params)}, |
| + {"Foreground", base::ThreadPriority::NORMAL, |
| + offsetof(TaskScheduler::InitParams, foreground_worker_pool_params)}, |
| + {"ForegroundBlocking", base::ThreadPriority::NORMAL, |
| + offsetof(TaskScheduler::InitParams, |
| + foreground_blocking_worker_pool_params)}, |
| +}; |
| + |
| +size_t GetEnvironmentIndexForTraits(const TaskTraits& traits) { |
| + const bool is_background = |
| + traits.priority() == base::TaskPriority::BACKGROUND; |
| + if (traits.may_block() || traits.with_base_sync_primitives()) |
| + return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; |
| + return is_background ? BACKGROUND : FOREGROUND; |
| +} |
| + |
| +} // namespace |
| + |
| // static |
| std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
| const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector, |
| @@ -35,6 +81,31 @@ std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
| return scheduler; |
| } |
| +// static |
| +std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( |
| + const std::string& name, |
| + const TaskScheduler::InitParams& init_params) { |
| + // Create a vector of SchedulerWorkerPoolParams using names and priority hints |
| + // derived from |kEnvironmentParams| and other params from |init_params|. |
| + std::vector<SchedulerWorkerPoolParams> worker_pool_params_vector; |
| + for (const auto& environment_params : kEnvironmentParams) { |
| + const SchedulerWorkerPoolParams& worker_pool_params_in = |
| + *reinterpret_cast<const SchedulerWorkerPoolParams*>( |
| + reinterpret_cast<const char*>(&init_params) + |
|
robliao
2017/03/23 18:00:52
Instead of const char*, should this be uintptr_t i
fdoray
2017/03/24 14:00:38
Done.
|
| + environment_params.offset); |
| + worker_pool_params_vector.emplace_back( |
| + name + environment_params.name_suffix, environment_params.priority_hint, |
| + worker_pool_params_in.standby_thread_policy(), |
| + worker_pool_params_in.max_threads(), |
| + worker_pool_params_in.suggested_reclaim_time(), |
| + worker_pool_params_in.backward_compatibility()); |
| + } |
| + DCHECK_EQ(static_cast<size_t>(ENVIRONMENT_COUNT), |
| + worker_pool_params_vector.size()); |
| + |
| + return Create(worker_pool_params_vector, Bind(&GetEnvironmentIndexForTraits)); |
| +} |
| + |
| TaskSchedulerImpl::~TaskSchedulerImpl() { |
| #if DCHECK_IS_ON() |
| DCHECK(join_for_testing_returned_.IsSet()); |