Title: [273646] trunk/Source/WebKit
Revision
273646
Author
[email protected]
Date
2021-03-01 05:27:34 -0800 (Mon, 01 Mar 2021)

Log Message

Timeout calculations are error-prone for compound IPC operations
https://bugs.webkit.org/show_bug.cgi?id=222305

Patch by Kimmo Kinnunen <[email protected]> on 2021-03-01
Reviewed by Geoffrey Garen.

Add Timeout class to hold the timeout value in blocking functions
of IPC::.
This makes it possible to forward absolute points of time as the timeout
value, as well as preserving the ability to pass points of time relative
to the invocation.

Call the concept Timeout instead of TimeOut since word 'timeout' is an
English word and preferable when used as a noun according to ell stackoverflow.

No new tests, a refactor.

* Platform/IPC/Connection.cpp:
(IPC::Connection::sendMessage):
(IPC::Connection::waitForMessage):
(IPC::Connection::sendSyncMessage):
(IPC::Connection::waitForSyncReply):
* Platform/IPC/Connection.h:
(IPC::Connection::sendSync):
(IPC::Connection::waitForAndDispatchImmediately):
(IPC::Connection::waitForAsyncCallbackAndDispatchImmediately):
* Platform/IPC/IPCSemaphore.h:
* Platform/IPC/MessageSender.h:
(IPC::MessageSender::sendSync):
* Platform/IPC/StreamClientConnection.h:
(IPC::StreamClientConnection::send):
(IPC::StreamClientConnection::sendSync):
(IPC::StreamClientConnection::trySendDestinationIDIfNeeded):
(IPC::StreamClientConnection::tryAcquire):
Here are few examples of compound use-case.

* Platform/IPC/Timeout.h: Copied from Source/WebKit/Platform/IPC/IPCSemaphore.h.
(IPC::Timeout::Timeout):
(IPC::Timeout::infinity):
(IPC::Timeout::operator Seconds const):
(IPC::Timeout::hasPassed const):
* Platform/IPC/darwin/IPCSemaphoreDarwin.cpp:
(IPC::Semaphore::waitFor):
* UIProcess/AuxiliaryProcessProxy.h:
(WebKit::AuxiliaryProcessProxy::sendSync):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (273645 => 273646)


--- trunk/Source/WebKit/ChangeLog	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/ChangeLog	2021-03-01 13:27:34 UTC (rev 273646)
@@ -1,3 +1,50 @@
+2021-03-01  Kimmo Kinnunen  <[email protected]>
+
+        Timeout calculations are error-prone for compound IPC operations
+        https://bugs.webkit.org/show_bug.cgi?id=222305
+
+        Reviewed by Geoffrey Garen.
+
+        Add Timeout class to hold the timeout value in blocking functions
+        of IPC::.
+        This makes it possible to forward absolute points of time as the timeout
+        value, as well as preserving the ability to pass points of time relative
+        to the invocation.
+
+        Call the concept Timeout instead of TimeOut since word 'timeout' is an
+        English word and preferable when used as a noun according to ell stackoverflow.
+
+        No new tests, a refactor.
+
+        * Platform/IPC/Connection.cpp:
+        (IPC::Connection::sendMessage):
+        (IPC::Connection::waitForMessage):
+        (IPC::Connection::sendSyncMessage):
+        (IPC::Connection::waitForSyncReply):
+        * Platform/IPC/Connection.h:
+        (IPC::Connection::sendSync):
+        (IPC::Connection::waitForAndDispatchImmediately):
+        (IPC::Connection::waitForAsyncCallbackAndDispatchImmediately):
+        * Platform/IPC/IPCSemaphore.h:
+        * Platform/IPC/MessageSender.h:
+        (IPC::MessageSender::sendSync):
+        * Platform/IPC/StreamClientConnection.h:
+        (IPC::StreamClientConnection::send):
+        (IPC::StreamClientConnection::sendSync):
+        (IPC::StreamClientConnection::trySendDestinationIDIfNeeded):
+        (IPC::StreamClientConnection::tryAcquire):
+        Here are few examples of compound use-case.
+
+        * Platform/IPC/Timeout.h: Copied from Source/WebKit/Platform/IPC/IPCSemaphore.h.
+        (IPC::Timeout::Timeout):
+        (IPC::Timeout::infinity):
+        (IPC::Timeout::operator Seconds const):
+        (IPC::Timeout::hasPassed const):
+        * Platform/IPC/darwin/IPCSemaphoreDarwin.cpp:
+        (IPC::Semaphore::waitFor):
+        * UIProcess/AuxiliaryProcessProxy.h:
+        (WebKit::AuxiliaryProcessProxy::sendSync):
+
 2021-03-01  Alberto Garcia  <[email protected]>
 
         [GTK] Fails to build in i386: static assertion failed: divisor must be a power of two

