Bug 1417741 - Add support of Atmoic<> for Preferences::Add*VarCache(). r?baku draft
authorTim Huang <tihuang@mozilla.com>
Thu, 16 Nov 2017 11:02:50 +0800
changeset 1353375 af57c6cd486557e25493194dac229041ce858762
parent 1351204 f0c0fb9182d695081edf170d8e3bcb8164f2c96a
child 1353376 1c826441ee12cfc1cc5735613cdca5c2f76e9d88
push id235926
push usertihuang@mozilla.com
push dateThu, 16 Nov 2017 16:45:34 +0000
treeherdertry@1c826441ee12 [default view] [failures only]
reviewersbaku
bugs1417741
milestone59.0a1
Bug 1417741 - Add support of Atmoic<> for Preferences::Add*VarCache(). r?baku MozReview-Commit-ID: 9aeLvgWdZxy
modules/libpref/Preferences.cpp
modules/libpref/Preferences.h
modules/libpref/test/gtest/CallbackAndVarCacheOrder.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -4892,16 +4892,43 @@ Preferences::AddBoolVarCache(bool* aCach
   CacheData* data = new CacheData();
   data->mCacheLocation = aCache;
   data->mDefaultValueBool = aDefault;
   CacheDataAppendElement(data);
   RegisterVarCacheCallback(BoolVarChanged, aPref, data);
   return NS_OK;
 }
 
+template<MemoryOrdering Order>
+static void
+AtomicBoolVarChanged(const char* aPref, void* aClosure)
+{
+  CacheData* cache = static_cast<CacheData*>(aClosure);
+  *static_cast<Atomic<bool, Order>*>(cache->mCacheLocation) =
+    Preferences::GetBool(aPref, cache->mDefaultValueBool);
+}
+
+template<MemoryOrdering Order>
+/* static */ nsresult
+Preferences::AddAtomicBoolVarCache(Atomic<bool, Order>* aCache, const char* aPref, bool aDefault)
+{
+  WATCHING_PREF_RAII();
+  NS_ASSERTION(aCache, "aCache must not be NULL");
+#ifdef DEBUG
+  AssertNotAlreadyCached("bool", aPref, aCache);
+#endif
+  *aCache = Preferences::GetBool(aPref, aDefault);
+  CacheData* data = new CacheData();
+  data->mCacheLocation = aCache;
+  data->mDefaultValueBool = aDefault;
+  CacheDataAppendElement(data);
+  RegisterVarCacheCallback(AtomicBoolVarChanged<Order>, aPref, data);
+  return NS_OK;
+}
+
 static void
 IntVarChanged(const char* aPref, void* aClosure)
 {
   CacheData* cache = static_cast<CacheData*>(aClosure);
   *static_cast<int32_t*>(cache->mCacheLocation) =
     Preferences::GetInt(aPref, cache->mDefaultValueInt);
 }
 
@@ -4919,16 +4946,45 @@ Preferences::AddIntVarCache(int32_t* aCa
   CacheData* data = new CacheData();
   data->mCacheLocation = aCache;
   data->mDefaultValueInt = aDefault;
   CacheDataAppendElement(data);
   RegisterVarCacheCallback(IntVarChanged, aPref, data);
   return NS_OK;
 }
 
+template<MemoryOrdering Order>
+static void
+AtomicIntVarChanged(const char* aPref, void* aClosure)
+{
+  CacheData* cache = static_cast<CacheData*>(aClosure);
+  *static_cast<Atomic<int32_t, Order>*>(cache->mCacheLocation) =
+    Preferences::GetInt(aPref, cache->mDefaultValueUint);
+}
+
+template<MemoryOrdering Order>
+/* static */ nsresult
+Preferences::AddAtomicIntVarCache(Atomic<int32_t, Order>* aCache,
+                                  const char* aPref,
+                                  int32_t aDefault)
+{
+  WATCHING_PREF_RAII();
+  NS_ASSERTION(aCache, "aCache must not be NULL");
+#ifdef DEBUG
+  AssertNotAlreadyCached("int", aPref, aCache);
+#endif
+  *aCache = Preferences::GetInt(aPref, aDefault);
+  CacheData* data = new CacheData();
+  data->mCacheLocation = aCache;
+  data->mDefaultValueUint = aDefault;
+  CacheDataAppendElement(data);
+  RegisterVarCacheCallback(AtomicIntVarChanged<Order>, aPref, data);
+  return NS_OK;
+}
+
 static void
 UintVarChanged(const char* aPref, void* aClosure)
 {
   CacheData* cache = static_cast<CacheData*>(aClosure);
   *static_cast<uint32_t*>(cache->mCacheLocation) =
     Preferences::GetUint(aPref, cache->mDefaultValueUint);
 }
 
@@ -4975,20 +5031,30 @@ Preferences::AddAtomicUintVarCache(Atomi
   CacheData* data = new CacheData();
   data->mCacheLocation = aCache;
   data->mDefaultValueUint = aDefault;
   CacheDataAppendElement(data);
   RegisterVarCacheCallback(AtomicUintVarChanged<Order>, aPref, data);
   return NS_OK;
 }
 
