Title: [293842] trunk
Revision
293842
Author
[email protected]
Date
2022-05-05 09:40:55 -0700 (Thu, 05 May 2022)

Log Message

SuspendableWorkQueue::suspend should invoke callback immediately when queue is suspended
https://bugs.webkit.org/show_bug.cgi?id=240070

Reviewed by Chris Dumez.

Source/WTF:

With current implementation, if suspend() is called when queue is suspended, the completionHandler is not
invoked unitl the queue is resumed and suspended again. This might cause confusion for callers. To fix it,
now SuspendableWorkQueue will invoke callback immediately when queue is already suspended.

API test: WTF_SuspendableWorkQueue.SuspendTwice

* wtf/SuspendableWorkQueue.cpp:
(WTF::SuspendableWorkQueue::suspend):
(WTF::SuspendableWorkQueue::resume):
(WTF::SuspendableWorkQueue::dispatchSync):
(WTF::SuspendableWorkQueue::suspendIfNeeded):
* wtf/SuspendableWorkQueue.h:

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/SuspendableWorkQueue.cpp: Added.
(TestWebKitAPI::TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (293841 => 293842)


--- trunk/Source/WTF/ChangeLog	2022-05-05 16:34:33 UTC (rev 293841)
+++ trunk/Source/WTF/ChangeLog	2022-05-05 16:40:55 UTC (rev 293842)
@@ -1,3 +1,23 @@
+2022-05-05  Sihui Liu  <[email protected]>
+
+        SuspendableWorkQueue::suspend should invoke callback immediately when queue is suspended
+        https://bugs.webkit.org/show_bug.cgi?id=240070
+
+        Reviewed by Chris Dumez.
+
+        With current implementation, if suspend() is called when queue is suspended, the completionHandler is not 
+        invoked unitl the queue is resumed and suspended again. This might cause confusion for callers. To fix it,
+        now SuspendableWorkQueue will invoke callback immediately when queue is already suspended.
+
+        API test: WTF_SuspendableWorkQueue.SuspendTwice
+
+        * wtf/SuspendableWorkQueue.cpp:
+        (WTF::SuspendableWorkQueue::suspend):
+        (WTF::SuspendableWorkQueue::resume):
+        (WTF::SuspendableWorkQueue::dispatchSync):
+        (WTF::SuspendableWorkQueue::suspendIfNeeded):
+        * wtf/SuspendableWorkQueue.h:
+
 2022-05-04  Kimmo Kinnunen  <[email protected]>
 
         SharedMemory::systemPageSize is redundant function

Modified: trunk/Source/WTF/wtf/SuspendableWorkQueue.cpp (293841 => 293842)


--- trunk/Source/WTF/wtf/SuspendableWorkQueue.cpp	2022-05-05 16:34:33 UTC (rev 293841)
+++ trunk/Source/WTF/wtf/SuspendableWorkQueue.cpp	2022-05-05 16:40:55 UTC (rev 293842)
@@ -44,13 +44,16 @@
     ASSERT(isMainThread());
     Locker suspensionLocker { m_suspensionLock };
 
+    if (m_state == State::Suspended)
+        return completionHandler();
+
     // Last suspend function will be the one that is used.
     m_suspendFunction = WTFMove(suspendFunction);
     m_suspensionCompletionHandlers.append(WTFMove(completionHandler));
-    if (m_isOrWillBeSuspended)
+    if (m_state == State::WillSuspend)
         return;
 
-    m_isOrWillBeSuspended = true;
+    m_state = State::WillSuspend;
     // Make sure queue will be suspended when there is no task scheduled on the queue.
     WorkQueue::dispatch([this] {
         suspendIfNeeded();
@@ -62,11 +65,13 @@
     ASSERT(isMainThread());
     Locker suspensionLocker { m_suspensionLock };
 
-    if (!m_isOrWillBeSuspended)
+    if (m_state == State::Running)
         return;
 
-    m_isOrWillBeSuspended = false;
-    m_suspensionCondition.notifyOne();
+    if (m_state == State::Suspended)
+        m_suspensionCondition.notifyOne();
+
+    m_state = State::Running;
 }
 
 void SuspendableWorkQueue::dispatch(Function<void()>&& function)
@@ -92,7 +97,7 @@
     // otherwise thread may be blocked.
     if (isMainThread()) {
         Locker suspensionLocker { m_suspensionLock };
-        RELEASE_ASSERT(!m_isOrWillBeSuspended);
+        RELEASE_ASSERT(m_state == State::Running);
     }
     WorkQueue::dispatchSync(WTFMove(function));
 }
@@ -118,12 +123,14 @@
 
     Locker suspensionLocker { m_suspensionLock };
     auto suspendFunction = std::exchange(m_suspendFunction, { });
-    if (m_isOrWillBeSuspended)
-        suspendFunction();
+    if (m_state != State::WillSuspend)
+        return;
 
+    m_state = State::Suspended;
+    suspendFunction();
     invokeAllSuspensionCompletionHandlers();
 
-    while (m_isOrWillBeSuspended)
+    while (m_state != State::Running)
         m_suspensionCondition.wait(m_suspensionLock);
 }
 

