xref: /aosp_15_r20/external/llvm-libc/src/__support/threads/identifier.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===--- Thread Identifier Header --------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H
10 #define LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H
11 
12 #ifdef LIBC_FULL_BUILD
13 #include "src/__support/threads/thread.h"
14 #endif // LIBC_FULL_BUILD
15 
16 #include "hdr/types/pid_t.h"
17 #include "src/__support/OSUtil/syscall.h"
18 #include "src/__support/macros/optimization.h"
19 #include <sys/syscall.h>
20 
21 namespace LIBC_NAMESPACE_DECL {
22 namespace internal {
23 
get_tid_cache()24 LIBC_INLINE pid_t *get_tid_cache() {
25 #ifdef LIBC_FULL_BUILD
26   return &self.attrib->tid;
27 #else
28   // in non-full build mode, we do not control the fork routine. Therefore,
29   // we do not cache tid at all.
30   return nullptr;
31 #endif
32 }
33 
gettid()34 LIBC_INLINE pid_t gettid() {
35   pid_t *cache = get_tid_cache();
36   if (LIBC_UNLIKELY(!cache || *cache <= 0))
37     return syscall_impl<pid_t>(SYS_gettid);
38   return *cache;
39 }
40 
force_set_tid(pid_t tid)41 LIBC_INLINE void force_set_tid(pid_t tid) {
42   pid_t *cache = get_tid_cache();
43   if (cache)
44     *cache = tid;
45 }
46 
47 } // namespace internal
48 } // namespace LIBC_NAMESPACE_DECL
49 
50 #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H
51