[compiler-rt] [scudo] Small refactor of getTSDXXX functions. (PR #208127)

Christopher Ferris via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 7 17:56:23 PDT 2026


https://github.com/cferris1000 created https://github.com/llvm/llvm-project/pull/208127

Small modification in getTSDAndLock() to have a single shared TSD avoid the tryLock call. Also, remove some unnecessary comments.

Add a DCHECK in getTSDSlow and only check for == 1 since a 0 number of shared TSDs is not supported.

>From 33418a0c7f1c2a3a3ff04fb831efde235c161a4f Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Tue, 7 Jul 2026 22:06:42 +0000
Subject: [PATCH] [scudo] Small refactor of getTSDXXX functions.

Small modification in getTSDAndLock() to have a single shared TSD
avoid the tryLock call. Also, remove some unnecessary comments.

Add a DCHECK in getTSDSlow and only check for == 1 since a 0 number
of shared TSDs is not supported.
---
 compiler-rt/lib/scudo/standalone/tsd_shared.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/tsd_shared.h b/compiler-rt/lib/scudo/standalone/tsd_shared.h
index 5f9092b36d42a..859f42eb373b2 100644
--- a/compiler-rt/lib/scudo/standalone/tsd_shared.h
+++ b/compiler-rt/lib/scudo/standalone/tsd_shared.h
@@ -141,16 +141,14 @@ struct TSDRegistrySharedT {
   ALWAYS_INLINE TSD<Allocator> *getTSDAndLock() NO_THREAD_SAFETY_ANALYSIS {
     TSD<Allocator> *TSD = getCurrentTSD();
     DCHECK(TSD);
-    // Try to lock the currently associated context.
-    if (TSD->tryLock())
-      return TSD;
-    // If that fails, go down the slow path.
     if (TSDsArraySize == 1U) {
-      // Only 1 TSD, not need to go any further.
-      // The compiler will optimize this one way or the other.
+      // Only 1 TSD, lock and return.
       TSD->lock();
       return TSD;
     }
+
+    if (TSD->tryLock())
+      return TSD;
     return getTSDSlow(TSD);
   }
 
@@ -176,6 +174,7 @@ struct TSDRegistrySharedT {
 
   // Requires a lock to avoid multiple threads trying to set the TSDs at once.
   bool setNumberOfTSDs(u32 N) REQUIRES(Mutex) {
+    DCHECK_GT(N, 0);
     const u32 TotalTSDs = atomic_load_relaxed(&NumberOfTSDs);
     // In order to avoid needing locks for these values, the number of TSDs
     // can never be decreased.
@@ -234,7 +233,7 @@ struct TSDRegistrySharedT {
   // capability.
   NOINLINE TSD<Allocator> *getTSDSlow(TSD<Allocator> *CurrentTSD) {
     const u32 TotalTSDs = atomic_load_relaxed(&NumberOfTSDs);
-    if (UNLIKELY(TotalTSDs <= 1U)) {
+    if (UNLIKELY(TotalTSDs == 1U)) {
       CurrentTSD->lock();
       return CurrentTSD;
     }



More information about the llvm-commits mailing list