Modified: trunk/Source/WTF/wtf/SuspendableWorkQueue.h (293841 => 293842)


--- trunk/Source/WTF/wtf/SuspendableWorkQueue.h	2022-05-05 16:34:33 UTC (rev 293841)
+++ trunk/Source/WTF/wtf/SuspendableWorkQueue.h	2022-05-05 16:40:55 UTC (rev 293842)
@@ -56,7 +56,8 @@
 
     Lock m_suspensionLock;
     Condition m_suspensionCondition;
-    bool m_isOrWillBeSuspended WTF_GUARDED_BY_LOCK(m_suspensionLock) { false };
+    enum class State : uint8_t { Running, WillSuspend, Suspended };
+    State m_state WTF_GUARDED_BY_LOCK(m_suspensionLock) { State::Running };
     Function<void()> m_suspendFunction WTF_GUARDED_BY_LOCK(m_suspensionLock);
     Vector<CompletionHandler<void()>> m_suspensionCompletionHandlers WTF_GUARDED_BY_LOCK(m_suspensionLock);
 };

Modified: trunk/Tools/ChangeLog (293841 => 293842)


--- trunk/Tools/ChangeLog	2022-05-05 16:34:33 UTC (rev 293841)
+++ trunk/Tools/ChangeLog	2022-05-05 16:40:55 UTC (rev 293842)
@@ -1,3 +1,14 @@
+2022-05-05  Sihui Liu  <[email protected]>
+
+        SuspendableWorkQueue::suspend should invoke callback immediately when queue is suspended
+        https://bugs.webkit.org/show_bug.cgi?id=240070
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/SuspendableWorkQueue.cpp: Added.
+        (TestWebKitAPI::TEST):
+
 2022-05-05  Kate Cheney  <[email protected]>
 
         Right click using trackpad crashes Safari

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (293841 => 293842)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2022-05-05 16:34:33 UTC (rev 293841)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2022-05-05 16:40:55 UTC (rev 293842)
@@ -753,6 +753,7 @@
 		93625D271CD9741C006DC1F1 /* large-video-without-audio.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 93625D261CD973AF006DC1F1 /* large-video-without-audio.html */; };
 		9368A25E229EFB4700A829CA /* local-storage-process-suspends-1.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 9368A25D229EFB3A00A829CA /* local-storage-process-suspends-1.html */; };
 		9368A25F229EFB4700A829CA /* local-storage-process-suspends-2.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 9368A25C229EFB3A00A829CA /* local-storage-process-suspends-2.html */; };
+		936E4AE42821BB6D00208654 /* SuspendableWorkQueue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 936E4AE32821BB6D00208654 /* SuspendableWorkQueue.cpp */; };
 		936EC36627BA3B0D00AFA8AE /* general-storage-directory.salt in Copy Resources */ = {isa = PBXBuildFile; fileRef = 936EC36527BA3AF200AFA8AE /* general-storage-directory.salt */; };
 		936F72801CD7D9EC0068A0FB /* large-video-with-audio.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 936F727E1CD7D9D00068A0FB /* large-video-with-audio.html */; };
 		936F72811CD7D9EC0068A0FB /* large-video-with-audio.mp4 in Copy Resources */ = {isa = PBXBuildFile; fileRef = 936F727F1CD7D9D00068A0FB /* large-video-with-audio.mp4 */; };
