Title: [283685] trunk/Source/WebCore
Revision
283685
Author
[email protected]
Date
2021-10-06 17:43:09 -0700 (Wed, 06 Oct 2021)

Log Message

Have SourceBufferPrivateAVJObjC use WorkQueue instead of dispatch_async
https://bugs.webkit.org/show_bug.cgi?id=230881
rdar://83429276

This change is twofold:
1- dispatch_async takes an objective-C block which doesn't support C++ move
semantics. As such the lambda would be copied including the captured variables.
When use with a non-thread safe refcounted object it could lead to a race
on the refcount which would result in a leak.
By using a WorkQueue instead, we avoid the above problem.
2- We want to unify in the media code how threads are created and task
dispatched: WorkQueue is a nice solution.

Reviewed by Chris Dumez.

No observable difference.

* platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
* platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:
(WebCore::SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC):
(WebCore::SourceBufferPrivateAVFObjC::append):
(WebCore::SourceBufferPrivateAVFObjC::resetParserState):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (283684 => 283685)


--- trunk/Source/WebCore/ChangeLog	2021-10-07 00:32:40 UTC (rev 283684)
+++ trunk/Source/WebCore/ChangeLog	2021-10-07 00:43:09 UTC (rev 283685)
@@ -1,3 +1,28 @@
+2021-10-06  Jean-Yves Avenard  <[email protected]>
+
+        Have SourceBufferPrivateAVJObjC use WorkQueue instead of dispatch_async
+        https://bugs.webkit.org/show_bug.cgi?id=230881
+        rdar://83429276
+
+        This change is twofold:
+        1- dispatch_async takes an objective-C block which doesn't support C++ move
+        semantics. As such the lambda would be copied including the captured variables.
+        When use with a non-thread safe refcounted object it could lead to a race
+        on the refcount which would result in a leak.
+        By using a WorkQueue instead, we avoid the above problem.
+        2- We want to unify in the media code how threads are created and task
+        dispatched: WorkQueue is a nice solution.
+
+        Reviewed by Chris Dumez.
+
+        No observable difference.
+
+        * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
+        * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:
+        (WebCore::SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC):
+        (WebCore::SourceBufferPrivateAVFObjC::append):
+        (WebCore::SourceBufferPrivateAVFObjC::resetParserState):
+
 2021-10-06  Tyler Wilcock  <[email protected]>
 
         AX: Make AccessibilityObjectInterface::contents return-by-value instead of return-by-out-parameter

Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h (283684 => 283685)


--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h	2021-10-07 00:32:40 UTC (rev 283684)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h	2021-10-07 00:43:09 UTC (rev 283685)
@@ -56,6 +56,10 @@
 typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
 typedef const struct opaqueCMFormatDescription *CMFormatDescriptionRef;
 
+namespace WTF {
+class WorkQueue;
+}
+
 namespace WebCore {
 
 class CDMInstance;
@@ -206,7 +210,7 @@
     RetainPtr<NSError> m_hdcpError;
     Box<BinarySemaphore> m_hasSessionSemaphore;
     Box<Semaphore> m_abortSemaphore;
-    OSObjectPtr<dispatch_group_t> m_isAppendingGroup;
+    const Ref<WTF::WorkQueue> m_appendQueue;
     RefPtr<WebCoreDecompressionSession> m_decompressionSession;
 
     MediaSourcePrivateAVFObjC* m_mediaSource;

Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm (283684 => 283685)


--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm	2021-10-07 00:32:40 UTC (rev 283684)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm	2021-10-07 00:43:09 UTC (rev 283685)
@@ -60,6 +60,7 @@
 #import <wtf/SoftLinking.h>
 #import <wtf/WTFSemaphore.h>
 #import <wtf/WeakPtr.h>
+#import <wtf/WorkQueue.h>
 #import <wtf/text/AtomString.h>
 #import <wtf/text/CString.h>
 #import <wtf/text/StringToIntegerConversion.h>
@@ -304,7 +305,7 @@
 SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC* parent, Ref<SourceBufferParser>&& parser)
     : m_parser(WTFMove(parser))
     , m_errorListener(adoptNS([[WebAVSampleBufferErrorListener alloc] initWithParent:makeWeakPtr(*this)]))
-    , m_isAppendingGroup(adoptOSObject(dispatch_group_create()))
+    , m_appendQueue(WorkQueue::create("SourceBufferPrivateAVFObjC data parser queue"))
     , m_mediaSource(parent)
     , m_mapID(nextMapID())
 #if !RELEASE_LOG_DISABLED
@@ -562,16 +563,6 @@
     UNUSED_PARAM(hasSessionSemaphore);
 }
 
-static dispatch_queue_t globalDataParserQueue()
-{
-    static dispatch_queue_t globalQueue;
-    static dispatch_once_t onceToken;
-    dispatch_once(&onceToken, ^{
-        globalQueue = dispatch_queue_create("SourceBufferPrivateAVFObjC data parser queue", DISPATCH_QUEUE_CONCURRENT);
-    });
-    return globalQueue;
-}
-
 void SourceBufferPrivateAVFObjC::append(Ref<SharedBuffer>&& data)
 {
     ALWAYS_LOG(LOGIDENTIFIER, "data length = ", data->size());
@@ -646,9 +637,8 @@
     });
 
     m_parsingSucceeded = true;
-    dispatch_group_enter(m_isAppendingGroup.get());
 
-    dispatch_async(globalDataParserQueue(), [data = "" weakThis = m_appendWeakFactory.createWeakPtr(*this), parser = m_parser, isAppendingGroup = m_isAppendingGroup, abortCalled = m_abortCalled]() mutable {
+    m_appendQueue->dispatch([data = "" weakThis = m_appendWeakFactory.createWeakPtr(*this), parser = m_parser, abortCalled = m_abortCalled]() mutable {
         parser->appendData(WTFMove(data), [weakThis = WTFMove(weakThis), abortCalled]() mutable {
             callOnMainThread([weakThis = WTFMove(weakThis), abortCalled] {
                 if (!weakThis || abortCalled != weakThis->m_abortCalled)
@@ -662,7 +652,6 @@
                 weakThis->appendCompleted();
             });
         });
-        dispatch_group_leave(isAppendingGroup.get());
     });
 }
 
@@ -708,7 +697,8 @@
 {
     ALWAYS_LOG(LOGIDENTIFIER);
 
-    dispatch_group_wait(m_isAppendingGroup.get(), DISPATCH_TIME_FOREVER);
+    // Wait until all tasks in the workqueue have run.
+    m_appendQueue->dispatchSync([] { });
     m_mediaSamples.clear();
     m_hasPendingAppendCompletedCallback = false;
     m_processingInitializationSegment = false;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to