1 //===-- memprof_posix.cpp ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 // Posix-specific details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
16 #error Only Posix supported
19 #include "memprof_thread.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
26 // ---------------------- TSD ---------------- {{{1
28 static pthread_key_t tsd_key
;
29 static bool tsd_key_inited
= false;
30 void TSDInit(void (*destructor
)(void *tsd
)) {
31 CHECK(!tsd_key_inited
);
32 tsd_key_inited
= true;
33 CHECK_EQ(0, pthread_key_create(&tsd_key
, destructor
));
37 CHECK(tsd_key_inited
);
38 return pthread_getspecific(tsd_key
);
41 void TSDSet(void *tsd
) {
42 CHECK(tsd_key_inited
);
43 pthread_setspecific(tsd_key
, tsd
);
46 void PlatformTSDDtor(void *tsd
) {
47 MemprofThreadContext
*context
= (MemprofThreadContext
*)tsd
;
48 if (context
->destructor_iterations
> 1) {
49 context
->destructor_iterations
--;
50 CHECK_EQ(0, pthread_setspecific(tsd_key
, tsd
));
53 MemprofThread::TSDDtor(tsd
);
55 } // namespace __memprof