Changeset 261494 in webkit


Ignore:
Timestamp:
May 11, 2020 2:51:26 PM (4 years ago)
Author:
Simon Fraser
Message:

Have ScrollingThread use a RunLoop rather than rolling its own
https://bugs.webkit.org/show_bug.cgi?id=211730

Reviewed by Darin Adler.

ScrollingThread rolled its own runloop/function dispatch by dropping to CF for macOS.
Fix to use RunLoop which provides the same functionality.

There was also a race creating the ScrollingThread for the first time, since both
the main thread, and EventDispatcher can call ScrollingThread::dispatch() early on,
so fix with a std::once block.

  • SourcesCocoa.txt:
  • WebCore.xcodeproj/project.pbxproj:
  • page/scrolling/ScrollingThread.cpp:

(WebCore::ScrollingThread::singleton):
(WebCore::ScrollingThread::dispatch):
(WebCore::ScrollingThread::createThreadIfNeeded):
(WebCore::ScrollingThread::initializeRunLoop):
(WebCore::ScrollingThread::dispatchFunctionsFromScrollingThread): Deleted.
(WebCore::ScrollingThread::wakeUpRunLoop): Deleted.
(WebCore::ScrollingThread::threadRunLoopSourceCallback): Deleted.

  • page/scrolling/ScrollingThread.h:

(WebCore::ScrollingThread::runLoop):

  • page/scrolling/mac/ScrollingThreadMac.mm: Removed.