Modified: trunk/Source/WebKit/Platform/IPC/Connection.cpp (273645 => 273646)


--- trunk/Source/WebKit/Platform/IPC/Connection.cpp	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/Platform/IPC/Connection.cpp	2021-03-01 13:27:34 UTC (rev 273646)
@@ -81,9 +81,9 @@
         m_waitForSyncReplySemaphore.signal();
     }
 
-    bool wait(TimeWithDynamicClockType absoluteTime)
+    bool wait(Timeout timeout)
     {
-        return m_waitForSyncReplySemaphore.waitUntil(absoluteTime);
+        return m_waitForSyncReplySemaphore.waitUntil(timeout.deadline());
     }
 
     // Returns true if this message will be handled on a client thread that is currently
@@ -450,7 +450,7 @@
         auto wrappedMessage = createSyncMessageEncoder(MessageName::WrappedAsyncMessageForTesting, encoder->destinationID(), syncRequestID);
         wrappedMessage->setFullySynchronousModeForTesting();
         wrappedMessage->wrapForTesting(WTFMove(encoder));
-        return static_cast<bool>(sendSyncMessage(syncRequestID, WTFMove(wrappedMessage), Seconds::infinity(), { }));
+        return static_cast<bool>(sendSyncMessage(syncRequestID, WTFMove(wrappedMessage), Timeout::infinity(), { }));
     }
 
     if (sendOptions.contains(SendOption::DispatchMessageEvenWhenWaitingForSyncReply)
@@ -480,12 +480,12 @@
     return sendMessage(WTFMove(encoder), { });
 }
 
-Seconds Connection::timeoutRespectingIgnoreTimeoutsForTesting(Seconds timeout) const
+Timeout Connection::timeoutRespectingIgnoreTimeoutsForTesting(Timeout timeout) const
 {
-    return m_ignoreTimeoutsForTesting ? Seconds::infinity() : timeout;
+    return m_ignoreTimeoutsForTesting ? Timeout::infinity() : timeout;
 }
 
-std::unique_ptr<Decoder> Connection::waitForMessage(MessageName messageName, uint64_t destinationID, Seconds timeout, OptionSet<WaitForOption> waitForOptions)
+std::unique_ptr<Decoder> Connection::waitForMessage(MessageName messageName, uint64_t destinationID, Timeout timeout, OptionSet<WaitForOption> waitForOptions)
 {
     ASSERT(RunLoop::isMain());
     auto protectedThis = makeRef(*this);
@@ -539,8 +539,6 @@
         m_waitingForMessage = &waitingForMessage;
     }
 
-    MonotonicTime absoluteTimeout = MonotonicTime::now() + timeout;
-
     // Now wait for it to be set.
     while (true) {
         // Handle any messages that are blocked on a response from us.
@@ -555,7 +553,7 @@
         }
 
         // Now we wait.
-        bool didTimeout = !m_waitForMessageCondition.waitUntil(lock, absoluteTimeout);
+        bool didTimeout = !m_waitForMessageCondition.waitUntil(lock, timeout.deadline());
         // We timed out, lost our connection, or a sync message came in with InterruptWaitingIfSyncMessageArrives, so stop waiting.
         if (didTimeout || m_waitingForMessage->messageWaitingInterrupted) {
             m_waitingForMessage = nullptr;
@@ -566,7 +564,7 @@
     return nullptr;
 }
 
