diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-05-13 02:50:01 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-14 11:32:50 +0200 |
commit | 4728f2af8064e76f499d2ae5a957afff6330eb30 (patch) | |
tree | 88c7f4bc07221031fdefb0657028960937b1963a /Kernel/Net | |
parent | 879eec6aa8e5abed7bddcf293ff9e584f8ae2c86 (diff) | |
download | serenity-4728f2af8064e76f499d2ae5a957afff6330eb30.zip |
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.
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 11 |
1 files 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> 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() |