Skip to content

Truly independent web browser

LibIPC: Let a wait for a specific message give up at a deadline

Aliaksandr Kalenikbdc63282612c
diff --git a/Libraries/LibIPC/Connection.cpp b/Libraries/LibIPC/Connection.cpp
index 7b1d62807ac..f958f14c44f 100644
--- a/Libraries/LibIPC/Connection.cpp
+++ b/Libraries/LibIPC/Connection.cpp
@@ -189,9 +189,9 @@ void ConnectionBase::handle_messages()
}
}
-void ConnectionBase::wait_for_transport_to_become_readable()
+void ConnectionBase::wait_for_transport_to_become_readable(Optional<MonotonicTime> deadline)
{
- m_transport->wait_until_readable();
+ m_transport->wait_until_readable(deadline);
}
ConnectionBase::PeerEOF ConnectionBase::drain_messages_from_peer()
@@ -243,7 +243,7 @@ ConnectionBase::PeerEOF ConnectionBase::drain_messages_from_peer()
return PeerEOF::No;
}
-OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, MayCancel may_cancel)
+OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, MayCancel may_cancel, Optional<MonotonicTime> deadline)
{
VERIFY(m_owner_thread_id.is_current_thread());
TemporaryChange waiting_for_sync_response { m_is_waiting_for_sync_response, true };
@@ -284,8 +284,10 @@ OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32
if (!is_open())
break;
+ if (deadline.has_value() && MonotonicTime::now() >= *deadline)
+ return {};
- wait_for_transport_to_become_readable();
+ wait_for_transport_to_become_readable(deadline);
if (drain_messages_from_peer() == PeerEOF::Yes) {
peer_disconnected_during_wait = true;
break;
diff --git a/Libraries/LibIPC/Connection.h b/Libraries/LibIPC/Connection.h
index 6a3b46d5ee5..17db5cf164f 100644
--- a/Libraries/LibIPC/Connection.h
+++ b/Libraries/LibIPC/Connection.h
@@ -15,6 +15,7 @@
#include <AK/NonnullRefPtr.h>
#include <AK/Queue.h>
#include <AK/ThreadID.h>
+#include <AK/Time.h>
#include <LibCore/EventReceiver.h>
#include <LibIPC/Attachment.h>
#include <LibIPC/Forward.h>
@@ -112,6 +113,16 @@ public:
return wait_for_specific_endpoint_message<typename RequestType::ResponseType>(MayCancel::Yes);
}
+ // Waits for a message from the peer and handles none of the others meanwhile, which stay queued in order. None if
+ // the deadline passes first, or the peer goes away.
+ template<typename MessageType>
+ OwnPtr<MessageType> wait_for_specific_endpoint_message_until(MonotonicTime deadline)
+ {
+ if (auto message = wait_for_specific_endpoint_message_impl(MessageType::ENDPOINT_MAGIC, MessageType::static_message_id(), MayCancel::No, deadline))
+ return message.template release_nonnull<MessageType>();
+ return {};
+ }
+
protected:
enum class MayCancel {
No,
@@ -132,8 +143,8 @@ protected:
virtual OwnPtr<Message> try_parse_message(ReceivedMessageBytes const&, Queue<Attachment>&) = 0;
ErrorOr<void> post_sync_request(Message const&);
- OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, MayCancel = MayCancel::No);
- void wait_for_transport_to_become_readable();
+ OwnPtr<IPC::Message> wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id, MayCancel = MayCancel::No, Optional<MonotonicTime> deadline = {});
+ void wait_for_transport_to_become_readable(Optional<MonotonicTime> deadline = {});
enum class PeerEOF {
No,
Yes
diff --git a/Libraries/LibIPC/TransportMachPort.cpp b/Libraries/LibIPC/TransportMachPort.cpp
index b48d128f67b..5574885a0a3 100644
--- a/Libraries/LibIPC/TransportMachPort.cpp
+++ b/Libraries/LibIPC/TransportMachPort.cpp
@@ -680,11 +680,19 @@ void TransportMachPort::release_send_right_to_peer()
m_send_port = {};
}
-void TransportMachPort::wait_until_readable()
+void TransportMachPort::wait_until_readable(Optional<MonotonicTime> deadline)
{
MutexLocker lock(m_incoming_mutex);
- while (m_incoming_messages.is_empty() && !m_peer_eof && !m_wait_until_readable_interrupted)
- m_incoming_cv.wait();
+ while (m_incoming_messages.is_empty() && !m_peer_eof && !m_wait_until_readable_interrupted) {
+ if (!deadline.has_value()) {
+ m_incoming_cv.wait();
+ continue;
+ }
+ auto now = MonotonicTime::now();
+ if (now >= *deadline)
+ break;
+ (void)m_incoming_cv.wait_for(*deadline - now);
+ }
m_wait_until_readable_interrupted = false;
}
diff --git a/Libraries/LibIPC/TransportMachPort.h b/Libraries/LibIPC/TransportMachPort.h
index db76aa75876..25dccc9abbb 100644
--- a/Libraries/LibIPC/TransportMachPort.h
+++ b/Libraries/LibIPC/TransportMachPort.h
@@ -15,8 +15,10 @@
#include <AK/Atomic.h>
#include <AK/ConditionVariable.h>
#include <AK/Mutex.h>
+#include <AK/Optional.h>
#include <AK/Queue.h>
#include <AK/RefPtr.h>
+#include <AK/Time.h>
#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <LibCore/MachPort.h>
@@ -60,7 +62,8 @@ public:
void close();
void close_after_sending_all_pending_messages();
- void wait_until_readable();
+ // Returns at the deadline as well, if one is given.
+ void wait_until_readable(Optional<MonotonicTime> deadline = {});
// Makes wait_until_readable() return, even with nothing to read, now or if none is under way, the next time. Any
// thread may call this.
diff --git a/Libraries/LibIPC/TransportSocket.cpp b/Libraries/LibIPC/TransportSocket.cpp
index ea492930ea2..c5f5221fc3c 100644
--- a/Libraries/LibIPC/TransportSocket.cpp
+++ b/Libraries/LibIPC/TransportSocket.cpp
@@ -601,11 +601,18 @@ void TransportSocket::wait_until_incoming_is_current()
m_incoming_cv.wait();
}
-void TransportSocket::wait_until_readable()
+void TransportSocket::wait_until_readable(Optional<MonotonicTime> deadline)
{
MutexLocker lock(m_incoming_mutex);
while (m_incoming_messages.is_empty() && m_io_thread_state == IOThreadState::Running && !m_wait_until_readable_interrupted) {
- m_incoming_cv.wait();
+ if (!deadline.has_value()) {
+ m_incoming_cv.wait();
+ continue;
+ }
+ auto now = MonotonicTime::now();
+ if (now >= *deadline)
+ break;
+ (void)m_incoming_cv.wait_for(*deadline - now);
}
m_wait_until_readable_interrupted = false;
}
diff --git a/Libraries/LibIPC/TransportSocket.h b/Libraries/LibIPC/TransportSocket.h
index 879d0f61ead..f1d61fccf01 100644
--- a/Libraries/LibIPC/TransportSocket.h
+++ b/Libraries/LibIPC/TransportSocket.h
@@ -10,10 +10,12 @@
#include <AK/Atomic.h>
#include <AK/ConditionVariable.h>
#include <AK/Mutex.h>
+#include <AK/Optional.h>
#include <AK/Platform.h>
#include <AK/Queue.h>
#include <AK/SinglyLinkedList.h>
#include <AK/SinglyLinkedListSizePolicy.h>
+#include <AK/Time.h>
#include <LibCore/Socket.h>
#include <LibIPC/Attachment.h>
#include <LibIPC/AutoCloseFileDescriptor.h>
@@ -117,7 +119,8 @@ public:
void close();
void close_after_sending_all_pending_messages();
- void wait_until_readable();
+ // Returns at the deadline as well, if one is given.
+ void wait_until_readable(Optional<MonotonicTime> deadline = {});
// Makes wait_until_readable() return, even with nothing to read, now or if none is under way, the next time. Any
// thread may call this.
diff --git a/Libraries/LibIPC/TransportWindows.cpp b/Libraries/LibIPC/TransportWindows.cpp
index 40429e2eb35..8e7c2b68117 100644
--- a/Libraries/LibIPC/TransportWindows.cpp
+++ b/Libraries/LibIPC/TransportWindows.cpp
@@ -283,7 +283,18 @@ void TransportWindows::close_after_sending_all_pending_messages()
}
// Returns false if the event was signaled first.
-bool TransportWindows::wait_until_readable_or_signaled(HANDLE event)
+static DWORD milliseconds_until(Optional<MonotonicTime> deadline)
+{
+ if (!deadline.has_value())
+ return INFINITE;
+ auto remaining = *deadline - MonotonicTime::now();
+ if (remaining <= AK::Duration::zero())
+ return 0;
+ // NB: Rounded up, so that a wait that times out has reached the deadline.
+ return static_cast<DWORD>(min<i64>(remaining.to_milliseconds() + 1, INFINITE - 1));
+}
+
+bool TransportWindows::wait_until_readable_or_signaled(HANDLE event, Optional<MonotonicTime> deadline)
{
if (m_kind == Kind::Socket) {
for (;;) {
@@ -292,12 +303,14 @@ bool TransportWindows::wait_until_readable_or_signaled(HANDLE event)
.events = POLLRDNORM,
.revents = 0,
};
- auto result = WSAPoll(&poll_fd, 1, SOCKET_WAIT_SLICE_MS);
+ auto result = WSAPoll(&poll_fd, 1, static_cast<INT>(min<DWORD>(SOCKET_WAIT_SLICE_MS, milliseconds_until(deadline))));
VERIFY(result != SOCKET_ERROR);
if (result > 0)
return true;
if (WaitForSingleObject(event, 0) == WAIT_OBJECT_0)
return false;
+ if (deadline.has_value() && MonotonicTime::now() >= *deadline)
+ return false;
}
}
@@ -309,7 +322,7 @@ bool TransportWindows::wait_until_readable_or_signaled(HANDLE event)
if (GetLastError() != ERROR_IO_PENDING)
return true;
HANDLE events[] = { operation.event(), event };
- if (WaitForMultipleObjects(2, events, FALSE, INFINITE) == WAIT_OBJECT_0)
+ if (WaitForMultipleObjects(2, events, FALSE, milliseconds_until(deadline)) == WAIT_OBJECT_0)
return true;
// The read must have stopped writing to the OVERLAPPED before it goes away.
@@ -319,13 +332,13 @@ bool TransportWindows::wait_until_readable_or_signaled(HANDLE event)
return false;
}
-void TransportWindows::wait_until_readable()
+void TransportWindows::wait_until_readable(Optional<MonotonicTime> deadline)
{
if (m_io_thread) {
HANDLE events[] = { m_incoming_event, m_interrupt_event };
- WaitForMultipleObjects(2, events, FALSE, INFINITE);
+ WaitForMultipleObjects(2, events, FALSE, milliseconds_until(deadline));
} else {
- (void)wait_until_readable_or_signaled(m_interrupt_event);
+ (void)wait_until_readable_or_signaled(m_interrupt_event, deadline);
}
ResetEvent(m_interrupt_event);
}
diff --git a/Libraries/LibIPC/TransportWindows.h b/Libraries/LibIPC/TransportWindows.h
index 05c847b5ec9..05049819c85 100644
--- a/Libraries/LibIPC/TransportWindows.h
+++ b/Libraries/LibIPC/TransportWindows.h
@@ -13,8 +13,10 @@
#include <AK/Function.h>
#include <AK/Mutex.h>
#include <AK/NonnullOwnPtr.h>
+#include <AK/Optional.h>
#include <AK/Queue.h>
#include <AK/RefPtr.h>
+#include <AK/Time.h>
#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <LibCore/Forward.h>
@@ -62,7 +64,8 @@ public:
// Fails a send that the peer has no room for, rather than wait for the peer to read.
void set_disconnects_when_peer_stops_reading(bool value) { m_disconnects_when_peer_stops_reading = value; }
- void wait_until_readable();
+ // Returns at the deadline as well, if one is given.
+ void wait_until_readable(Optional<MonotonicTime> deadline = {});
void interrupt_wait_until_readable();
@@ -107,7 +110,7 @@ private:
ErrorOr<void> transfer(ReadonlyBytes);
ErrorOr<size_t> write_some(ReadonlyBytes);
ReadOutcome read_some_without_waiting(Bytes, size_t& bytes_read);
- bool wait_until_readable_or_signaled(void* event);
+ bool wait_until_readable_or_signaled(void* event, Optional<MonotonicTime> deadline = {});
ShouldShutdown read_and_parse_messages(Function<void(NonnullOwnPtr<Message>)>&&);
void stop_watching();