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