2525#include < cstdlib>
2626#include < unistd.h>
2727
28+ #ifdef DDPROF_USE_LOADER
29+ extern " C"
30+ __attribute ((tls_model(" initial-exec" ))) __thread void *ddprof_lib_state;
31+ #else
32+ __attribute ((tls_model(" initial-exec" ))) __thread void *ddprof_lib_state;
33+ #endif
34+
2835namespace ddprof {
2936
3037// Static declarations
@@ -53,13 +60,7 @@ DDPROF_NOINLINE auto sleep_and_retry_reserve(MPSCRingBufferWriter &writer,
5360} // namespace
5461
5562TrackerThreadLocalState *AllocationTracker::get_tl_state () {
56- // In shared libraries, TLS access requires a call to tls_get_addr,
57- // tls_get_addr can call into malloc, which can create a recursive loop
58- // instead we call pthread APIs to control the creation of TLS objects
59- pthread_once (&_key_once, make_key);
60- auto *tl_state = static_cast <TrackerThreadLocalState *>(
61- pthread_getspecific (_tl_state_key));
62- return tl_state;
63+ return static_cast <TrackerThreadLocalState *>(ddprof_lib_state);
6364}
6465
6566TrackerThreadLocalState *AllocationTracker::init_tl_state () {
@@ -69,13 +70,7 @@ TrackerThreadLocalState *AllocationTracker::init_tl_state() {
6970 auto tl_state = std::make_unique<TrackerThreadLocalState>();
7071 tl_state->tid = ddprof::gettid ();
7172 tl_state->stack_bounds = retrieve_stack_bounds ();
72-
73- if (int const res = pthread_setspecific (_tl_state_key, tl_state.get ());
74- res != 0 ) {
75- // should return 0
76- LG_DBG (" Unable to store tl_state. Error %d: %s\n " , res, strerror (res));
77- tl_state.reset ();
78- }
73+ ddprof_lib_state = tl_state.get ();
7974
8075 return tl_state.release ();
8176}
@@ -91,11 +86,6 @@ void AllocationTracker::delete_tl_state(void *tl_state) {
9186 delete static_cast <TrackerThreadLocalState *>(tl_state);
9287}
9388
94- void AllocationTracker::make_key () {
95- // delete is called on all key objects
96- pthread_key_create (&_tl_state_key, delete_tl_state);
97- }
98-
9989DDRes AllocationTracker::allocation_tracking_init (
10090 uint64_t allocation_profiling_rate, uint32_t flags,
10191 uint32_t stack_sample_size, const RingBufferInfo &ring_buffer,
0 commit comments