<!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>[165508] trunk/Source/JavaScriptCore</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/165508">165508</a></dd>
<dt>Author</dt> <dd>joepeck@webkit.org</dd>
<dt>Date</dt> <dd>2014-03-12 16:37:17 -0700 (Wed, 12 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: Hang in Remote Inspection triggering breakpoint from console
https://bugs.webkit.org/show_bug.cgi?id=130032

Reviewed by Timothy Hatcher.

* inspector/EventLoop.h:
* inspector/EventLoop.cpp:
(Inspector::EventLoop::remoteInspectorRunLoopMode):
(Inspector::EventLoop::cycle):
Expose the run loop mode name so it can be used if needed by others.

* inspector/remote/RemoteInspectorDebuggableConnection.h:
* inspector/remote/RemoteInspectorDebuggableConnection.mm:
(Inspector::RemoteInspectorBlock::RemoteInspectorBlock):
(Inspector::RemoteInspectorBlock::~RemoteInspectorBlock):
(Inspector::RemoteInspectorBlock::operator=):
(Inspector::RemoteInspectorBlock::operator()):
(Inspector::RemoteInspectorQueueTask):
Instead of a dispatch_queue, have our own static Vector of debugger tasks.

(Inspector::RemoteInspectorHandleRunSource):
(Inspector::RemoteInspectorInitializeQueue):
Initialize the static queue and run loop source. When the run loop source
fires, it will exhaust the queue of debugger messages.

(Inspector::RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection):
(Inspector::RemoteInspectorDebuggableConnection::~RemoteInspectorDebuggableConnection):
When we get a debuggable connection add a run loop source for inspector commands.

(Inspector::RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable):
(Inspector::RemoteInspectorDebuggableConnection::sendMessageToBackend):
Enqueue blocks on our Vector instead of our dispatch_queue.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreinspectorEventLoopcpp">trunk/Source/JavaScriptCore/inspector/EventLoop.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreinspectorEventLooph">trunk/Source/JavaScriptCore/inspector/EventLoop.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreinspectorremoteRemoteInspectorDebuggableConnectionh">trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreinspectorremoteRemoteInspectorDebuggableConnectionmm">trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (165507 => 165508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2014-03-12 23:36:31 UTC (rev 165507)
+++ trunk/Source/JavaScriptCore/ChangeLog        2014-03-12 23:37:17 UTC (rev 165508)
</span><span class="lines">@@ -1,3 +1,38 @@
</span><ins>+2014-03-12  Joseph Pecoraro  &lt;pecoraro@apple.com&gt;
+
+        Web Inspector: Hang in Remote Inspection triggering breakpoint from console
+        https://bugs.webkit.org/show_bug.cgi?id=130032
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/EventLoop.h:
+        * inspector/EventLoop.cpp:
+        (Inspector::EventLoop::remoteInspectorRunLoopMode):
+        (Inspector::EventLoop::cycle):
+        Expose the run loop mode name so it can be used if needed by others.
+
+        * inspector/remote/RemoteInspectorDebuggableConnection.h:
+        * inspector/remote/RemoteInspectorDebuggableConnection.mm:
+        (Inspector::RemoteInspectorBlock::RemoteInspectorBlock):
+        (Inspector::RemoteInspectorBlock::~RemoteInspectorBlock):
+        (Inspector::RemoteInspectorBlock::operator=):
+        (Inspector::RemoteInspectorBlock::operator()):
+        (Inspector::RemoteInspectorQueueTask):
+        Instead of a dispatch_queue, have our own static Vector of debugger tasks.
+
+        (Inspector::RemoteInspectorHandleRunSource):
+        (Inspector::RemoteInspectorInitializeQueue):
+        Initialize the static queue and run loop source. When the run loop source
+        fires, it will exhaust the queue of debugger messages.
+
+        (Inspector::RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection):
+        (Inspector::RemoteInspectorDebuggableConnection::~RemoteInspectorDebuggableConnection):
+        When we get a debuggable connection add a run loop source for inspector commands.
+
+        (Inspector::RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable):
+        (Inspector::RemoteInspectorDebuggableConnection::sendMessageToBackend):
+        Enqueue blocks on our Vector instead of our dispatch_queue.
+
</ins><span class="cx"> 2014-03-12  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r165482.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreinspectorEventLoopcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/inspector/EventLoop.cpp (165507 => 165508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/inspector/EventLoop.cpp        2014-03-12 23:36:31 UTC (rev 165507)
+++ trunk/Source/JavaScriptCore/inspector/EventLoop.cpp        2014-03-12 23:37:17 UTC (rev 165508)
</span><span class="lines">@@ -28,8 +28,6 @@
</span><span class="cx"> 
</span><span class="cx"> #if OS(WINDOWS)
</span><span class="cx"> #include &lt;windows.h&gt;
</span><del>-#elif USE(CF)
-#include &lt;CoreFoundation/CFRunLoop.h&gt;
</del><span class="cx"> #elif PLATFORM(EFL)
</span><span class="cx"> #include &lt;Ecore.h&gt;
</span><span class="cx"> #elif PLATFORM(GTK)
</span><span class="lines">@@ -38,6 +36,13 @@
</span><span class="cx"> 
</span><span class="cx"> namespace Inspector {
</span><span class="cx"> 
</span><ins>+#if USE(CF) &amp;&amp; !OS(WINDOWS)
+CFStringRef EventLoop::remoteInspectorRunLoopMode()
+{
+    return CFSTR(&quot;com.apple.JavaScriptCore.remote-inspector-runloop-mode&quot;);
+}
+#endif
+
</ins><span class="cx"> void EventLoop::cycle()
</span><span class="cx"> {
</span><span class="cx"> #if OS(WINDOWS)
</span><span class="lines">@@ -54,9 +59,8 @@
</span><span class="cx">     // nested. Only the debugger should control things until we continue.
</span><span class="cx">     // FIXME: This is not a perfect solution, as background threads are not
</span><span class="cx">     // paused and can still access and evalute script in the JSContext.
</span><del>-    static const CFStringRef kRWIRunLoopMode = CFSTR(&quot;com.apple.JavaScriptCore.remote-inspector-runloop-mode&quot;);
</del><span class="cx">     CFTimeInterval timeInterval = 0.05;
</span><del>-    CFRunLoopRunInMode(kRWIRunLoopMode, timeInterval, true);
</del><ins>+    CFRunLoopRunInMode(remoteInspectorRunLoopMode(), timeInterval, true);
</ins><span class="cx"> #elif PLATFORM(EFL)
</span><span class="cx">     ecore_main_loop_iterate();
</span><span class="cx"> #elif PLATFORM(GTK)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreinspectorEventLooph"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/inspector/EventLoop.h (165507 => 165508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/inspector/EventLoop.h        2014-03-12 23:36:31 UTC (rev 165507)
+++ trunk/Source/JavaScriptCore/inspector/EventLoop.h        2014-03-12 23:37:17 UTC (rev 165508)
</span><span class="lines">@@ -28,6 +28,10 @@
</span><span class="cx"> 
</span><span class="cx"> #include &lt;wtf/Noncopyable.h&gt;
</span><span class="cx"> 
</span><ins>+#if USE(CF) &amp;&amp; !OS(WINDOWS)
+#include &lt;CoreFoundation/CFRunLoop.h&gt;
+#endif
+
</ins><span class="cx"> namespace Inspector {
</span><span class="cx"> 
</span><span class="cx"> class EventLoop {
</span><span class="lines">@@ -41,6 +45,10 @@
</span><span class="cx">     void cycle();
</span><span class="cx">     bool ended() const { return m_ended; }
</span><span class="cx"> 
</span><ins>+#if USE(CF) &amp;&amp; !OS(WINDOWS)
+    static CFStringRef remoteInspectorRunLoopMode();
+#endif
+
</ins><span class="cx"> private:
</span><span class="cx">     bool m_ended;
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreinspectorremoteRemoteInspectorDebuggableConnectionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h (165507 => 165508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h        2014-03-12 23:36:31 UTC (rev 165507)
+++ trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h        2014-03-12 23:37:17 UTC (rev 165508)
</span><span class="lines">@@ -65,7 +65,6 @@
</span><span class="cx">     std::mutex m_debuggableMutex;
</span><span class="cx"> 
</span><span class="cx">     RemoteInspectorDebuggable* m_debuggable;
</span><del>-    dispatch_queue_t m_queueForDebuggable;
</del><span class="cx">     RetainPtr&lt;NSString&gt; m_connectionIdentifier;
</span><span class="cx">     RetainPtr&lt;NSString&gt; m_destination;
</span><span class="cx">     unsigned m_identifier;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreinspectorremoteRemoteInspectorDebuggableConnectionmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm (165507 => 165508)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm        2014-03-12 23:36:31 UTC (rev 165507)
+++ trunk/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm        2014-03-12 23:37:17 UTC (rev 165508)
</span><span class="lines">@@ -28,7 +28,9 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(REMOTE_INSPECTOR)
</span><span class="cx"> 
</span><ins>+#import &quot;EventLoop.h&quot;
</ins><span class="cx"> #import &quot;RemoteInspector.h&quot;
</span><ins>+#import &lt;wtf/Vector.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> #import &lt;wtf/ios/WebCoreThread.h&gt;
</span><span class="lines">@@ -36,26 +38,105 @@
</span><span class="cx"> 
</span><span class="cx"> namespace Inspector {
</span><span class="cx"> 
</span><del>-RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection(RemoteInspectorDebuggable* debuggable, NSString *connectionIdentifier, NSString *destination, RemoteInspectorDebuggable::DebuggableType type)
</del><ins>+class RemoteInspectorBlock {
+public:
+    RemoteInspectorBlock(void (^task)())
+        : m_task(Block_copy(task))
+    {
+    }
+
+    RemoteInspectorBlock(const RemoteInspectorBlock&amp; other)
+        : m_task(Block_copy(other.m_task))
+    {
+    }
+
+    ~RemoteInspectorBlock()
+    {
+        Block_release(m_task);
+    }
+
+    RemoteInspectorBlock&amp; operator=(const RemoteInspectorBlock&amp; other)
+    {
+        void (^oldTask)() = m_task;
+        m_task = Block_copy(other.m_task);
+        Block_release(oldTask);
+        return *this;
+    }
+
+    void operator()() const
+    {
+        m_task();
+    }
+
+private:
+    void (^m_task)();
+};
+
+typedef Vector&lt;RemoteInspectorBlock&gt; RemoteInspectorQueue;
+static std::mutex* rwiQueueMutex;
+static CFRunLoopSourceRef rwiRunLoopSource;
+static RemoteInspectorQueue* rwiQueue;
+
+static void RemoteInspectorHandleRunSource(void*)
+{
+    ASSERT(CFRunLoopGetCurrent() == CFRunLoopGetMain());
+    ASSERT(rwiQueueMutex);
+    ASSERT(rwiRunLoopSource);
+    ASSERT(rwiQueue);
+
+    RemoteInspectorQueue queueCopy;
+    {
+        std::lock_guard&lt;std::mutex&gt; lock(*rwiQueueMutex);
+        queueCopy = *rwiQueue;
+        rwiQueue-&gt;clear();
+    }
+
+    for (const auto&amp; block : queueCopy)
+        block();
+}
+
+static void RemoteInspectorQueueTask(void (^task)())
+{
+    ASSERT(rwiQueueMutex);
+    ASSERT(rwiRunLoopSource);
+    ASSERT(rwiQueue);
+
+    {
+        std::lock_guard&lt;std::mutex&gt; lock(*rwiQueueMutex);
+        rwiQueue-&gt;append(RemoteInspectorBlock(task));
+    }
+
+    CFRunLoopSourceSignal(rwiRunLoopSource);
+    CFRunLoopWakeUp(CFRunLoopGetMain());
+}
+
+static void RemoteInspectorInitializeQueue()
+{
+    static dispatch_once_t pred;
+    dispatch_once(&amp;pred, ^{
+        rwiQueue = new RemoteInspectorQueue;
+        rwiQueueMutex = std::make_unique&lt;std::mutex&gt;().release();
+
+        CFRunLoopSourceContext runLoopSourceContext = {0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, RemoteInspectorHandleRunSource};
+        rwiRunLoopSource = CFRunLoopSourceCreate(nullptr, 1, &amp;runLoopSourceContext);
+        // Add to the default run loop mode for default handling, and the JSContext remote inspector run loop mode when paused.
+        CFRunLoopAddSource(CFRunLoopGetMain(), rwiRunLoopSource, kCFRunLoopDefaultMode);
+        CFRunLoopAddSource(CFRunLoopGetMain(), rwiRunLoopSource, EventLoop::remoteInspectorRunLoopMode());
+    });
+}
+
+RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection(RemoteInspectorDebuggable* debuggable, NSString *connectionIdentifier, NSString *destination, RemoteInspectorDebuggable::DebuggableType)
</ins><span class="cx">     : m_debuggable(debuggable)
</span><del>-    , m_queueForDebuggable(NULL)
</del><span class="cx">     , m_connectionIdentifier(connectionIdentifier)
</span><span class="cx">     , m_destination(destination)
</span><span class="cx">     , m_identifier(debuggable-&gt;identifier())
</span><span class="cx">     , m_connected(false)
</span><span class="cx"> {
</span><del>-    // Web debuggables must be accessed on the main queue (or the WebThread on iOS). Signal that with a NULL m_queueForDebuggable.
-    // However, JavaScript debuggables can be accessed from any thread/queue, so we create a queue for each JavaScript debuggable.
-    if (type == RemoteInspectorDebuggable::JavaScript)
-        m_queueForDebuggable = dispatch_queue_create(&quot;com.apple.JavaScriptCore.remote-inspector-debuggable-connection&quot;, DISPATCH_QUEUE_SERIAL);
</del><ins>+    RemoteInspectorInitializeQueue();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> RemoteInspectorDebuggableConnection::~RemoteInspectorDebuggableConnection()
</span><span class="cx"> {
</span><del>-    if (m_queueForDebuggable) {
-        dispatch_release(m_queueForDebuggable);
-        m_queueForDebuggable = NULL;
-    }
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> NSString *RemoteInspectorDebuggableConnection::destination() const
</span><span class="lines">@@ -70,14 +151,14 @@
</span><span class="cx"> 
</span><span class="cx"> void RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable(void (^block)())
</span><span class="cx"> {
</span><del>-    if (m_queueForDebuggable)
-        dispatch_async(m_queueForDebuggable, block);
</del><span class="cx"> #if PLATFORM(IOS)
</span><del>-    else if (WebCoreWebThreadIsEnabled &amp;&amp; WebCoreWebThreadIsEnabled())
</del><ins>+    if (WebCoreWebThreadIsEnabled &amp;&amp; WebCoreWebThreadIsEnabled()) {
</ins><span class="cx">         WebCoreWebThreadRun(block);
</span><ins>+        return;
+    }
</ins><span class="cx"> #endif
</span><del>-    else
-        dispatch_async(dispatch_get_main_queue(), block);
</del><ins>+
+    RemoteInspectorQueueTask(block);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool RemoteInspectorDebuggableConnection::setup()
</span><span class="lines">@@ -135,10 +216,15 @@
</span><span class="cx">     ref();
</span><span class="cx">     dispatchAsyncOnDebuggable(^{
</span><span class="cx">         {
</span><del>-            std::lock_guard&lt;std::mutex&gt; lock(m_debuggableMutex);
</del><ins>+            RemoteInspectorDebuggable* debuggable = nullptr;
+            {
+                std::lock_guard&lt;std::mutex&gt; lock(m_debuggableMutex);
+                if (!m_debuggable)
+                    return;
+                debuggable = m_debuggable;
+            }
</ins><span class="cx"> 
</span><del>-            if (m_debuggable)
-                m_debuggable-&gt;dispatchMessageFromRemoteFrontend(message);
</del><ins>+            debuggable-&gt;dispatchMessageFromRemoteFrontend(message);
</ins><span class="cx">         }
</span><span class="cx">         deref();
</span><span class="cx">     });
</span></span></pre>
</div>
</div>

</body>
</html>