Index: compiler-rt/trunk/lib/lsan/lsan_common.h =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_common.h +++ compiler-rt/trunk/lib/lsan/lsan_common.h @@ -55,6 +55,8 @@ kIgnored = 3 }; +const u32 kInvalidTid = (u32) -1; + struct Flags { #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name; #include "lsan_flags.inc" Index: compiler-rt/trunk/lib/lsan/lsan_common_linux.cc =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_common_linux.cc +++ compiler-rt/trunk/lib/lsan/lsan_common_linux.cc @@ -34,6 +34,10 @@ return LibraryNameIs(full_name, kLinkerName); } +static THREADLOCAL u32 current_thread_tid = kInvalidTid; +u32 GetCurrentThread() { return current_thread_tid; } +void SetCurrentThread(u32 tid) { current_thread_tid = tid; } + __attribute__((tls_model("initial-exec"))) THREADLOCAL int disable_counter; bool DisabledInThisThread() { return disable_counter > 0; } Index: compiler-rt/trunk/lib/lsan/lsan_common_mac.cc =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_common_mac.cc +++ compiler-rt/trunk/lib/lsan/lsan_common_mac.cc @@ -22,43 +22,46 @@ namespace __lsan { +typedef struct { + int disable_counter; + u32 current_thread_id; +} thread_local_data_t; + static pthread_key_t key; static pthread_once_t key_once = PTHREAD_ONCE_INIT; static void make_tls_key() { CHECK_EQ(pthread_key_create(&key, NULL), 0); } -static int *get_tls_val(bool allocate) { +static thread_local_data_t *get_tls_val() { pthread_once(&key_once, make_tls_key); - int *ptr = (int *)pthread_getspecific(key); - if (ptr == NULL && allocate) { - ptr = (int *)InternalAlloc(sizeof(*ptr)); - *ptr = 0; + thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key); + if (ptr == NULL) { + ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr)); + ptr->disable_counter = 0; + ptr->current_thread_id = kInvalidTid; pthread_setspecific(key, ptr); } return ptr; } -bool DisabledInThisThread() { - int *disable_counter = get_tls_val(false); - return disable_counter ? *disable_counter > 0 : false; -} - -void DisableInThisThread() { - int *disable_counter = get_tls_val(true); +bool DisabledInThisThread() { return get_tls_val()->disable_counter > 0; } - ++*disable_counter; -} +void DisableInThisThread() { ++get_tls_val()->disable_counter; } void EnableInThisThread() { - int *disable_counter = get_tls_val(true); + int *disable_counter = &get_tls_val()->disable_counter; if (*disable_counter == 0) { DisableCounterUnderflow(); } --*disable_counter; } +u32 GetCurrentThread() { return get_tls_val()->current_thread_id; } + +void SetCurrentThread(u32 tid) { get_tls_val()->current_thread_id = tid; } + void InitializePlatformSpecificModules() { CHECK(0 && "unimplemented"); } Index: compiler-rt/trunk/lib/lsan/lsan_thread.cc =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_thread.cc +++ compiler-rt/trunk/lib/lsan/lsan_thread.cc @@ -22,10 +22,7 @@ namespace __lsan { -const u32 kInvalidTid = (u32) -1; - static ThreadRegistry *thread_registry; -static THREADLOCAL u32 current_thread_tid = kInvalidTid; static ThreadContextBase *CreateThreadContext(u32 tid) { void *mem = MmapOrDie(sizeof(ThreadContext), "ThreadContext"); @@ -41,14 +38,6 @@ ThreadRegistry(CreateThreadContext, kMaxThreads, kThreadQuarantineSize); } -u32 GetCurrentThread() { - return current_thread_tid; -} - -void SetCurrentThread(u32 tid) { - current_thread_tid = tid; -} - ThreadContext::ThreadContext(int tid) : ThreadContextBase(tid), stack_begin_(0),