Changeset 140040 in webkit


Ignore:
Timestamp:
Jan 17, 2013 2:17:12 PM (11 years ago)
Author:
abarth@webkit.org
Message:

Teach Functional.h about WeakPtr
https://bugs.webkit.org/show_bug.cgi?id=107105

Reviewed by Anders Carlsson.

A common pattern in cross-thread communication is to call member
functions of an object on a remote thread. If the caller's reference to
the object on the remote thread is a WeakPtr, the caller usually wants
to validate that the object still exists when the call actually takes
place.

It's possible to do this manually for every cross-thread call, but that
is tiresome and error prone. Instead, we can teach bind to validate
WeakPtr arguments when passed as the "this" parameter to a member
function.

  • wtf/Functional.h:

(WTF::ParamStorageTraits::validate):

Location:
trunk/Source/WTF
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r139982 r140040  
     12013-01-17  Adam Barth  <abarth@webkit.org>
     2
     3        Teach Functional.h about WeakPtr
     4        https://bugs.webkit.org/show_bug.cgi?id=107105
     5
     6        Reviewed by Anders Carlsson.
     7
     8        A common pattern in cross-thread communication is to call member
     9        functions of an object on a remote thread. If the caller's reference to
     10        the object on the remote thread is a WeakPtr, the caller usually wants
     11        to validate that the object still exists when the call actually takes
     12        place.
     13
     14        It's possible to do this manually for every cross-thread call, but that
     15        is tiresome and error prone. Instead, we can teach bind to validate
     16        WeakPtr arguments when passed as the "this" parameter to a member
     17        function.
     18
     19        * wtf/Functional.h:
     20        (WTF::ParamStorageTraits::validate):
     21
    1222013-01-17  David Kilzer  <ddkilzer@apple.com>
    223
  • trunk/Source/WTF/wtf/Functional.h

    r135882 r140040  
    3131#include <wtf/RefPtr.h>
    3232#include <wtf/ThreadSafeRefCounted.h>
     33#include <wtf/WeakPtr.h>
    3334
    3435#if PLATFORM(MAC) && COMPILER_SUPPORTS(BLOCKS)
     
    8384    typedef R ResultType;
    8485    static const bool shouldRefFirstParameter = false;
     86    static const bool shouldValidateFirstParameter = false;
    8587
    8688    explicit FunctionWrapper(R (*function)())
     
    103105    typedef R ResultType;
    104106    static const bool shouldRefFirstParameter = false;
     107    static const bool shouldValidateFirstParameter = false;
    105108
    106109    explicit FunctionWrapper(R (*function)(P1))
     
    123126    typedef R ResultType;
    124127    static const bool shouldRefFirstParameter = false;
     128    static const bool shouldValidateFirstParameter = false;
    125129
    126130    explicit FunctionWrapper(R (*function)(P1, P2))
     
    143147    typedef R ResultType;
    144148    static const bool shouldRefFirstParameter = false;
     149    static const bool shouldValidateFirstParameter = false;
    145150
    146151    explicit FunctionWrapper(R (*function)(P1, P2, P3))
     
    163168    typedef R ResultType;
    164169    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
     170    static const bool shouldValidateFirstParameter = true;
    165171
    166172    explicit FunctionWrapper(R (C::*function)())
     
    183189    typedef R ResultType;
    184190    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
     191    static const bool shouldValidateFirstParameter = true;
    185192
    186193    explicit FunctionWrapper(R (C::*function)(P1))
     
    203210    typedef R ResultType;
    204211    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
     212    static const bool shouldValidateFirstParameter = true;
    205213
    206214    explicit FunctionWrapper(R (C::*function)(P1, P2))
     
    223231    typedef R ResultType;
    224232    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
     233    static const bool shouldValidateFirstParameter = true;
    225234
    226235    explicit FunctionWrapper(R (C::*function)(P1, P2, P3))
     
    243252    typedef R ResultType;
    244253    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
     254    static const bool shouldValidateFirstParameter = true;
    245255
    246256    explicit FunctionWrapper(R (C::*function)(P1, P2, P3, P4))
     
    263273    typedef R ResultType;
    264274    static const bool shouldRefFirstParameter = HasRefAndDeref<C>::value;
     275    static const bool shouldValidateFirstParameter = true;
    265276
    266277    explicit FunctionWrapper(R (C::*function)(P1, P2, P3, P4, P5))
     
    284295    typedef R ResultType;
    285296    static const bool shouldRefFirstParameter = false;
     297    static const bool shouldValidateFirstParameter = false;
    286298
    287299    explicit FunctionWrapper(R (^block)())
     
    324336
    325337    static StorageType wrap(const T& value) { return value; }
     338    static bool validate(const StorageType&) { return true; }
    326339    static const T& unwrap(const StorageType& value) { return value; }
    327340};
     
    331344
    332345    static StorageType wrap(PassRefPtr<T> value) { return value; }
     346    static bool validate(const StorageType&) { return true; }
    333347    static T* unwrap(const StorageType& value) { return value.get(); }
    334348};
     
    338352
    339353    static StorageType wrap(RefPtr<T> value) { return value.release(); }
     354    static bool validate(const StorageType&) { return true; }
     355    static T* unwrap(const StorageType& value) { return value.get(); }
     356};
     357
     358template<typename T> struct ParamStorageTraits<WeakPtr<T> > {
     359    typedef WeakPtr<T> StorageType;
     360
     361    static StorageType wrap(WeakPtr<T> value) { return value; }
     362    static bool validate(const StorageType& value) { return value.get(); }
    340363    static T* unwrap(const StorageType& value) { return value.get(); }
    341364};
     
    348371
    349372    static StorageType wrap(const RetainPtr<T>& value) { return value; }
     373    static bool validate(const StorageType&) { return true; }
    350374    static typename RetainPtr<T>::PtrType unwrap(const StorageType& value) { return value.get(); }
    351375};
     
    376400    }
    377401
    378     virtual R operator()()
     402    virtual typename FunctionWrapper::ResultType operator()()
    379403    {
    380404        return m_functionWrapper();
     
    387411template<typename FunctionWrapper, typename R, typename P1>
    388412class BoundFunctionImpl<FunctionWrapper, R (P1)> : public FunctionImpl<typename FunctionWrapper::ResultType ()> {
    389 
    390413public:
    391414    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
     
    401424    }
    402425
    403     virtual R operator()()
    404     {
     426    virtual typename FunctionWrapper::ResultType operator()()
     427    {
     428        if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))
     429            return typename FunctionWrapper::ResultType();
    405430        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1));
    406431    }
     
    429454    virtual typename FunctionWrapper::ResultType operator()()
    430455    {
     456        if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))
     457            return typename FunctionWrapper::ResultType();
    431458        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2));
    432459    }
     
    457484    virtual typename FunctionWrapper::ResultType operator()()
    458485    {
     486        if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))
     487            return typename FunctionWrapper::ResultType();
    459488        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3));
    460489    }
     
    487516    virtual typename FunctionWrapper::ResultType operator()()
    488517    {
     518        if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))
     519            return typename FunctionWrapper::ResultType();
    489520        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4));
    490521    }
     
    519550    virtual typename FunctionWrapper::ResultType operator()()
    520551    {
     552        if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))
     553            return typename FunctionWrapper::ResultType();
    521554        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5));
    522555    }
     
    553586    virtual typename FunctionWrapper::ResultType operator()()
    554587    {
     588        if (FunctionWrapper::shouldValidateFirstParameter && !ParamStorageTraits<P1>::validate(m_p1))
     589            return typename FunctionWrapper::ResultType();
    555590        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5), ParamStorageTraits<P6>::unwrap(m_p6));
    556591    }
Note: See TracChangeset for help on using the changeset viewer.