1 //===--- Thread Identifier Header --------------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H
10 #define LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H
12 #ifdef LIBC_FULL_BUILD
13 #include "src/__support/threads/thread.h"
14 #endif // LIBC_FULL_BUILD
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>
21 namespace LIBC_NAMESPACE_DECL
{
24 LIBC_INLINE pid_t
*get_tid_cache() {
25 #ifdef LIBC_FULL_BUILD
26 return &self
.attrib
->tid
;
28 // in non-full build mode, we do not control the fork routine. Therefore,
29 // we do not cache tid at all.
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
);
41 LIBC_INLINE
void force_set_tid(pid_t tid
) {
42 pid_t
*cache
= get_tid_cache();
47 } // namespace internal
48 } // namespace LIBC_NAMESPACE_DECL
50 #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H