Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Unified Diff: base/metrics/histogram_snapshot_manager.cc

Issue 2871663003: Add concurrency check to HistogramSnapshotManager. (Closed)
Patch Set: disallow copy and assign Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/metrics/histogram_snapshot_manager.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/histogram_snapshot_manager.cc
diff --git a/base/metrics/histogram_snapshot_manager.cc b/base/metrics/histogram_snapshot_manager.cc
index 32702d8b3eb94a8800b4fe4f45f487ee4a26df74..89d4f8ef6fe32a1cae712ff524601b52e6c11d3e 100644
--- a/base/metrics/histogram_snapshot_manager.cc
+++ b/base/metrics/histogram_snapshot_manager.cc
@@ -14,10 +14,31 @@
namespace base {
+namespace {
+
+// A simple object to set an "active" flag and clear it upon destruction. It is
+// an error if the flag is already set.
+class MakeActive {
+ public:
+ MakeActive(std::atomic<bool>* is_active) : is_active_(is_active) {
+ bool was_active = is_active_->exchange(true, std::memory_order_relaxed);
+ CHECK(!was_active);
+ }
+ ~MakeActive() { is_active_->store(false, std::memory_order_relaxed); }
+
+ private:
+ std::atomic<bool>* is_active_;
+
+ DISALLOW_COPY_AND_ASSIGN(MakeActive);
+};
+
+} // namespace
+
HistogramSnapshotManager::HistogramSnapshotManager(
HistogramFlattener* histogram_flattener)
: histogram_flattener_(histogram_flattener) {
DCHECK(histogram_flattener_);
+ is_active_.store(false, std::memory_order_relaxed);
}
HistogramSnapshotManager::~HistogramSnapshotManager() {
@@ -35,9 +56,13 @@ void HistogramSnapshotManager::PrepareFinalDelta(
void HistogramSnapshotManager::PrepareSamples(
const HistogramBase* histogram,
std::unique_ptr<HistogramSamples> samples) {
- DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(histogram_flattener_);
+ // Ensure that there is no concurrent access going on while accessing the
+ // set of known histograms. The flag will be reset when this object goes
+ // out of scope.
+ MakeActive make_active(&is_active_);
+
// Get information known about this histogram. If it did not previously
// exist, one will be created and initialized.
SampleInfo* sample_info = &known_histograms_[histogram->name_hash()];
« no previous file with comments | « base/metrics/histogram_snapshot_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698