diff options
author | Andreas Kling <kling@serenityos.org> | 2023-03-10 07:53:02 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-10 13:15:44 +0100 |
commit | 03cc45e5a2ecc5d173aa121404d7ed8251c109f8 (patch) | |
tree | 7737b1bdd4ccbf03012d7ae5e20d99073ed5da1c /Kernel/Net | |
parent | 61bb9103c28ac053a52b00fa5e7235ab315444b5 (diff) | |
download | serenity-03cc45e5a2ecc5d173aa121404d7ed8251c109f8.zip |
Kernel: Use RefPtr instead of LockRefPtr for File and subclasses
This was mostly straightforward, as all the storage locations are
guarded by some related mutex.
The use of old-school associated mutexes instead of MutexProtected
is unfortunate, but the process to modernize such code is ongoing.
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/IPv4Socket.cpp | 4 | ||||
-rw-r--r-- | Kernel/Net/IPv4Socket.h | 2 | ||||
-rw-r--r-- | Kernel/Net/LocalSocket.cpp | 4 | ||||
-rw-r--r-- | Kernel/Net/LocalSocket.h | 2 | ||||
-rw-r--r-- | Kernel/Net/NetworkTask.cpp | 28 | ||||
-rw-r--r-- | Kernel/Net/Socket.cpp | 6 | ||||
-rw-r--r-- | Kernel/Net/Socket.h | 8 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 18 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.h | 12 | ||||
-rw-r--r-- | Kernel/Net/UDPSocket.cpp | 8 | ||||
-rw-r--r-- | Kernel/Net/UDPSocket.h | 4 |
11 files changed, 48 insertions, 48 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index c6dfeceb77..39318f75ff 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -40,7 +40,7 @@ ErrorOr<NonnullOwnPtr<DoubleBuffer>> IPv4Socket::try_create_receive_buffer() return DoubleBuffer::try_create("IPv4Socket: Receive buffer"sv, 256 * KiB); } -ErrorOr<NonnullLockRefPtr<Socket>> IPv4Socket::create(int type, int protocol) +ErrorOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol) { auto receive_buffer = TRY(IPv4Socket::try_create_receive_buffer()); @@ -49,7 +49,7 @@ ErrorOr<NonnullLockRefPtr<Socket>> IPv4Socket::create(int type, int protocol) if (type == SOCK_DGRAM) return TRY(UDPSocket::try_create(protocol, move(receive_buffer))); if (type == SOCK_RAW) { - auto raw_socket = adopt_lock_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol, move(receive_buffer), {})); + auto raw_socket = adopt_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol, move(receive_buffer), {})); if (raw_socket) return raw_socket.release_nonnull(); return ENOMEM; diff --git a/Kernel/Net/IPv4Socket.h b/Kernel/Net/IPv4Socket.h index 74169b5d8a..3cc5d360e7 100644 --- a/Kernel/Net/IPv4Socket.h +++ b/Kernel/Net/IPv4Socket.h @@ -28,7 +28,7 @@ struct PortAllocationResult { class IPv4Socket : public Socket { public: - static ErrorOr<NonnullLockRefPtr<Socket>> create(int type, int protocol); + static ErrorOr<NonnullRefPtr<Socket>> create(int type, int protocol); virtual ~IPv4Socket() override; virtual ErrorOr<void> close() override; diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 6b88b10d35..f5963e8be2 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -43,11 +43,11 @@ ErrorOr<void> LocalSocket::try_for_each(Function<ErrorOr<void>(LocalSocket const }); } -ErrorOr<NonnullLockRefPtr<LocalSocket>> LocalSocket::try_create(int type) +ErrorOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type) { auto client_buffer = TRY(DoubleBuffer::try_create("LocalSocket: Client buffer"sv)); auto server_buffer = TRY(DoubleBuffer::try_create("LocalSocket: Server buffer"sv)); - return adopt_nonnull_lock_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer))); + return adopt_nonnull_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer))); } ErrorOr<SocketPair> LocalSocket::try_create_connected_pair(int type) diff --git a/Kernel/Net/LocalSocket.h b/Kernel/Net/LocalSocket.h index 5668b30f25..25d1716122 100644 --- a/Kernel/Net/LocalSocket.h +++ b/Kernel/Net/LocalSocket.h @@ -22,7 +22,7 @@ struct SocketPair { class LocalSocket final : public Socket { public: - static ErrorOr<NonnullLockRefPtr<LocalSocket>> try_create(int type); + static ErrorOr<NonnullRefPtr<LocalSocket>> try_create(int type); static ErrorOr<SocketPair> try_create_connected_pair(int type); virtual ~LocalSocket() override; diff --git a/Kernel/Net/NetworkTask.cpp b/Kernel/Net/NetworkTask.cpp index 5b90d4ad33..cb7586b7a5 100644 --- a/Kernel/Net/NetworkTask.cpp +++ b/Kernel/Net/NetworkTask.cpp @@ -30,13 +30,13 @@ static void handle_ipv4(EthernetFrameHeader const&, size_t frame_size, Time cons static void handle_icmp(EthernetFrameHeader const&, IPv4Packet const&, Time const& packet_timestamp); static void handle_udp(IPv4Packet const&, Time const& packet_timestamp); static void handle_tcp(IPv4Packet const&, Time const& packet_timestamp); -static void send_delayed_tcp_ack(LockRefPtr<TCPSocket> socket); +static void send_delayed_tcp_ack(TCPSocket& socket); static void send_tcp_rst(IPv4Packet const& ipv4_packet, TCPPacket const& tcp_packet, LockRefPtr<NetworkAdapter> adapter); static void flush_delayed_tcp_acks(); static void retransmit_tcp_packets(); static Thread* network_task = nullptr; -static HashTable<LockRefPtr<TCPSocket>>* delayed_ack_sockets; +static HashTable<NonnullRefPtr<TCPSocket>>* delayed_ack_sockets; [[noreturn]] static void NetworkTask_main(void*); @@ -57,7 +57,7 @@ bool NetworkTask::is_current() void NetworkTask_main(void*) { - delayed_ack_sockets = new HashTable<LockRefPtr<TCPSocket>>; + delayed_ack_sockets = new HashTable<NonnullRefPtr<TCPSocket>>; WaitQueue packet_wait_queue; int pending_packets = 0; @@ -229,7 +229,7 @@ void handle_icmp(EthernetFrameHeader const& eth, IPv4Packet const& ipv4_packet, dbgln_if(ICMP_DEBUG, "handle_icmp: source={}, destination={}, type={:#02x}, code={:#02x}", ipv4_packet.source().to_string(), ipv4_packet.destination().to_string(), icmp_header.type(), icmp_header.code()); { - Vector<NonnullLockRefPtr<IPv4Socket>> icmp_sockets; + Vector<NonnullRefPtr<IPv4Socket>> icmp_sockets; IPv4Socket::all_sockets().with_exclusive([&](auto& sockets) { for (auto& socket : sockets) { if (socket.protocol() == (unsigned)IPv4Protocol::ICMP) @@ -302,11 +302,11 @@ void handle_udp(IPv4Packet const& ipv4_packet, Time const& packet_timestamp) socket->did_receive(ipv4_packet.source(), udp_packet.source_port(), { &ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size() }, packet_timestamp); } -void send_delayed_tcp_ack(LockRefPtr<TCPSocket> socket) +void send_delayed_tcp_ack(TCPSocket& socket) { - VERIFY(socket->mutex().is_locked()); - if (!socket->should_delay_next_ack()) { - [[maybe_unused]] auto result = socket->send_ack(); + VERIFY(socket.mutex().is_locked()); + if (!socket.should_delay_next_ack()) { + [[maybe_unused]] auto result = socket.send_ack(); return; } @@ -315,11 +315,11 @@ void send_delayed_tcp_ack(LockRefPtr<TCPSocket> socket) void flush_delayed_tcp_acks() { - Vector<LockRefPtr<TCPSocket>, 32> remaining_sockets; + Vector<NonnullRefPtr<TCPSocket>, 32> remaining_sockets; for (auto& socket : *delayed_ack_sockets) { MutexLocker locker(socket->mutex()); if (socket->should_delay_next_ack()) { - MUST(remaining_sockets.try_append(socket)); + MUST(remaining_sockets.try_append(*socket)); continue; } [[maybe_unused]] auto result = socket->send_ack(); @@ -484,7 +484,7 @@ void handle_tcp(IPv4Packet const& ipv4_packet, Time const& packet_timestamp) return; case TCPFlags::ACK | TCPFlags::FIN: socket->set_ack_number(tcp_packet.sequence_number() + payload_size + 1); - send_delayed_tcp_ack(socket); + send_delayed_tcp_ack(*socket); socket->set_state(TCPSocket::State::Closed); socket->set_error(TCPSocket::Error::FINDuringConnect); socket->set_setup_state(Socket::SetupState::Completed); @@ -636,7 +636,7 @@ void handle_tcp(IPv4Packet const& ipv4_packet, Time const& packet_timestamp) socket->did_receive(ipv4_packet.source(), tcp_packet.source_port(), { &ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size() }, packet_timestamp); socket->set_ack_number(tcp_packet.sequence_number() + payload_size + 1); - send_delayed_tcp_ack(socket); + send_delayed_tcp_ack(*socket); socket->set_state(TCPSocket::State::CloseWait); socket->set_connected(false); return; @@ -647,7 +647,7 @@ void handle_tcp(IPv4Packet const& ipv4_packet, Time const& packet_timestamp) socket->set_ack_number(tcp_packet.sequence_number() + payload_size); dbgln_if(TCP_DEBUG, "Got packet with ack_no={}, seq_no={}, payload_size={}, acking it with new ack_no={}, seq_no={}", tcp_packet.ack_number(), tcp_packet.sequence_number(), payload_size, socket->ack_number(), socket->sequence_number()); - send_delayed_tcp_ack(socket); + send_delayed_tcp_ack(*socket); } } } @@ -657,7 +657,7 @@ void retransmit_tcp_packets() { // We must keep the sockets alive until after we've unlocked the hash table // in case retransmit_packets() realizes that it wants to close the socket. - Vector<NonnullLockRefPtr<TCPSocket>, 16> sockets; + Vector<NonnullRefPtr<TCPSocket>, 16> sockets; TCPSocket::sockets_for_retransmit().for_each_shared([&](auto const& socket) { // We ignore allocation failures above the first 16 guaranteed socket slots, as // we will just retransmit their packets the next time around diff --git a/Kernel/Net/Socket.cpp b/Kernel/Net/Socket.cpp index 961534ba58..9ae1585b94 100644 --- a/Kernel/Net/Socket.cpp +++ b/Kernel/Net/Socket.cpp @@ -17,7 +17,7 @@ namespace Kernel { -ErrorOr<NonnullLockRefPtr<Socket>> Socket::create(int domain, int type, int protocol) +ErrorOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol) { switch (domain) { case AF_LOCAL: @@ -46,7 +46,7 @@ void Socket::set_setup_state(SetupState new_setup_state) evaluate_block_conditions(); } -LockRefPtr<Socket> Socket::accept() +RefPtr<Socket> Socket::accept() { MutexLocker locker(mutex()); if (m_pending.is_empty()) @@ -63,7 +63,7 @@ LockRefPtr<Socket> Socket::accept() return client; } -ErrorOr<void> Socket::queue_connection_from(NonnullLockRefPtr<Socket> peer) +ErrorOr<void> Socket::queue_connection_from(NonnullRefPtr<Socket> peer) { dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this); MutexLocker locker(mutex()); diff --git a/Kernel/Net/Socket.h b/Kernel/Net/Socket.h index 04fe6efd86..746274e0ff 100644 --- a/Kernel/Net/Socket.h +++ b/Kernel/Net/Socket.h @@ -20,7 +20,7 @@ class OpenFileDescription; class Socket : public File { public: - static ErrorOr<NonnullLockRefPtr<Socket>> create(int domain, int type, int protocol); + static ErrorOr<NonnullRefPtr<Socket>> create(int domain, int type, int protocol); virtual ~Socket() override; int domain() const { return m_domain; } @@ -67,7 +67,7 @@ public: void set_connected(bool); bool can_accept() const { return !m_pending.is_empty(); } - LockRefPtr<Socket> accept(); + RefPtr<Socket> accept(); ErrorOr<void> shutdown(int how); @@ -111,7 +111,7 @@ public: protected: Socket(int domain, int type, int protocol); - ErrorOr<void> queue_connection_from(NonnullLockRefPtr<Socket>); + ErrorOr<void> queue_connection_from(NonnullRefPtr<Socket>); size_t backlog() const { return m_backlog; } void set_backlog(size_t backlog) { m_backlog = backlog; } @@ -181,7 +181,7 @@ private: Optional<ErrnoCode> m_so_error; - Vector<NonnullLockRefPtr<Socket>> m_pending; + Vector<NonnullRefPtr<Socket>> m_pending; }; // This is a special variant of TRY() that also updates the socket's SO_ERROR field on error. diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index b053d61bd4..f238c47a7d 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -88,9 +88,9 @@ void TCPSocket::set_state(State new_state) evaluate_block_conditions(); } -static Singleton<MutexProtected<HashMap<IPv4SocketTuple, LockRefPtr<TCPSocket>>>> s_socket_closing; +static Singleton<MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>> s_socket_closing; -MutexProtected<HashMap<IPv4SocketTuple, LockRefPtr<TCPSocket>>>& TCPSocket::closing_sockets() +MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& TCPSocket::closing_sockets() { return *s_socket_closing; } @@ -102,9 +102,9 @@ MutexProtected<HashMap<IPv4SocketTuple, TCPSocket*>>& TCPSocket::sockets_by_tupl return *s_socket_tuples; } -LockRefPtr<TCPSocket> TCPSocket::from_tuple(IPv4SocketTuple const& tuple) +RefPtr<TCPSocket> TCPSocket::from_tuple(IPv4SocketTuple const& tuple) { - return sockets_by_tuple().with_shared([&](auto const& table) -> LockRefPtr<TCPSocket> { + return sockets_by_tuple().with_shared([&](auto const& table) -> RefPtr<TCPSocket> { auto exact_match = table.get(tuple); if (exact_match.has_value()) return { *exact_match.value() }; @@ -122,10 +122,10 @@ LockRefPtr<TCPSocket> TCPSocket::from_tuple(IPv4SocketTuple const& tuple) return {}; }); } -ErrorOr<NonnullLockRefPtr<TCPSocket>> TCPSocket::try_create_client(IPv4Address const& new_local_address, u16 new_local_port, IPv4Address const& new_peer_address, u16 new_peer_port) +ErrorOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create_client(IPv4Address const& new_local_address, u16 new_local_port, IPv4Address const& new_peer_address, u16 new_peer_port) { auto tuple = IPv4SocketTuple(new_local_address, new_local_port, new_peer_address, new_peer_port); - return sockets_by_tuple().with_exclusive([&](auto& table) -> ErrorOr<NonnullLockRefPtr<TCPSocket>> { + return sockets_by_tuple().with_exclusive([&](auto& table) -> ErrorOr<NonnullRefPtr<TCPSocket>> { if (table.contains(tuple)) return EEXIST; @@ -154,7 +154,7 @@ void TCPSocket::release_to_originator() m_originator.clear(); } -void TCPSocket::release_for_accept(NonnullLockRefPtr<TCPSocket> socket) +void TCPSocket::release_for_accept(NonnullRefPtr<TCPSocket> socket) { VERIFY(m_pending_release_for_accept.contains(socket->tuple())); m_pending_release_for_accept.remove(socket->tuple()); @@ -175,11 +175,11 @@ TCPSocket::~TCPSocket() dbgln_if(TCP_SOCKET_DEBUG, "~TCPSocket in state {}", to_string(state())); } -ErrorOr<NonnullLockRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer) +ErrorOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer) { // Note: Scratch buffer is only used for SOCK_STREAM sockets. auto scratch_buffer = TRY(KBuffer::try_create_with_size("TCPSocket: Scratch buffer"sv, 65536)); - return adopt_nonnull_lock_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer))); + return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer))); } ErrorOr<size_t> TCPSocket::protocol_size(ReadonlyBytes raw_ipv4_packet) diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h index b27e09cb2b..ae73102832 100644 --- a/Kernel/Net/TCPSocket.h +++ b/Kernel/Net/TCPSocket.h @@ -20,7 +20,7 @@ class TCPSocket final : public IPv4Socket { public: static void for_each(Function<void(TCPSocket const&)>); static ErrorOr<void> try_for_each(Function<ErrorOr<void>(TCPSocket const&)>); - static ErrorOr<NonnullLockRefPtr<TCPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); + static ErrorOr<NonnullRefPtr<TCPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); virtual ~TCPSocket() override; virtual bool unref() const override; @@ -146,15 +146,15 @@ public: bool should_delay_next_ack() const; static MutexProtected<HashMap<IPv4SocketTuple, TCPSocket*>>& sockets_by_tuple(); - static LockRefPtr<TCPSocket> from_tuple(IPv4SocketTuple const& tuple); + static RefPtr<TCPSocket> from_tuple(IPv4SocketTuple const& tuple); - static MutexProtected<HashMap<IPv4SocketTuple, LockRefPtr<TCPSocket>>>& closing_sockets(); + static MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& closing_sockets(); - ErrorOr<NonnullLockRefPtr<TCPSocket>> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port); + ErrorOr<NonnullRefPtr<TCPSocket>> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port); void set_originator(TCPSocket& originator) { m_originator = originator; } bool has_originator() { return !!m_originator; } void release_to_originator(); - void release_for_accept(NonnullLockRefPtr<TCPSocket>); + void release_for_accept(NonnullRefPtr<TCPSocket>); void retransmit_packets(); @@ -186,7 +186,7 @@ private: void dequeue_for_retransmit(); LockWeakPtr<TCPSocket> m_originator; - HashMap<IPv4SocketTuple, NonnullLockRefPtr<TCPSocket>> m_pending_release_for_accept; + HashMap<IPv4SocketTuple, NonnullRefPtr<TCPSocket>> m_pending_release_for_accept; Direction m_direction { Direction::Unspecified }; Error m_error { Error::None }; LockRefPtr<NetworkAdapter> m_adapter; diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index a109414568..88fc3388e2 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -38,9 +38,9 @@ MutexProtected<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port() return *s_map; } -LockRefPtr<UDPSocket> UDPSocket::from_port(u16 port) +RefPtr<UDPSocket> UDPSocket::from_port(u16 port) { - return sockets_by_port().with_shared([&](auto const& table) -> LockRefPtr<UDPSocket> { + return sockets_by_port().with_shared([&](auto const& table) -> RefPtr<UDPSocket> { auto it = table.find(port); if (it == table.end()) return {}; @@ -60,9 +60,9 @@ UDPSocket::~UDPSocket() }); } -ErrorOr<NonnullLockRefPtr<UDPSocket>> UDPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer) +ErrorOr<NonnullRefPtr<UDPSocket>> UDPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer) { - return adopt_nonnull_lock_ref_or_enomem(new (nothrow) UDPSocket(protocol, move(receive_buffer))); + return adopt_nonnull_ref_or_enomem(new (nothrow) UDPSocket(protocol, move(receive_buffer))); } ErrorOr<size_t> UDPSocket::protocol_size(ReadonlyBytes raw_ipv4_packet) diff --git a/Kernel/Net/UDPSocket.h b/Kernel/Net/UDPSocket.h index 3cdb9f60ea..d904f97e24 100644 --- a/Kernel/Net/UDPSocket.h +++ b/Kernel/Net/UDPSocket.h @@ -14,10 +14,10 @@ namespace Kernel { class UDPSocket final : public IPv4Socket { public: - static ErrorOr<NonnullLockRefPtr<UDPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); + static ErrorOr<NonnullRefPtr<UDPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); virtual ~UDPSocket() override; - static LockRefPtr<UDPSocket> from_port(u16); + static RefPtr<UDPSocket> from_port(u16); static void for_each(Function<void(UDPSocket const&)>); static ErrorOr<void> try_for_each(Function<ErrorOr<void>(UDPSocket const&)>); |