aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-02-15 18:43:47 +0900
committerShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-02-15 19:11:52 +0900
commit545b6a29c829b9fade0f1405e638f86c8f4668b1 (patch)
treece6288bac852e8d1227d2c1a3f61af00b2840bc8
parent2d353a0c5c1e5eee1e986334445063fd78cf1c97 (diff)
downloadplatform_build_kati-545b6a29c829b9fade0f1405e638f86c8f4668b1.tar.gz
platform_build_kati-545b6a29c829b9fade0f1405e638f86c8f4668b1.tar.bz2
platform_build_kati-545b6a29c829b9fade0f1405e638f86c8f4668b1.zip
[C++] Use TLS for Stats again
-rw-r--r--stats.cc12
-rw-r--r--stats.h1
-rw-r--r--thread_local.h11
3 files changed, 18 insertions, 6 deletions
diff --git a/stats.cc b/stats.cc
index 920d13f..7afda4c 100644
--- a/stats.cc
+++ b/stats.cc
@@ -22,12 +22,14 @@
#include "log.h"
#include "mutex.h"
#include "stringprintf.h"
+#include "thread_local.h"
#include "timeutil.h"
namespace {
mutex g_mu;
vector<Stats*>* g_stats;
+DEFINE_THREAD_LOCAL(double, g_start_time);
} // namespace
@@ -45,17 +47,17 @@ string Stats::String() const {
}
void Stats::Start() {
- CHECK(!start_time_);
+ CHECK(!TLS_REF(g_start_time));
+ TLS_REF(g_start_time) = GetTime();
unique_lock<mutex> lock(mu_);
- start_time_ = GetTime();
cnt_++;
}
double Stats::End() {
+ CHECK(TLS_REF(g_start_time));
+ double e = GetTime() - TLS_REF(g_start_time);
+ TLS_REF(g_start_time) = 0;
unique_lock<mutex> lock(mu_);
- CHECK(start_time_);
- double e = GetTime() - start_time_;
- start_time_ = 0;
elapsed_ += e;
return e;
}
diff --git a/stats.h b/stats.h
index 8d3a41b..6244b54 100644
--- a/stats.h
+++ b/stats.h
@@ -34,7 +34,6 @@ class Stats {
friend class ScopedStatsRecorder;
const char* name_;
- double start_time_;
double elapsed_;
int cnt_;
mutable mutex mu_;
diff --git a/thread_local.h b/thread_local.h
index 3bbf663..28b146a 100644
--- a/thread_local.h
+++ b/thread_local.h
@@ -39,6 +39,13 @@
#include "log.h"
+#ifdef __linux__
+
+#define DEFINE_THREAD_LOCAL(Type, name) thread_local Type name
+#define TLS_REF(x) x
+
+#else
+
// Thread local storage implementation which uses pthread.
// Note that DEFINE_THREAD_LOCAL creates a global variable just like
// thread local storage based on __thread keyword. So we should not use
@@ -88,4 +95,8 @@ class ThreadLocal {
} \
ThreadLocal<Type, &name##_key, &name##_once> name;
+#define TLS_REF(x) x.Ref()
+
+#endif
+
#endif // THREAD_LOCAL_H_