From b6e597713293980189a083764f8a9b1bc6321add Mon Sep 17 00:00:00 2001 From: zjx20 Date: Sun, 5 Oct 2014 16:12:31 +0800 Subject: [PATCH 1/3] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 144ab319e6..a97c79ca0f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -15,6 +15,7 @@ Dominic Hamon Eugene Zhuk Felix Homann Google Inc. +JianXiong Zhou Lei Xu Matt Clarkson Oleksandr Sochka From 0fcd190cd68e9a2c7574e45f3a4cbba4807c1530 Mon Sep 17 00:00:00 2001 From: zjx20 Date: Sun, 5 Oct 2014 16:13:35 +0800 Subject: [PATCH 2/3] Update CONTRIBUTORS --- CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 6651b37c81..4c113857ca 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -29,6 +29,7 @@ David Coeurjolly Dominic Hamon Eugene Zhuk Felix Homann +JianXiong Zhou Lei Xu Matt Clarkson Oleksandr Sochka From 4a3348ce221eb5747b11d40afad1020eddd6c6fe Mon Sep 17 00:00:00 2001 From: "x.zhou" Date: Sun, 12 Oct 2014 18:01:45 +0800 Subject: [PATCH 3/3] Turn the global mutex into instance variables To fix #52 --- include/benchmark/benchmark.h | 2 ++ src/benchmark.cc | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index c0e4bb2697..89cea31640 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -142,6 +142,7 @@ BENCHMARK(BM_MultiThreaded)->Threads(4); #include #include #include +#include #include "macros.h" @@ -462,6 +463,7 @@ class Benchmark { std::vector rangeX_; std::vector rangeY_; std::vector thread_counts_; + std::mutex mutex_; // Special value placed in thread_counts_ to stand for NumCPUs() static const int kNumCpuMarker = -1; diff --git a/src/benchmark.cc b/src/benchmark.cc index babffa9797..72b59f124d 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -184,7 +184,6 @@ inline std::string HumanReadableNumber(double n) { // For non-dense Range, intermediate values are powers of kRangeMultiplier. static const int kRangeMultiplier = 8; -static std::mutex benchmark_mutex; std::mutex starting_mutex; std::condition_variable starting_cv; @@ -326,6 +325,7 @@ class BenchmarkFamilies { ~BenchmarkFamilies(); std::vector families_; + std::mutex mutex_; }; BenchmarkFamilies* BenchmarkFamilies::GetInstance() { @@ -342,7 +342,7 @@ BenchmarkFamilies::~BenchmarkFamilies() { } int BenchmarkFamilies::AddBenchmark(Benchmark* family) { - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); // This loop attempts to reuse an entry that was previously removed to avoid // unncessary growth of the vector. for (size_t index = 0; index < families_.size(); ++index) { @@ -357,7 +357,7 @@ int BenchmarkFamilies::AddBenchmark(Benchmark* family) { } void BenchmarkFamilies::RemoveBenchmark(int index) { - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); families_[index] = NULL; // Don't shrink families_ here, we might be called by the destructor of // BenchmarkFamilies which iterates over the vector. @@ -374,7 +374,7 @@ void BenchmarkFamilies::FindBenchmarks( return; } - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); for (internal::Benchmark* family : families_) { if (family == nullptr) continue; // Family was deleted @@ -707,7 +707,7 @@ Benchmark::~Benchmark() { } Benchmark* Benchmark::Arg(int x) { - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); rangeX_.push_back(x); return this; } @@ -716,7 +716,7 @@ Benchmark* Benchmark::Range(int start, int limit) { std::vector arglist; AddRange(&arglist, start, limit, kRangeMultiplier); - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); for (size_t i = 0; i < arglist.size(); ++i) rangeX_.push_back(arglist[i]); return this; } @@ -724,13 +724,13 @@ Benchmark* Benchmark::Range(int start, int limit) { Benchmark* Benchmark::DenseRange(int start, int limit) { CHECK_GE(start, 0); CHECK_LE(start, limit); - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); for (int arg = start; arg <= limit; ++arg) rangeX_.push_back(arg); return this; } Benchmark* Benchmark::ArgPair(int x, int y) { - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); rangeX_.push_back(x); rangeY_.push_back(y); return this; @@ -741,7 +741,7 @@ Benchmark* Benchmark::RangePair(int lo1, int hi1, int lo2, int hi2) { AddRange(&arglist1, lo1, hi1, kRangeMultiplier); AddRange(&arglist2, lo2, hi2, kRangeMultiplier); - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); rangeX_.resize(arglist1.size()); std::copy(arglist1.begin(), arglist1.end(), rangeX_.begin()); rangeY_.resize(arglist2.size()); @@ -756,7 +756,7 @@ Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) { Benchmark* Benchmark::Threads(int t) { CHECK_GT(t, 0); - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); thread_counts_.push_back(t); return this; } @@ -765,13 +765,13 @@ Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) { CHECK_GT(min_threads, 0); CHECK_GE(max_threads, min_threads); - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); AddRange(&thread_counts_, min_threads, max_threads, 2); return this; } Benchmark* Benchmark::ThreadPerCpu() { - std::lock_guard l(benchmark_mutex); + std::lock_guard l(mutex_); thread_counts_.push_back(NumCPUs()); return this; }