1 // Copyright 2012 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 "partition_alloc/partition_alloc_base/threading/platform_thread.h"
6
7 #include <pthread.h>
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include <cerrno>
13 #include <cstddef>
14 #include <cstdint>
15
16 #include "build/build_config.h"
17 #include "partition_alloc/partition_alloc_base/debug/debugging_buildflags.h"
18 #include "partition_alloc/partition_alloc_base/logging.h"
19 #include "partition_alloc/partition_alloc_base/threading/platform_thread_internal_posix.h"
20
21 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
22 #include <sys/syscall.h>
23 #include <atomic>
24 #endif
25
26 #if BUILDFLAG(IS_FUCHSIA)
27 #include <zircon/process.h>
28 #endif
29
30 namespace partition_alloc::internal::base {
31
32 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
33
34 namespace {
35
36 // Store the thread ids in local storage since calling the SWI can be
37 // expensive and PlatformThread::CurrentId is used liberally.
38 thread_local pid_t g_thread_id = -1;
39
40 // A boolean value that indicates that the value stored in |g_thread_id| on the
41 // main thread is invalid, because it hasn't been updated since the process
42 // forked.
43 //
44 // This used to work by setting |g_thread_id| to -1 in a pthread_atfork handler.
45 // However, when a multithreaded process forks, it is only allowed to call
46 // async-signal-safe functions until it calls an exec() syscall. However,
47 // accessing TLS may allocate (see crbug.com/1275748), which is not
48 // async-signal-safe and therefore causes deadlocks, corruption, and crashes.
49 //
50 // It's Atomic to placate TSAN.
51 std::atomic<bool> g_main_thread_tid_cache_valid = false;
52
53 // Tracks whether the current thread is the main thread, and therefore whether
54 // |g_main_thread_tid_cache_valid| is relevant for the current thread. This is
55 // also updated by PlatformThread::CurrentId().
56 thread_local bool g_is_main_thread = true;
57
58 class InitAtFork {
59 public:
InitAtFork()60 InitAtFork() {
61 pthread_atfork(nullptr, nullptr, internal::InvalidateTidCache);
62 }
63 };
64
65 } // namespace
66
67 namespace internal {
68
InvalidateTidCache()69 void InvalidateTidCache() {
70 g_main_thread_tid_cache_valid.store(false, std::memory_order_relaxed);
71 }
72
73 } // namespace internal
74
75 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
76
77 // static
CurrentId()78 PlatformThreadId PlatformThread::CurrentId() {
79 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
80 // into the kernel.
81 #if BUILDFLAG(IS_APPLE)
82 return pthread_mach_thread_np(pthread_self());
83 #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
84 static InitAtFork init_at_fork;
85 if (g_thread_id == -1 ||
86 (g_is_main_thread &&
87 !g_main_thread_tid_cache_valid.load(std::memory_order_relaxed))) {
88 // Update the cached tid.
89 g_thread_id = syscall(__NR_gettid);
90 // If this is the main thread, we can mark the tid_cache as valid.
91 // Otherwise, stop the current thread from always entering this slow path.
92 if (g_thread_id == getpid()) {
93 g_main_thread_tid_cache_valid.store(true, std::memory_order_relaxed);
94 } else {
95 g_is_main_thread = false;
96 }
97 } else {
98 #if BUILDFLAG(PA_DCHECK_IS_ON)
99 if (g_thread_id != syscall(__NR_gettid)) {
100 PA_RAW_LOG(
101 FATAL,
102 "Thread id stored in TLS is different from thread id returned by "
103 "the system. It is likely that the process was forked without going "
104 "through fork().");
105 }
106 #endif
107 }
108 return g_thread_id;
109 #elif BUILDFLAG(IS_ANDROID)
110 // Note: do not cache the return value inside a thread_local variable on
111 // Android (as above). The reasons are:
112 // - thread_local is slow on Android (goes through emutls)
113 // - gettid() is fast, since its return value is cached in pthread (in the
114 // thread control block of pthread). See gettid.c in bionic.
115 return gettid();
116 #elif BUILDFLAG(IS_FUCHSIA)
117 return zx_thread_self();
118 #elif BUILDFLAG(IS_SOLARIS) || BUILDFLAG(IS_QNX)
119 return pthread_self();
120 #elif BUILDFLAG(IS_POSIX) && BUILDFLAG(IS_AIX)
121 return pthread_self();
122 #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_AIX)
123 return reinterpret_cast<int64_t>(pthread_self());
124 #endif
125 }
126
127 // static
CurrentRef()128 PlatformThreadRef PlatformThread::CurrentRef() {
129 return PlatformThreadRef(pthread_self());
130 }
131
132 // static
Sleep(TimeDelta duration)133 void PlatformThread::Sleep(TimeDelta duration) {
134 struct timespec sleep_time, remaining;
135
136 // Break the duration into seconds and nanoseconds.
137 // NOTE: TimeDelta's microseconds are int64s while timespec's
138 // nanoseconds are longs, so this unpacking must prevent overflow.
139 sleep_time.tv_sec = duration.InSeconds();
140 duration -= Seconds(sleep_time.tv_sec);
141 sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds
142
143 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) {
144 sleep_time = remaining;
145 }
146 }
147
148 } // namespace partition_alloc::internal::base
149