1//===-- scudo_tsd_shared.inc ------------------------------------*- 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/// Scudo shared TSD fastpath functions implementation.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef SCUDO_TSD_H_
14# error "This file must be included inside scudo_tsd.h."
15#endif  // SCUDO_TSD_H_
16
17#if !SCUDO_TSD_EXCLUSIVE
18
19extern pthread_key_t PThreadKey;
20
21#if SANITIZER_LINUX && !SANITIZER_ANDROID
22__attribute__((tls_model("initial-exec")))
23extern THREADLOCAL ScudoTSD *CurrentTSD;
24#endif
25
26ALWAYS_INLINE ScudoTSD* getCurrentTSD() {
27#if SANITIZER_ANDROID
28  return reinterpret_cast<ScudoTSD *>(*get_android_tls_ptr());
29#elif SANITIZER_LINUX
30  return CurrentTSD;
31#else
32  return reinterpret_cast<ScudoTSD *>(pthread_getspecific(PThreadKey));
33#endif  // SANITIZER_ANDROID
34}
35
36ALWAYS_INLINE void initThreadMaybe(bool MinimalInit = false) {
37  if (LIKELY(getCurrentTSD()))
38    return;
39  initThread(MinimalInit);
40}
41
42ScudoTSD *getTSDAndLockSlow(ScudoTSD *TSD);
43
44ALWAYS_INLINE ScudoTSD *getTSDAndLock(bool *UnlockRequired) {
45  ScudoTSD *TSD = getCurrentTSD();
46  DCHECK(TSD && "No TSD associated with the current thread!");
47  *UnlockRequired = true;
48  // Try to lock the currently associated context.
49  if (TSD->tryLock())
50    return TSD;
51  // If it failed, go the slow path.
52  return getTSDAndLockSlow(TSD);
53}
54
55#endif  // !SCUDO_TSD_EXCLUSIVE
56