]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/scudo/scudo_tsd_shared.inc
MFV r349134:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / scudo / scudo_tsd_shared.inc
1 //===-- scudo_tsd_shared.inc ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// Scudo shared TSD fastpath functions implementation.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef SCUDO_TSD_H_
15 # error "This file must be included inside scudo_tsd.h."
16 #endif  // SCUDO_TSD_H_
17
18 #if !SCUDO_TSD_EXCLUSIVE
19
20 extern pthread_key_t PThreadKey;
21
22 #if SANITIZER_LINUX && !SANITIZER_ANDROID
23 __attribute__((tls_model("initial-exec")))
24 extern THREADLOCAL ScudoTSD *CurrentTSD;
25 #endif
26
27 ALWAYS_INLINE ScudoTSD* getCurrentTSD() {
28 #if SANITIZER_ANDROID
29   return reinterpret_cast<ScudoTSD *>(*get_android_tls_ptr());
30 #elif SANITIZER_LINUX
31   return CurrentTSD;
32 #else
33   return reinterpret_cast<ScudoTSD *>(pthread_getspecific(PThreadKey));
34 #endif  // SANITIZER_ANDROID
35 }
36
37 ALWAYS_INLINE void initThreadMaybe(bool MinimalInit = false) {
38   if (LIKELY(getCurrentTSD()))
39     return;
40   initThread(MinimalInit);
41 }
42
43 ScudoTSD *getTSDAndLockSlow(ScudoTSD *TSD);
44
45 ALWAYS_INLINE ScudoTSD *getTSDAndLock(bool *UnlockRequired) {
46   ScudoTSD *TSD = getCurrentTSD();
47   DCHECK(TSD && "No TSD associated with the current thread!");
48   *UnlockRequired = true;
49   // Try to lock the currently associated context.
50   if (TSD->tryLock())
51     return TSD;
52   // If it failed, go the slow path.
53   return getTSDAndLockSlow(TSD);
54 }
55
56 #endif  // !SCUDO_TSD_EXCLUSIVE