Skip to content

Commit c3a8866

Browse files
authored
Merge d2cbeac into 94c2a30
2 parents 94c2a30 + d2cbeac commit c3a8866

18 files changed

Lines changed: 532 additions & 743 deletions

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ else()
8787
add_cxx_compiler_flag(-Wstrict-aliasing)
8888
endif()
8989
add_cxx_compiler_flag(-Wthread-safety)
90-
if (HAVE_WTHREAD_SAFETY)
91-
add_definitions(-DHAVE_WTHREAD_SAFETY)
90+
if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
9291
cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
9392
endif()
9493

@@ -152,7 +151,6 @@ cxx_feature_check(STD_REGEX)
152151
cxx_feature_check(GNU_POSIX_REGEX)
153152
cxx_feature_check(POSIX_REGEX)
154153
cxx_feature_check(STEADY_CLOCK)
155-
156154
# Ensure we have pthreads
157155
find_package(Threads REQUIRED)
158156

include/benchmark/benchmark_api.h

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -270,31 +270,25 @@ enum BigO {
270270
// computational complexity for the benchmark.
271271
typedef 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.
275280
class State {
276281
public:
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

src/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ endif()
88

99
# Define the source files
1010
set(SOURCE_FILES "benchmark.cc" "colorprint.cc" "commandlineflags.cc"
11-
"console_reporter.cc" "csv_reporter.cc" "json_reporter.cc"
12-
"log.cc" "reporter.cc" "sleep.cc" "string_util.cc"
13-
"sysinfo.cc" "walltime.cc" "complexity.cc")
11+
"console_reporter.cc" "csv_reporter.cc"
12+
"json_reporter.cc" "reporter.cc" "sleep.cc"
13+
"string_util.cc" "sysinfo.cc" "complexity.cc" "timers.cc")
1414
# Add headers to the list of source files. cmake does not require this,
1515
# but IDEs such as Visual Studio need this to add the headers
1616
# to the generated project.
@@ -19,8 +19,7 @@ list(APPEND SOURCE_FILES "${_d}/benchmark.h" "${_d}/benchmark_api.h"
1919
"${_d}/macros.h" "${_d}/reporter.h" "arraysize.h" "check.h"
2020
"colorprint.h" "commandlineflags.h" "complexity.h"
2121
"cycleclock.h" "internal_macros.h" "log.h" "mutex.h"
22-
"re.h" "sleep.h" "stat.h" "string_util.h" "sysinfo.h"
23-
"walltime.h")
22+
"re.h" "sleep.h" "stat.h" "string_util.h" "sysinfo.h" "timers.h")
2423
unset(_d)
2524

2625
# Determine the correct regular expression engine to use

0 commit comments

Comments
 (0)