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.
This commit is contained in:
Brian Gianforcaro 2021-05-13 02:50:01 -07:00 committed by Andreas Kling
parent 879eec6aa8
commit 4728f2af80

View file

@ -88,9 +88,11 @@ RefPtr<TCPSocket> 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> 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()