Title: [261494] trunk/Source/WebCore
Revision
261494
Author
[email protected]
Date
2020-05-11 14:51:26 -0700 (Mon, 11 May 2020)

Log 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.

Modified Paths

Removed Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (261493 => 261494)


--- trunk/Source/WebCore/ChangeLog	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/ChangeLog	2020-05-11 21:51:26 UTC (rev 261494)
@@ -1,3 +1,31 @@
+2020-05-11  Simon Fraser  <[email protected]>
+
+        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.
+
 2020-05-11  Peng Liu  <[email protected]>
 
         Enable the mock video presentation mode in related layout tests and fix test failures

Modified: trunk/Source/WebCore/SourcesCocoa.txt (261493 => 261494)


--- trunk/Source/WebCore/SourcesCocoa.txt	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/SourcesCocoa.txt	2020-05-11 21:51:26 UTC (rev 261494)
@@ -159,7 +159,6 @@
 page/scrolling/mac/ScrollingCoordinatorMac.mm
 page/scrolling/mac/ScrollingMomentumCalculatorMac.mm
 page/scrolling/mac/ScrollingStateScrollingNodeMac.mm
-page/scrolling/mac/ScrollingThreadMac.mm
 page/scrolling/mac/ScrollingTreeFrameScrollingNodeMac.mm
 page/scrolling/mac/ScrollingTreeOverflowScrollingNodeMac.mm
 page/scrolling/mac/ScrollingTreeScrollingNodeDelegateMac.mm

Modified: trunk/Source/WebCore/SourcesGTK.txt (261493 => 261494)


--- trunk/Source/WebCore/SourcesGTK.txt	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/SourcesGTK.txt	2020-05-11 21:51:26 UTC (rev 261494)
@@ -60,8 +60,6 @@
 page/scrolling/nicosia/ScrollingTreePositionedNode.cpp
 page/scrolling/nicosia/ScrollingTreeStickyNode.cpp
 
-page/scrolling/generic/ScrollingThreadGeneric.cpp
-
 platform/ScrollAnimationKinetic.cpp
 platform/UserAgentQuirks.cpp
 

Modified: trunk/Source/WebCore/SourcesWPE.txt (261493 => 261494)


--- trunk/Source/WebCore/SourcesWPE.txt	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/SourcesWPE.txt	2020-05-11 21:51:26 UTC (rev 261494)
@@ -57,8 +57,6 @@
 page/scrolling/nicosia/ScrollingTreePositionedNode.cpp
 page/scrolling/nicosia/ScrollingTreeStickyNode.cpp
 
-page/scrolling/generic/ScrollingThreadGeneric.cpp
-
 platform/ScrollAnimationKinetic.cpp
 platform/UserAgentQuirks.cpp
 

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (261493 => 261494)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2020-05-11 21:51:26 UTC (rev 261494)
@@ -6503,7 +6503,6 @@
 		1AF62EE514DA22A70041556C /* ScrollingCoordinator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScrollingCoordinator.h; sourceTree = "<group>"; };
 		1AF62F2014DAFE790041556C /* ScrollingThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScrollingThread.cpp; sourceTree = "<group>"; };
 		1AF62F2114DAFE790041556C /* ScrollingThread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScrollingThread.h; sourceTree = "<group>"; };
-		1AF62F2314DAFE910041556C /* ScrollingThreadMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ScrollingThreadMac.mm; sourceTree = "<group>"; };
 		1AF7AFC51A48A8BC00C8E4E7 /* SecurityOriginPolicy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SecurityOriginPolicy.cpp; sourceTree = "<group>"; };
 		1AF7AFC61A48A8BC00C8E4E7 /* SecurityOriginPolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SecurityOriginPolicy.h; sourceTree = "<group>"; };
 		1AF89A411518FDEA00E547B5 /* TiledBacking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiledBacking.h; sourceTree = "<group>"; };
@@ -17674,7 +17673,6 @@
 				517DEEE71DE94B0800B91644 /* ScrollingMomentumCalculatorMac.h */,
 				517DEEE31DE94ADC00B91644 /* ScrollingMomentumCalculatorMac.mm */,
 				0F73B765222B327F00805316 /* ScrollingStateScrollingNodeMac.mm */,
-				1AF62F2314DAFE910041556C /* ScrollingThreadMac.mm */,
 				93C4A4131629DF5A00C3EB6E /* ScrollingTreeFrameScrollingNodeMac.h */,
 				93C4A4141629DF5A00C3EB6E /* ScrollingTreeFrameScrollingNodeMac.mm */,
 				0FE5806219327A6200DE32EB /* ScrollingTreeMac.h */,

Modified: trunk/Source/WebCore/page/MemoryRelease.cpp (261493 => 261494)


