51 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H10#define LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H11 12#ifdef LIBC_FULL_BUILD13#include "src/__support/threads/thread.h"14#endif // LIBC_FULL_BUILD15 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 21namespace LIBC_NAMESPACE_DECL {22namespace internal {23 24LIBC_INLINE pid_t *get_tid_cache() {25#ifdef LIBC_FULL_BUILD26 return &self.attrib->tid;27#else28 // 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#endif32}33 34LIBC_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 41LIBC_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 internal48} // namespace LIBC_NAMESPACE_DECL49 50#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_IDENTIFIER_H51