xref: /aosp_15_r20/external/cronet/base/threading/platform_thread_internal_posix.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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 #ifndef BASE_THREADING_PLATFORM_THREAD_INTERNAL_POSIX_H_
6 #define BASE_THREADING_PLATFORM_THREAD_INTERNAL_POSIX_H_
7 
8 #include <optional>
9 
10 #include "base/base_export.h"
11 #include "base/threading/platform_thread.h"
12 #include "build/build_config.h"
13 
14 namespace base {
15 
16 namespace internal {
17 
18 struct ThreadTypeToNiceValuePair {
19   ThreadType thread_type;
20   int nice_value;
21 };
22 
23 struct ThreadPriorityToNiceValuePairForTest {
24   ThreadPriorityForTest priority;
25   int nice_value;
26 };
27 
28 // The elements must be listed in the order of increasing priority (lowest
29 // priority first), that is, in the order of decreasing nice values (highest
30 // nice value first).
31 extern const ThreadTypeToNiceValuePair kThreadTypeToNiceValueMap[7];
32 
33 // The elements must be listed in the order of decreasing priority (highest
34 // priority first), that is, in the order of increasing nice values (lowest nice
35 // value first).
36 extern const ThreadPriorityToNiceValuePairForTest
37     kThreadPriorityToNiceValueMapForTest[7];
38 
39 // Returns the nice value matching |priority| based on the platform-specific
40 // implementation of kThreadTypeToNiceValueMap.
41 int ThreadTypeToNiceValue(ThreadType thread_type);
42 
43 // Returns whether SetCurrentThreadTypeForPlatform can set a thread as
44 // kRealtimeAudio.
45 bool CanSetThreadTypeToRealtimeAudio();
46 
47 // Allows platform specific tweaks to the generic POSIX solution for
48 // SetCurrentThreadType(). Returns true if the platform-specific
49 // implementation handled this |thread_type| change, false if the generic
50 // implementation should instead proceed.
51 bool SetCurrentThreadTypeForPlatform(ThreadType thread_type,
52                                      MessagePumpType pump_type_hint);
53 
54 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
55 // Current thread id is cached in thread local storage for performance reasons.
56 // In some rare cases it's important to invalidate that cache explicitly (e.g.
57 // after going through clone() syscall which does not call pthread_atfork()
58 // handlers).
59 // This can only be called when the process is single-threaded.
60 BASE_EXPORT void InvalidateTidCache();
61 #endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
62 
63 // Returns the ThreadPrioirtyForTest matching |nice_value| based on the
64 // platform-specific implementation of kThreadPriorityToNiceValueMapForTest.
65 ThreadPriorityForTest NiceValueToThreadPriorityForTest(int nice_value);
66 
67 std::optional<ThreadPriorityForTest>
68 GetCurrentThreadPriorityForPlatformForTest();
69 
70 int GetCurrentThreadNiceValue();
71 
72 }  // namespace internal
73 
74 }  // namespace base
75 
76 #endif  // BASE_THREADING_PLATFORM_THREAD_INTERNAL_POSIX_H_
77