memprof_posix.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===-- memprof_posix.cpp ------------------------------------------------===//
  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. // This file is a part of MemProfiler, a memory profiler.
  10. //
  11. // Posix-specific details.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if !SANITIZER_POSIX
  15. #error Only Posix supported
  16. #endif
  17. #include "memprof_thread.h"
  18. #include "sanitizer_common/sanitizer_internal_defs.h"
  19. #include <pthread.h>
  20. namespace __memprof {
  21. // ---------------------- TSD ---------------- {{{1
  22. static pthread_key_t tsd_key;
  23. static bool tsd_key_inited = false;
  24. void TSDInit(void (*destructor)(void *tsd)) {
  25. CHECK(!tsd_key_inited);
  26. tsd_key_inited = true;
  27. CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
  28. }
  29. void *TSDGet() {
  30. CHECK(tsd_key_inited);
  31. return pthread_getspecific(tsd_key);
  32. }
  33. void TSDSet(void *tsd) {
  34. CHECK(tsd_key_inited);
  35. pthread_setspecific(tsd_key, tsd);
  36. }
  37. void PlatformTSDDtor(void *tsd) {
  38. MemprofThreadContext *context = (MemprofThreadContext *)tsd;
  39. if (context->destructor_iterations > 1) {
  40. context->destructor_iterations--;
  41. CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
  42. return;
  43. }
  44. MemprofThread::TSDDtor(tsd);
  45. }
  46. } // namespace __memprof