--- trunk/Source/WebCore/page/MemoryRelease.cpp	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/page/MemoryRelease.cpp	2020-05-11 21:51:26 UTC (rev 261494)
@@ -148,7 +148,7 @@
     if (synchronous == Synchronous::Yes) {
         // FastMalloc has lock-free thread specific caches that can only be cleared from the thread itself.
         WorkerThread::releaseFastMallocFreeMemoryInAllThreads();
-#if ENABLE(ASYNC_SCROLLING) && !PLATFORM(IOS_FAMILY)
+#if ENABLE(SCROLLING_THREAD)
         ScrollingThread::dispatch(WTF::releaseFastMallocFreeMemory);
 #endif
         WTF::releaseFastMallocFreeMemory();

Modified: trunk/Source/WebCore/page/scrolling/ScrollingThread.cpp (261493 => 261494)


--- trunk/Source/WebCore/page/scrolling/ScrollingThread.cpp	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/page/scrolling/ScrollingThread.cpp	2020-05-11 21:51:26 UTC (rev 261494)
@@ -26,16 +26,37 @@
 #include "config.h"
 #include "ScrollingThread.h"
 
-#if ENABLE(ASYNC_SCROLLING)
+#if ENABLE(SCROLLING_THREAD)
 
 #include <mutex>
 #include <wtf/MainThread.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/threads/BinarySemaphore.h>
 
 namespace WebCore {
 
+ScrollingThread& ScrollingThread::singleton()
+{
+    static LazyNeverDestroyed<ScrollingThread> scrollingThread;
+    static std::once_flag onceFlag;
+    std::call_once(onceFlag, [] {
+        scrollingThread.construct();
+    });
+
+    return scrollingThread;
+}
+
 ScrollingThread::ScrollingThread()
 {
+    BinarySemaphore semaphore;
+    m_thread = Thread::create("WebCore: Scrolling", [this, &semaphore] {
+        WTF::Thread::setCurrentThreadIsUserInteractive();
+        m_runLoop = &RunLoop::current();
+        semaphore.signal();
+        m_runLoop->run();
+    });
+
+    semaphore.wait();
 }
 
 bool ScrollingThread::isCurrentThread()
@@ -45,15 +66,7 @@
 
 void ScrollingThread::dispatch(Function<void ()>&& function)
 {
-    auto& scrollingThread = ScrollingThread::singleton();
-    scrollingThread.createThreadIfNeeded();
-
-    {
-        auto locker = holdLock(scrollingThread.m_functionsMutex);
-        scrollingThread.m_functions.append(WTFMove(function));
-    }
-
-    scrollingThread.wakeUpRunLoop();
+    ScrollingThread::singleton().runLoop().dispatch(WTFMove(function));
 }
 
 void ScrollingThread::dispatchBarrier(Function<void ()>&& function)
@@ -63,66 +76,6 @@
     });
 }
 
-ScrollingThread& ScrollingThread::singleton()
-{
-    static NeverDestroyed<ScrollingThread> scrollingThread;
-
-    return scrollingThread;
-}
-
-void ScrollingThread::createThreadIfNeeded()
-{
-    // Wait for the thread to initialize the run loop.
-    std::unique_lock<Lock> lock(m_initializeRunLoopMutex);
-
-    if (!m_thread) {
-        m_thread = Thread::create("WebCore: Scrolling", [this] {
-            WTF::Thread::setCurrentThreadIsUserInteractive();
-            initializeRunLoop();
-        });
-    }
-
-#if PLATFORM(COCOA)
-    m_initializeRunLoopConditionVariable.wait(lock, [this]{ return m_threadRunLoop; });
-#else
-    m_initializeRunLoopConditionVariable.wait(lock, [this]{ return m_runLoop; });
-#endif
-}
-
-void ScrollingThread::dispatchFunctionsFromScrollingThread()
-{
-    ASSERT(isCurrentThread());
-
-    Vector<Function<void ()>> functions;
-    
-    {
-        auto locker = holdLock(m_functionsMutex);
-        functions = WTFMove(m_functions);
-    }
-
-    for (auto& function : functions)
-        function();
-}
-
-#if PLATFORM(IOS_FAMILY)
-NO_RETURN_DUE_TO_ASSERT void ScrollingThread::initializeRunLoop()
-{
-    ASSERT_NOT_REACHED();
-}
-
-void ScrollingThread::wakeUpRunLoop()
-{
-}
-
-void ScrollingThread::threadRunLoopSourceCallback(void*)
-{
-}
-
-void ScrollingThread::threadRunLoopSourceCallback()
-{
-}
-#endif // PLATFORM(IOS_FAMILY)
-
 } // namespace WebCore
 
-#endif // ENABLE(ASYNC_SCROLLING)
+#endif // ENABLE(SCROLLING_THREAD)