-std::unique_ptr<Decoder> Connection::sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<Encoder> encoder, Seconds timeout, OptionSet<SendSyncOption> sendSyncOptions)
+std::unique_ptr<Decoder> Connection::sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<Encoder> encoder, Timeout timeout, OptionSet<SendSyncOption> sendSyncOptions)
 {
     ASSERT(RunLoop::isMain());
 
@@ -616,10 +614,9 @@
     return reply;
 }
 
-std::unique_ptr<Decoder> Connection::waitForSyncReply(uint64_t syncRequestID, MessageName messageName, Seconds timeout, OptionSet<SendSyncOption> sendSyncOptions)
+std::unique_ptr<Decoder> Connection::waitForSyncReply(uint64_t syncRequestID, MessageName messageName, Timeout timeout, OptionSet<SendSyncOption> sendSyncOptions)
 {
     timeout = timeoutRespectingIgnoreTimeoutsForTesting(timeout);
-    MonotonicTime absoluteTime = MonotonicTime::now() + timeout;
 
     willSendSyncMessage(sendSyncOptions);
     
@@ -657,7 +654,7 @@
         // We didn't find a sync reply yet, keep waiting.
         // This allows the WebProcess to still serve clients while waiting for the message to return.
         // Notably, it can continue to process accessibility requests, which are on the main thread.
-        timedOut = !SyncMessageState::singleton().wait(absoluteTime);
+        timedOut = !SyncMessageState::singleton().wait(timeout);
     }
 
 #if OS(DARWIN)

Modified: trunk/Source/WebKit/Platform/IPC/Connection.h (273645 => 273646)


--- trunk/Source/WebKit/Platform/IPC/Connection.h	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/Platform/IPC/Connection.h	2021-03-01 13:27:34 UTC (rev 273646)
@@ -32,6 +32,7 @@
 #include "Encoder.h"
 #include "MessageReceiveQueueMap.h"
 #include "MessageReceiver.h"
+#include "Timeout.h"
 #include <wtf/CompletionHandler.h>
 #include <wtf/Condition.h>
 #include <wtf/Deque.h>
@@ -240,9 +241,9 @@
     // Sync senders should check the SendSyncResult for true/false in case they need to know if the result was really received.
     // Sync senders should hold on to the SendSyncResult in case they reference the contents of the reply via DataRefererence / ArrayReference.
     using SendSyncResult = std::unique_ptr<Decoder>;
-    template<typename T> SendSyncResult sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { }); // Main thread only.
-    template<typename> bool waitForAndDispatchImmediately(uint64_t destinationID, Seconds timeout, OptionSet<WaitForOption> waitForOptions = { }); // Main thread only.
-    template<typename> bool waitForAsyncCallbackAndDispatchImmediately(uint64_t callbackID, Seconds timeout); // Main thread only.
+    template<typename T> SendSyncResult sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Timeout = Timeout::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { }); // Main thread only.
+    template<typename> bool waitForAndDispatchImmediately(uint64_t destinationID, Timeout, OptionSet<WaitForOption> waitForOptions = { }); // Main thread only.
+    template<typename> bool waitForAsyncCallbackAndDispatchImmediately(uint64_t callbackID, Timeout); // Main thread only.
 
     // Thread-safe.
     template<typename T, typename C, typename U>
@@ -260,7 +261,7 @@
 
     // Main thread only.
     template<typename T, typename U>
-    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
+    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, Timeout timeout = Timeout::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
     {
         return sendSync<T>(WTFMove(message), WTFMove(reply), destinationID.toUInt64(), timeout, sendSyncOptions);
     }
@@ -267,7 +268,7 @@
     
     // Main thread only.
     template<typename T, typename U>
-    bool waitForAndDispatchImmediately(ObjectIdentifier<U> destinationID, Seconds timeout, OptionSet<WaitForOption> waitForOptions = { })
+    bool waitForAndDispatchImmediately(ObjectIdentifier<U> destinationID, Timeout timeout, OptionSet<WaitForOption> waitForOptions = { })
     {
         return waitForAndDispatchImmediately<T>(destinationID.toUInt64(), timeout, waitForOptions);
     }
