• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include "base/check.h"
10 #include "base/cxx17_backports.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(StringPiece name)88 void ThreadPoolInstance::CreateAndStartWithDefaultParams(StringPiece name) {
89   Create(name);
90   g_thread_pool->StartWithDefaultParams();
91 }
92 
StartWithDefaultParams()93 void ThreadPoolInstance::StartWithDefaultParams() {
94   // Values were chosen so that:
95   // * There are few background threads.
96   // * Background threads never outnumber foreground threads.
97   // * The system is utilized maximally by foreground threads.
98   // * The main thread is assumed to be busy, cap foreground workers at
99   //   |num_cores - 1|.
100   const size_t max_num_foreground_threads =
101       static_cast<size_t>(std::max(3, SysInfo::NumberOfProcessors() - 1));
102   Start({max_num_foreground_threads});
103 }
104 #endif  // !BUILDFLAG(IS_NACL)
105 
Create(StringPiece name)106 void ThreadPoolInstance::Create(StringPiece name) {
107   Set(std::make_unique<internal::ThreadPoolImpl>(name));
108 }
109 
110 // static
Set(std::unique_ptr<ThreadPoolInstance> thread_pool)111 void ThreadPoolInstance::Set(std::unique_ptr<ThreadPoolInstance> thread_pool) {
112   delete g_thread_pool;
113   g_thread_pool = thread_pool.release();
114 }
115 
116 // static
Get()117 ThreadPoolInstance* ThreadPoolInstance::Get() {
118   return g_thread_pool;
119 }
120 
121 }  // namespace base
122