xref: /aosp_15_r20/external/cronet/base/task/thread_pool/environment_config.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 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/environment_config.h"
6 
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "base/synchronization/lock.h"
10 #include "base/threading/platform_thread.h"
11 #include "build/build_config.h"
12 
13 namespace base {
14 namespace internal {
15 
16 namespace {
17 
CanUseBackgroundThreadTypeForWorkerThreadImpl()18 bool CanUseBackgroundThreadTypeForWorkerThreadImpl() {
19   // Commandline flag overrides (e.g. for experiments). Note that it may not be
20   // initialized yet, e.g. in cronet.
21   if (CommandLine::InitializedForCurrentProcess() &&
22       CommandLine::ForCurrentProcess()->HasSwitch(
23           switches::kEnableBackgroundThreadPool)) {
24     return true;
25   }
26 
27   // When Lock doesn't handle multiple thread priorities, run all
28   // WorkerThread with a normal priority to avoid priority inversion when a
29   // thread running with a normal priority tries to acquire a lock held by a
30   // thread running with a background priority.
31   if (!Lock::HandlesMultipleThreadPriorities())
32     return false;
33 
34 #if !BUILDFLAG(IS_ANDROID)
35   // When thread type can't be increased to kNormal, run all threads with a
36   // kNormal thread type to avoid priority inversions on shutdown
37   // (ThreadPoolImpl increases kBackground threads type to kNormal on shutdown
38   // while resolving remaining shutdown blocking tasks).
39   //
40   // This is ignored on Android, because it doesn't have a clean shutdown phase.
41   if (!PlatformThread::CanChangeThreadType(ThreadType::kBackground,
42                                            ThreadType::kDefault))
43     return false;
44 #endif  // BUILDFLAG(IS_ANDROID)
45 
46   return true;
47 }
48 
CanUseUtilityThreadTypeForWorkerThreadImpl()49 bool CanUseUtilityThreadTypeForWorkerThreadImpl() {
50 #if !BUILDFLAG(IS_ANDROID)
51   // Same as CanUseBackgroundThreadTypeForWorkerThreadImpl()
52   if (!PlatformThread::CanChangeThreadType(ThreadType::kUtility,
53                                            ThreadType::kDefault))
54     return false;
55 #endif  // BUILDFLAG(IS_ANDROID)
56 
57   return true;
58 }
59 
60 }  // namespace
61 
CanUseBackgroundThreadTypeForWorkerThread()62 bool CanUseBackgroundThreadTypeForWorkerThread() {
63   static const bool can_use_background_thread_type_for_worker_thread =
64       CanUseBackgroundThreadTypeForWorkerThreadImpl();
65   return can_use_background_thread_type_for_worker_thread;
66 }
67 
CanUseUtilityThreadTypeForWorkerThread()68 bool CanUseUtilityThreadTypeForWorkerThread() {
69   static const bool can_use_utility_thread_type_for_worker_thread =
70       CanUseUtilityThreadTypeForWorkerThreadImpl();
71   return can_use_utility_thread_type_for_worker_thread;
72 }
73 
74 }  // namespace internal
75 }  // namespace base
76