Location:
trunk/Source/WebCore
Files:
2 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r261493 r261494  
     12020-05-11  Simon Fraser  <simon.fraser@apple.com>
     2
     3        Have ScrollingThread use a RunLoop rather than rolling its own
     4        https://bugs.webkit.org/show_bug.cgi?id=211730
     5
     6        Reviewed by Darin Adler.
     7
     8        ScrollingThread rolled its own runloop/function dispatch by dropping to CF for macOS.
     9        Fix to use RunLoop which provides the same functionality.
     10
     11        There was also a race creating the ScrollingThread for the first time, since both
     12        the main thread, and EventDispatcher can call ScrollingThread::dispatch() early on,
     13        so fix with a std::once block.
     14
     15        * SourcesCocoa.txt:
     16        * WebCore.xcodeproj/project.pbxproj:
     17        * page/scrolling/ScrollingThread.cpp:
     18        (WebCore::ScrollingThread::singleton):
     19        (WebCore::ScrollingThread::dispatch):
     20        (WebCore::ScrollingThread::createThreadIfNeeded):
     21        (WebCore::ScrollingThread::initializeRunLoop):
     22        (WebCore::ScrollingThread::dispatchFunctionsFromScrollingThread): Deleted.
     23        (WebCore::ScrollingThread::wakeUpRunLoop): Deleted.
     24        (WebCore::ScrollingThread::threadRunLoopSourceCallback): Deleted.
     25        * page/scrolling/ScrollingThread.h:
     26        (WebCore::ScrollingThread::runLoop):
     27        * page/scrolling/mac/ScrollingThreadMac.mm: Removed.
     28
    1292020-05-11  Peng Liu  <peng.liu6@apple.com>
    230
  • trunk/Source/WebCore/SourcesCocoa.txt

    r261442 r261494  
    160160page/scrolling/mac/ScrollingMomentumCalculatorMac.mm
    161161page/scrolling/mac/ScrollingStateScrollingNodeMac.mm
    162 page/scrolling/mac/ScrollingThreadMac.mm
    163162page/scrolling/mac/ScrollingTreeFrameScrollingNodeMac.mm
    164163page/scrolling/mac/ScrollingTreeOverflowScrollingNodeMac.mm
  • trunk/Source/WebCore/SourcesGTK.txt

    r260420 r261494  
    6060page/scrolling/nicosia/ScrollingTreePositionedNode.cpp
    6161page/scrolling/nicosia/ScrollingTreeStickyNode.cpp
    62 
    63 page/scrolling/generic/ScrollingThreadGeneric.cpp
    6462
    6563platform/ScrollAnimationKinetic.cpp
  • trunk/Source/WebCore/SourcesWPE.txt

    r257299 r261494  
    5858page/scrolling/nicosia/ScrollingTreeStickyNode.cpp
    5959
    60 page/scrolling/generic/ScrollingThreadGeneric.cpp
    61 
    6260platform/ScrollAnimationKinetic.cpp
    6361platform/UserAgentQuirks.cpp
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r261442 r261494  
    65046504                1AF62F2014DAFE790041556C /* ScrollingThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScrollingThread.cpp; sourceTree = "<group>"; };
    65056505                1AF62F2114DAFE790041556C /* ScrollingThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScrollingThread.h; sourceTree = "<group>"; };
    6506                 1AF62F2314DAFE910041556C /* ScrollingThreadMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ScrollingThreadMac.mm; sourceTree = "<group>"; };
    65076506                1AF7AFC51A48A8BC00C8E4E7 /* SecurityOriginPolicy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SecurityOriginPolicy.cpp; sourceTree = "<group>"; };
    65086507                1AF7AFC61A48A8BC00C8E4E7 /* SecurityOriginPolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SecurityOriginPolicy.h; sourceTree = "<group>"; };
     
    1767517674                                517DEEE31DE94ADC00B91644 /* ScrollingMomentumCalculatorMac.mm */,
    1767617675                                0F73B765222B327F00805316 /* ScrollingStateScrollingNodeMac.mm */,
    17677                                 1AF62F2314DAFE910041556C /* ScrollingThreadMac.mm */,
    1767817676                                93C4A4131629DF5A00C3EB6E /* ScrollingTreeFrameScrollingNodeMac.h */,
    1767917677                                93C4A4141629DF5A00C3EB6E /* ScrollingTreeFrameScrollingNodeMac.mm */,
  • trunk/Source/WebCore/page/MemoryRelease.cpp

    r260195 r261494  
    149149        // FastMalloc has lock-free thread specific caches that can only be cleared from the thread itself.
    150150        WorkerThread::releaseFastMallocFreeMemoryInAllThreads();
    151 #if ENABLE(ASYNC_SCROLLING) && !PLATFORM(IOS_FAMILY)
     151#if ENABLE(SCROLLING_THREAD)
    152152        ScrollingThread::dispatch(WTF::releaseFastMallocFreeMemory);
    153153#endif
  • trunk/Source/WebCore/page/scrolling/ScrollingThread.cpp

    r257688 r261494  
    2727#include "ScrollingThread.h"
    2828
    29 #if ENABLE(ASYNC_SCROLLING)
     29#if ENABLE(SCROLLING_THREAD)
    3030
    3131#include <mutex>
    3232#include <wtf/MainThread.h>
    3333#include <wtf/NeverDestroyed.h>
     34#include <wtf/threads/BinarySemaphore.h>
    3435
    3536namespace WebCore {
    3637
     38ScrollingThread& ScrollingThread::singleton()
     39{
     40    static LazyNeverDestroyed<ScrollingThread> scrollingThread;
     41    static std::once_flag onceFlag;
     42    std::call_once(onceFlag, [] {
     43        scrollingThread.construct();
     44    });
     45
     46    return scrollingThread;
     47}
     48
    3749ScrollingThread::ScrollingThread()
    3850{
     51    BinarySemaphore semaphore;
     52    m_thread = Thread::create("WebCore: Scrolling", [this, &semaphore] {
     53        WTF::Thread::setCurrentThreadIsUserInteractive();
     54        m_runLoop = &RunLoop::current();
     55        semaphore.signal();
     56        m_runLoop->run();
     57    });
     58
     59    semaphore.wait();
    3960}
    4061
     
    4667void ScrollingThread::dispatch(Function<void ()>&& function)
    4768{
    48     auto& scrollingThread = ScrollingThread::singleton();
    49     scrollingThread.createThreadIfNeeded();
    50 
    51     {
    52         auto locker = holdLock(scrollingThread.m_functionsMutex);
    53         scrollingThread.m_functions.append(WTFMove(function));
    54     }
    55 
    56     scrollingThread.wakeUpRunLoop();
     69    ScrollingThread::singleton().runLoop().dispatch(WTFMove(function));
    5770}
    5871
     
    6477}
    6578
    66 ScrollingThread& ScrollingThread::singleton()
    67 {
    68     static NeverDestroyed<ScrollingThread> scrollingThread;
    69 
    70     return scrollingThread;
    71 }
    72 
    73 void ScrollingThread::createThreadIfNeeded()
    74 {
    75     // Wait for the thread to initialize the run loop.
    76     std::unique_lock<Lock> lock(m_initializeRunLoopMutex);
    77 
    78     if (!m_thread) {
    79         m_thread = Thread::create("WebCore: Scrolling", [this] {
    80             WTF::Thread::setCurrentThreadIsUserInteractive();
    81             initializeRunLoop();
    82         });
    83     }
    84 
    85 #if PLATFORM(COCOA)
    86     m_initializeRunLoopConditionVariable.wait(lock, [this]{ return m_threadRunLoop; });
    87 #else
    88     m_initializeRunLoopConditionVariable.wait(lock, [this]{ return m_runLoop; });
    89 #endif
    90 }
    91 
    92 void ScrollingThread::dispatchFunctionsFromScrollingThread()
    93 {
    94     ASSERT(isCurrentThread());
    95 
    96     Vector<Function<void ()>> functions;
    97    
    98     {
    99         auto locker = holdLock(m_functionsMutex);
    100         functions = WTFMove(m_functions);
    101     }
    102 
    103     for (auto& function : functions)
    104         function();
    105 }
    106 
    107 #if PLATFORM(IOS_FAMILY)
    108 NO_RETURN_DUE_TO_ASSERT void ScrollingThread::initializeRunLoop()
    109 {
    110     ASSERT_NOT_REACHED();
    111 }
    112 
    113 void ScrollingThread::wakeUpRunLoop()
    114 {
    115 }
    116 
    117 void ScrollingThread::threadRunLoopSourceCallback(void*)
    118 {
    119 }
    120 
    121 void ScrollingThread::threadRunLoopSourceCallback()
    122 {
    123 }
    124 #endif // PLATFORM(IOS_FAMILY)
    125 
    12679} // namespace WebCore
    12780
    128 #endif // ENABLE(ASYNC_SCROLLING)
     81#endif // ENABLE(SCROLLING_THREAD)
  • trunk/Source/WebCore/page/scrolling/ScrollingThread.h

    r259805 r261494  
    2626#pragma once
    2727
    28 #if ENABLE(ASYNC_SCROLLING)
     28#if ENABLE(SCROLLING_THREAD)
    2929
    3030#include <functional>
     
    3434#include <wtf/Lock.h>
    3535#include <wtf/Noncopyable.h>
     36#include <wtf/RunLoop.h>
    3637#include <wtf/Threading.h>
    3738#include <wtf/Vector.h>
    38 
    39 #if PLATFORM(COCOA)
    40 #include <wtf/RetainPtr.h>
    41 #else
    42 #include <wtf/RunLoop.h>
    43 #endif
    4439
    4540namespace WebCore {
     
    5752
    5853private:
    59     friend NeverDestroyed<ScrollingThread>;
     54    friend LazyNeverDestroyed<ScrollingThread>;
     55
     56    static ScrollingThread& singleton();
    6057
    6158    ScrollingThread();
    6259
    63     static ScrollingThread& singleton();
    64 
    65     void createThreadIfNeeded();
    6660    void dispatchFunctionsFromScrollingThread();
    67 
    68     void initializeRunLoop();
    69     void wakeUpRunLoop();
    70 
    71 #if PLATFORM(COCOA)
    72     static void threadRunLoopSourceCallback(void* scrollingThread);
    73     void threadRunLoopSourceCallback();
    74 #endif
     61    RunLoop& runLoop() { return *m_runLoop; }
    7562
    7663    RefPtr<Thread> m_thread;
    77 
    78     Condition m_initializeRunLoopConditionVariable;
    79     Lock m_initializeRunLoopMutex;
    80 
    81     Lock m_functionsMutex;
    82     Vector<Function<void ()>> m_functions;
    83 
    84 #if PLATFORM(COCOA)
    85     // FIXME: We should use WebCore::RunLoop here.
    86     RetainPtr<CFRunLoopRef> m_threadRunLoop;
    87     RetainPtr<CFRunLoopSourceRef> m_threadRunLoopSource;
    88 #else
    8964    RunLoop* m_runLoop { nullptr };
    90 #endif
    9165};
    9266
    9367} // namespace WebCore
    9468
    95 #endif // ENABLE(ASYNC_SCROLLING)
     69#endif // ENABLE(SCROLLING_THREAD)
Note: See TracChangeset for help on using the changeset viewer.