Changeset 139780 in webkit


Ignore:
Timestamp:
Jan 15, 2013 1:35:08 PM (11 years ago)
Author:
abarth@webkit.org
Message:

Generalize DocumentWeakReference into WTF::WeakPtr
https://bugs.webkit.org/show_bug.cgi?id=106854

Reviewed by Darin Adler.

Source/WebCore:

This patch replaces DocumentWeakReference with WeakPtr. There should be
no change in behavior.

  • dom/Document.cpp:

(WebCore):
(WebCore::Document::Document):
(WebCore::Document::~Document):
(WebCore::PerformTaskContext::PerformTaskContext):
(PerformTaskContext):
(WebCore::Document::didReceiveTask):
(WebCore::Document::postTask):
(WebCore::Document::reportMemoryUsage):

  • dom/Document.h:

(WebCore):
(Document):

Source/WTF:

This patch adds a simple WeakPtr object to WTF. To use WeakPtr, objects
must hold a WeakPtrFactory member variable, which clears the WeakPtrs
when the object is destructed. The underlying storage is a
ThreadSafeRefCounted "WeakReference" object, which is shared by all the
WeakPtrs.

WeakPtr is a generalization of DocumentWeakReference, which is used to
check whether the Document object is alive when tasks arive on the main
thread from worker threads. We plan to use a similar pattern in the
threaded HTML parser, but we want to send tasks to objects other than
Document.

  • GNUmakefile.list.am:
  • WTF.gypi:
  • WTF.pro:
  • WTF.vcproj/WTF.vcproj:
  • WTF.xcodeproj/project.pbxproj:
  • wtf/CMakeLists.txt:
  • wtf/WeakPtr.h: Added.

(WeakReference):

  • The ThreadSafeRefCounted object shared by all the WeakPtr instances. This patch uses ThreadSafeRefCounted so that we can use WeakPtrs to cancel cross-thread messages.

(WTF::WeakReference::create):
(WTF::WeakReference::get):
(WTF::WeakReference::clear):

  • When the object is destroyed, WeakPtrFactory calls this function to clear all the WeakPtrs.

(WTF::WeakReference::WeakReference):
(WTF::WeakPtr::WeakPtr):
(WTF::WeakPtr::get):

  • We might want to add implicit conversions and Boolean operators in the future, but I've kept this class simple for now.

(WTF::WeakPtrFactory::WeakPtrFactory):
(WTF::WeakPtrFactory::~WeakPtrFactory):
(WTF::WeakPtrFactory::createWeakPtr):

