From f9228ec834edef9b623d4824dd006890c203adc3 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 26 Dec 2023 21:22:51 -0800 Subject: Migrate static objects to NoDestructor in tests, testing libraries and benchmarks. PiperOrigin-RevId: 593918110 Change-Id: Ide100c69b10e28011af17c7f82bb10eea072cad4 --- absl/synchronization/BUILD.bazel | 2 ++ absl/synchronization/blocking_counter_benchmark.cc | 5 +++-- absl/synchronization/mutex_benchmark.cc | 15 ++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'absl/synchronization') diff --git a/absl/synchronization/BUILD.bazel b/absl/synchronization/BUILD.bazel index 0550b61c..de06ebdd 100644 --- a/absl/synchronization/BUILD.bazel +++ b/absl/synchronization/BUILD.bazel @@ -191,6 +191,7 @@ cc_binary( deps = [ ":synchronization", ":thread_pool", + "//absl/base:no_destructor", "@com_github_google_benchmark//:benchmark_main", ], ) @@ -291,6 +292,7 @@ cc_library( ":thread_pool", "//absl/base", "//absl/base:config", + "//absl/base:no_destructor", "@com_github_google_benchmark//:benchmark_main", ], alwayslink = 1, diff --git a/absl/synchronization/blocking_counter_benchmark.cc b/absl/synchronization/blocking_counter_benchmark.cc index b504d1a5..ea2bf9f9 100644 --- a/absl/synchronization/blocking_counter_benchmark.cc +++ b/absl/synchronization/blocking_counter_benchmark.cc @@ -14,6 +14,7 @@ #include +#include "absl/base/no_destructor.h" #include "absl/synchronization/blocking_counter.h" #include "absl/synchronization/internal/thread_pool.h" #include "benchmark/benchmark.h" @@ -39,8 +40,8 @@ BENCHMARK(BM_BlockingCounter_SingleThread) ->Arg(256); void BM_BlockingCounter_DecrementCount(benchmark::State& state) { - static absl::BlockingCounter* counter = - new absl::BlockingCounter{std::numeric_limits::max()}; + static absl::NoDestructor counter( + std::numeric_limits::max()); for (auto _ : state) { counter->DecrementCount(); } diff --git a/absl/synchronization/mutex_benchmark.cc b/absl/synchronization/mutex_benchmark.cc index c3f54764..06888dfe 100644 --- a/absl/synchronization/mutex_benchmark.cc +++ b/absl/synchronization/mutex_benchmark.cc @@ -19,6 +19,7 @@ #include "absl/base/config.h" #include "absl/base/internal/cycleclock.h" #include "absl/base/internal/spinlock.h" +#include "absl/base/no_destructor.h" #include "absl/synchronization/blocking_counter.h" #include "absl/synchronization/internal/thread_pool.h" #include "absl/synchronization/mutex.h" @@ -27,17 +28,17 @@ namespace { void BM_Mutex(benchmark::State& state) { - static absl::Mutex* mu = new absl::Mutex; + static absl::NoDestructor mu; for (auto _ : state) { - absl::MutexLock lock(mu); + absl::MutexLock lock(mu.get()); } } BENCHMARK(BM_Mutex)->UseRealTime()->Threads(1)->ThreadPerCpu(); void BM_ReaderLock(benchmark::State& state) { - static absl::Mutex* mu = new absl::Mutex; + static absl::NoDestructor mu; for (auto _ : state) { - absl::ReaderMutexLock lock(mu); + absl::ReaderMutexLock lock(mu.get()); } } BENCHMARK(BM_ReaderLock)->UseRealTime()->Threads(1)->ThreadPerCpu(); @@ -53,7 +54,7 @@ void BM_TryLock(benchmark::State& state) { BENCHMARK(BM_TryLock); void BM_ReaderTryLock(benchmark::State& state) { - static absl::Mutex* mu = new absl::Mutex; + static absl::NoDestructor mu; for (auto _ : state) { if (mu->ReaderTryLock()) { mu->ReaderUnlock(); @@ -133,7 +134,7 @@ void BM_MutexEnqueue(benchmark::State& state) { std::atomic blocked_threads{0}; std::atomic thread_has_mutex{false}; }; - static Shared* shared = new Shared; + static absl::NoDestructor shared; // Set up 'blocked_threads' to count how many threads are currently blocked // in Abseil synchronization code. @@ -211,7 +212,7 @@ void BM_Contended(benchmark::State& state) { MutexType mu; int data = 0; }; - static auto* shared = new Shared; + static absl::NoDestructor shared; int local = 0; for (auto _ : state) { // Here we model both local work outside of the critical section as well as -- cgit v1.2.3