<!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>[169825] trunk/Source/WebKit2</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/169825">169825</a></dd>
<dt>Author</dt> <dd>timothy_horton@apple.com</dd>
<dt>Date</dt> <dd>2014-06-11 13:37:59 -0700 (Wed, 11 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make it possible for waitForAndDispatchImmediately to bail if a sync message comes in from the other direction
https://bugs.webkit.org/show_bug.cgi?id=133708

Reviewed by Anders Carlsson.

* Platform/IPC/Connection.cpp:
(IPC::WaitForMessageState):
(IPC::Connection::Connection):
(IPC::Connection::waitForMessage):
(IPC::Connection::processIncomingMessage):
(IPC::Connection::connectionDidClose):
* Platform/IPC/Connection.h:
(IPC::Connection::waitForAndDispatchImmediately):
Remove the waitForMessageMap, and assert that we're only ever waiting for one message at a time.
This simplifies this code a bit, and we never wait on more than one message at a time, so it was unnecessary.

Add a flag to waitForAndDispatchImmediately, InterruptWaitingIfSyncMessageArrives, which will cause
waitForAndDispatchImmediately to bail if a sync message arrives, to avoid pointlessly blocking both processes
for the entire timeout.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2PlatformIPCConnectioncpp">trunk/Source/WebKit2/Platform/IPC/Connection.cpp</a></li>
<li><a href="#trunkSourceWebKit2PlatformIPCConnectionh">trunk/Source/WebKit2/Platform/IPC/Connection.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (169824 => 169825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2014-06-11 20:21:34 UTC (rev 169824)
+++ trunk/Source/WebKit2/ChangeLog        2014-06-11 20:37:59 UTC (rev 169825)
</span><span class="lines">@@ -1,3 +1,25 @@
</span><ins>+2014-06-11  Timothy Horton  &lt;timothy_horton@apple.com&gt;
+
+        Make it possible for waitForAndDispatchImmediately to bail if a sync message comes in from the other direction
+        https://bugs.webkit.org/show_bug.cgi?id=133708
+
+        Reviewed by Anders Carlsson.
+
+        * Platform/IPC/Connection.cpp:
+        (IPC::WaitForMessageState):
+        (IPC::Connection::Connection):
+        (IPC::Connection::waitForMessage):
+        (IPC::Connection::processIncomingMessage):
+        (IPC::Connection::connectionDidClose):
+        * Platform/IPC/Connection.h:
+        (IPC::Connection::waitForAndDispatchImmediately):
+        Remove the waitForMessageMap, and assert that we're only ever waiting for one message at a time.
+        This simplifies this code a bit, and we never wait on more than one message at a time, so it was unnecessary.
+
+        Add a flag to waitForAndDispatchImmediately, InterruptWaitingIfSyncMessageArrives, which will cause
+        waitForAndDispatchImmediately to bail if a sync message arrives, to avoid pointlessly blocking both processes
+        for the entire timeout.
+
</ins><span class="cx"> 2014-06-11  Oliver Hunt  &lt;oliver@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Restrict database process profile
</span></span></pre></div>
<a id="trunkSourceWebKit2PlatformIPCConnectioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Platform/IPC/Connection.cpp (169824 => 169825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Platform/IPC/Connection.cpp        2014-06-11 20:21:34 UTC (rev 169824)
+++ trunk/Source/WebKit2/Platform/IPC/Connection.cpp        2014-06-11 20:37:59 UTC (rev 169825)
</span><span class="lines">@@ -36,6 +36,25 @@
</span><span class="cx"> 
</span><span class="cx"> namespace IPC {
</span><span class="cx"> 
</span><ins>+struct WaitForMessageState {
+    WaitForMessageState(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, unsigned waitForMessageFlags)
+        : messageReceiverName(messageReceiverName)
+        , messageName(messageName)
+        , destinationID(destinationID)
+        , waitForMessageFlags(waitForMessageFlags)
+    {
+    }
+
+    StringReference messageReceiverName;
+    StringReference messageName;
+    uint64_t destinationID;
+
+    unsigned waitForMessageFlags;
+    bool messageWaitingInterrupted = false;
+
+    std::unique_ptr&lt;MessageDecoder&gt; decoder;
+};
+
</ins><span class="cx"> class Connection::SyncMessageState : public ThreadSafeRefCounted&lt;Connection::SyncMessageState&gt; {
</span><span class="cx"> public:
</span><span class="cx">     static PassRefPtr&lt;SyncMessageState&gt; getOrCreate(RunLoop&amp;);
</span><span class="lines">@@ -225,7 +244,7 @@
</span><span class="cx">     , m_inDispatchMessageCount(0)
</span><span class="cx">     , m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount(0)
</span><span class="cx">     , m_didReceiveInvalidMessage(false)
</span><del>-    , m_messageWaitingInterruptedByClosedConnection(false)
</del><ins>+    , m_waitingForMessage(nullptr)
</ins><span class="cx">     , m_syncMessageState(SyncMessageState::getOrCreate(clientRunLoop))
</span><span class="cx">     , m_shouldWaitForSyncReplies(true)
</span><span class="cx"> {
</span><span class="lines">@@ -371,8 +390,10 @@
</span><span class="cx">     return sendMessage(std::move(encoder));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-std::unique_ptr&lt;MessageDecoder&gt; Connection::waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout)
</del><ins>+std::unique_ptr&lt;MessageDecoder&gt; Connection::waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags)
</ins><span class="cx"> {
</span><ins>+    ASSERT(&amp;m_clientRunLoop == &amp;RunLoop::current());
+
</ins><span class="cx">     // First, check if this message is already in the incoming messages queue.
</span><span class="cx">     {
</span><span class="cx">         MutexLocker locker(m_incomingMessagesLock);
</span><span class="lines">@@ -389,45 +410,36 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    std::pair&lt;std::pair&lt;StringReference, StringReference&gt;, uint64_t&gt; messageAndDestination(std::make_pair(std::make_pair(messageReceiverName, messageName), destinationID));
-    
</del><ins>+    WaitForMessageState waitingForMessage(messageReceiverName, messageName, destinationID, waitForMessageFlags);
+
</ins><span class="cx">     {
</span><span class="cx">         std::lock_guard&lt;std::mutex&gt; lock(m_waitForMessageMutex);
</span><span class="cx"> 
</span><del>-        // We don't support having multiple clients wait for the same message.
-        ASSERT(!m_waitForMessageMap.contains(messageAndDestination));
-    
-        // Insert our pending wait.
-        m_waitForMessageMap.set(messageAndDestination, nullptr);
</del><ins>+        // We don't support having multiple clients waiting for messages.
+        ASSERT(!m_waitingForMessage);
+
+        m_waitingForMessage = &amp;waitingForMessage;
</ins><span class="cx">     }
</span><span class="cx">     
</span><span class="cx">     // Now wait for it to be set.
</span><span class="cx">     while (true) {
</span><span class="cx">         std::unique_lock&lt;std::mutex&gt; lock(m_waitForMessageMutex);
</span><span class="cx"> 
</span><del>-        auto it = m_waitForMessageMap.find(messageAndDestination);
-        if (it-&gt;value) {
-            std::unique_ptr&lt;MessageDecoder&gt; decoder = std::move(it-&gt;value);
-            m_waitForMessageMap.remove(it);
-
</del><ins>+        if (m_waitingForMessage-&gt;decoder) {
+            auto decoder = std::move(m_waitingForMessage-&gt;decoder);
+            m_waitingForMessage = nullptr;
</ins><span class="cx">             return decoder;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         // Now we wait.
</span><span class="cx">         std::cv_status status = m_waitForMessageCondition.wait_for(lock, timeout);
</span><del>-        if (status == std::cv_status::timeout || m_messageWaitingInterruptedByClosedConnection) {
-            // We timed out or lost our connection, now remove the pending wait.
-            m_waitForMessageMap.remove(messageAndDestination);
-
-            // If there are no more waiters, reset m_messageWaitingInterruptedByClosedConnection while we already hold
-            // the necessary lock in case the connection is later reopened.
-            if (m_waitForMessageMap.isEmpty())
-                m_messageWaitingInterruptedByClosedConnection = false;
-
</del><ins>+        // We timed out, lost our connection, or a sync message came in with InterruptWaitingIfSyncMessageArrives, so stop waiting.
+        if (status == std::cv_status::timeout || m_waitingForMessage-&gt;messageWaitingInterrupted)
</ins><span class="cx">             break;
</span><del>-        }
</del><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    m_waitingForMessage = nullptr;
+
</ins><span class="cx">     return nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -647,14 +659,17 @@
</span><span class="cx">     {
</span><span class="cx">         std::lock_guard&lt;std::mutex&gt; lock(m_waitForMessageMutex);
</span><span class="cx"> 
</span><del>-        auto it = m_waitForMessageMap.find(std::make_pair(std::make_pair(message-&gt;messageReceiverName(), message-&gt;messageName()), message-&gt;destinationID()));
-        if (it != m_waitForMessageMap.end()) {
-            it-&gt;value = std::move(message);
-            ASSERT(it-&gt;value);
-        
</del><ins>+        if (m_waitingForMessage &amp;&amp; m_waitingForMessage-&gt;messageReceiverName == message-&gt;messageReceiverName() &amp;&amp; m_waitingForMessage-&gt;messageName == message-&gt;messageName() &amp;&amp; m_waitingForMessage-&gt;destinationID == message-&gt;destinationID()) {
+            m_waitingForMessage-&gt;decoder = std::move(message);
+            ASSERT(m_waitingForMessage-&gt;decoder);
</ins><span class="cx">             m_waitForMessageCondition.notify_one();
</span><span class="cx">             return;
</span><span class="cx">         }
</span><ins>+
+        if (m_waitingForMessage &amp;&amp; (m_waitingForMessage-&gt;waitForMessageFlags &amp; InterruptWaitingIfSyncMessageArrives) &amp;&amp; message-&gt;isSyncMessage()) {
+            m_waitingForMessage-&gt;messageWaitingInterrupted = true;
+            m_waitForMessageCondition.notify_one();
+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     enqueueIncomingMessage(std::move(message));
</span><span class="lines">@@ -685,7 +700,8 @@
</span><span class="cx"> 
</span><span class="cx">     {
</span><span class="cx">         std::lock_guard&lt;std::mutex&gt; lock(m_waitForMessageMutex);
</span><del>-        m_messageWaitingInterruptedByClosedConnection = true;
</del><ins>+        if (m_waitingForMessage)
+            m_waitingForMessage-&gt;messageWaitingInterrupted = true;
</ins><span class="cx">     }
</span><span class="cx">     m_waitForMessageCondition.notify_all();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2PlatformIPCConnectionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Platform/IPC/Connection.h (169824 => 169825)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Platform/IPC/Connection.h        2014-06-11 20:21:34 UTC (rev 169824)
+++ trunk/Source/WebKit2/Platform/IPC/Connection.h        2014-06-11 20:37:59 UTC (rev 169825)
</span><span class="lines">@@ -55,6 +55,8 @@
</span><span class="cx"> 
</span><span class="cx"> namespace IPC {
</span><span class="cx"> 
</span><ins>+struct WaitForMessageState;
+
</ins><span class="cx"> enum MessageSendFlags {
</span><span class="cx">     // Whether this message should be dispatched when waiting for a sync reply.
</span><span class="cx">     // This is the default for synchronous messages.
</span><span class="lines">@@ -68,6 +70,11 @@
</span><span class="cx">     // FIXME (126021): Remove when no platforms need to support this.
</span><span class="cx">     SpinRunLoopWhileWaitingForReply = 1 &lt;&lt; 1,
</span><span class="cx"> };
</span><ins>+
+enum WaitForMessageFlags {
+    // Use this to make waitForMessage be interrupted immediately by any incoming sync messages.
+    InterruptWaitingIfSyncMessageArrives = 1 &lt;&lt; 0,
+};
</ins><span class="cx">     
</span><span class="cx"> #define MESSAGE_CHECK_BASE(assertion, connection) do \
</span><span class="cx">     if (!(assertion)) { \
</span><span class="lines">@@ -164,7 +171,7 @@
</span><span class="cx"> 
</span><span class="cx">     template&lt;typename T&gt; bool send(T&amp;&amp; message, uint64_t destinationID, unsigned messageSendFlags = 0);
</span><span class="cx">     template&lt;typename T&gt; bool sendSync(T&amp;&amp; message, typename T::Reply&amp;&amp; reply, uint64_t destinationID, std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), unsigned syncSendFlags = 0);
</span><del>-    template&lt;typename T&gt; bool waitForAndDispatchImmediately(uint64_t destinationID, std::chrono::milliseconds timeout);
</del><ins>+    template&lt;typename T&gt; bool waitForAndDispatchImmediately(uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags = 0);
</ins><span class="cx"> 
</span><span class="cx">     std::unique_ptr&lt;MessageEncoder&gt; createSyncMessageEncoder(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, uint64_t&amp; syncRequestID);
</span><span class="cx">     bool sendMessage(std::unique_ptr&lt;MessageEncoder&gt;, unsigned messageSendFlags = 0);
</span><span class="lines">@@ -193,7 +200,7 @@
</span><span class="cx">     
</span><span class="cx">     bool isValid() const { return m_client; }
</span><span class="cx">     
</span><del>-    std::unique_ptr&lt;MessageDecoder&gt; waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout);
</del><ins>+    std::unique_ptr&lt;MessageDecoder&gt; waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags);
</ins><span class="cx">     
</span><span class="cx">     std::unique_ptr&lt;MessageDecoder&gt; waitForSyncReply(uint64_t syncRequestID, std::chrono::milliseconds timeout, unsigned syncSendFlags);
</span><span class="cx"> 
</span><span class="lines">@@ -253,9 +260,9 @@
</span><span class="cx">     
</span><span class="cx">     std::condition_variable m_waitForMessageCondition;
</span><span class="cx">     std::mutex m_waitForMessageMutex;
</span><del>-    bool m_messageWaitingInterruptedByClosedConnection;
-    HashMap&lt;std::pair&lt;std::pair&lt;StringReference, StringReference&gt;, uint64_t&gt;, std::unique_ptr&lt;MessageDecoder&gt;&gt; m_waitForMessageMap;
</del><span class="cx"> 
</span><ins>+    WaitForMessageState* m_waitingForMessage;
+
</ins><span class="cx">     // Represents a sync request for which we're waiting on a reply.
</span><span class="cx">     struct PendingSyncReply {
</span><span class="cx">         // The request ID.
</span><span class="lines">@@ -357,9 +364,9 @@
</span><span class="cx">     return replyDecoder-&gt;decode(reply);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-template&lt;typename T&gt; bool Connection::waitForAndDispatchImmediately(uint64_t destinationID, std::chrono::milliseconds timeout)
</del><ins>+template&lt;typename T&gt; bool Connection::waitForAndDispatchImmediately(uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags)
</ins><span class="cx"> {
</span><del>-    std::unique_ptr&lt;MessageDecoder&gt; decoder = waitForMessage(T::receiverName(), T::name(), destinationID, timeout);
</del><ins>+    std::unique_ptr&lt;MessageDecoder&gt; decoder = waitForMessage(T::receiverName(), T::name(), destinationID, timeout, waitForMessageFlags);
</ins><span class="cx">     if (!decoder)
</span><span class="cx">         return false;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>