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

Unified Diff: base/metrics/statistics_recorder.cc

Issue 1779503002: Fix StatisticsRecorder to handle re-entry during tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: always do global UninitializeForTesting when uninitializing Created 4 years, 8 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/statistics_recorder.h ('k') | base/metrics/statistics_recorder_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/statistics_recorder.cc
diff --git a/base/metrics/statistics_recorder.cc b/base/metrics/statistics_recorder.cc
index b19a897a96c1ab6e2d7737bd2648100c9bd10281..43e3070077821b412e0fac91e4961b98bdfcc44b 100644
--- a/base/metrics/statistics_recorder.cc
+++ b/base/metrics/statistics_recorder.cc
@@ -74,8 +74,12 @@ StatisticsRecorder::~StatisticsRecorder() {
DCHECK(histograms_);
DCHECK(ranges_);
- // Global clean up.
+ // Clean out what this object created and then restore what existed before.
Reset();
+ base::AutoLock auto_lock(*lock_);
+ histograms_ = existing_histograms_.release();
+ callbacks_ = existing_callbacks_.release();
+ ranges_ = existing_ranges_.release();
}
// static
@@ -393,22 +397,32 @@ size_t StatisticsRecorder::GetHistogramCount() {
}
// static
-void StatisticsRecorder::ResetForTesting() {
- // Just call the private version that is used also by the destructor.
- Reset();
-}
-
-// static
void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) {
if (histograms_)
histograms_->erase(name);
}
+// static
+void StatisticsRecorder::UninitializeForTesting() {
+ // Stop now if it's never been initialized.
+ if (lock_ == NULL || histograms_ == NULL)
+ return;
+
+ // Get the global instance and destruct it. It's held in static memory so
+ // can't "delete" it; call the destructor explicitly.
+ DCHECK(g_statistics_recorder_.private_instance_);
+ g_statistics_recorder_.Get().~StatisticsRecorder();
+
+ // Now the ugly part. There's no official way to release a LazyInstance once
+ // created so it's necessary to clear out an internal variable which
+ // shouldn't be publicly visible but is for initialization reasons.
+ g_statistics_recorder_.private_instance_ = 0;
+}
+
// This singleton instance should be started during the single threaded portion
// of main(), and hence it is not thread safe. It initializes globals to
// provide support for all future calls.
StatisticsRecorder::StatisticsRecorder() {
- DCHECK(!histograms_);
if (lock_ == NULL) {
// This will leak on purpose. It's the only way to make sure we won't race
// against the static uninitialization of the module while one of our
@@ -418,7 +432,13 @@ StatisticsRecorder::StatisticsRecorder() {
// during static initialization and released only on process termination.
lock_ = new base::Lock;
}
+
base::AutoLock auto_lock(*lock_);
+
+ existing_histograms_.reset(histograms_);
+ existing_callbacks_.reset(callbacks_);
+ existing_ranges_.reset(ranges_);
+
histograms_ = new HistogramMap;
callbacks_ = new CallbackMap;
ranges_ = new RangesMap;
« no previous file with comments | « base/metrics/statistics_recorder.h ('k') | base/metrics/statistics_recorder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698