diff --git a/absl/base/internal/spinlock_benchmark.cc b/absl/base/internal/spinlock_benchmark.cc index 0451c65f955..7135d3f5616 100644 --- a/absl/base/internal/spinlock_benchmark.cc +++ b/absl/base/internal/spinlock_benchmark.cc @@ -23,9 +23,23 @@ namespace { +template +static void BM_TryLock(benchmark::State& state) { + // Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect. + ABSL_INTERNAL_CHECK( + absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() != + nullptr, + "GetOrCreateCurrentThreadIdentity() failed"); + + static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode); + for (auto _ : state) { + if (spinlock->TryLock()) spinlock->Unlock(); + } +} + template static void BM_SpinLock(benchmark::State& state) { - // Ensure a ThreadIdentity is installed. + // Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect. ABSL_INTERNAL_CHECK( absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() != nullptr, @@ -49,4 +63,15 @@ BENCHMARK_TEMPLATE(BM_SpinLock, ->Threads(1) ->ThreadPerCpu(); +BENCHMARK_TEMPLATE(BM_TryLock, absl::base_internal::SCHEDULE_KERNEL_ONLY) + ->UseRealTime() + ->Threads(1) + ->ThreadPerCpu(); + +BENCHMARK_TEMPLATE(BM_TryLock, + absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL) + ->UseRealTime() + ->Threads(1) + ->ThreadPerCpu(); + } // namespace