@@ -270,31 +270,25 @@ enum BigO {
270270// computational complexity for the benchmark.
271271typedef double (BigOFunc)(int );
272272
273+ namespace internal {
274+ class ThreadTimer ;
275+ class ThreadManager ;
276+ }
277+
273278// State is passed to a running Benchmark and contains state for the
274279// benchmark to use.
275280class State {
276281public:
277- State (size_t max_iters, const std::vector<int >& ranges,
278- int thread_i, int n_threads);
279-
280282 // Returns true if the benchmark should continue through another iteration.
281283 // NOTE: A benchmark may not return from the test until KeepRunning() has
282284 // returned false.
283285 bool KeepRunning () {
284286 if (BENCHMARK_BUILTIN_EXPECT (!started_, false )) {
285- assert (!finished_);
286- started_ = true ;
287- ResumeTiming ();
287+ StartKeepRunning ();
288288 }
289289 bool const res = total_iterations_++ < max_iterations;
290290 if (BENCHMARK_BUILTIN_EXPECT (!res, false )) {
291- assert (started_ && (!finished_ || error_occurred_));
292- if (!error_occurred_) {
293- PauseTiming ();
294- }
295- // Total iterations now is one greater than max iterations. Fix this.
296- total_iterations_ = max_iterations;
297- finished_ = true ;
291+ FinishKeepRunning ();
298292 }
299293 return res;
300294 }
@@ -304,10 +298,11 @@ class State {
304298 // Stop the benchmark timer. If not called, the timer will be
305299 // automatically stopped after KeepRunning() returns false for the first time.
306300 //
307- // For threaded benchmarks the PauseTiming() function acts
308- // like a barrier. I.e., the ith call by a particular thread to this
309- // function will block until all active threads have made their ith call.
310- // The timer will stop when the last thread has called this function.
301+ // For threaded benchmarks the PauseTiming() function only pauses the timing
302+ // for the current thread.
303+ //
304+ // NOTE: The "real time" measurement is per-thread. If different threads
305+ // report different measurements the largest one is reported.
311306 //
312307 // NOTE: PauseTiming()/ResumeTiming() are relatively
313308 // heavyweight, and so their use should generally be avoided
@@ -319,11 +314,6 @@ class State {
319314 // Start the benchmark timer. The timer is NOT running on entrance to the
320315 // benchmark function. It begins running after the first call to KeepRunning()
321316 //
322- // For threaded benchmarks the ResumeTiming() function acts
323- // like a barrier. I.e., the ith call by a particular thread to this
324- // function will block until all active threads have made their ith call.
325- // The timer will start when the last thread has called this function.
326- //
327317 // NOTE: PauseTiming()/ResumeTiming() are relatively
328318 // heavyweight, and so their use should generally be avoided
329319 // within each benchmark iteration, if possible.
@@ -335,10 +325,10 @@ class State {
335325 // thread and report an error with the specified 'msg'. After this call
336326 // the user may explicitly 'return' from the benchmark.
337327 //
338- // For threaded benchmarks only the current thread stops executing. If
339- // multiple threads report an error only the first error message is used.
340- // The current thread is no longer considered 'active' by
341- // 'PauseTiming()' and 'ResumingTiming()' .
328+ // For threaded benchmarks only the current thread stops executing and future
329+ // calls to `KeepRunning()` will block until all threads have completed
330+ // the `KeepRunning()` loop. If multiple threads report an error only the
331+ // first error message is used .
342332 //
343333 // NOTE: Calling 'SkipWithError(...)' does not cause the benchmark to exit
344334 // the current scope immediately. If the function is called from within
@@ -351,10 +341,8 @@ class State {
351341 // is used instead of automatically measured time if UseManualTime() was
352342 // specified.
353343 //
354- // For threaded benchmarks the SetIterationTime() function acts
355- // like a barrier. I.e., the ith call by a particular thread to this
356- // function will block until all threads have made their ith call.
357- // The time will be set by the last thread to call this function.
344+ // For threaded benchmarks the final value will be set to the largest
345+ // reported values.
358346 void SetIterationTime (double seconds);
359347
360348 // Set the number of bytes processed by the current benchmark
@@ -465,7 +453,16 @@ class State {
465453 const int threads;
466454 const size_t max_iterations;
467455
468- private:
456+ // TODO make me private
457+ State (size_t max_iters, const std::vector<int >& ranges, int thread_i,
458+ int n_threads, internal::ThreadTimer* timer,
459+ internal::ThreadManager* manager);
460+
461+ private:
462+ void StartKeepRunning ();
463+ void FinishKeepRunning ();
464+ internal::ThreadTimer* timer_;
465+ internal::ThreadManager* manager_;
469466 BENCHMARK_DISALLOW_COPY_AND_ASSIGN (State);
470467};
471468
0 commit comments