From dfd07b2125cb461946608509ed57a400fa843f7e Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Mon, 5 Oct 2026 15:22:23 +0200 Subject: [PATCH] Add `State::in_timed_section()` helper In some scenarios, it can be useful to know if we are currently in a benchmark loop. The "are we currently in a timed section" check was already used internally, but only in asserts around timings. This change just packages that check into a public API on `State`, which makes user queries on the benchmark status a bit nicer. --- include/benchmark/state.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/benchmark/state.h b/include/benchmark/state.h index 69c4211b0..c0837d8f2 100644 --- a/include/benchmark/state.h +++ b/include/benchmark/state.h @@ -58,7 +58,7 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { // State is exported, and MSVC otherwise calls the copy of this function // inside benchmark.dll, which was built with the library's NDEBUG. inline BENCHMARK_ALWAYS_INLINE void PauseTiming() { - assert(started_ && !finished_ && !skipped() && + assert(in_timed_section() && "PauseTiming() called outside of the benchmark loop"); PauseTimingImpl(); } @@ -66,7 +66,7 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { // Only valid while the benchmark loop is running. Forced inline for the // same reason as PauseTiming(). inline BENCHMARK_ALWAYS_INLINE void ResumeTiming() { - assert(started_ && !finished_ && !skipped() && + assert(in_timed_section() && "ResumeTiming() called outside of the benchmark loop"); ResumeTimingImpl(); } @@ -77,6 +77,8 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { bool skipped() const { return internal::NotSkipped != skipped_; } + bool in_timed_section() const { return started_ && !finished_ && !skipped(); } + bool error_occurred() const { return internal::SkippedWithError == skipped_; } void SetIterationTime(double seconds);