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
72 ThreadPoolInstance::ScopedFizzleBlockShutdownTasks::
ScopedFizzleBlockShutdownTasks()73 ScopedFizzleBlockShutdownTasks() {
74 // It's possible for this to be called without a ThreadPool present in tests.
75 if (g_thread_pool)
76 g_thread_pool->BeginFizzlingBlockShutdownTasks();
77 }
78
79 ThreadPoolInstance::ScopedFizzleBlockShutdownTasks::
~ScopedFizzleBlockShutdownTasks()80 ~ScopedFizzleBlockShutdownTasks() {
81 // It's possible for this to be called without a ThreadPool present in tests.
82 if (g_thread_pool)
83 g_thread_pool->EndFizzlingBlockShutdownTasks();
84 }
85
86 #if !BUILDFLAG(IS_NACL)
87 // static
CreateAndStartWithDefaultParams(std::string_view name)88 void ThreadPoolInstance::CreateAndStartWithDefaultParams(
89 std::string_view name) {
90 Create(name);
91 g_thread_pool->StartWithDefaultParams();
92 }
93
StartWithDefaultParams()94 void ThreadPoolInstance::StartWithDefaultParams() {
95 // Values were chosen so that:
96 // * There are few background threads.
97 // * Background threads never outnumber foreground threads.
98 // * The system is utilized maximally by foreground threads.
99 // * The main thread is assumed to be busy, cap foreground workers at
100 // |num_cores - 1|.
101 const size_t max_num_foreground_threads =
102 static_cast<size_t>(std::max(3, SysInfo::NumberOfProcessors() - 1));
103 Start({max_num_foreground_threads});
104 }
105 #endif // !BUILDFLAG(IS_NACL)
106
Create(std::string_view name)107 void ThreadPoolInstance::Create(std::string_view name) {
108 Set(std::make_unique<internal::ThreadPoolImpl>(name));
109 }
110
111 // static
Set(std::unique_ptr<ThreadPoolInstance> thread_pool)112 void ThreadPoolInstance::Set(std::unique_ptr<ThreadPoolInstance> thread_pool) {
113 delete g_thread_pool;
114 g_thread_pool = thread_pool.release();
115 }
116
117 // static
Get()118 ThreadPoolInstance* ThreadPoolInstance::Get() {
119 return g_thread_pool;
120 }
121
122 } // namespace base
123