Use pthreads to store current thread id on darwin

Summary:
__thread is not supported by all darwin versions and architectures,
use pthreads instead to allow for building darwin lsan on iossim.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29993

llvm-svn: 295405
This commit is contained in:
Francis Ricci 2017-02-17 03:23:07 +00:00
parent ff266f5236
commit 571c05a550
4 changed files with 24 additions and 26 deletions

View File

@ -55,6 +55,8 @@ enum ChunkTag {
kIgnored = 3
};
const u32 kInvalidTid = (u32) -1;
struct Flags {
#define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
#include "lsan_flags.inc"

View File

@ -34,6 +34,10 @@ static bool IsLinker(const char* full_name) {
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; }

View File

@ -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;
}
bool DisabledInThisThread() { return get_tls_val()->disable_counter > 0; }
void DisableInThisThread() {
int *disable_counter = get_tls_val(true);
++*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");
}

View File

@ -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 @@ void InitializeThreadRegistry() {
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),