[NFC][lsan] Move SetCurrentThread call
authorVitaly Buka <vitalybuka@google.com>
Fri, 14 Apr 2023 22:43:56 +0000 (15:43 -0700)
committerVitaly Buka <vitalybuka@google.com>
Fri, 14 Apr 2023 23:28:13 +0000 (16:28 -0700)
Future patch will set the current context as well.
Existing callsite requires additional lock to find context.

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

compiler-rt/lib/lsan/lsan_fuchsia.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 03ac0af..9bf18cc 100644 (file)
@@ -46,6 +46,7 @@ struct OnStartedArgs {
 };
 
 void ThreadContext::OnStarted(void *arg) {
+  ThreadContextLsanBase::OnStarted(arg);
   auto args = reinterpret_cast<const OnStartedArgs *>(arg);
   cache_begin_ = args->cache_begin;
   cache_end_ = args->cache_end;
index 6964a9b..8302efc 100644 (file)
@@ -70,7 +70,6 @@ void lsan_register_worker_thread(int parent_tid) {
   if (GetCurrentThread() == kInvalidTid) {
     u32 tid = ThreadCreate(parent_tid, true);
     ThreadStart(tid, GetTid());
-    SetCurrentThread(tid);
   }
 }
 
index 3c7bc15..097dfe0 100644 (file)
@@ -35,6 +35,7 @@ struct OnStartedArgs {
 };
 
 void ThreadContext::OnStarted(void *arg) {
+  ThreadContextLsanBase::OnStarted(arg);
   auto args = reinterpret_cast<const OnStartedArgs *>(arg);
   stack_begin_ = args->stack_begin;
   stack_end_ = args->stack_end;
index 137c7e4..a0fe95d 100644 (file)
@@ -39,9 +39,12 @@ void InitializeThreadRegistry() {
 ThreadContextLsanBase::ThreadContextLsanBase(int tid)
     : ThreadContextBase(tid) {}
 
+void ThreadContextLsanBase::OnStarted(void *arg) { SetCurrentThread(tid); }
+
 void ThreadContextLsanBase::OnFinished() {
   AllocatorThreadFinish();
   DTLS_Destroy();
+  SetCurrentThread(kInvalidTid);
 }
 
 u32 ThreadCreate(u32 parent_tid, bool detached, void *arg) {
@@ -51,13 +54,9 @@ u32 ThreadCreate(u32 parent_tid, bool detached, void *arg) {
 void ThreadContextLsanBase::ThreadStart(u32 tid, tid_t os_id,
                                         ThreadType thread_type, void *arg) {
   thread_registry->StartThread(tid, os_id, thread_type, arg);
-  SetCurrentThread(tid);
 }
 
-void ThreadFinish() {
-  thread_registry->FinishThread(GetCurrentThread());
-  SetCurrentThread(kInvalidTid);
-}
+void ThreadFinish() { thread_registry->FinishThread(GetCurrentThread()); }
 
 ThreadContext *CurrentThreadContext() {
   if (!thread_registry)
index 049c7e2..66dbc5f 100644 (file)
@@ -21,6 +21,7 @@ namespace __lsan {
 class ThreadContextLsanBase : public ThreadContextBase {
  public:
   explicit ThreadContextLsanBase(int tid);
+  void OnStarted(void *arg) override;
   void OnFinished() override;
   uptr stack_begin() { return stack_begin_; }
   uptr stack_end() { return stack_end_; }