summaryrefslogtreecommitdiff
path: root/chromium/base/task_scheduler/task_scheduler.cc
blob: ee2ccf441418bc67453b986d2603d0c08345c6bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/task_scheduler/task_scheduler.h"

#include <algorithm>

#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/sys_info.h"
#include "base/task_scheduler/scheduler_worker_pool_params.h"
#include "base/task_scheduler/task_scheduler_impl.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"

namespace base {

namespace {

// |g_task_scheduler| is intentionally leaked on shutdown.
TaskScheduler* g_task_scheduler = nullptr;

}  // namespace

TaskScheduler::InitParams::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)
    : background_worker_pool_params(background_worker_pool_params_in),
      background_blocking_worker_pool_params(
          background_blocking_worker_pool_params_in),
      foreground_worker_pool_params(foreground_worker_pool_params_in),
      foreground_blocking_worker_pool_params(
          foreground_blocking_worker_pool_params_in) {}

TaskScheduler::InitParams::~InitParams() = default;

#if !defined(OS_NACL)
// static
void TaskScheduler::CreateAndStartWithDefaultParams(StringPiece name) {
  using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy;

  // Values were chosen so that:
  // * There are few background threads.
  // * Background threads never outnumber foreground threads.
  // * The system is utilized maximally by foreground threads.
  const int num_cores = SysInfo::NumberOfProcessors();
  constexpr int kBackgroundMaxThreads = 1;
  constexpr int kBackgroundBlockingMaxThreads = 2;
  const int kForegroundMaxThreads = std::max(1, num_cores);
  const int kForegroundBlockingMaxThreads = std::max(2, num_cores);

  constexpr TimeDelta kSuggestedReclaimTime = TimeDelta::FromSeconds(30);

  Create(name);
  GetInstance()->Start(
      {{StandbyThreadPolicy::LAZY, kBackgroundMaxThreads,
        kSuggestedReclaimTime},
       {StandbyThreadPolicy::LAZY, kBackgroundBlockingMaxThreads,
        kSuggestedReclaimTime},
       {StandbyThreadPolicy::LAZY, kForegroundMaxThreads,
        kSuggestedReclaimTime},
       {StandbyThreadPolicy::LAZY, kForegroundBlockingMaxThreads,
        kSuggestedReclaimTime}});
}
#endif  // !defined(OS_NACL)

void TaskScheduler::Create(StringPiece name) {
  SetInstance(MakeUnique<internal::TaskSchedulerImpl>(name));
}

// static
void TaskScheduler::SetInstance(std::unique_ptr<TaskScheduler> task_scheduler) {
  delete g_task_scheduler;
  g_task_scheduler = task_scheduler.release();
}

// static
TaskScheduler* TaskScheduler::GetInstance() {
  return g_task_scheduler;
}

}  // namespace base