scudo_tsd_shared.inc 1.7 KB

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