1*635a8641SAndroid Build Coastguard Worker // Copyright 2016 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/task_scheduler.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include <algorithm>
8*635a8641SAndroid Build Coastguard Worker
9*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/sys_info.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/scheduler_worker_pool_params.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/task_scheduler_impl.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
16*635a8641SAndroid Build Coastguard Worker
17*635a8641SAndroid Build Coastguard Worker namespace base {
18*635a8641SAndroid Build Coastguard Worker
19*635a8641SAndroid Build Coastguard Worker namespace {
20*635a8641SAndroid Build Coastguard Worker
21*635a8641SAndroid Build Coastguard Worker // |g_task_scheduler| is intentionally leaked on shutdown.
22*635a8641SAndroid Build Coastguard Worker TaskScheduler* g_task_scheduler = nullptr;
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker } // namespace
25*635a8641SAndroid Build Coastguard Worker
InitParams(const SchedulerWorkerPoolParams & background_worker_pool_params_in,const SchedulerWorkerPoolParams & background_blocking_worker_pool_params_in,const SchedulerWorkerPoolParams & foreground_worker_pool_params_in,const SchedulerWorkerPoolParams & foreground_blocking_worker_pool_params_in,SharedWorkerPoolEnvironment shared_worker_pool_environment_in)26*635a8641SAndroid Build Coastguard Worker TaskScheduler::InitParams::InitParams(
27*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& background_worker_pool_params_in,
28*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& background_blocking_worker_pool_params_in,
29*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& foreground_worker_pool_params_in,
30*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& foreground_blocking_worker_pool_params_in,
31*635a8641SAndroid Build Coastguard Worker SharedWorkerPoolEnvironment shared_worker_pool_environment_in)
32*635a8641SAndroid Build Coastguard Worker : background_worker_pool_params(background_worker_pool_params_in),
33*635a8641SAndroid Build Coastguard Worker background_blocking_worker_pool_params(
34*635a8641SAndroid Build Coastguard Worker background_blocking_worker_pool_params_in),
35*635a8641SAndroid Build Coastguard Worker foreground_worker_pool_params(foreground_worker_pool_params_in),
36*635a8641SAndroid Build Coastguard Worker foreground_blocking_worker_pool_params(
37*635a8641SAndroid Build Coastguard Worker foreground_blocking_worker_pool_params_in),
38*635a8641SAndroid Build Coastguard Worker shared_worker_pool_environment(shared_worker_pool_environment_in) {}
39*635a8641SAndroid Build Coastguard Worker
40*635a8641SAndroid Build Coastguard Worker TaskScheduler::InitParams::~InitParams() = default;
41*635a8641SAndroid Build Coastguard Worker
42*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL)
43*635a8641SAndroid Build Coastguard Worker // static
CreateAndStartWithDefaultParams(StringPiece name)44*635a8641SAndroid Build Coastguard Worker void TaskScheduler::CreateAndStartWithDefaultParams(StringPiece name) {
45*635a8641SAndroid Build Coastguard Worker Create(name);
46*635a8641SAndroid Build Coastguard Worker GetInstance()->StartWithDefaultParams();
47*635a8641SAndroid Build Coastguard Worker }
48*635a8641SAndroid Build Coastguard Worker
StartWithDefaultParams()49*635a8641SAndroid Build Coastguard Worker void TaskScheduler::StartWithDefaultParams() {
50*635a8641SAndroid Build Coastguard Worker // Values were chosen so that:
51*635a8641SAndroid Build Coastguard Worker // * There are few background threads.
52*635a8641SAndroid Build Coastguard Worker // * Background threads never outnumber foreground threads.
53*635a8641SAndroid Build Coastguard Worker // * The system is utilized maximally by foreground threads.
54*635a8641SAndroid Build Coastguard Worker // * The main thread is assumed to be busy, cap foreground workers at
55*635a8641SAndroid Build Coastguard Worker // |num_cores - 1|.
56*635a8641SAndroid Build Coastguard Worker const int num_cores = SysInfo::NumberOfProcessors();
57*635a8641SAndroid Build Coastguard Worker constexpr int kBackgroundMaxThreads = 1;
58*635a8641SAndroid Build Coastguard Worker constexpr int kBackgroundBlockingMaxThreads = 2;
59*635a8641SAndroid Build Coastguard Worker const int kForegroundMaxThreads = std::max(1, num_cores - 1);
60*635a8641SAndroid Build Coastguard Worker const int kForegroundBlockingMaxThreads = std::max(2, num_cores - 1);
61*635a8641SAndroid Build Coastguard Worker
62*635a8641SAndroid Build Coastguard Worker constexpr TimeDelta kSuggestedReclaimTime = TimeDelta::FromSeconds(30);
63*635a8641SAndroid Build Coastguard Worker
64*635a8641SAndroid Build Coastguard Worker Start({{kBackgroundMaxThreads, kSuggestedReclaimTime},
65*635a8641SAndroid Build Coastguard Worker {kBackgroundBlockingMaxThreads, kSuggestedReclaimTime},
66*635a8641SAndroid Build Coastguard Worker {kForegroundMaxThreads, kSuggestedReclaimTime},
67*635a8641SAndroid Build Coastguard Worker {kForegroundBlockingMaxThreads, kSuggestedReclaimTime}});
68*635a8641SAndroid Build Coastguard Worker }
69*635a8641SAndroid Build Coastguard Worker #endif // !defined(OS_NACL)
70*635a8641SAndroid Build Coastguard Worker
Create(StringPiece name)71*635a8641SAndroid Build Coastguard Worker void TaskScheduler::Create(StringPiece name) {
72*635a8641SAndroid Build Coastguard Worker SetInstance(std::make_unique<internal::TaskSchedulerImpl>(name));
73*635a8641SAndroid Build Coastguard Worker }
74*635a8641SAndroid Build Coastguard Worker
75*635a8641SAndroid Build Coastguard Worker // static
SetInstance(std::unique_ptr<TaskScheduler> task_scheduler)76*635a8641SAndroid Build Coastguard Worker void TaskScheduler::SetInstance(std::unique_ptr<TaskScheduler> task_scheduler) {
77*635a8641SAndroid Build Coastguard Worker delete g_task_scheduler;
78*635a8641SAndroid Build Coastguard Worker g_task_scheduler = task_scheduler.release();
79*635a8641SAndroid Build Coastguard Worker }
80*635a8641SAndroid Build Coastguard Worker
81*635a8641SAndroid Build Coastguard Worker // static
GetInstance()82*635a8641SAndroid Build Coastguard Worker TaskScheduler* TaskScheduler::GetInstance() {
83*635a8641SAndroid Build Coastguard Worker return g_task_scheduler;
84*635a8641SAndroid Build Coastguard Worker }
85*635a8641SAndroid Build Coastguard Worker
86*635a8641SAndroid Build Coastguard Worker } // namespace base
87