1 // Copyright 2016 The Chromium Authors
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/thread_pool/thread_pool_instance.h"
6
7 #include <algorithm>
8 #include <string_view>
9
10 #include "base/check.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/system/sys_info.h"
13 #include "base/task/thread_pool/thread_pool_impl.h"
14 #include "base/threading/platform_thread.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17
18 namespace base {
19
20 namespace {
21
22 // |g_thread_pool| is intentionally leaked on shutdown.
23 ThreadPoolInstance* g_thread_pool = nullptr;
24
GetDefaultMaxNumUtilityThreads(size_t max_num_foreground_threads_in)25 size_t GetDefaultMaxNumUtilityThreads(size_t max_num_foreground_threads_in) {
26 int num_of_efficient_processors = SysInfo::NumberOfEfficientProcessors();
27 if (num_of_efficient_processors != 0) {
28 DCHECK_GT(num_of_efficient_processors, 0);
29 return std::max<size_t>(
30 2, std::min(max_num_foreground_threads_in,
31 static_cast<size_t>(num_of_efficient_processors)));
32 }
33 return std::max<size_t>(2, max_num_foreground_threads_in / 2);
34 }
35
36 } // namespace
37
InitParams(size_t max_num_foreground_threads_in)38 ThreadPoolInstance::InitParams::InitParams(size_t max_num_foreground_threads_in)
39 : max_num_foreground_threads(max_num_foreground_threads_in),
40 max_num_utility_threads(
41 GetDefaultMaxNumUtilityThreads(max_num_foreground_threads_in)) {}
42
InitParams(size_t max_num_foreground_threads_in,size_t max_num_utility_threads_in)43 ThreadPoolInstance::InitParams::InitParams(size_t max_num_foreground_threads_in,
44 size_t max_num_utility_threads_in)
45 : max_num_foreground_threads(max_num_foreground_threads_in),
46 max_num_utility_threads(max_num_utility_threads_in) {}
47
48 ThreadPoolInstance::InitParams::~InitParams() = default;
49
ScopedExecutionFence()50 ThreadPoolInstance::ScopedExecutionFence::ScopedExecutionFence() {
51 DCHECK(g_thread_pool);
52 g_thread_pool->BeginFence();
53 }
54
~ScopedExecutionFence()55 ThreadPoolInstance::ScopedExecutionFence::~ScopedExecutionFence() {
56 DCHECK(g_thread_pool);
57 g_thread_pool->EndFence();
58 }
59
60 ThreadPoolInstance::ScopedBestEffortExecutionFence::
ScopedBestEffortExecutionFence()61 ScopedBestEffortExecutionFence() {
62 DCHECK(g_thread_pool);
63 g_thread_pool->BeginBestEffortFence();
64 }
65
66 ThreadPoolInstance::ScopedBestEffortExecutionFence::
~ScopedBestEffortExecutionFence()67 ~ScopedBestEffortExecutionFence() {
68 DCHECK(g_thread_pool);
69 g_thread_pool->EndBestEffortFence();
70 }
71
ScopedRestrictedTasks()72 ThreadPoolInstance::ScopedRestrictedTasks::ScopedRestrictedTasks() {
73 DCHECK(g_thread_pool);
74 g_thread_pool->BeginRestrictedTasks();
75 }
76
~ScopedRestrictedTasks()77 ThreadPoolInstance::ScopedRestrictedTasks::~ScopedRestrictedTasks() {
78 DCHECK(g_thread_pool);
79 g_thread_pool->EndRestrictedTasks();
80 }
81
82 ThreadPoolInstance::ScopedFizzleBlockShutdownTasks::
ScopedFizzleBlockShutdownTasks()83 ScopedFizzleBlockShutdownTasks() {
84 // It's possible for this to be called without a ThreadPool present in tests.
85 if (g_thread_pool)
86 g_thread_pool->BeginFizzlingBlockShutdownTasks();
87 }
88
89 ThreadPoolInstance::ScopedFizzleBlockShutdownTasks::
~ScopedFizzleBlockShutdownTasks()90 ~ScopedFizzleBlockShutdownTasks() {
91 // It's possible for this to be called without a ThreadPool present in tests.
92 if (g_thread_pool)
93 g_thread_pool->EndFizzlingBlockShutdownTasks();
94 }
95
96 #if !BUILDFLAG(IS_NACL)
97 // static
CreateAndStartWithDefaultParams(std::string_view name)98 void ThreadPoolInstance::CreateAndStartWithDefaultParams(
99 std::string_view name) {
100 Create(name);
101 g_thread_pool->StartWithDefaultParams();
102 }
103
StartWithDefaultParams()104 void ThreadPoolInstance::StartWithDefaultParams() {
105 // Values were chosen so that:
106 // * There are few background threads.
107 // * Background threads never outnumber foreground threads.
108 // * The system is utilized maximally by foreground threads.
109 // * The main thread is assumed to be busy, cap foreground workers at
110 // |num_cores - 1|.
111 const size_t max_num_foreground_threads =
112 static_cast<size_t>(std::max(3, SysInfo::NumberOfProcessors() - 1));
113 Start({max_num_foreground_threads});
114 }
115 #endif // !BUILDFLAG(IS_NACL)
116
Create(std::string_view name)117 void ThreadPoolInstance::Create(std::string_view name) {
118 Set(std::make_unique<internal::ThreadPoolImpl>(name));
119 }
120
121 // static
Set(std::unique_ptr<ThreadPoolInstance> thread_pool)122 void ThreadPoolInstance::Set(std::unique_ptr<ThreadPoolInstance> thread_pool) {
123 delete g_thread_pool;
124 g_thread_pool = thread_pool.release();
125 }
126
127 // static
Get()128 ThreadPoolInstance* ThreadPoolInstance::Get() {
129 return g_thread_pool;
130 }
131
132 } // namespace base
133