1 //===-- scudo_tsd_shared.inc ------------------------------------*- 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 /// Scudo shared TSD fastpath functions implementation.
11 //===----------------------------------------------------------------------===//
14 # error "This file must be included inside scudo_tsd.h."
15 #endif // SCUDO_TSD_H_
17 #if !SCUDO_TSD_EXCLUSIVE
19 extern pthread_key_t PThreadKey;
21 #if SANITIZER_LINUX && !SANITIZER_ANDROID
22 __attribute__((tls_model("initial-exec")))
23 extern THREADLOCAL ScudoTSD *CurrentTSD;
26 ALWAYS_INLINE ScudoTSD* getCurrentTSD() {
28 return reinterpret_cast<ScudoTSD *>(*get_android_tls_ptr());
32 return reinterpret_cast<ScudoTSD *>(pthread_getspecific(PThreadKey));
33 #endif // SANITIZER_ANDROID
36 ALWAYS_INLINE void initThreadMaybe(bool MinimalInit = false) {
37 if (LIKELY(getCurrentTSD()))
39 initThread(MinimalInit);
42 ScudoTSD *getTSDAndLockSlow(ScudoTSD *TSD);
44 ALWAYS_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.
51 // If it failed, go the slow path.
52 return getTSDAndLockSlow(TSD);
55 #endif // !SCUDO_TSD_EXCLUSIVE