platform_thread.cc (2084B)
1 // Copyright 2018 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/threading/platform_thread.h" 6 #include "base/threading/thread_id_name_manager.h" 7 8 #include "base/task/current_thread.h" 9 #include "third_party/abseil-cpp/absl/base/attributes.h" 10 11 #if BUILDFLAG(IS_FUCHSIA) 12 #include "base/fuchsia/scheduler.h" 13 #endif 14 15 namespace base { 16 17 #if !defined(MOZ_SANDBOX) 18 namespace { 19 ABSL_CONST_INIT thread_local ThreadType current_thread_type = 20 ThreadType::kDefault; 21 22 } // namespace 23 24 // static 25 void PlatformThreadBase::SetCurrentThreadType(ThreadType thread_type) { 26 MessagePumpType message_pump_type = MessagePumpType::DEFAULT; 27 if (CurrentIOThread::IsSet()) { 28 message_pump_type = MessagePumpType::IO; 29 } 30 #if !BUILDFLAG(IS_NACL) 31 else if (CurrentUIThread::IsSet()) { 32 message_pump_type = MessagePumpType::UI; 33 } 34 #endif 35 internal::SetCurrentThreadType(thread_type, message_pump_type); 36 } 37 38 // static 39 ThreadType PlatformThreadBase::GetCurrentThreadType() { 40 return current_thread_type; 41 } 42 43 // static 44 absl::optional<TimeDelta> PlatformThreadBase::GetThreadLeewayOverride() { 45 #if BUILDFLAG(IS_FUCHSIA) 46 // On Fuchsia, all audio threads run with the CPU scheduling profile that uses 47 // an interval of |kAudioSchedulingPeriod|. Using the default leeway may lead 48 // to some tasks posted to audio threads to be executed too late (see 49 // http://crbug.com/1368858). 50 if (GetCurrentThreadType() == ThreadType::kRealtimeAudio) 51 return kAudioSchedulingPeriod; 52 #endif 53 return absl::nullopt; 54 } 55 #endif 56 57 // static 58 void PlatformThreadBase::SetNameCommon(const std::string& name) { 59 ThreadIdNameManager::GetInstance()->SetName(name); 60 } 61 62 #if !defined(MOZ_SANDBOX) 63 namespace internal { 64 65 void SetCurrentThreadType(ThreadType thread_type, 66 MessagePumpType pump_type_hint) { 67 CHECK_LE(thread_type, ThreadType::kMaxValue); 68 SetCurrentThreadTypeImpl(thread_type, pump_type_hint); 69 current_thread_type = thread_type; 70 } 71 72 } // namespace internal 73 #endif 74 75 } // namespace base