@@ -274,7 +275,7 @@
 
     bool sendMessage(std::unique_ptr<Encoder>, OptionSet<SendOption> sendOptions);
     std::unique_ptr<Encoder> createSyncMessageEncoder(MessageName, uint64_t destinationID, uint64_t& syncRequestID);
-    std::unique_ptr<Decoder> sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<Encoder>, Seconds timeout, OptionSet<SendSyncOption> sendSyncOptions);
+    std::unique_ptr<Decoder> sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<Encoder>, Timeout, OptionSet<SendSyncOption> sendSyncOptions);
     bool sendSyncReply(std::unique_ptr<Encoder>);
 
     void wakeUpRunLoop();
@@ -320,9 +321,9 @@
 
     bool isIncomingMessagesThrottlingEnabled() const { return !!m_incomingMessagesThrottler; }
     
-    std::unique_ptr<Decoder> waitForMessage(MessageName, uint64_t destinationID, Seconds timeout, OptionSet<WaitForOption>);
+    std::unique_ptr<Decoder> waitForMessage(MessageName, uint64_t destinationID, Timeout, OptionSet<WaitForOption>);
     
-    std::unique_ptr<Decoder> waitForSyncReply(uint64_t syncRequestID, MessageName, Seconds timeout, OptionSet<SendSyncOption>);
+    std::unique_ptr<Decoder> waitForSyncReply(uint64_t syncRequestID, MessageName, Timeout, OptionSet<SendSyncOption>);
 
     // Called on the connection work queue.
     void processIncomingMessage(std::unique_ptr<Decoder>);
@@ -349,7 +350,7 @@
     void willSendSyncMessage(OptionSet<SendSyncOption>);
     void didReceiveSyncReply(OptionSet<SendSyncOption>);
 
-    Seconds timeoutRespectingIgnoreTimeoutsForTesting(Seconds) const;
+    Timeout timeoutRespectingIgnoreTimeoutsForTesting(Timeout) const;
 
 #if PLATFORM(COCOA)
     bool sendMessage(std::unique_ptr<MachMessage>);
@@ -549,7 +550,7 @@
     TupleMover<sizeof...(A), std::tuple<A...>, std::tuple<B...>>::move(WTFMove(a), b);
 }
 
-template<typename T> Connection::SendSyncResult Connection::sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Seconds timeout, OptionSet<SendSyncOption> sendSyncOptions)
+template<typename T> Connection::SendSyncResult Connection::sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Timeout timeout, OptionSet<SendSyncOption> sendSyncOptions)
 {
     COMPILE_ASSERT(T::isSync, SyncMessageExpected);
     RELEASE_ASSERT(RunLoop::isMain());
@@ -579,7 +580,7 @@
     return replyDecoder;
 }
 
-template<typename T> bool Connection::waitForAndDispatchImmediately(uint64_t destinationID, Seconds timeout, OptionSet<WaitForOption> waitForOptions)
+template<typename T> bool Connection::waitForAndDispatchImmediately(uint64_t destinationID, Timeout timeout, OptionSet<WaitForOption> waitForOptions)
 {
     RELEASE_ASSERT(RunLoop::isMain());
     std::unique_ptr<Decoder> decoder = waitForMessage(T::name(), destinationID, timeout, waitForOptions);
@@ -591,7 +592,7 @@
     return true;
 }
 
