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 namespace { 18 19 ABSL_CONST_INIT thread_local ThreadType current_thread_type = 20 ThreadType::kDefault; 21 22 } // namespace 23 24 // static SetCurrentThreadType(ThreadType thread_type)25void 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 GetCurrentThreadType()39ThreadType PlatformThreadBase::GetCurrentThreadType() { 40 return current_thread_type; 41 } 42 43 // static GetThreadLeewayOverride()44std::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 std::nullopt; 54 } 55 56 // static SetNameCommon(const std::string & name)57void PlatformThreadBase::SetNameCommon(const std::string& name) { 58 ThreadIdNameManager::GetInstance()->SetName(name); 59 } 60 61 namespace internal { 62 SetCurrentThreadType(ThreadType thread_type,MessagePumpType pump_type_hint)63void SetCurrentThreadType(ThreadType thread_type, 64 MessagePumpType pump_type_hint) { 65 CHECK_LE(thread_type, ThreadType::kMaxValue); 66 SetCurrentThreadTypeImpl(thread_type, pump_type_hint); 67 current_thread_type = thread_type; 68 } 69 70 } // namespace internal 71 72 } // namespace base 73