[NFC][lsan] Add GetCurrentThreadId wrapper for GetCurrentThread
authorVitaly Buka <vitalybuka@google.com>
Fri, 14 Apr 2023 23:22:54 +0000 (16:22 -0700)
committerVitaly Buka <vitalybuka@google.com>
Sat, 15 Apr 2023 06:02:15 +0000 (23:02 -0700)
I am going to change return type of GetCurrentThreadId() in the next
patch.

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

compiler-rt/lib/lsan/lsan_fuchsia.cpp
compiler-rt/lib/lsan/lsan_interceptors.cpp
compiler-rt/lib/lsan/lsan_mac.cpp
compiler-rt/lib/lsan/lsan_posix.cpp
compiler-rt/lib/lsan/lsan_thread.cpp
compiler-rt/lib/lsan/lsan_thread.h

index 9bf18cc..4edac97 100644 (file)
@@ -99,7 +99,7 @@ void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
   OnCreatedArgs args;
   args.stack_begin = reinterpret_cast<uptr>(stack_base);
   args.stack_end = args.stack_begin + stack_size;
-  u32 parent_tid = GetCurrentThread();
+  u32 parent_tid = GetCurrentThreadId();
   u32 tid = ThreadCreate(parent_tid, detached, &args);
   return reinterpret_cast<void *>(static_cast<uptr>(tid));
 }
index 3a1b2af..3f8ef3f 100644 (file)
@@ -468,7 +468,7 @@ INTERCEPTOR(int, pthread_create, void *th, void *attr,
     res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
   }
   if (res == 0) {
-    int tid = ThreadCreate(GetCurrentThread(), IsStateDetached(detached));
+    int tid = ThreadCreate(GetCurrentThreadId(), IsStateDetached(detached));
     CHECK_NE(tid, kMainTid);
     atomic_store(&p.tid, tid, memory_order_release);
     while (atomic_load(&p.tid, memory_order_acquire) != 0)
index 8302efc..2bcd005 100644 (file)
@@ -67,7 +67,7 @@ typedef struct {
 
 ALWAYS_INLINE
 void lsan_register_worker_thread(int parent_tid) {
-  if (GetCurrentThread() == kInvalidTid) {
+  if (GetCurrentThreadId() == kInvalidTid) {
     u32 tid = ThreadCreate(parent_tid, true);
     ThreadStart(tid, GetTid());
   }
@@ -100,7 +100,7 @@ extern "C" lsan_block_context_t *alloc_lsan_context(void *ctxt,
       (lsan_block_context_t *)lsan_malloc(sizeof(lsan_block_context_t), stack);
   lsan_ctxt->block = ctxt;
   lsan_ctxt->func = func;
-  lsan_ctxt->parent_tid = GetCurrentThread();
+  lsan_ctxt->parent_tid = GetCurrentThreadId();
   return lsan_ctxt;
 }
 
@@ -145,13 +145,13 @@ void dispatch_source_set_event_handler(dispatch_source_t ds,
                                        void (^work)(void));
 }
 
-#define GET_LSAN_BLOCK(work)                 \
-  void (^lsan_block)(void);                  \
-  int parent_tid = GetCurrentThread();       \
-  lsan_block = ^(void) {                     \
-    lsan_register_worker_thread(parent_tid); \
-    work();                                  \
-  }
+#    define GET_LSAN_BLOCK(work)                 \
+      void (^lsan_block)(void);                  \
+      int parent_tid = GetCurrentThreadId();     \
+      lsan_block = ^(void) {                     \
+        lsan_register_worker_thread(parent_tid); \
+        work();                                  \
+      }
 
 INTERCEPTOR(void, dispatch_async, dispatch_queue_t dq, void (^work)(void)) {
   GET_LSAN_BLOCK(work);
index 097dfe0..d99e1cc 100644 (file)
@@ -89,7 +89,7 @@ static void OnStackUnwind(const SignalContext &sig, const void *,
 }
 
 void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {
-  HandleDeadlySignal(siginfo, context, GetCurrentThread(), &OnStackUnwind,
+  HandleDeadlySignal(siginfo, context, GetCurrentThreadId(), &OnStackUnwind,
                      nullptr);
 }
 
index a0fe95d..bc05021 100644 (file)
@@ -56,19 +56,20 @@ void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
   thread_registry->StartThread(tid, os_id, thread_type, arg);
 }
 
-void ThreadFinish() { thread_registry->FinishThread(GetCurrentThread()); }
+void ThreadFinish() { thread_registry->FinishThread(GetCurrentThreadId()); }
 
 ThreadContext *CurrentThreadContext() {
   if (!thread_registry)
     return nullptr;
-  if (GetCurrentThread() == kInvalidTid)
+  if (GetCurrentThreadId() == kInvalidTid)
     return nullptr;
   // No lock needed when getting current thread.
-  return (ThreadContext *)thread_registry->GetThreadLocked(GetCurrentThread());
+  return (ThreadContext *)thread_registry->GetThreadLocked(
+      GetCurrentThreadId());
 }
 
 void EnsureMainThreadIDIsCorrect() {
-  if (GetCurrentThread() == kMainTid)
+  if (GetCurrentThreadId() == kMainTid)
     CurrentThreadContext()->os_id = GetTid();
 }
 
index 66dbc5f..5abbaff 100644 (file)
@@ -52,6 +52,7 @@ u32 ThreadCreate(u32 tid, bool detached, void *arg = nullptr);
 void ThreadFinish();
 
 u32 GetCurrentThread();
+inline u32 GetCurrentThreadId() { return GetCurrentThread(); }
 void SetCurrentThread(u32 tid);
 ThreadContext *CurrentThreadContext();
 void EnsureMainThreadIDIsCorrect();