From 4728f2af8064e76f499d2ae5a957afff6330eb30 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Thu, 13 May 2021 02:50:01 -0700 Subject: [PATCH] Kernel: Avoid unnecessary time under lock in TCPSocket::create Avoid holding the sockets_by_tuple lock while allocating the TCPSocket. While checking if the list contains the item we can also hold the lock in shared mode, as we are only reading the hash table. In addition the call to from_tuple appears to be superfluous, as we created the socket, so we should be able to just return it directly. This avoids the recursive lock acquisition, as well as the unnecessary hash table lookups. --- Kernel/Net/TCPSocket.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 9434d82139..00c89f6a32 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -88,9 +88,11 @@ RefPtr TCPSocket::create_client(const IPv4Address& new_local_address, { auto tuple = IPv4SocketTuple(new_local_address, new_local_port, new_peer_address, new_peer_port); - Locker locker(sockets_by_tuple().lock()); - if (sockets_by_tuple().resource().contains(tuple)) - return {}; + { + Locker locker(sockets_by_tuple().lock(), Lock::Mode::Shared); + if (sockets_by_tuple().resource().contains(tuple)) + return {}; + } auto result = TCPSocket::create(protocol()); if (result.is_error()) @@ -105,10 +107,11 @@ RefPtr TCPSocket::create_client(const IPv4Address& new_local_address, client->set_direction(Direction::Incoming); client->set_originator(*this); + Locker locker(sockets_by_tuple().lock()); m_pending_release_for_accept.set(tuple, client); sockets_by_tuple().resource().set(tuple, client); - return from_tuple(tuple); + return client; } void TCPSocket::release_to_originator()