-// Since the definition of this template function is not in a header file, we
+// Since the definition of template functions is not in a header file, we
 // need to explicitly specify the instantiations that are required. Currently
 // only the order=Relaxed variant is needed.
 template nsresult
+Preferences::AddAtomicBoolVarCache(Atomic<bool, Relaxed>*,
+                                   const char*,
+                                   bool);
+
+template nsresult
+Preferences::AddAtomicIntVarCache(Atomic<int32_t, Relaxed>*,
+                                  const char*,
+                                  int32_t);
+
+template nsresult
 Preferences::AddAtomicUintVarCache(Atomic<uint32_t, Relaxed>*,
                                    const char*,
                                    uint32_t);
 
 static void
 FloatVarChanged(const char* aPref, void* aClosure)
 {
   CacheData* cache = static_cast<CacheData*>(aClosure);
--- a/modules/libpref/Preferences.h
+++ b/modules/libpref/Preferences.h
@@ -270,19 +270,27 @@ public:
 
   // Adds the aVariable to cache table. |aVariable| must be a pointer for a
   // static variable. The value will be modified when the pref value is changed
   // but note that even if you modified it, the value isn't assigned to the
   // pref.
   static nsresult AddBoolVarCache(bool* aVariable,
                                   const char* aPref,
                                   bool aDefault = false);
+  template<MemoryOrdering Order>
+  static nsresult AddAtomicBoolVarCache(Atomic<bool, Order>* aVariable,
+                                        const char* aPref,
+                                        bool aDefault = false);
   static nsresult AddIntVarCache(int32_t* aVariable,
                                  const char* aPref,
                                  int32_t aDefault = 0);
+  template<MemoryOrdering Order>
+  static nsresult AddAtomicIntVarCache(Atomic<int32_t, Order>* aVariable,
+                                       const char* aPref,
+                                       int32_t aDefault = 0);
   static nsresult AddUintVarCache(uint32_t* aVariable,
                                   const char* aPref,
                                   uint32_t aDefault = 0);
   template<MemoryOrdering Order>
   static nsresult AddAtomicUintVarCache(Atomic<uint32_t, Order>* aVariable,
                                         const char* aPref,
                                         uint32_t aDefault = 0);
   static nsresult AddFloatVarCache(float* aVariable,
--- a/modules/libpref/test/gtest/CallbackAndVarCacheOrder.cpp
+++ b/modules/libpref/test/gtest/CallbackAndVarCacheOrder.cpp
@@ -60,23 +60,37 @@ SetFunc(const char* aPrefName, float aVa
 void
 AddVarCacheFunc(bool* aVar, const char* aPrefName)
 {
   nsresult rv = Preferences::AddBoolVarCache(aVar, aPrefName);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 }
 
 void
+AddVarCacheFunc(Atomic<bool, Relaxed>* aVar, const char* aPrefName)
+{
+  nsresult rv = Preferences::AddAtomicBoolVarCache(aVar, aPrefName);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+}
+
+void
 AddVarCacheFunc(int32_t* aVar, const char* aPrefName)
 {
   nsresult rv = Preferences::AddIntVarCache(aVar, aPrefName);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 }
 
 void
+AddVarCacheFunc(Atomic<int32_t, Relaxed>* aVar, const char* aPrefName)
+{
+  nsresult rv = Preferences::AddAtomicIntVarCache(aVar, aPrefName);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
+}
+
+void
 AddVarCacheFunc(uint32_t* aVar, const char* aPrefName)
 {
   nsresult rv = Preferences::AddUintVarCache(aVar, aPrefName);
   ASSERT_TRUE(NS_SUCCEEDED(rv));
 }
 
 void
 AddVarCacheFunc(Atomic<uint32_t, Relaxed>* aVar, const char* aPrefName)
@@ -136,21 +150,31 @@ RunTest(const char* aPrefName1, const ch
   ASSERT_TRUE(closure2.mCalled);
 }
 
 TEST(CallbackAndVarCacheOrder, Bool)
 {
   RunTest<bool>("test_pref.bool.1", "test_pref.bool.2", false, true);
 }
 
+TEST(CallbackAndVarCacheOrder, AtomicBool)
+{
+  RunTest<bool, Atomic<bool, Relaxed>>("test_pref.atomic_bool.1", "test_pref.atomic_bool.2", false, true);
+}
+
 TEST(CallbackAndVarCacheOrder, Int)
 {
   RunTest<int32_t>("test_pref.int.1", "test_pref.int.2", -2, 3);
 }
 
+TEST(CallbackAndVarCacheOrder, AtomicInt)
+{
+  RunTest<int32_t, Atomic<int32_t, Relaxed>>("test_pref.atomic_int.1", "test_pref.atomic_int.2", -3, 4);
+}
+
 TEST(CallbackAndVarCacheOrder, Uint)
 {
   RunTest<uint32_t>("test_pref.uint.1", "test_pref.uint.2", 4u, 5u);
 }
 
 TEST(CallbackAndVarCacheOrder, AtomicUint)
 {
   RunTest<uint32_t, Atomic<uint32_t, Relaxed>>(