[compiler-rt] r295405 - Use pthreads to store current thread id on darwin

Francis Ricci via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 16 19:23:07 PST 2017


Author: fjricci
Date: Thu Feb 16 21:23:07 2017
New Revision: 295405

URL: http://llvm.org/viewvc/llvm-project?rev=295405&view=rev
Log:
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

Modified:
    compiler-rt/trunk/lib/lsan/lsan_common.h
    compiler-rt/trunk/lib/lsan/lsan_common_linux.cc
    compiler-rt/trunk/lib/lsan/lsan_common_mac.cc
    compiler-rt/trunk/lib/lsan/lsan_thread.cc

Modified: compiler-rt/trunk/lib/lsan/lsan_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common.h?rev=295405&r1=295404&r2=295405&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common.h (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common.h Thu Feb 16 21:23:07 2017
@@ -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"

Modified: compiler-rt/trunk/lib/lsan/lsan_common_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common_linux.cc?rev=295405&r1=295404&r2=295405&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common_linux.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common_linux.cc Thu Feb 16 21:23:07 2017
@@ -34,6 +34,10 @@ static bool IsLinker(const char* full_na
   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; }

Modified: compiler-rt/trunk/lib/lsan/lsan_common_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common_mac.cc?rev=295405&r1=295404&r2=295405&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common_mac.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common_mac.cc Thu Feb 16 21:23:07 2017
@@ -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");
 }

Modified: compiler-rt/trunk/lib/lsan/lsan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_thread.cc?rev=295405&r1=295404&r2=295405&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_thread.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_thread.cc Thu Feb 16 21:23:07 2017
@@ -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),




More information about the llvm-commits mailing list