Commit 373cc411 by Matt Clarkson Committed by Dominic Hamon

C++11 concurrency instead of pthread

parent 6b1a6958
...@@ -139,8 +139,8 @@ BENCHMARK(BM_MultiThreaded)->Threads(4); ...@@ -139,8 +139,8 @@ BENCHMARK(BM_MultiThreaded)->Threads(4);
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <pthread.h>
#include <string> #include <string>
#include <thread>
#include <vector> #include <vector>
#include "macros.h" #include "macros.h"
...@@ -263,7 +263,7 @@ class State { ...@@ -263,7 +263,7 @@ class State {
// BenchmarkInstance // BenchmarkInstance
SharedState* shared_; SharedState* shared_;
pthread_t thread_; std::thread thread_;
// Custom label set by the user. // Custom label set by the user.
std::string label_; std::string label_;
......
#ifndef BENCHMARK_MUTEX_LOCK_H_
#define BENCHMARK_MUTEX_LOCK_H_
#include <pthread.h>
namespace benchmark {
class mutex_lock {
public:
explicit mutex_lock(pthread_mutex_t* mu) : mu_(mu) {
pthread_mutex_lock(mu_);
}
~mutex_lock() { pthread_mutex_unlock(mu_); }
private:
pthread_mutex_t* mu_;
};
} // end namespace benchmark
#endif // BENCHMARK_MUTEX_LOCK_H_
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -28,18 +27,18 @@ ...@@ -28,18 +27,18 @@
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <mutex>
#include "benchmark/macros.h" #include "benchmark/macros.h"
#include "cycleclock.h" #include "cycleclock.h"
#include "mutex_lock.h"
#include "sleep.h" #include "sleep.h"
namespace benchmark { namespace benchmark {
namespace { namespace {
pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT; std::once_flag cpuinfo_init;
double cpuinfo_cycles_per_second = 1.0; double cpuinfo_cycles_per_second = 1.0;
int cpuinfo_num_cpus = 1; // Conservative guess int cpuinfo_num_cpus = 1; // Conservative guess
pthread_mutex_t cputimens_mutex; std::mutex cputimens_mutex;
#if !defined OS_MACOSX #if !defined OS_MACOSX
const int64_t estimate_time_ms = 1000; const int64_t estimate_time_ms = 1000;
...@@ -76,9 +75,6 @@ bool ReadIntFromFile(const char* file, int* value) { ...@@ -76,9 +75,6 @@ bool ReadIntFromFile(const char* file, int* value) {
#endif #endif
void InitializeSystemInfo() { void InitializeSystemInfo() {
// TODO: destroy this
pthread_mutex_init(&cputimens_mutex, NULL);
#if defined OS_LINUX || defined OS_CYGWIN #if defined OS_LINUX || defined OS_CYGWIN
char line[1024]; char line[1024];
char* err; char* err;
...@@ -315,7 +311,7 @@ static bool MyCPUUsageCPUTimeNsLocked(double* cputime) { ...@@ -315,7 +311,7 @@ static bool MyCPUUsageCPUTimeNsLocked(double* cputime) {
double MyCPUUsage() { double MyCPUUsage() {
{ {
mutex_lock l(&cputimens_mutex); std::lock_guard<std::mutex> l(cputimens_mutex);
static bool use_cputime_ns = true; static bool use_cputime_ns = true;
if (use_cputime_ns) { if (use_cputime_ns) {
double value; double value;
...@@ -344,12 +340,12 @@ double ChildrenCPUUsage() { ...@@ -344,12 +340,12 @@ double ChildrenCPUUsage() {
#endif // OS_WINDOWS #endif // OS_WINDOWS
double CyclesPerSecond(void) { double CyclesPerSecond(void) {
pthread_once(&cpuinfo_init, &InitializeSystemInfo); std::call_once(cpuinfo_init, InitializeSystemInfo);
return cpuinfo_cycles_per_second; return cpuinfo_cycles_per_second;
} }
int NumCPUs(void) { int NumCPUs(void) {
pthread_once(&cpuinfo_init, &InitializeSystemInfo); std::call_once(cpuinfo_init, InitializeSystemInfo);
return cpuinfo_num_cpus; return cpuinfo_num_cpus;
} }
} // end namespace benchmark } // end namespace benchmark
# Demonstration executable # Demonstration executable
add_executable(benchmark_test benchmark_test.cc) add_executable(benchmark_test benchmark_test.cc)
target_link_libraries(benchmark_test benchmark ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(benchmark_test benchmark ${CMAKE_THREAD_LIBS_INIT})
add_test(benchmark benchmark_test)
# Test harness for regex wrapper # Test harness for regex wrapper
add_executable(re_test ${RE_FILES} "re_test.cc") add_executable(re_test ${RE_FILES} "re_test.cc")
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <limits> #include <limits>
#include <list> #include <list>
#include <map> #include <map>
#include <mutex>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
...@@ -34,7 +35,7 @@ std::set<int> ConstructRandomSet(int size) { ...@@ -34,7 +35,7 @@ std::set<int> ConstructRandomSet(int size) {
return s; return s;
} }
pthread_mutex_t test_vector_mu; std::mutex test_vector_mu;
std::vector<int>* test_vector = nullptr; std::vector<int>* test_vector = nullptr;
} // end namespace } // end namespace
...@@ -113,23 +114,20 @@ BENCHMARK(BM_StringCompare)->Range(1, 1<<20); ...@@ -113,23 +114,20 @@ BENCHMARK(BM_StringCompare)->Range(1, 1<<20);
static void BM_SetupTeardown(benchmark::State& state) { static void BM_SetupTeardown(benchmark::State& state) {
if (state.thread_index == 0) { if (state.thread_index == 0) {
pthread_mutex_init(&test_vector_mu, nullptr);
// No need to lock test_vector_mu here as this is running single-threaded. // No need to lock test_vector_mu here as this is running single-threaded.
test_vector = new std::vector<int>(); test_vector = new std::vector<int>();
} }
int i = 0; int i = 0;
while (state.KeepRunning()) { while (state.KeepRunning()) {
pthread_mutex_lock(&test_vector_mu); std::lock_guard<std::mutex> l(test_vector_mu);
if (i%2 == 0) if (i%2 == 0)
test_vector->push_back(i); test_vector->push_back(i);
else else
test_vector->pop_back(); test_vector->pop_back();
pthread_mutex_unlock(&test_vector_mu);
++i; ++i;
} }
if (state.thread_index == 0) { if (state.thread_index == 0) {
delete test_vector; delete test_vector;
pthread_mutex_destroy(&test_vector_mu);
} }
} }
BENCHMARK(BM_SetupTeardown)->ThreadPerCpu(); BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment