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