<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[182889] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/182889">182889</a></dd>
<dt>Author</dt> <dd>ap@apple.com</dd>
<dt>Date</dt> <dd>2015-04-16 09:19:09 -0700 (Thu, 16 Apr 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Minor AudioContext cleanup
https://bugs.webkit.org/show_bug.cgi?id=143816

Reviewed by Jer Noble.

* Modules/webaudio/AudioContext.cpp:
(WebCore::AudioContext::~AudioContext):
(WebCore::AudioContext::lazyInitialize):
(WebCore::AudioContext::stop):
(WebCore::AudioContext::derefNode):
(WebCore::AudioContext::scheduleNodeDeletion):
(WebCore::AudioContext::deleteMarkedNodes):
(WebCore::AudioContext::stopDispatch): Deleted.
(WebCore::AudioContext::deleteMarkedNodesDispatch): Deleted.
* Modules/webaudio/AudioContext.h:

* Modules/webaudio/AudioNode.cpp: (WebCore::AudioNode::~AudioNode):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModuleswebaudioAudioContextcpp">trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebaudioAudioContexth">trunk/Source/WebCore/Modules/webaudio/AudioContext.h</a></li>
<li><a href="#trunkSourceWebCoreModuleswebaudioAudioNodecpp">trunk/Source/WebCore/Modules/webaudio/AudioNode.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (182888 => 182889)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-04-16 15:43:30 UTC (rev 182888)
+++ trunk/Source/WebCore/ChangeLog        2015-04-16 16:19:09 UTC (rev 182889)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2015-04-16  Alexey Proskuryakov  &lt;ap@apple.com&gt;
+
+        Minor AudioContext cleanup
+        https://bugs.webkit.org/show_bug.cgi?id=143816
+
+        Reviewed by Jer Noble.
+
+        * Modules/webaudio/AudioContext.cpp:
+        (WebCore::AudioContext::~AudioContext):
+        (WebCore::AudioContext::lazyInitialize):
+        (WebCore::AudioContext::stop):
+        (WebCore::AudioContext::derefNode):
+        (WebCore::AudioContext::scheduleNodeDeletion):
+        (WebCore::AudioContext::deleteMarkedNodes):
+        (WebCore::AudioContext::stopDispatch): Deleted.
+        (WebCore::AudioContext::deleteMarkedNodesDispatch): Deleted.
+        * Modules/webaudio/AudioContext.h:
+
+        * Modules/webaudio/AudioNode.cpp: (WebCore::AudioNode::~AudioNode):
+
</ins><span class="cx"> 2015-04-16  Chris Dumez  &lt;cdumez@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed attempt to fix Windows build after r182881.
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebaudioAudioContextcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp (182888 => 182889)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp        2015-04-16 15:43:30 UTC (rev 182888)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp        2015-04-16 16:19:09 UTC (rev 182889)
</span><span class="lines">@@ -187,42 +187,43 @@
</span><span class="cx"> #if DEBUG_AUDIONODE_REFERENCES
</span><span class="cx">     fprintf(stderr, &quot;%p: AudioContext::~AudioContext()\n&quot;, this);
</span><span class="cx"> #endif
</span><del>-    // AudioNodes keep a reference to their context, so there should be no way to be in the destructor if there are still AudioNodes around.
</del><span class="cx">     ASSERT(!m_isInitialized);
</span><span class="cx">     ASSERT(m_isStopScheduled);
</span><del>-    ASSERT(!m_nodesToDelete.size());
-    ASSERT(!m_referencedNodes.size());
-    ASSERT(!m_finishedNodes.size());
-    ASSERT(!m_automaticPullNodes.size());
</del><ins>+    ASSERT(m_nodesToDelete.isEmpty());
+    ASSERT(m_referencedNodes.isEmpty());
+    ASSERT(m_finishedNodes.isEmpty()); // FIXME (bug 105870): This assertion fails on tests sometimes.
+    ASSERT(m_automaticPullNodes.isEmpty());
</ins><span class="cx">     if (m_automaticPullNodesNeedUpdating)
</span><span class="cx">         m_renderingAutomaticPullNodes.resize(m_automaticPullNodes.size());
</span><del>-    ASSERT(!m_renderingAutomaticPullNodes.size());
</del><ins>+    ASSERT(m_renderingAutomaticPullNodes.isEmpty());
+    // FIXME: Can we assert that m_deferredFinishDerefList is empty?
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AudioContext::lazyInitialize()
</span><span class="cx"> {
</span><del>-    if (!m_isInitialized) {
-        // Don't allow the context to initialize a second time after it's already been explicitly uninitialized.
-        ASSERT(!m_isAudioThreadFinished);
-        if (!m_isAudioThreadFinished) {
-            if (m_destinationNode.get()) {
-                m_destinationNode-&gt;initialize();
</del><ins>+    if (m_isInitialized)
+        return;
</ins><span class="cx"> 
</span><del>-                if (!isOfflineContext()) {
-                    document()-&gt;addAudioProducer(this);
</del><ins>+    // Don't allow the context to initialize a second time after it's already been explicitly uninitialized.
+    ASSERT(!m_isAudioThreadFinished);
+    if (m_isAudioThreadFinished)
+        return;
</ins><span class="cx"> 
</span><del>-                    // This starts the audio thread. The destination node's provideInput() method will now be called repeatedly to render audio.
-                    // Each time provideInput() is called, a portion of the audio stream is rendered. Let's call this time period a &quot;render quantum&quot;.
-                    // NOTE: for now default AudioContext does not need an explicit startRendering() call from JavaScript.
-                    // We may want to consider requiring it for symmetry with OfflineAudioContext.
-                    startRendering();
-                    ++s_hardwareContextCount;
-                }
</del><ins>+    if (m_destinationNode.get()) {
+        m_destinationNode-&gt;initialize();
</ins><span class="cx"> 
</span><del>-            }
-            m_isInitialized = true;
</del><ins>+        if (!isOfflineContext()) {
+            document()-&gt;addAudioProducer(this);
+
+            // This starts the audio thread. The destination node's provideInput() method will now be called repeatedly to render audio.
+            // Each time provideInput() is called, a portion of the audio stream is rendered. Let's call this time period a &quot;render quantum&quot;.
+            // NOTE: for now default AudioContext does not need an explicit startRendering() call from JavaScript.
+            // We may want to consider requiring it for symmetry with OfflineAudioContext.
+            startRendering();
+            ++s_hardwareContextCount;
</ins><span class="cx">         }
</span><span class="cx">     }
</span><ins>+    m_isInitialized = true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AudioContext::clear()
</span><span class="lines">@@ -326,19 +327,10 @@
</span><span class="cx">     return suspended;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void AudioContext::stopDispatch(void* userData)
-{
-    AudioContext* context = reinterpret_cast&lt;AudioContext*&gt;(userData);
-    ASSERT(context);
-    if (!context)
-        return;
-
-    context-&gt;uninitialize();
-    context-&gt;clear();
-}
-
</del><span class="cx"> void AudioContext::stop()
</span><span class="cx"> {
</span><ins>+    ASSERT(isMainThread());
+
</ins><span class="cx">     // Usually ScriptExecutionContext calls stop twice.
</span><span class="cx">     if (m_isStopScheduled)
</span><span class="cx">         return;
</span><span class="lines">@@ -352,7 +344,12 @@
</span><span class="cx">     // of dealing with all of its ActiveDOMObjects at this point. uninitialize() can de-reference other
</span><span class="cx">     // ActiveDOMObjects so let's schedule uninitialize() to be called later.
</span><span class="cx">     // FIXME: see if there's a more direct way to handle this issue.
</span><del>-    callOnMainThread(stopDispatch, this);
</del><ins>+    // FIXME: This sounds very wrong. The whole idea of stop() is that it stops everything, and if we
+    // schedule some observable work for later, the work likely happens at an inappropriate time.
+    callOnMainThread([this] {
+        uninitialize();
+        clear();
+    });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool AudioContext::canSuspendForPageCache() const
</span><span class="lines">@@ -688,12 +685,8 @@
</span><span class="cx">     
</span><span class="cx">     node-&gt;deref(AudioNode::RefTypeConnection);
</span><span class="cx"> 
</span><del>-    for (unsigned i = 0; i &lt; m_referencedNodes.size(); ++i) {
-        if (node == m_referencedNodes[i]) {
-            m_referencedNodes.remove(i);
-            break;
-        }
-    }
</del><ins>+    ASSERT(m_referencedNodes.contains(node));
+    m_referencedNodes.removeFirst(node);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AudioContext::derefUnfinishedSourceNodes()
</span><span class="lines">@@ -870,24 +863,13 @@
</span><span class="cx"> 
</span><span class="cx">         m_isDeletionScheduled = true;
</span><span class="cx"> 
</span><del>-        // Don't let ourself get deleted before the callback.
-        // See matching deref() in deleteMarkedNodesDispatch().
-        ref();
-        callOnMainThread(deleteMarkedNodesDispatch, this);
</del><ins>+        RefPtr&lt;AudioContext&gt; strongThis(this);
+        callOnMainThread([strongThis] {
+            strongThis-&gt;deleteMarkedNodes();
+        });
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void AudioContext::deleteMarkedNodesDispatch(void* userData)
-{
-    AudioContext* context = reinterpret_cast&lt;AudioContext*&gt;(userData);
-    ASSERT(context);
-    if (!context)
-        return;
-
-    context-&gt;deleteMarkedNodes();
-    context-&gt;deref();
-}
-
</del><span class="cx"> void AudioContext::deleteMarkedNodes()
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span><span class="lines">@@ -897,9 +879,8 @@
</span><span class="cx">     {
</span><span class="cx">         AutoLocker locker(*this);
</span><span class="cx"> 
</span><del>-        while (size_t n = m_nodesToDelete.size()) {
-            AudioNode* node = m_nodesToDelete[n - 1];
-            m_nodesToDelete.removeLast();
</del><ins>+        while (m_nodesToDelete.size()) {
+            AudioNode* node = m_nodesToDelete.takeLast();
</ins><span class="cx"> 
</span><span class="cx">             // Before deleting the node, clear out any AudioNodeInputs from m_dirtySummingJunctions.
</span><span class="cx">             unsigned numberOfInputs = node-&gt;numberOfInputs();
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebaudioAudioContexth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.h (182888 => 182889)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webaudio/AudioContext.h        2015-04-16 15:43:30 UTC (rev 182888)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.h        2015-04-16 16:19:09 UTC (rev 182889)
</span><span class="lines">@@ -274,13 +274,9 @@
</span><span class="cx">     enum class State { Suspended, Running, Interrupted, Closed };
</span><span class="cx">     void setState(State);
</span><span class="cx"> 
</span><del>-    // ScriptExecutionContext calls stop twice.
-    // We'd like to schedule only one stop action for them.
-    static void stopDispatch(void* userData);
</del><span class="cx">     void clear();
</span><span class="cx"> 
</span><span class="cx">     void scheduleNodeDeletion();
</span><del>-    static void deleteMarkedNodesDispatch(void* userData);
</del><span class="cx"> 
</span><span class="cx">     virtual void mediaCanStart() override;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebaudioAudioNodecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webaudio/AudioNode.cpp (182888 => 182889)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webaudio/AudioNode.cpp        2015-04-16 15:43:30 UTC (rev 182888)
+++ trunk/Source/WebCore/Modules/webaudio/AudioNode.cpp        2015-04-16 16:19:09 UTC (rev 182889)
</span><span class="lines">@@ -67,6 +67,7 @@
</span><span class="cx"> 
</span><span class="cx"> AudioNode::~AudioNode()
</span><span class="cx"> {
</span><ins>+    ASSERT(isMainThread());
</ins><span class="cx"> #if DEBUG_AUDIONODE_REFERENCES
</span><span class="cx">     --s_nodeCount[nodeType()];
</span><span class="cx">     fprintf(stderr, &quot;%p: %d: AudioNode::~AudioNode() %d %d\n&quot;, this, nodeType(), m_normalRefCount.load(), m_connectionRefCount);
</span></span></pre>
</div>
</div>

</body>
</html>