Location:
trunk/Source
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r139553 r139780  
     12013-01-15  Adam Barth  <abarth@webkit.org>
     2
     3        Generalize DocumentWeakReference into WTF::WeakPtr
     4        https://bugs.webkit.org/show_bug.cgi?id=106854
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch adds a simple WeakPtr object to WTF. To use WeakPtr, objects
     9        must hold a WeakPtrFactory member variable, which clears the WeakPtrs
     10        when the object is destructed. The underlying storage is a
     11        ThreadSafeRefCounted "WeakReference" object, which is shared by all the
     12        WeakPtrs.
     13
     14        WeakPtr is a generalization of DocumentWeakReference, which is used to
     15        check whether the Document object is alive when tasks arive on the main
     16        thread from worker threads. We plan to use a similar pattern in the
     17        threaded HTML parser, but we want to send tasks to objects other than
     18        Document.
     19
     20        * GNUmakefile.list.am:
     21        * WTF.gypi:
     22        * WTF.pro:
     23        * WTF.vcproj/WTF.vcproj:
     24        * WTF.xcodeproj/project.pbxproj:
     25        * wtf/CMakeLists.txt:
     26        * wtf/WeakPtr.h: Added.
     27        (WeakReference):
     28            - The ThreadSafeRefCounted object shared by all the WeakPtr
     29              instances. This patch uses ThreadSafeRefCounted so that we can
     30              use WeakPtrs to cancel cross-thread messages.
     31        (WTF::WeakReference::create):
     32        (WTF::WeakReference::get):
     33        (WTF::WeakReference::clear):
     34            - When the object is destroyed, WeakPtrFactory calls this function
     35              to clear all the WeakPtrs.
     36        (WTF::WeakReference::WeakReference):
     37        (WTF::WeakPtr::WeakPtr):
     38        (WTF::WeakPtr::get):
     39            - We might want to add implicit conversions and Boolean operators
     40              in the future, but I've kept this class simple for now.
     41        (WTF::WeakPtrFactory::WeakPtrFactory):
     42        (WTF::WeakPtrFactory::~WeakPtrFactory):
     43        (WTF::WeakPtrFactory::createWeakPtr):
     44
    1452013-01-12  Csaba Osztrogonác  <ossy@webkit.org>
    246
  • trunk/Source/WTF/GNUmakefile.list.am

    r139184 r139780  
    194194    Source/WTF/wtf/WTFThreadData.cpp \
    195195    Source/WTF/wtf/WTFThreadData.h \
     196    Source/WTF/wtf/WeakPtr.h \
    196197    Source/WTF/wtf/dtoa.cpp \
    197198    Source/WTF/wtf/dtoa.h \
  • trunk/Source/WTF/WTF.gypi

    r138628 r139780  
    113113            'wtf/VectorTraits.h',
    114114            'wtf/WTFThreadData.h',
     115            'wtf/WeakPtr.h',
    115116            'wtf/dtoa.h',
    116117            'wtf/dtoa/bignum-dtoa.h',
  • trunk/Source/WTF/WTF.pro

    r136096 r139780  
    189189    VectorTraits.h \
    190190    VMTags.h \
    191     WTFThreadData.h
     191    WTFThreadData.h \
     192    WeakPtr.h
    192193
    193194unix: HEADERS += ThreadIdentifierDataPthreads.h
  • trunk/Source/WTF/WTF.vcproj/WTF.vcproj

    r136096 r139780  
    13291329                        >
    13301330                </File>
     1331                <File
     1332                        RelativePath="..\wtf\WeakPtr.h"
     1333                        >
     1334                </File>
    13311335        </Files>
    13321336        <Globals>
  • trunk/Source/WTF/WTF.xcodeproj/project.pbxproj

    r139010 r139780  
    7373                8134013815B092FD001FF0B8 /* Base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8134013615B092FD001FF0B8 /* Base64.cpp */; };
    7474                8134013915B092FD001FF0B8 /* Base64.h in Headers */ = {isa = PBXBuildFile; fileRef = 8134013715B092FD001FF0B8 /* Base64.h */; };
     75                974CFC8E16A4F327006D5404 /* WeakPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = 974CFC8D16A4F327006D5404 /* WeakPtr.h */; };
    7576                A876DBD8151816E500DADB95 /* Platform.h in Headers */ = {isa = PBXBuildFile; fileRef = A876DBD7151816E500DADB95 /* Platform.h */; };
    7677                A8A4737F151A825B004123FF /* Alignment.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A47254151A825A004123FF /* Alignment.h */; };
     
    380381                8134013615B092FD001FF0B8 /* Base64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Base64.cpp; sourceTree = "<group>"; };
    381382                8134013715B092FD001FF0B8 /* Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Base64.h; sourceTree = "<group>"; };
     383                974CFC8D16A4F327006D5404 /* WeakPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WeakPtr.h; sourceTree = "<group>"; };
    382384                A876DBD7151816E500DADB95 /* Platform.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Platform.h; sourceTree = "<group>"; };
    383385                A8A47254151A825A004123FF /* Alignment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Alignment.h; sourceTree = "<group>"; };
     
    877879                                A8A47371151A825B004123FF /* VectorTraits.h */,
    878880                                A8A47372151A825B004123FF /* VMTags.h */,
     881                                974CFC8D16A4F327006D5404 /* WeakPtr.h */,
    879882                                A8A4737A151A825B004123FF /* WTFThreadData.cpp */,
    880883                                A8A4737B151A825B004123FF /* WTFThreadData.h */,
     
    12511254                                A8A4747F151A825B004123FF /* VectorTraits.h in Headers */,
    12521255                                A8A47480151A825B004123FF /* VMTags.h in Headers */,
     1256                                974CFC8E16A4F327006D5404 /* WeakPtr.h in Headers */,
    12531257                                A8A47446151A825B004123FF /* WTFString.h in Headers */,
    12541258                                A8A47487151A825B004123FF /* WTFThreadData.h in Headers */,
  • trunk/Source/WTF/wtf/CMakeLists.txt

    r136790 r139780  
    123123    VectorTraits.h
    124124    WTFThreadData.h
     125    WeakPtr.h
    125126    dtoa.h
    126127
  • trunk/Source/WebCore/ChangeLog

    r139777 r139780  
     12013-01-15  Adam Barth  <abarth@webkit.org>
     2
     3        Generalize DocumentWeakReference into WTF::WeakPtr
     4        https://bugs.webkit.org/show_bug.cgi?id=106854
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch replaces DocumentWeakReference with WeakPtr. There should be
     9        no change in behavior.
     10
     11        * dom/Document.cpp:
     12        (WebCore):
     13        (WebCore::Document::Document):
     14        (WebCore::Document::~Document):
     15        (WebCore::PerformTaskContext::PerformTaskContext):
     16        (PerformTaskContext):
     17        (WebCore::Document::didReceiveTask):
     18        (WebCore::Document::postTask):
     19        (WebCore::Document::reportMemoryUsage):
     20        * dom/Document.h:
     21        (WebCore):
     22        (Document):
     23
    1242013-01-15  James Simonsen  <simonjam@chromium.org>
    225
  • trunk/Source/WebCore/dom/Document.cpp

    r139762 r139780  
    405405static HashSet<Document*>* documentsThatNeedStyleRecalc = 0;
    406406
    407 class DocumentWeakReference : public ThreadSafeRefCounted<DocumentWeakReference> {
    408 public:
    409     static PassRefPtr<DocumentWeakReference> create(Document* document)
    410     {
    411         return adoptRef(new DocumentWeakReference(document));
    412     }
    413 
    414     Document* document()
    415     {
    416         ASSERT(isMainThread());
    417         return m_document;
    418     }
    419 
    420     void clear()
    421     {
    422         ASSERT(isMainThread());
    423         m_document = 0;
    424     }
    425 
    426 private:
    427     DocumentWeakReference(Document* document)
    428         : m_document(document)
    429     {
    430         ASSERT(isMainThread());
    431     }
    432 
    433     Document* m_document;
    434 };
    435 
    436407uint64_t Document::s_globalTreeVersion = 0;
    437408
     
    482453    , m_renderer(0)
    483454    , m_eventQueue(DocumentEventQueue::create(this))
    484     , m_weakReference(DocumentWeakReference::create(this))
     455    , m_weakFactory(this)
    485456    , m_idAttributeName(idAttr)
    486457#if ENABLE(FULLSCREEN_API)
     
    657628    if (m_elemSheet)
    658629        m_elemSheet->clearOwnerNode();
    659 
    660     m_weakReference->clear();
    661630
    662631    clearStyleResolver(); // We need to destory CSSFontSelector before destroying m_cachedResourceLoader.
     
    48094778    WTF_MAKE_NONCOPYABLE(PerformTaskContext); WTF_MAKE_FAST_ALLOCATED;
    48104779public:
    4811     PerformTaskContext(PassRefPtr<DocumentWeakReference> documentReference, PassOwnPtr<ScriptExecutionContext::Task> task)
    4812         : documentReference(documentReference)
     4780    PerformTaskContext(WeakPtr<Document> document, PassOwnPtr<ScriptExecutionContext::Task> task)
     4781        : documentReference(document)
    48134782        , task(task)
    48144783    {
    48154784    }
    48164785
    4817     RefPtr<DocumentWeakReference> documentReference;
     4786    WeakPtr<Document> documentReference;
    48184787    OwnPtr<ScriptExecutionContext::Task> task;
    48194788};
     
    48264795    ASSERT(context);
    48274796
    4828     Document* document = context->documentReference->document();
     4797    Document* document = context->documentReference.get();
    48294798    if (!document)
    48304799        return;
     
    48414810void Document::postTask(PassOwnPtr<Task> task)
    48424811{
    4843     callOnMainThread(didReceiveTask, new PerformTaskContext(m_weakReference, task));
     4812    callOnMainThread(didReceiveTask, new PerformTaskContext(m_weakFactory.createWeakPtr(), task));
    48444813}
    48454814
     
    59935962    info.addMember(m_selectorQueryCache);
    59945963    info.addMember(m_renderer);
    5995     info.addMember(m_weakReference);
     5964    info.addMember(m_weakFactory);
    59965965    info.addMember(m_idAttributeName);
    59975966#if ENABLE(FULLSCREEN_API)
  • trunk/Source/WebCore/dom/Document.h

    r139751 r139780  
    5454#include <wtf/PassOwnPtr.h>
    5555#include <wtf/PassRefPtr.h>
     56#include <wtf/WeakPtr.h>
    5657
    5758namespace WebCore {
     
    8283class DocumentStyleSheetCollection;
    8384class DocumentType;
    84 class DocumentWeakReference;
    8585class Element;
    8686class EntityReference;
     
    14761476    RefPtr<DocumentEventQueue> m_eventQueue;
    14771477
    1478     RefPtr<DocumentWeakReference> m_weakReference;
     1478    WeakPtrFactory<Document> m_weakFactory;
    14791479
    14801480    HashSet<MediaCanStartListener*> m_mediaCanStartListeners;
Note: See TracChangeset for help on using the changeset viewer.