Skip to content

Commit 6c5a175

Browse files
Abseil Teamcopybara-github
authored andcommitted
Change Abseil's SpinLock adaptive_spin_count to a class static variable that can be set by tcmalloc friend classes.
PiperOrigin-RevId: 826261810 Change-Id: I48eedaf091aff3d9c6b97b7eb8c71958711348cf
1 parent ed0efc0 commit 6c5a175

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

absl/base/internal/spinlock.cc

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,17 @@ void RegisterSpinLockProfiler(void (*fn)(const void *contendedlock,
7171
}
7272

7373
// Monitor the lock to see if its value changes within some time period
74-
// (adaptive_spin_count_ loop iterations). The last value read from the lock
74+
// (adaptive_spin_count loop iterations). The last value read from the lock
7575
// is returned from the method.
76-
ABSL_CONST_INIT std::atomic<int> SpinLock::adaptive_spin_count_{0};
7776
uint32_t SpinLock::SpinLoop() {
7877
// We are already in the slow path of SpinLock, initialize the
7978
// adaptive_spin_count here.
80-
if (adaptive_spin_count_.load(std::memory_order_relaxed) == 0) {
81-
int current_spin_count = 0;
82-
int new_spin_count = NumCPUs() > 1 ? 1000 : 1;
83-
// If this fails, the value will remain unchanged. We may not spin for the
84-
// intended duration, but that is still safe. We will try again on the next
85-
// call to SpinLoop.
86-
adaptive_spin_count_.compare_exchange_weak(
87-
current_spin_count, new_spin_count, std::memory_order_relaxed,
88-
std::memory_order_relaxed);
89-
}
90-
int c = adaptive_spin_count_.load(std::memory_order_relaxed);
79+
ABSL_CONST_INIT static absl::once_flag init_adaptive_spin_count;
80+
ABSL_CONST_INIT static int adaptive_spin_count = 0;
81+
LowLevelCallOnce(&init_adaptive_spin_count,
82+
[]() { adaptive_spin_count = NumCPUs() > 1 ? 1000 : 1; });
83+
84+
int c = adaptive_spin_count;
9185
uint32_t lock_value;
9286
do {
9387
lock_value = lockword_.load(std::memory_order_relaxed);

absl/base/internal/spinlock.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ namespace tcmalloc {
4747
namespace tcmalloc_internal {
4848

4949
class AllocationGuardSpinLockHolder;
50-
class Static;
5150

5251
} // namespace tcmalloc_internal
5352
} // namespace tcmalloc
@@ -174,16 +173,6 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
174173
// Provide access to protected method above. Use for testing only.
175174
friend struct SpinLockTest;
176175
friend class tcmalloc::tcmalloc_internal::AllocationGuardSpinLockHolder;
177-
friend class tcmalloc::tcmalloc_internal::Static;
178-
179-
static int GetAdaptiveSpinCount() {
180-
return adaptive_spin_count_.load(std::memory_order_relaxed);
181-
}
182-
static void SetAdaptiveSpinCount(int count) {
183-
adaptive_spin_count_.store(count, std::memory_order_relaxed);
184-
}
185-
186-
static std::atomic<int> adaptive_spin_count_;
187176

188177
private:
189178
// lockword_ is used to store the following:

0 commit comments

Comments
 (0)