Skip to content

Commit

Permalink
absl: add Mutex::[Reader]TryLock benchmark
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 566991965
Change-Id: I6c4d64de79d303e69b18330bda04fdc84d40893d
  • Loading branch information
dvyukov authored and copybara-github committed Sep 20, 2023
1 parent 28549d1 commit adcaae4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions absl/synchronization/mutex_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ void BM_ReaderLock(benchmark::State& state) {
}
BENCHMARK(BM_ReaderLock)->UseRealTime()->Threads(1)->ThreadPerCpu();

void BM_TryLock(benchmark::State& state) {
absl::Mutex mu;
for (auto _ : state) {
if (mu.TryLock()) {
mu.Unlock();
}
}
}
BENCHMARK(BM_TryLock);

void BM_ReaderTryLock(benchmark::State& state) {
static absl::Mutex* mu = new absl::Mutex;
for (auto _ : state) {
if (mu->ReaderTryLock()) {
mu->ReaderUnlock();
}
}
}
BENCHMARK(BM_ReaderTryLock)->UseRealTime()->Threads(1)->ThreadPerCpu();

static void DelayNs(int64_t ns, int* data) {
int64_t end = absl::base_internal::CycleClock::Now() +
ns * absl::base_internal::CycleClock::Frequency() / 1e9;
Expand Down

0 comments on commit adcaae4

Please sign in to comment.