Path: blob/main/contrib/llvm-project/compiler-rt/lib/memprof/memprof_posix.cpp
35236 views
//===-- memprof_posix.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of MemProfiler, a memory profiler.9//10// Posix-specific details.11//===----------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"14#if !SANITIZER_POSIX15#error Only Posix supported16#endif1718#include "memprof_thread.h"19#include "sanitizer_common/sanitizer_internal_defs.h"2021#include <pthread.h>2223namespace __memprof {2425// ---------------------- TSD ---------------- {{{12627static pthread_key_t tsd_key;28static bool tsd_key_inited = false;29void TSDInit(void (*destructor)(void *tsd)) {30CHECK(!tsd_key_inited);31tsd_key_inited = true;32CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));33}3435void *TSDGet() {36CHECK(tsd_key_inited);37return pthread_getspecific(tsd_key);38}3940void TSDSet(void *tsd) {41CHECK(tsd_key_inited);42pthread_setspecific(tsd_key, tsd);43}4445void PlatformTSDDtor(void *tsd) {46MemprofThreadContext *context = (MemprofThreadContext *)tsd;47if (context->destructor_iterations > 1) {48context->destructor_iterations--;49CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));50return;51}52MemprofThread::TSDDtor(tsd);53}54} // namespace __memprof555657