Bug 714450 - Mark the DB whitelist in Telemetry immutable after it is initialized. r=benjmain
authorVladan Djeric <vdjeric@mozilla.com>
Tue, 10 Jan 2012 15:31:34 -0500
changeset 85374 b18d407e2321e01fcb286ed45080546d71db0c88
parent 85373 a537453b1d667a66d5272915e43499eedb3554b5
child 85375 018cfb14c03e95b679c793cfed1aeae941470771
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbenjmain
bugs714450
milestone12.0a1
Bug 714450 - Mark the DB whitelist in Telemetry immutable after it is initialized. r=benjmain
toolkit/components/telemetry/Telemetry.cpp
xpcom/glue/nsTHashtable.h
--- a/toolkit/components/telemetry/Telemetry.cpp
+++ b/toolkit/components/telemetry/Telemetry.cpp
@@ -328,23 +328,27 @@ mHashMutex("Telemetry::mHashMutex")
     "search.sqlite", "signons.sqlite", "urlclassifier3.sqlite",
     "webappsstore.sqlite"
   };
 
   mTrackedDBs.Init();
   for (int i = 0; i < sizeof(trackedDBs)/sizeof(const char*); i++)
     mTrackedDBs.PutEntry(nsDependentCString(trackedDBs[i]));
 
+#ifdef DEBUG
+  // Mark immutable to prevent asserts on simultaneous access from multiple threads
+  mTrackedDBs.MarkImmutable();
+#endif
+
   mSlowSQLOnMainThread.Init();
   mSlowSQLOnOtherThread.Init();
   mHistogramMap.Init(Telemetry::HistogramCount);
 }
 
 TelemetryImpl::~TelemetryImpl() {
-  mTrackedDBs.Clear();
   mSlowSQLOnMainThread.Clear();
   mSlowSQLOnOtherThread.Clear();
   mHistogramMap.Clear();
 }
 
 NS_IMETHODIMP
 TelemetryImpl::NewHistogram(const nsACString &name, PRUint32 min, PRUint32 max, PRUint32 bucketCount, PRUint32 histogramType, JSContext *cx, jsval *ret)
 {
--- a/xpcom/glue/nsTHashtable.h
+++ b/xpcom/glue/nsTHashtable.h
@@ -280,16 +280,31 @@ public:
   {
     if (IsInitialized()) {
       s_SizeOfArgs args = { sizeOfEntryExcludingThis, userArg };
       return PL_DHashTableSizeOfExcludingThis(&mTable, s_SizeOfStub, mallocSizeOf, &args);
     }
     return 0;
   }
 
+#ifdef DEBUG
+  /**
+   * Mark the table as constant after initialization.
+   *
+   * This will prevent assertions when a read-only hash is accessed on multiple
+   * threads without synchronization.
+   */
+  void MarkImmutable()
+  {
+    NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
+
+    PL_DHashMarkTableImmutable(&mTable);
+  }
+#endif
+
 protected:
   PLDHashTable mTable;
 
   static const void* s_GetKey(PLDHashTable    *table,
                               PLDHashEntryHdr *entry);
 
   static PLDHashNumber s_HashKey(PLDHashTable *table,
                                  const void   *key);