@@ -2563,6 +2564,7 @@
 		93625D261CD973AF006DC1F1 /* large-video-without-audio.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "large-video-without-audio.html"; sourceTree = "<group>"; };
 		9368A25C229EFB3A00A829CA /* local-storage-process-suspends-2.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "local-storage-process-suspends-2.html"; sourceTree = "<group>"; };
 		9368A25D229EFB3A00A829CA /* local-storage-process-suspends-1.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "local-storage-process-suspends-1.html"; sourceTree = "<group>"; };
+		936E4AE32821BB6D00208654 /* SuspendableWorkQueue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SuspendableWorkQueue.cpp; sourceTree = "<group>"; };
 		936EC36527BA3AF200AFA8AE /* general-storage-directory.salt */ = {isa = PBXFileReference; lastKnownFileType = file; path = "general-storage-directory.salt"; sourceTree = "<group>"; };
 		936F727E1CD7D9D00068A0FB /* large-video-with-audio.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "large-video-with-audio.html"; sourceTree = "<group>"; };
 		936F727F1CD7D9D00068A0FB /* large-video-with-audio.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = "large-video-with-audio.mp4"; sourceTree = "<group>"; };
@@ -4725,6 +4727,7 @@
 				7CD70C4E24A436EE00E61040 /* StringParsingBuffer.cpp */,
 				9332EF932649BB68009F5D6D /* StringToIntegerConversion.cpp */,
 				7C74D42D188228F300E5ED57 /* StringView.cpp */,
+				936E4AE32821BB6D00208654 /* SuspendableWorkQueue.cpp */,
 				5597F8341D9596C80066BC21 /* SynchronizedFixedQueue.cpp */,
 				9329AA281DE3F81E003ABD07 /* TextBreakIterator.cpp */,
 				7B2739F22632AB640040F182 /* ThreadAssertionsTest.cpp */,
@@ -5522,6 +5525,7 @@
 				7CD70C4F24A436EE00E61040 /* StringParsingBuffer.cpp in Sources */,
 				9332EF942649BB68009F5D6D /* StringToIntegerConversion.cpp in Sources */,
 				7C83DF3A1D0A590C00FEBCF3 /* StringView.cpp in Sources */,
+				936E4AE42821BB6D00208654 /* SuspendableWorkQueue.cpp in Sources */,
 				5597F8361D9596C80066BC21 /* SynchronizedFixedQueue.cpp in Sources */,
 				7C83DF401D0A590C00FEBCF3 /* TestsController.cpp in Sources */,
 				9329AA291DE3F81E003ABD07 /* TextBreakIterator.cpp in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WTF/SuspendableWorkQueue.cpp (0 => 293842)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/SuspendableWorkQueue.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/SuspendableWorkQueue.cpp	2022-05-05 16:40:55 UTC (rev 293842)
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#include "config.h"
+#include <wtf/SuspendableWorkQueue.h>
+
+#include "Test.h"
+#include "Utilities.h"
+#include <wtf/Lock.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF_SuspendableWorkQueue, Suspend)
+{
+    Lock lock;
+    int taskCount = 100;
+    int completedTaskCount = 0;
+    bool allTasksCompleted = false;
+    bool suspended = false;
+    bool suspendCompletionHandlerCalled = false;
+    auto queue = SuspendableWorkQueue::create("com.apple.WebKit.Test.simple");
+    for (int i = 0; i < taskCount; i ++) {
+        queue->dispatch([&]() mutable {
+            Locker locker { lock };
+            suspended = false;
+            ++completedTaskCount;
+            if (completedTaskCount == taskCount)
+                allTasksCompleted = true;
+        });
+    }
+    queue->suspend([&] {
+        Locker locker { lock };
+        suspended = true;
+    }, [&] {
+        suspendCompletionHandlerCalled = true;
+    });
+    Util::run(&suspendCompletionHandlerCalled);
+    {
+        Locker locker { lock };
+        EXPECT_LE(completedTaskCount, taskCount);
+        EXPECT_TRUE(suspended);
+    }
+
+    queue->resume();
+    Util::run(&allTasksCompleted);
+    Locker locker { lock };
+    EXPECT_EQ(completedTaskCount, taskCount);
+    EXPECT_FALSE(suspended);
+}
+
+TEST(WTF_SuspendableWorkQueue, SuspendTwice)
+{
+    Lock lock;
+    bool suspendCompletionHandlerCalled = false;
+    int suspendCount = 0;
+    auto queue = SuspendableWorkQueue::create("com.apple.WebKit.Test.simple");
+    queue->suspend([&]() {
+        Locker locker { lock };
+        ++suspendCount;
+    }, [&] {
+        suspendCompletionHandlerCalled = true;
+    });
+    Util::run(&suspendCompletionHandlerCalled);
+    {
+        Locker locker { lock };
+        EXPECT_EQ(1, suspendCount);
+    }
+    
+    suspendCompletionHandlerCalled = false;
+    queue->suspend([&]() {
+        Locker locker { lock };
+        ++suspendCount;
+    }, [&] {
+        suspendCompletionHandlerCalled = true;
+    });
+    Util::run(&suspendCompletionHandlerCalled);
+    Locker locker { lock };
+    EXPECT_EQ(1, suspendCount);
+}
+
+} // namesapce TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to