[scudo] Small refactor of getTSDXXX functions. (#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.
This commit is contained in:
Christopher Ferris 2026-07-09 20:08:28 +00:00 committed by GitHub
parent 1b5d76004b
commit 766161cd2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -141,16 +141,14 @@ private:
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 @@ private:
// 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 @@ private:
// 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;
}