Changeset 142305 in webkit
- Timestamp:
- Feb 8, 2013 11:30:54 AM (11 years ago)
- Location:
- trunk/Source
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r142298 r142305 1 2013-02-08 Adam Barth <abarth@webkit.org> 2 3 Use WeakPtrs to communicate between the HTMLDocumentParser and the BackgroundHTMLParser 4 https://bugs.webkit.org/show_bug.cgi?id=107190 5 6 Reviewed by Eric Seidel. 7 8 Add the ability to create an unbound weak reference. This facility lets 9 you start sending messages to a WeakPtr on another thread before the 10 object backing the WeakPtr has actually been created. 11 12 * wtf/WeakPtr.h: 13 (WTF::WeakReference::createUnbound): 14 (WTF::WeakReference::bindTo): 15 (WeakReference): 16 (WTF::WeakReference::WeakReference): 17 (WTF::WeakPtr::WeakPtr): 18 (WeakPtr): 19 (WTF::WeakPtrFactory::WeakPtrFactory): 20 (WeakPtrFactory): 21 (WTF::WeakPtrFactory::revokeAll): 22 1 23 2013-02-08 Martin Robinson <mrobinson@igalia.com> 2 24 -
trunk/Source/WTF/wtf/WeakPtr.h
r140589 r142305 35 35 namespace WTF { 36 36 37 namespace Internal {38 39 37 template<typename T> 40 38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T> > { … … 43 41 public: 44 42 static PassRefPtr<WeakReference<T> > create(T* ptr) { return adoptRef(new WeakReference(ptr)); } 43 static PassRefPtr<WeakReference<T> > createUnbound() { return adoptRef(new WeakReference()); } 45 44 46 45 T* get() const … … 56 55 } 57 56 57 void bindTo(T* ptr) 58 { 59 ASSERT(!m_ptr); 60 #ifndef NDEBUG 61 m_boundThread = currentThread(); 62 #endif 63 m_ptr = ptr; 64 } 65 58 66 private: 67 WeakReference() : m_ptr(0) { } 68 59 69 explicit WeakReference(T* ptr) 60 70 : m_ptr(ptr) … … 71 81 }; 72 82 73 }74 75 83 template<typename T> 76 84 class WeakPtr { … … 78 86 public: 79 87 WeakPtr() { } 80 WeakPtr(PassRefPtr< Internal::WeakReference<T> > ref) : m_ref(ref) { }88 WeakPtr(PassRefPtr<WeakReference<T> > ref) : m_ref(ref) { } 81 89 82 90 T* get() const { return m_ref->get(); } 83 91 84 92 private: 85 RefPtr< Internal::WeakReference<T> > m_ref;93 RefPtr<WeakReference<T> > m_ref; 86 94 }; 87 95 … … 91 99 WTF_MAKE_FAST_ALLOCATED; 92 100 public: 93 explicit WeakPtrFactory(T* ptr) { m_ref = Internal::WeakReference<T>::create(ptr); } 101 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { } 102 103 WeakPtrFactory(PassRefPtr<WeakReference<T> > ref, T* ptr) 104 : m_ref(ref) 105 { 106 m_ref->bindTo(ptr); 107 } 108 94 109 ~WeakPtrFactory() { m_ref->clear(); } 95 110 … … 102 117 m_ref->clear(); 103 118 // We create a new WeakReference so that future calls to createWeakPtr() create nonzero WeakPtrs. 104 m_ref = Internal::WeakReference<T>::create(ptr);119 m_ref = WeakReference<T>::create(ptr); 105 120 } 106 121 107 122 private: 108 RefPtr< Internal::WeakReference<T> > m_ref;123 RefPtr<WeakReference<T> > m_ref; 109 124 }; 110 125 … … 113 128 using WTF::WeakPtr; 114 129 using WTF::WeakPtrFactory; 130 using WTF::WeakReference; 115 131 116 132 #endif -
trunk/Source/WebCore/ChangeLog
r142303 r142305 1 2013-02-08 Adam Barth <abarth@webkit.org> 2 3 Use WeakPtrs to communicate between the HTMLDocumentParser and the BackgroundHTMLParser 4 https://bugs.webkit.org/show_bug.cgi?id=107190 5 6 Reviewed by Eric Seidel. 7 8 This patch replaces the parser map with WeakPtr. We now use WeakPtrs to 9 communicate from the main thread to the background thread. (We were 10 already using WeakPtrs to communicate from the background thread to the 11 main thread.) This change lets us remove a bunch of boilerplate code. 12 13 * html/parser/BackgroundHTMLParser.cpp: 14 (WebCore::BackgroundHTMLParser::BackgroundHTMLParser): 15 (WebCore::BackgroundHTMLParser::stop): 16 (WebCore): 17 * html/parser/BackgroundHTMLParser.h: 18 (WebCore::BackgroundHTMLParser::create): 19 (BackgroundHTMLParser): 20 * html/parser/HTMLDocumentParser.cpp: 21 (WebCore::HTMLDocumentParser::didFailSpeculation): 22 (WebCore::HTMLDocumentParser::startBackgroundParser): 23 (WebCore::HTMLDocumentParser::stopBackgroundParser): 24 (WebCore::HTMLDocumentParser::append): 25 (WebCore::HTMLDocumentParser::finish): 26 * html/parser/HTMLDocumentParser.h: 27 (WebCore): 28 (HTMLDocumentParser): 29 1 30 2013-02-07 Roger Fong <roger_fong@apple.com> 2 31 -
trunk/Source/WebCore/html/parser/BackgroundHTMLParser.cpp
r142099 r142305 59 59 static const size_t pendingTokenLimit = 4000; 60 60 61 ParserMap& parserMap() 62 { 63 // This initialization assumes that this will be initialize on the main thread before 64 // any parser thread is started. 65 static ParserMap* sParserMap = new ParserMap; 66 return *sParserMap; 67 } 68 69 ParserMap::BackgroundParserMap& ParserMap::backgroundParsers() 70 { 71 ASSERT(HTMLParserThread::shared()->threadId() == currentThread()); 72 return m_backgroundParsers; 73 } 74 75 BackgroundHTMLParser::BackgroundHTMLParser(const HTMLParserOptions& options, const WeakPtr<HTMLDocumentParser>& parser, PassOwnPtr<XSSAuditor> xssAuditor) 61 BackgroundHTMLParser::BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHTMLParser> > reference, const HTMLParserOptions& options, const WeakPtr<HTMLDocumentParser>& parser, PassOwnPtr<XSSAuditor> xssAuditor) 76 62 : m_inForeignContent(false) 63 , m_weakFactory(reference, this) 77 64 , m_token(adoptPtr(new HTMLToken)) 78 65 , m_tokenizer(HTMLTokenizer::create(options)) … … 103 90 markEndOfFile(); 104 91 pumpTokenizer(); 92 } 93 94 void BackgroundHTMLParser::stop() 95 { 96 delete this; 105 97 } 106 98 … … 195 187 } 196 188 197 void BackgroundHTMLParser::createPartial(ParserIdentifier identifier, const HTMLParserOptions& options, const WeakPtr<HTMLDocumentParser>& parser, PassOwnPtr<XSSAuditor> xssAuditor)198 {199 ASSERT(!parserMap().backgroundParsers().get(identifier));200 parserMap().backgroundParsers().set(identifier, BackgroundHTMLParser::create(options, parser, xssAuditor));201 }202 203 void BackgroundHTMLParser::stopPartial(ParserIdentifier identifier)204 {205 parserMap().backgroundParsers().remove(identifier);206 }207 208 void BackgroundHTMLParser::appendPartial(ParserIdentifier identifier, const String& input)209 {210 ASSERT(!input.impl() || input.impl()->hasOneRef() || input.isEmpty());211 if (BackgroundHTMLParser* parser = parserMap().backgroundParsers().get(identifier))212 parser->append(input);213 }214 215 void BackgroundHTMLParser::resumeFromPartial(ParserIdentifier identifier, const WeakPtr<HTMLDocumentParser>& parser, PassOwnPtr<HTMLToken> token, PassOwnPtr<HTMLTokenizer> tokenizer, HTMLInputCheckpoint checkpoint)216 {217 if (BackgroundHTMLParser* backgroundParser = parserMap().backgroundParsers().get(identifier))218 backgroundParser->resumeFrom(parser, token, tokenizer, checkpoint);219 }220 221 void BackgroundHTMLParser::finishPartial(ParserIdentifier identifier)222 {223 if (BackgroundHTMLParser* parser = parserMap().backgroundParsers().get(identifier))224 parser->finish();225 }226 227 189 } 228 190 -
trunk/Source/WebCore/html/parser/BackgroundHTMLParser.h
r142099 r142305 48 48 WTF_MAKE_FAST_ALLOCATED; 49 49 public: 50 static void create(PassRefPtr<WeakReference<BackgroundHTMLParser> > reference, const HTMLParserOptions& options, const WeakPtr<HTMLDocumentParser>& parser, PassOwnPtr<XSSAuditor> xssAuditor) 51 { 52 new BackgroundHTMLParser(reference, options, parser, xssAuditor); 53 // Caller must free by calling stop(). 54 } 55 50 56 void append(const String&); 51 57 void resumeFrom(const WeakPtr<HTMLDocumentParser>&, PassOwnPtr<HTMLToken>, PassOwnPtr<HTMLTokenizer>, HTMLInputCheckpoint); 52 58 void finish(); 53 54 static PassOwnPtr<BackgroundHTMLParser> create(const HTMLParserOptions& options, const WeakPtr<HTMLDocumentParser>& parser, PassOwnPtr<XSSAuditor> xssAuditor) 55 { 56 return adoptPtr(new BackgroundHTMLParser(options, parser, xssAuditor)); 57 } 58 59 static void createPartial(ParserIdentifier, const HTMLParserOptions&, const WeakPtr<HTMLDocumentParser>&, PassOwnPtr<XSSAuditor>); 60 static void stopPartial(ParserIdentifier); 61 static void appendPartial(ParserIdentifier, const String& input); 62 static void resumeFromPartial(ParserIdentifier, const WeakPtr<HTMLDocumentParser>&, PassOwnPtr<HTMLToken>, PassOwnPtr<HTMLTokenizer>, HTMLInputCheckpoint); 63 static void finishPartial(ParserIdentifier); 59 void stop(); 64 60 65 61 private: 66 BackgroundHTMLParser( const HTMLParserOptions&, const WeakPtr<HTMLDocumentParser>&, PassOwnPtr<XSSAuditor>);62 BackgroundHTMLParser(PassRefPtr<WeakReference<BackgroundHTMLParser> >, const HTMLParserOptions&, const WeakPtr<HTMLDocumentParser>&, PassOwnPtr<XSSAuditor>); 67 63 68 64 void markEndOfFile(); … … 73 69 74 70 bool m_inForeignContent; // FIXME: We need a stack of foreign content markers. 71 WeakPtrFactory<BackgroundHTMLParser> m_weakFactory; 75 72 BackgroundHTMLInputStream m_input; 76 73 HTMLSourceTracker m_sourceTracker; … … 83 80 }; 84 81 85 class ParserMap {86 public:87 static ParserIdentifier identifierForParser(HTMLDocumentParser* parser)88 {89 return reinterpret_cast<ParserIdentifier>(parser);90 }91 92 typedef HashMap<ParserIdentifier, OwnPtr<BackgroundHTMLParser> > BackgroundParserMap;93 94 BackgroundParserMap& backgroundParsers();95 96 private:97 BackgroundParserMap m_backgroundParsers;98 };99 100 ParserMap& parserMap();101 102 82 } 103 83 -
trunk/Source/WebCore/html/parser/HTMLDocumentParser.cpp
r142200 r142305 302 302 m_speculations.clear(); 303 303 304 ParserIdentifier identifier = ParserMap::identifierForParser(this); 305 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::resumeFromPartial, 306 identifier, m_weakFactory.createWeakPtr(), token, tokenizer, m_currentChunk->checkpoint)); 304 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::resumeFrom, 305 m_backgroundParser, m_weakFactory.createWeakPtr(), token, tokenizer, m_currentChunk->checkpoint)); 307 306 } 308 307 … … 513 512 m_haveBackgroundParser = true; 514 513 514 RefPtr<WeakReference<BackgroundHTMLParser> > reference = WeakReference<BackgroundHTMLParser>::createUnbound(); 515 m_backgroundParser = WeakPtr<BackgroundHTMLParser>(reference); 516 515 517 WeakPtr<HTMLDocumentParser> parser = m_weakFactory.createWeakPtr(); 516 518 OwnPtr<XSSAuditor> xssAuditor = adoptPtr(new XSSAuditor); 517 519 xssAuditor->init(document()); 518 520 ASSERT(xssAuditor->isSafeToSendToAnotherThread()); 519 ParserIdentifier identifier = ParserMap::identifierForParser(this); 520 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::createPartial, identifier, m_options, parser, xssAuditor.release())); 521 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::create, reference.release(), m_options, parser, xssAuditor.release())); 521 522 } 522 523 … … 527 528 m_haveBackgroundParser = false; 528 529 529 ParserIdentifier identifier = ParserMap::identifierForParser(this); 530 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::stopPartial, identifier)); 530 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::stop, m_backgroundParser)); 531 531 m_weakFactory.revokeAll(); 532 532 } … … 544 544 startBackgroundParser(); 545 545 546 ParserIdentifier identifier = ParserMap::identifierForParser(this); 547 const Closure& appendPartial = bind(&BackgroundHTMLParser::appendPartial, identifier, source.toString().isolatedCopy()); 548 HTMLParserThread::shared()->postTask(appendPartial); 546 HTMLParserThread::shared()->postTask(bind( 547 &BackgroundHTMLParser::append, m_backgroundParser, source.toString().isolatedCopy())); 549 548 return; 550 549 } … … 646 645 // and fall through to the non-threading case. 647 646 if (m_haveBackgroundParser) { 648 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::finish Partial, ParserMap::identifierForParser(this)));647 HTMLParserThread::shared()->postTask(bind(&BackgroundHTMLParser::finish, m_backgroundParser)); 649 648 return; 650 649 } -
trunk/Source/WebCore/html/parser/HTMLDocumentParser.h
r142200 r142305 47 47 namespace WebCore { 48 48 49 class BackgroundHTMLParser; 49 50 class CompactHTMLToken; 50 51 class Document; … … 183 184 Deque<OwnPtr<ParsedChunk> > m_speculations; 184 185 WeakPtrFactory<HTMLDocumentParser> m_weakFactory; 186 WeakPtr<BackgroundHTMLParser> m_backgroundParser; 185 187 #endif 186 188
Note: See TracChangeset
for help on using the changeset viewer.