-template<typename T> bool Connection::waitForAsyncCallbackAndDispatchImmediately(uint64_t destinationID, Seconds timeout)
+template<typename T> bool Connection::waitForAsyncCallbackAndDispatchImmediately(uint64_t destinationID, Timeout timeout)
 {
     RELEASE_ASSERT(RunLoop::isMain());
     std::unique_ptr<Decoder> decoder = waitForMessage(T::asyncMessageReplyName(), destinationID, timeout, { });

Modified: trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h (273645 => 273646)


--- trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h	2021-03-01 13:27:34 UTC (rev 273646)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include "Timeout.h"
 #include <wtf/Noncopyable.h>
 #include <wtf/Optional.h>
 #include <wtf/Seconds.h>
@@ -56,7 +57,7 @@
 
     void signal();
     void wait();
-    void waitFor(Seconds);
+    void waitFor(Timeout);
     MachSendRight createSendRight() const;
     explicit operator bool() const { return m_sendRight || m_semaphore != SEMAPHORE_NULL; }
 #else

Modified: trunk/Source/WebKit/Platform/IPC/MessageSender.h (273645 => 273646)


--- trunk/Source/WebKit/Platform/IPC/MessageSender.h	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/Platform/IPC/MessageSender.h	2021-03-01 13:27:34 UTC (rev 273646)
@@ -57,7 +57,7 @@
     using SendSyncResult = Connection::SendSyncResult;
 
     template<typename T>
-    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
+    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, Timeout timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
     {
         static_assert(T::isSync, "Message is not sync!");
 
@@ -65,7 +65,7 @@
     }
 
     template<typename T>
-    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
+    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, Timeout timeout = Timeout::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
     {
         if (auto* connection = messageSenderConnection())
             return connection->sendSync(WTFMove(message), WTFMove(reply), destinationID, timeout, sendSyncOptions);
@@ -74,7 +74,7 @@
     }
 
     template<typename U, typename T>
-    SendSyncResult sendSync(U&& message, typename U::Reply&& reply, ObjectIdentifier<T> destinationID, Seconds timeout = Seconds::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
+    SendSyncResult sendSync(U&& message, typename U::Reply&& reply, ObjectIdentifier<T> destinationID, Timeout timeout = Timeout::infinity(), OptionSet<SendSyncOption> sendSyncOptions = { })
     {
         return sendSync<U>(std::forward<U>(message), WTFMove(reply), destinationID.toUInt64(), timeout, sendSyncOptions);
     }

Modified: trunk/Source/WebKit/Platform/IPC/StreamClientConnection.h (273645 => 273646)


--- trunk/Source/WebKit/Platform/IPC/StreamClientConnection.h	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/Platform/IPC/StreamClientConnection.h	2021-03-01 13:27:34 UTC (rev 273646)
@@ -54,11 +54,11 @@
     StreamConnectionBuffer& streamBuffer() { return m_buffer; }
     void setWakeUpSemaphore(IPC::Semaphore&&);
 
-    template<typename T, typename U> bool send(T&& message, ObjectIdentifier<U> destinationID, Seconds timeout);
+    template<typename T, typename U> bool send(T&& message, ObjectIdentifier<U> destinationID, Timeout);
 
     using SendSyncResult = Connection::SendSyncResult;
     template<typename T, typename U>
-    SendSyncResult sendSync(T&& message, typename T::Reply&&, ObjectIdentifier<U> destinationID, Seconds timeout);
+    SendSyncResult sendSync(T&& message, typename T::Reply&&, ObjectIdentifier<U> destinationID, Timeout);
 
 private:
     struct Span {
@@ -67,10 +67,10 @@
     };
     static constexpr size_t minimumMessageSize = StreamConnectionEncoder::minimumMessageSize;
     static constexpr size_t messageAlignment = StreamConnectionEncoder::messageAlignment;
-    bool trySendDestinationIDIfNeeded(uint64_t destinationID, MonotonicTime timeout);
+    bool trySendDestinationIDIfNeeded(uint64_t destinationID, Timeout);
     void sendProcessOutOfStreamMessage(Span&&);
 
-    Optional<Span> tryAcquire(MonotonicTime timeout);
+    Optional<Span> tryAcquire(Timeout);
     enum class WakeUpReceiver : bool {
         No,
         Yes
@@ -100,13 +100,12 @@
 };
 
 template<typename T, typename U>
-bool StreamClientConnection::send(T&& message, ObjectIdentifier<U> destinationID, Seconds timeout)
+bool StreamClientConnection::send(T&& message, ObjectIdentifier<U> destinationID, Timeout timeout)
 {
     static_assert(!T::isSync, "Message is sync!");
-    MonotonicTime absoluteTimeout = MonotonicTime::now() + timeout;
-    if (!trySendDestinationIDIfNeeded(destinationID.toUInt64(), absoluteTimeout))
+    if (!trySendDestinationIDIfNeeded(destinationID.toUInt64(), timeout))
         return false;
-    auto span = tryAcquire(absoluteTimeout);
+    auto span = tryAcquire(timeout);
     if (!span)
         return false;
     {
@@ -125,13 +124,12 @@
 }
 
 template<typename T, typename U>
-StreamClientConnection::SendSyncResult StreamClientConnection::sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, Seconds timeout)
+StreamClientConnection::SendSyncResult StreamClientConnection::sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, Timeout timeout)
 {
     static_assert(T::isSync, "Message is not sync!");
-    MonotonicTime absoluteTimeout = MonotonicTime::now() + timeout;
-    if (!trySendDestinationIDIfNeeded(destinationID.toUInt64(), absoluteTimeout))
+    if (!trySendDestinationIDIfNeeded(destinationID.toUInt64(), timeout))
         return { };
-    auto span = tryAcquire(absoluteTimeout);
+    auto span = tryAcquire(timeout);
     if (!span)
         return { };
     // FIXME: implement send through stream.
@@ -142,11 +140,11 @@
     return result;
 }
 
-inline bool StreamClientConnection::trySendDestinationIDIfNeeded(uint64_t destinationID, MonotonicTime absoluteTimeout)
+inline bool StreamClientConnection::trySendDestinationIDIfNeeded(uint64_t destinationID, Timeout timeout)
 {
     if (destinationID == m_currentDestinationID)
         return true;
-    auto span = tryAcquire(absoluteTimeout);
+    auto span = tryAcquire(timeout);
     if (!span)
         return false;
     StreamConnectionEncoder encoder { MessageName::SetStreamDestinationID, span->data, span->size };
@@ -168,19 +166,18 @@
     auto result = release(encoder.size());
     UNUSED_VARIABLE(result);
 }
-inline Optional<StreamClientConnection::Span> StreamClientConnection::tryAcquire(MonotonicTime timeout)
+inline Optional<StreamClientConnection::Span> StreamClientConnection::tryAcquire(Timeout timeout)
 {
     for (;;) {
         size_t senderLimit = clampedLimit(sharedReceiverOffset().load(std::memory_order_acquire));
         auto result = alignedSpan(m_senderOffset, senderLimit);
         if (result.size < minimumMessageSize) {
-            auto now = MonotonicTime::now();
-            if (now >= timeout)
+            if (timeout.didTimeOut())
                 break;
 #if PLATFORM(COCOA)
             size_t oldSenderLimit = sharedReceiverOffset().compareExchangeStrong(senderLimit, StreamConnectionBuffer::receiverOffsetSenderIsWaitingTag, std::memory_order_acq_rel, std::memory_order_acq_rel);
             if (senderLimit == oldSenderLimit)
-                m_buffer.senderWaitSemaphore().waitFor(timeout - now);
+                m_buffer.senderWaitSemaphore().waitFor(timeout);
             else {
                 senderLimit = clampedLimit(oldSenderLimit);
                 result = alignedSpan(m_senderOffset, senderLimit);

Copied: trunk/Source/WebKit/Platform/IPC/Timeout.h (from rev 273645, trunk/Source/WebKit/Platform/IPC/IPCSemaphore.h) (0 => 273646)


--- trunk/Source/WebKit/Platform/IPC/Timeout.h	                        (rev 0)
+++ trunk/Source/WebKit/Platform/IPC/Timeout.h	2021-03-01 13:27:34 UTC (rev 273646)
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <wtf/MonotonicTime.h>
+#include <wtf/TimeWithDynamicClockType.h>
+
+namespace IPC {
+
+class Timeout {
+public:
+    Timeout(Seconds timeDelta)
+        : m_deadline(MonotonicTime::now() + timeDelta)
+    {
+    }
+    static constexpr Timeout infinity() { return Timeout { }; }
+    static Timeout now() { return 0_s; }
+    Seconds secondsUntilDeadline() const { return std::max(m_deadline - MonotonicTime::now(), 0_s ); }
+    constexpr MonotonicTime deadline() const { return m_deadline; }
+    bool didTimeOut() const { return MonotonicTime::now() >= m_deadline; }
+private:
+    explicit constexpr Timeout()
+        : m_deadline(MonotonicTime::infinity())
+    {
+    }
+    MonotonicTime m_deadline;
+};
+
+}

Modified: trunk/Source/WebKit/Platform/IPC/darwin/IPCSemaphoreDarwin.cpp (273645 => 273646)


--- trunk/Source/WebKit/Platform/IPC/darwin/IPCSemaphoreDarwin.cpp	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/Platform/IPC/darwin/IPCSemaphoreDarwin.cpp	2021-03-01 13:27:34 UTC (rev 273646)
@@ -78,10 +78,11 @@
     ASSERT_UNUSED(ret, ret == KERN_SUCCESS);
 }
 
-void Semaphore::waitFor(Seconds timeout)
+void Semaphore::waitFor(Timeout timeout)
 {
-    auto seconds = timeout.secondsAs<unsigned>();
-    auto ret = semaphore_timedwait(m_semaphore, { seconds, static_cast<clock_res_t>(timeout.nanosecondsAs<uint64_t>() - seconds * NSEC_PER_SEC) });
+    Seconds waitTime = timeout.secondsUntilDeadline();
+    auto seconds = waitTime.secondsAs<unsigned>();
+    auto ret = semaphore_timedwait(m_semaphore, { seconds, static_cast<clock_res_t>(waitTime.nanosecondsAs<uint64_t>() - seconds * NSEC_PER_SEC) });
     ASSERT_UNUSED(ret, ret == KERN_SUCCESS || ret == KERN_OPERATION_TIMED_OUT);
 }
 

Modified: trunk/Source/WebKit/UIProcess/AuxiliaryProcessProxy.h (273645 => 273646)


--- trunk/Source/WebKit/UIProcess/AuxiliaryProcessProxy.h	2021-03-01 10:44:46 UTC (rev 273645)
+++ trunk/Source/WebKit/UIProcess/AuxiliaryProcessProxy.h	2021-03-01 13:27:34 UTC (rev 273646)
@@ -54,7 +54,7 @@
 
     template<typename T> bool send(T&& message, uint64_t destinationID, OptionSet<IPC::SendOption> sendOptions = { });
     using SendSyncResult = IPC::Connection::SendSyncResult;
-    template<typename T> SendSyncResult sendSync(T&& message, typename T::Reply&&, uint64_t destinationID, Seconds timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { });
+    template<typename T> SendSyncResult sendSync(T&& message, typename T::Reply&&, uint64_t destinationID, IPC::Timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { });
 
     enum class ShouldStartProcessThrottlerActivity : bool { No, Yes };
     template<typename T, typename C> uint64_t sendWithAsyncReply(T&&, C&&, uint64_t destinationID = 0, OptionSet<IPC::SendOption> = { }, ShouldStartProcessThrottlerActivity = ShouldStartProcessThrottlerActivity::Yes);
@@ -66,7 +66,7 @@
     }
     
     template<typename T, typename U>
-    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, Seconds timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { })
+    SendSyncResult sendSync(T&& message, typename T::Reply&& reply, ObjectIdentifier<U> destinationID, IPC::Timeout timeout = 1_s, OptionSet<IPC::SendSyncOption> sendSyncOptions = { })
     {
         return sendSync<T>(WTFMove(message), WTFMove(reply), destinationID.toUInt64(), timeout, sendSyncOptions);
     }
@@ -169,7 +169,7 @@
 }
 
 template<typename U> 
-AuxiliaryProcessProxy::SendSyncResult AuxiliaryProcessProxy::sendSync(U&& message, typename U::Reply&& reply, uint64_t destinationID, Seconds timeout, OptionSet<IPC::SendSyncOption> sendSyncOptions)
+AuxiliaryProcessProxy::SendSyncResult AuxiliaryProcessProxy::sendSync(U&& message, typename U::Reply&& reply, uint64_t destinationID, IPC::Timeout timeout, OptionSet<IPC::SendSyncOption> sendSyncOptions)
 {
     COMPILE_ASSERT(U::isSync, SyncMessageExpected);
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to