scudo_tsd.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===-- scudo_tsd.h ---------------------------------------------*- 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 thread specific data definition.
  10. /// Implementation will differ based on the thread local storage primitives
  11. /// offered by the underlying platform.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef SCUDO_TSD_H_
  15. #define SCUDO_TSD_H_
  16. #include "scudo_allocator.h"
  17. #include "scudo_utils.h"
  18. #include <pthread.h>
  19. namespace __scudo {
  20. struct ALIGNED(SANITIZER_CACHE_LINE_SIZE) ScudoTSD {
  21. AllocatorCacheT Cache;
  22. uptr QuarantineCachePlaceHolder[4];
  23. void init();
  24. void commitBack();
  25. inline bool tryLock() SANITIZER_TRY_ACQUIRE(true, Mutex) {
  26. if (Mutex.TryLock()) {
  27. atomic_store_relaxed(&Precedence, 0);
  28. return true;
  29. }
  30. if (atomic_load_relaxed(&Precedence) == 0)
  31. atomic_store_relaxed(&Precedence, static_cast<uptr>(
  32. MonotonicNanoTime() >> FIRST_32_SECOND_64(16, 0)));
  33. return false;
  34. }
  35. inline void lock() SANITIZER_ACQUIRE(Mutex) {
  36. atomic_store_relaxed(&Precedence, 0);
  37. Mutex.Lock();
  38. }
  39. inline void unlock() SANITIZER_RELEASE(Mutex) { Mutex.Unlock(); }
  40. inline uptr getPrecedence() { return atomic_load_relaxed(&Precedence); }
  41. private:
  42. StaticSpinMutex Mutex;
  43. atomic_uintptr_t Precedence;
  44. };
  45. void initThread(bool MinimalInit);
  46. // TSD model specific fastpath functions definitions.
  47. #include "scudo_tsd_exclusive.inc"
  48. #include "scudo_tsd_shared.inc"
  49. } // namespace __scudo
  50. #endif // SCUDO_TSD_H_