diff options
author | Abseil Team <absl-team@google.com> | 2023-12-26 21:22:51 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-12-26 21:23:47 -0800 |
commit | f9228ec834edef9b623d4824dd006890c203adc3 (patch) | |
tree | c41a1f543553fb5c8416005bfdb55048b2902d10 /absl/synchronization | |
parent | bd47468324e0db12aac9a57128d40fc23c233fb4 (diff) |
Migrate static objects to NoDestructor in tests, testing libraries and benchmarks.
PiperOrigin-RevId: 593918110
Change-Id: Ide100c69b10e28011af17c7f82bb10eea072cad4
Diffstat (limited to 'absl/synchronization')
-rw-r--r-- | absl/synchronization/BUILD.bazel | 2 | ||||
-rw-r--r-- | absl/synchronization/blocking_counter_benchmark.cc | 5 | ||||
-rw-r--r-- | absl/synchronization/mutex_benchmark.cc | 15 |
3 files changed, 13 insertions, 9 deletions
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 <limits> +#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<int>::max()}; + static absl::NoDestructor<absl::BlockingCounter> counter( + std::numeric_limits<int>::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<absl::Mutex> 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<absl::Mutex> 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<absl::Mutex> mu; for (auto _ : state) { if (mu->ReaderTryLock()) { mu->ReaderUnlock(); @@ -133,7 +134,7 @@ void BM_MutexEnqueue(benchmark::State& state) { std::atomic<int> blocked_threads{0}; std::atomic<bool> thread_has_mutex{false}; }; - static Shared* shared = new Shared; + static absl::NoDestructor<Shared> 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> shared; int local = 0; for (auto _ : state) { // Here we model both local work outside of the critical section as well as |