Modified: trunk/Source/WebCore/page/scrolling/ScrollingThread.h (261493 => 261494)


--- trunk/Source/WebCore/page/scrolling/ScrollingThread.h	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/page/scrolling/ScrollingThread.h	2020-05-11 21:51:26 UTC (rev 261494)
@@ -25,7 +25,7 @@
 
 #pragma once
 
-#if ENABLE(ASYNC_SCROLLING)
+#if ENABLE(SCROLLING_THREAD)
 
 #include <functional>
 #include <wtf/Condition.h>
@@ -33,15 +33,10 @@
 #include <wtf/Function.h>
 #include <wtf/Lock.h>
 #include <wtf/Noncopyable.h>
+#include <wtf/RunLoop.h>
 #include <wtf/Threading.h>
 #include <wtf/Vector.h>
 
-#if PLATFORM(COCOA)
-#include <wtf/RetainPtr.h>
-#else
-#include <wtf/RunLoop.h>
-#endif
-
 namespace WebCore {
 
 class ScrollingThread {
@@ -56,40 +51,19 @@
     WEBCORE_EXPORT static void dispatchBarrier(Function<void ()>&&);
 
 private:
-    friend NeverDestroyed<ScrollingThread>;
+    friend LazyNeverDestroyed<ScrollingThread>;
 
+    static ScrollingThread& singleton();
+
     ScrollingThread();
 
-    static ScrollingThread& singleton();
-
-    void createThreadIfNeeded();
     void dispatchFunctionsFromScrollingThread();
+    RunLoop& runLoop() { return *m_runLoop; }
 
-    void initializeRunLoop();
-    void wakeUpRunLoop();
-
-#if PLATFORM(COCOA)
-    static void threadRunLoopSourceCallback(void* scrollingThread);
-    void threadRunLoopSourceCallback();
-#endif
-
     RefPtr<Thread> m_thread;
-
-    Condition m_initializeRunLoopConditionVariable;
-    Lock m_initializeRunLoopMutex;
-
-    Lock m_functionsMutex;
-    Vector<Function<void ()>> m_functions;
-
-#if PLATFORM(COCOA)
-    // FIXME: We should use WebCore::RunLoop here.
-    RetainPtr<CFRunLoopRef> m_threadRunLoop;
-    RetainPtr<CFRunLoopSourceRef> m_threadRunLoopSource;
-#else
     RunLoop* m_runLoop { nullptr };
-#endif
 };
 
 } // namespace WebCore
 
-#endif // ENABLE(ASYNC_SCROLLING)
+#endif // ENABLE(SCROLLING_THREAD)

Deleted: trunk/Source/WebCore/page/scrolling/mac/ScrollingThreadMac.mm (261493 => 261494)


--- trunk/Source/WebCore/page/scrolling/mac/ScrollingThreadMac.mm	2020-05-11 21:03:24 UTC (rev 261493)
+++ trunk/Source/WebCore/page/scrolling/mac/ScrollingThreadMac.mm	2020-05-11 21:51:26 UTC (rev 261494)
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 2012 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#import "config.h"
-#import "ScrollingThread.h"
-
-#if ENABLE(ASYNC_SCROLLING) && PLATFORM(MAC)
-
-#import <mutex>
-
-namespace WebCore {
-
-void ScrollingThread::initializeRunLoop()
-{
-    // Initialize the run loop.
-    {
-        auto locker = holdLock(m_initializeRunLoopMutex);
-
-        m_threadRunLoop = CFRunLoopGetCurrent();
-
-        CFRunLoopSourceContext context = { 0, this, 0, 0, 0, 0, 0, 0, 0, threadRunLoopSourceCallback };
-        m_threadRunLoopSource = adoptCF(CFRunLoopSourceCreate(0, 0, &context));
-        CFRunLoopAddSource(CFRunLoopGetCurrent(), m_threadRunLoopSource.get(), kCFRunLoopDefaultMode);
-
-        m_initializeRunLoopConditionVariable.notifyAll();
-    }
-
-    ASSERT(isCurrentThread());
-
-    CFRunLoopRun();
-}
-
-void ScrollingThread::wakeUpRunLoop()
-{
-    CFRunLoopSourceSignal(m_threadRunLoopSource.get());
-    CFRunLoopWakeUp(m_threadRunLoop.get());
-}
-
-void ScrollingThread::threadRunLoopSourceCallback(void* scrollingThread)
-{
-    static_cast<ScrollingThread*>(scrollingThread)->threadRunLoopSourceCallback();
-}
-
-void ScrollingThread::threadRunLoopSourceCallback()
-{
-    @autoreleasepool {
-        dispatchFunctionsFromScrollingThread();
-    }
-}
-
-} // namespace WebCore
-
-#endif // ENABLE(ASYNC_SCROLLING) && PLATFORM(MAC)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to