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