Bug 1417741 - Add support of Atmoic<> for Preferences::Add*VarCache(). r=baku,njn

MozReview-Commit-ID: 9aeLvgWdZxy

--HG--
extra : rebase_source : b6130610ad8f80257022cb6e103addfe58b96af2
This commit is contained in:
Tim Huang 2017-11-16 11:02:50 +08:00
parent 3b6b25c474
commit f4abe5782a
7 changed files with 153 additions and 1 deletions

View File

@ -69,6 +69,20 @@ void MediaPrefs::PrefAddVarCache(float* aVariable,
Preferences::AddFloatVarCache(aVariable, aPref, aDefault);
}
void MediaPrefs::PrefAddVarCache(AtomicBool* aVariable,
const char* aPref,
bool aDefault)
{
Preferences::AddAtomicBoolVarCache(aVariable, aPref, aDefault);
}
void MediaPrefs::PrefAddVarCache(AtomicInt32* aVariable,
const char* aPref,
int32_t aDefault)
{
Preferences::AddAtomicIntVarCache(aVariable, aPref, aDefault);
}
void MediaPrefs::PrefAddVarCache(AtomicUint32* aVariable,
const char* aPref,
uint32_t aDefault)

View File

@ -49,6 +49,8 @@ template<class T> class StaticAutoPtr;
class MediaPrefs final
{
typedef Atomic<bool, Relaxed> AtomicBool;
typedef Atomic<int32_t, Relaxed> AtomicInt32;
typedef Atomic<uint32_t, Relaxed> AtomicUint32;
template <typename T>
@ -215,6 +217,8 @@ private:
static void PrefAddVarCache(int32_t*, const char*, int32_t);
static void PrefAddVarCache(uint32_t*, const char*, uint32_t);
static void PrefAddVarCache(float*, const char*, float);
static void PrefAddVarCache(AtomicBool*, const char*, bool);
static void PrefAddVarCache(AtomicInt32*, const char*, int32_t);
static void PrefAddVarCache(AtomicUint32*, const char*, uint32_t);
static void AssertMainThread();

View File

@ -169,6 +169,30 @@ void gfxPrefs::PrefAddVarCache(std::string* aVariable,
Preferences::SetCString(aPref, aVariable->c_str());
}
void gfxPrefs::PrefAddVarCache(AtomicBool* aVariable,
const char* aPref,
bool aDefault)
{
MOZ_ASSERT(IsPrefsServiceAvailable());
Preferences::AddAtomicBoolVarCache(aVariable, aPref, aDefault);
}
void gfxPrefs::PrefAddVarCache(AtomicInt32* aVariable,
const char* aPref,
int32_t aDefault)
{
MOZ_ASSERT(IsPrefsServiceAvailable());
Preferences::AddAtomicIntVarCache(aVariable, aPref, aDefault);
}
void gfxPrefs::PrefAddVarCache(AtomicUint32* aVariable,
const char* aPref,
uint32_t aDefault)
{
MOZ_ASSERT(IsPrefsServiceAvailable());
Preferences::AddAtomicUintVarCache(aVariable, aPref, aDefault);
}
bool gfxPrefs::PrefGet(const char* aPref, bool aDefault)
{
MOZ_ASSERT(IsPrefsServiceAvailable());

View File

@ -10,6 +10,7 @@
#include <stdint.h>
#include <string>
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/gfx/LoggingConstants.h"
#include "nsTArray.h"
@ -112,6 +113,10 @@ class gfxPrefs final
{
typedef mozilla::gfx::GfxPrefValue GfxPrefValue;
typedef mozilla::Atomic<bool, mozilla::Relaxed> AtomicBool;
typedef mozilla::Atomic<int32_t, mozilla::Relaxed> AtomicInt32;
typedef mozilla::Atomic<uint32_t, mozilla::Relaxed> AtomicUint32;
private:
// Enums for the update policy.
enum class UpdatePolicy {
@ -789,6 +794,9 @@ private:
static void PrefAddVarCache(uint32_t*, const char*, uint32_t);
static void PrefAddVarCache(float*, const char*, float);
static void PrefAddVarCache(std::string*, const char*, std::string);
static void PrefAddVarCache(AtomicBool*, const char*, bool);
static void PrefAddVarCache(AtomicInt32*, const char*, int32_t);
static void PrefAddVarCache(AtomicUint32*, const char*, uint32_t);
static bool PrefGet(const char*, bool);
static int32_t PrefGet(const char*, int32_t);
static uint32_t PrefGet(const char*, uint32_t);

View File

@ -4881,6 +4881,35 @@ Preferences::AddBoolVarCache(bool* aCache, const char* aPref, bool aDefault)
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)
{
NS_ASSERTION(aCache, "aCache must not be NULL");
#ifdef DEBUG
AssertNotAlreadyCached("bool", aPref, aCache);
#endif
{
AUTO_INSTALLING_CALLBACK();
*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)
{
@ -4910,6 +4939,37 @@ Preferences::AddIntVarCache(int32_t* aCache,
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)
{
NS_ASSERTION(aCache, "aCache must not be NULL");
#ifdef DEBUG
AssertNotAlreadyCached("int", aPref, aCache);
#endif
{
AUTO_INSTALLING_CALLBACK();
*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)
{
@ -4970,9 +5030,19 @@ Preferences::AddAtomicUintVarCache(Atomic<uint32_t, Order>* aCache,
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*,

View File

@ -314,9 +314,17 @@ public:
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);

View File

@ -64,6 +64,13 @@ AddVarCacheFunc(bool* aVar, const char* 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)
{
@ -71,6 +78,13 @@ AddVarCacheFunc(int32_t* aVar, const char* 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)
{
@ -141,11 +155,21 @@ 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);