Commit e17b7491 authored by yutak's avatar yutak Committed by Commit bot

CrossThreadCopier: Consider arithmetic and enum types as safe.

The original code assumed that types convertible to int were safe to
copy cross-thread. However, this condition is too narrow; any
arithmetic types (integers and floats) and enum types are actually
safe. This is fixed by this patch.

Additionally, this patch rearranges the order of CrossThreadCopierBase's
template parameters, because putting T in the last looks unusual. Also,
the specializations of CrossThreadCopierBase are updated so that the
readers can understand the specializations cover all the possible
combinations of template parameters (except for <T, false, false,
false>, which is intentionally undefined in the header).

BUG=565765
R=haraken@chromium.org, hiroshige@chromium.org, tzik@chromium.org

Review URL: https://codereview.chromium.org/1701013003

Cr-Commit-Position: refs/heads/master@{#376129}
parent 617a078c
......@@ -87,7 +87,7 @@ static_assert((std::is_same<
// Add a generic specialization which will let's us verify that no other template matches.
template<typename T> struct CrossThreadCopierBase<false, false, false, T> {
template<typename T> struct CrossThreadCopierBase<T, false, false, false> {
typedef int Type;
};
......
......@@ -63,18 +63,18 @@ struct CrossThreadCopierPassThrough {
}
};
template <bool isConvertibleToInteger, bool isThreadSafeRefCounted, bool isGarbageCollected, typename T>
template <typename T, bool isArithmeticOrEnum, bool isThreadSafeRefCounted, bool isGarbageCollected>
struct CrossThreadCopierBase;
// Integers get passed through without any changes.
template <typename T>
struct CrossThreadCopierBase<true, false, false, T> : public CrossThreadCopierPassThrough<T> {
// Arithmetic values (integers or floats) and enums can be safely copied.
template <typename T, bool isThreadSafeRefCounted, bool isGarbageCollected>
struct CrossThreadCopierBase<T, true, isThreadSafeRefCounted, isGarbageCollected> : public CrossThreadCopierPassThrough<T> {
STATIC_ONLY(CrossThreadCopierBase);
};
// Custom copy methods.
template <typename T>
struct CrossThreadCopierBase<false, true, false, T> {
// Custom copy method for ThreadSafeRefCounted.
template <typename T, bool isGarbageCollected>
struct CrossThreadCopierBase<T, false, true, isGarbageCollected> {
STATIC_ONLY(CrossThreadCopierBase);
typedef typename WTF::RemoveTemplate<T, RefPtr>::Type TypeWithoutRefPtr;
typedef typename WTF::RemoveTemplate<TypeWithoutRefPtr, PassRefPtr>::Type TypeWithoutPassRefPtr;
......@@ -93,8 +93,9 @@ struct CrossThreadCopierBase<false, true, false, T> {
}
};
// A pointer to GarbageCollected.
template <typename T>
struct CrossThreadCopierBase<false, false, true, T> {
struct CrossThreadCopierBase<T, false, false, true> {
STATIC_ONLY(CrossThreadCopierBase);
typedef typename std::remove_pointer<T>::type TypeWithoutPointer;
typedef RawPtr<TypeWithoutPointer> Type;
......@@ -105,12 +106,13 @@ struct CrossThreadCopierBase<false, false, true, T> {
};
template <typename T>
struct CrossThreadCopier : public CrossThreadCopierBase<std::is_convertible<T, int>::value,
struct CrossThreadCopier : public CrossThreadCopierBase<
T,
std::is_arithmetic<T>::value || std::is_enum<T>::value,
WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, RefPtr>::Type, ThreadSafeRefCounted>::value
|| WTF::IsSubclassOfTemplate<typename std::remove_pointer<T>::type, ThreadSafeRefCounted>::value
|| WTF::IsSubclassOfTemplate<typename WTF::RemoveTemplate<T, PassRefPtr>::Type, ThreadSafeRefCounted>::value,
WTF::IsSubclassOfTemplate<typename std::remove_pointer<T>::type, GarbageCollected>::value,
T> {
WTF::IsSubclassOfTemplate<typename std::remove_pointer<T>::type, GarbageCollected>::value> {
STATIC_ONLY(CrossThreadCopier);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment