1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:57:45 +00:00

Kernel: Make self-contained locking smart pointers their own classes

Until now, our kernel has reimplemented a number of AK classes to
provide automatic internal locking:

- RefPtr
- NonnullRefPtr
- WeakPtr
- Weakable

This patch renames the Kernel classes so that they can coexist with
the original AK classes:

- RefPtr => LockRefPtr
- NonnullRefPtr => NonnullLockRefPtr
- WeakPtr => LockWeakPtr
- Weakable => LockWeakable

The goal here is to eventually get rid of the Lock* classes in favor of
using external locking.
This commit is contained in:
Andreas Kling 2022-08-19 20:53:40 +02:00
parent e475263113
commit 11eee67b85
360 changed files with 1703 additions and 1672 deletions

View file

@ -40,7 +40,7 @@ ErrorOr<NonnullOwnPtr<DoubleBuffer>> IPv4Socket::try_create_receive_buffer()
return DoubleBuffer::try_create("IPv4Socket: Receive buffer"sv, 256 * KiB);
}
ErrorOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
ErrorOr<NonnullLockRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
{
auto receive_buffer = TRY(IPv4Socket::try_create_receive_buffer());
@ -49,7 +49,7 @@ ErrorOr<NonnullRefPtr<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_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol, move(receive_buffer), {}));
auto raw_socket = adopt_lock_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol, move(receive_buffer), {}));
if (raw_socket)
return raw_socket.release_nonnull();
return ENOMEM;

View file

@ -28,7 +28,7 @@ struct PortAllocationResult {
class IPv4Socket : public Socket {
public:
static ErrorOr<NonnullRefPtr<Socket>> create(int type, int protocol);
static ErrorOr<NonnullLockRefPtr<Socket>> create(int type, int protocol);
virtual ~IPv4Socket() override;
virtual ErrorOr<void> close() override;

View file

@ -181,7 +181,7 @@ static bool is_valid_device_id(u16 device_id)
}
}
UNMAP_AFTER_INIT RefPtr<E1000ENetworkAdapter> E1000ENetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
UNMAP_AFTER_INIT LockRefPtr<E1000ENetworkAdapter> E1000ENetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
{
if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Intel)
return {};
@ -192,7 +192,7 @@ UNMAP_AFTER_INIT RefPtr<E1000ENetworkAdapter> E1000ENetworkAdapter::try_to_initi
auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
if (interface_name_or_error.is_error())
return {};
auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000ENetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
auto adapter = adopt_lock_ref_if_nonnull(new (nothrow) E1000ENetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
if (!adapter)
return {};
if (adapter->initialize())

View file

@ -21,7 +21,7 @@ namespace Kernel {
class E1000ENetworkAdapter final
: public E1000NetworkAdapter {
public:
static RefPtr<E1000ENetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
static LockRefPtr<E1000ENetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
virtual bool initialize() override;

View file

@ -159,7 +159,7 @@ UNMAP_AFTER_INIT static bool is_valid_device_id(u16 device_id)
}
}
UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
UNMAP_AFTER_INIT LockRefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
{
if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Intel)
return {};
@ -170,7 +170,7 @@ UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initial
auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
if (interface_name_or_error.is_error())
return {};
auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
auto adapter = adopt_lock_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
if (!adapter)
return {};
if (adapter->initialize())

View file

@ -20,7 +20,7 @@ class E1000NetworkAdapter : public NetworkAdapter
, public PCI::Device
, public IRQHandler {
public:
static RefPtr<E1000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
static LockRefPtr<E1000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
virtual bool initialize();

View file

@ -43,11 +43,11 @@ ErrorOr<void> LocalSocket::try_for_each(Function<ErrorOr<void>(LocalSocket const
});
}
ErrorOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type)
ErrorOr<NonnullLockRefPtr<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_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer)));
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) LocalSocket(type, move(client_buffer), move(server_buffer)));
}
ErrorOr<SocketPair> LocalSocket::try_create_connected_pair(int type)
@ -469,7 +469,7 @@ ErrorOr<void> LocalSocket::chown(OpenFileDescription&, UserID uid, GroupID gid)
return {};
}
NonnullRefPtrVector<OpenFileDescription>& LocalSocket::recvfd_queue_for(OpenFileDescription const& description)
NonnullLockRefPtrVector<OpenFileDescription>& LocalSocket::recvfd_queue_for(OpenFileDescription const& description)
{
auto role = this->role(description);
if (role == Role::Connected)
@ -479,7 +479,7 @@ NonnullRefPtrVector<OpenFileDescription>& LocalSocket::recvfd_queue_for(OpenFile
VERIFY_NOT_REACHED();
}
NonnullRefPtrVector<OpenFileDescription>& LocalSocket::sendfd_queue_for(OpenFileDescription const& description)
NonnullLockRefPtrVector<OpenFileDescription>& LocalSocket::sendfd_queue_for(OpenFileDescription const& description)
{
auto role = this->role(description);
if (role == Role::Connected)
@ -489,7 +489,7 @@ NonnullRefPtrVector<OpenFileDescription>& LocalSocket::sendfd_queue_for(OpenFile
VERIFY_NOT_REACHED();
}
ErrorOr<void> LocalSocket::sendfd(OpenFileDescription const& socket_description, NonnullRefPtr<OpenFileDescription> passing_description)
ErrorOr<void> LocalSocket::sendfd(OpenFileDescription const& socket_description, NonnullLockRefPtr<OpenFileDescription> passing_description)
{
MutexLocker locker(mutex());
auto role = this->role(socket_description);
@ -503,7 +503,7 @@ ErrorOr<void> LocalSocket::sendfd(OpenFileDescription const& socket_description,
return {};
}
ErrorOr<NonnullRefPtr<OpenFileDescription>> LocalSocket::recvfd(OpenFileDescription const& socket_description)
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> LocalSocket::recvfd(OpenFileDescription const& socket_description)
{
MutexLocker locker(mutex());
auto role = this->role(socket_description);

View file

@ -15,19 +15,19 @@ namespace Kernel {
class OpenFileDescription;
struct SocketPair {
NonnullRefPtr<OpenFileDescription> description0;
NonnullRefPtr<OpenFileDescription> description1;
NonnullLockRefPtr<OpenFileDescription> description0;
NonnullLockRefPtr<OpenFileDescription> description1;
};
class LocalSocket final : public Socket {
public:
static ErrorOr<NonnullRefPtr<LocalSocket>> try_create(int type);
static ErrorOr<NonnullLockRefPtr<LocalSocket>> try_create(int type);
static ErrorOr<SocketPair> try_create_connected_pair(int type);
virtual ~LocalSocket() override;
ErrorOr<void> sendfd(OpenFileDescription const& socket_description, NonnullRefPtr<OpenFileDescription> passing_description);
ErrorOr<NonnullRefPtr<OpenFileDescription>> recvfd(OpenFileDescription const& socket_description);
ErrorOr<void> sendfd(OpenFileDescription const& socket_description, NonnullLockRefPtr<OpenFileDescription> passing_description);
ErrorOr<NonnullLockRefPtr<OpenFileDescription>> recvfd(OpenFileDescription const& socket_description);
static void for_each(Function<void(LocalSocket const&)>);
static ErrorOr<void> try_for_each(Function<ErrorOr<void>(LocalSocket const&)>);
@ -59,8 +59,8 @@ private:
bool has_attached_peer(OpenFileDescription const&) const;
DoubleBuffer* receive_buffer_for(OpenFileDescription&);
DoubleBuffer* send_buffer_for(OpenFileDescription&);
NonnullRefPtrVector<OpenFileDescription>& sendfd_queue_for(OpenFileDescription const&);
NonnullRefPtrVector<OpenFileDescription>& recvfd_queue_for(OpenFileDescription const&);
NonnullLockRefPtrVector<OpenFileDescription>& sendfd_queue_for(OpenFileDescription const&);
NonnullLockRefPtrVector<OpenFileDescription>& recvfd_queue_for(OpenFileDescription const&);
void set_connect_side_role(Role connect_side_role, bool force_evaluate_block_conditions = false)
{
@ -73,7 +73,7 @@ private:
ErrorOr<void> try_set_path(StringView);
// The inode this socket is bound to.
WeakPtr<Inode> m_inode;
LockWeakPtr<Inode> m_inode;
UserID m_prebind_uid { 0 };
GroupID m_prebind_gid { 0 };
@ -100,8 +100,8 @@ private:
NonnullOwnPtr<DoubleBuffer> m_for_client;
NonnullOwnPtr<DoubleBuffer> m_for_server;
NonnullRefPtrVector<OpenFileDescription> m_fds_for_client;
NonnullRefPtrVector<OpenFileDescription> m_fds_for_server;
NonnullLockRefPtrVector<OpenFileDescription> m_fds_for_client;
NonnullLockRefPtrVector<OpenFileDescription> m_fds_for_server;
IntrusiveListNode<LocalSocket> m_list_node;

View file

@ -11,12 +11,12 @@ namespace Kernel {
static bool s_loopback_initialized = false;
RefPtr<LoopbackAdapter> LoopbackAdapter::try_create()
LockRefPtr<LoopbackAdapter> LoopbackAdapter::try_create()
{
auto interface_name = KString::try_create("loop"sv);
if (interface_name.is_error())
return {};
return adopt_ref_if_nonnull(new LoopbackAdapter(interface_name.release_value()));
return adopt_lock_ref_if_nonnull(new LoopbackAdapter(interface_name.release_value()));
}
LoopbackAdapter::LoopbackAdapter(NonnullOwnPtr<KString> interface_name)

View file

@ -15,7 +15,7 @@ private:
LoopbackAdapter(NonnullOwnPtr<KString>);
public:
static RefPtr<LoopbackAdapter> try_create();
static LockRefPtr<LoopbackAdapter> try_create();
virtual ~LoopbackAdapter() override;
virtual void send_raw(ReadonlyBytes) override;

View file

@ -138,7 +138,7 @@ struct [[gnu::packed]] received_packet_header {
u16 length;
};
UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
UNMAP_AFTER_INIT LockRefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
{
constexpr auto ne2k_ids = Array {
PCI::HardwareID { 0x10EC, 0x8029 }, // RealTek RTL-8029(AS)
@ -162,7 +162,7 @@ UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initi
auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
if (interface_name_or_error.is_error())
return {};
return adopt_ref_if_nonnull(new (nothrow) NE2000NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
return adopt_lock_ref_if_nonnull(new (nothrow) NE2000NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
}
UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)

View file

@ -20,7 +20,7 @@ class NE2000NetworkAdapter final : public NetworkAdapter
, public PCI::Device
, public IRQHandler {
public:
static RefPtr<NE2000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
static LockRefPtr<NE2000NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
virtual ~NE2000NetworkAdapter() override;

View file

@ -111,9 +111,9 @@ size_t NetworkAdapter::dequeue_packet(u8* buffer, size_t buffer_size, Time& pack
return packet_size;
}
RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
LockRefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
{
auto packet = m_unused_packets.with([size](auto& unused_packets) -> RefPtr<PacketWithTimestamp> {
auto packet = m_unused_packets.with([size](auto& unused_packets) -> LockRefPtr<PacketWithTimestamp> {
if (unused_packets.is_empty())
return nullptr;
@ -135,7 +135,7 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
auto buffer_or_error = KBuffer::try_create_with_size("NetworkAdapter: Packet buffer"sv, size, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow);
if (buffer_or_error.is_error())
return {};
packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer_or_error.release_value(), kgettimeofday() });
packet = adopt_lock_ref_if_nonnull(new (nothrow) PacketWithTimestamp { buffer_or_error.release_value(), kgettimeofday() });
if (!packet)
return {};
packet->buffer->set_size(size);

View file

@ -12,10 +12,10 @@
#include <AK/IntrusiveList.h>
#include <AK/MACAddress.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <AK/Weakable.h>
#include <Kernel/Bus/PCI/Definitions.h>
#include <Kernel/KBuffer.h>
#include <Kernel/Library/LockWeakPtr.h>
#include <Kernel/Library/LockWeakable.h>
#include <Kernel/Net/ARP.h>
#include <Kernel/Net/EthernetFrameHeader.h>
#include <Kernel/Net/ICMP.h>
@ -39,12 +39,12 @@ struct PacketWithTimestamp final : public AtomicRefCounted<PacketWithTimestamp>
NonnullOwnPtr<KBuffer> buffer;
Time timestamp;
IntrusiveListNode<PacketWithTimestamp, RefPtr<PacketWithTimestamp>> packet_node;
IntrusiveListNode<PacketWithTimestamp, LockRefPtr<PacketWithTimestamp>> packet_node;
};
class NetworkAdapter
: public AtomicRefCounted<NetworkAdapter>
, public Weakable<NetworkAdapter> {
, public LockWeakable<NetworkAdapter> {
public:
static constexpr i32 LINKSPEED_INVALID = -1;
@ -83,7 +83,7 @@ public:
u32 packets_out() const { return m_packets_out; }
u32 bytes_out() const { return m_bytes_out; }
RefPtr<PacketWithTimestamp> acquire_packet_buffer(size_t);
LockRefPtr<PacketWithTimestamp> acquire_packet_buffer(size_t);
void release_packet_buffer(PacketWithTimestamp&);
constexpr size_t layer3_payload_offset() const { return sizeof(EthernetFrameHeader); }

View file

@ -30,19 +30,19 @@ 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(RefPtr<TCPSocket> socket);
static void send_tcp_rst(IPv4Packet const& ipv4_packet, TCPPacket const& tcp_packet, RefPtr<NetworkAdapter> adapter);
static void send_delayed_tcp_ack(LockRefPtr<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<RefPtr<TCPSocket>>* delayed_ack_sockets;
static HashTable<LockRefPtr<TCPSocket>>* delayed_ack_sockets;
[[noreturn]] static void NetworkTask_main(void*);
void NetworkTask::spawn()
{
RefPtr<Thread> thread;
LockRefPtr<Thread> thread;
auto name = KString::try_create("Network Task"sv);
if (name.is_error())
TODO();
@ -57,7 +57,7 @@ bool NetworkTask::is_current()
void NetworkTask_main(void*)
{
delayed_ack_sockets = new HashTable<RefPtr<TCPSocket>>;
delayed_ack_sockets = new HashTable<LockRefPtr<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());
{
NonnullRefPtrVector<IPv4Socket> icmp_sockets;
NonnullLockRefPtrVector<IPv4Socket> icmp_sockets;
IPv4Socket::all_sockets().with_exclusive([&](auto& sockets) {
for (auto& socket : sockets) {
if (socket.protocol() == (unsigned)IPv4Protocol::ICMP)
@ -302,7 +302,7 @@ 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(RefPtr<TCPSocket> socket)
void send_delayed_tcp_ack(LockRefPtr<TCPSocket> socket)
{
VERIFY(socket->mutex().is_locked());
if (!socket->should_delay_next_ack()) {
@ -315,7 +315,7 @@ void send_delayed_tcp_ack(RefPtr<TCPSocket> socket)
void flush_delayed_tcp_acks()
{
Vector<RefPtr<TCPSocket>, 32> remaining_sockets;
Vector<LockRefPtr<TCPSocket>, 32> remaining_sockets;
for (auto& socket : *delayed_ack_sockets) {
MutexLocker locker(socket->mutex());
if (socket->should_delay_next_ack()) {
@ -334,7 +334,7 @@ void flush_delayed_tcp_acks()
}
}
void send_tcp_rst(IPv4Packet const& ipv4_packet, TCPPacket const& tcp_packet, RefPtr<NetworkAdapter> adapter)
void send_tcp_rst(IPv4Packet const& ipv4_packet, TCPPacket const& tcp_packet, LockRefPtr<NetworkAdapter> adapter)
{
auto routing_decision = route_to(ipv4_packet.source(), ipv4_packet.destination(), adapter);
if (routing_decision.is_zero())
@ -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.
NonnullRefPtrVector<TCPSocket, 16> sockets;
NonnullLockRefPtrVector<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

View file

@ -38,7 +38,7 @@ UNMAP_AFTER_INIT NetworkingManagement::NetworkingManagement()
{
}
NonnullRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
NonnullLockRefPtr<NetworkAdapter> NetworkingManagement::loopback_adapter() const
{
return *m_loopback_adapter;
}
@ -59,13 +59,13 @@ ErrorOr<void> NetworkingManagement::try_for_each(Function<ErrorOr<void>(NetworkA
});
}
RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const& address) const
LockRefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const& address) const
{
if (address[0] == 0 && address[1] == 0 && address[2] == 0 && address[3] == 0)
return m_loopback_adapter;
if (address[0] == 127)
return m_loopback_adapter;
return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
return m_adapters.with([&](auto& adapters) -> LockRefPtr<NetworkAdapter> {
for (auto& adapter : adapters) {
if (adapter.ipv4_address() == address || adapter.ipv4_broadcast() == address)
return adapter;
@ -74,9 +74,9 @@ RefPtr<NetworkAdapter> NetworkingManagement::from_ipv4_address(IPv4Address const
});
}
RefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(StringView name) const
LockRefPtr<NetworkAdapter> NetworkingManagement::lookup_by_name(StringView name) const
{
return m_adapters.with([&](auto& adapters) -> RefPtr<NetworkAdapter> {
return m_adapters.with([&](auto& adapters) -> LockRefPtr<NetworkAdapter> {
for (auto& adapter : adapters) {
if (adapter.name() == name)
return adapter;
@ -94,7 +94,7 @@ ErrorOr<NonnullOwnPtr<KString>> NetworkingManagement::generate_interface_name_fr
return name;
}
UNMAP_AFTER_INIT RefPtr<NetworkAdapter> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
UNMAP_AFTER_INIT LockRefPtr<NetworkAdapter> NetworkingManagement::determine_network_device(PCI::DeviceIdentifier const& device_identifier) const
{
if (auto candidate = E1000NetworkAdapter::try_to_initialize(device_identifier); !candidate.is_null())
return candidate;

View file

@ -8,10 +8,10 @@
#include <AK/Function.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/NonnullRefPtr.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/Types.h>
#include <Kernel/Bus/PCI/Definitions.h>
#include <Kernel/Library/NonnullLockRefPtr.h>
#include <Kernel/Library/NonnullLockRefPtrVector.h>
#include <Kernel/Locking/SpinlockProtected.h>
#include <Kernel/Memory/Region.h>
#include <Kernel/Net/NetworkAdapter.h>
@ -34,16 +34,16 @@ public:
void for_each(Function<void(NetworkAdapter&)>);
ErrorOr<void> try_for_each(Function<ErrorOr<void>(NetworkAdapter&)>);
RefPtr<NetworkAdapter> from_ipv4_address(IPv4Address const&) const;
RefPtr<NetworkAdapter> lookup_by_name(StringView) const;
LockRefPtr<NetworkAdapter> from_ipv4_address(IPv4Address const&) const;
LockRefPtr<NetworkAdapter> lookup_by_name(StringView) const;
NonnullRefPtr<NetworkAdapter> loopback_adapter() const;
NonnullLockRefPtr<NetworkAdapter> loopback_adapter() const;
private:
RefPtr<NetworkAdapter> determine_network_device(PCI::DeviceIdentifier const&) const;
LockRefPtr<NetworkAdapter> determine_network_device(PCI::DeviceIdentifier const&) const;
SpinlockProtected<NonnullRefPtrVector<NetworkAdapter>> m_adapters { LockRank::None };
RefPtr<NetworkAdapter> m_loopback_adapter;
SpinlockProtected<NonnullLockRefPtrVector<NetworkAdapter>> m_adapters { LockRank::None };
LockRefPtr<NetworkAdapter> m_loopback_adapter;
};
}

View file

@ -113,7 +113,7 @@ namespace Kernel {
#define RX_BUFFER_SIZE 32768
#define TX_BUFFER_SIZE PACKET_SIZE_MAX
UNMAP_AFTER_INIT RefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
UNMAP_AFTER_INIT LockRefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
{
constexpr PCI::HardwareID rtl8139_id = { 0x10EC, 0x8139 };
if (pci_device_identifier.hardware_id() != rtl8139_id)
@ -123,7 +123,7 @@ UNMAP_AFTER_INIT RefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_ini
auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
if (interface_name_or_error.is_error())
return {};
return adopt_ref_if_nonnull(new (nothrow) RTL8139NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
return adopt_lock_ref_if_nonnull(new (nothrow) RTL8139NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
}
UNMAP_AFTER_INIT RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)

View file

@ -22,7 +22,7 @@ class RTL8139NetworkAdapter final : public NetworkAdapter
, public PCI::Device
, public IRQHandler {
public:
static RefPtr<RTL8139NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
static LockRefPtr<RTL8139NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
virtual ~RTL8139NetworkAdapter() override;

View file

@ -182,7 +182,7 @@ namespace Kernel {
#define TX_BUFFER_SIZE 0x1FF8
#define RX_BUFFER_SIZE 0x1FF8 // FIXME: this should be increased (0x3FFF)
UNMAP_AFTER_INIT RefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
UNMAP_AFTER_INIT LockRefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
{
if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Realtek)
return {};
@ -193,7 +193,7 @@ UNMAP_AFTER_INIT RefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to_ini
auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
if (interface_name_or_error.is_error())
return {};
return adopt_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
return adopt_lock_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
}
bool RTL8168NetworkAdapter::determine_supported_version() const

View file

@ -22,7 +22,7 @@ class RTL8168NetworkAdapter final : public NetworkAdapter
, public PCI::Device
, public IRQHandler {
public:
static RefPtr<RTL8168NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
static LockRefPtr<RTL8168NetworkAdapter> try_to_initialize(PCI::DeviceIdentifier const&);
virtual ~RTL8168NetworkAdapter() override;

View file

@ -135,11 +135,11 @@ SpinlockProtected<Route::RouteList>& routing_table()
return *s_routing_table;
}
ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, RefPtr<NetworkAdapter> adapter, UpdateTable update)
ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, LockRefPtr<NetworkAdapter> adapter, UpdateTable update)
{
dbgln_if(ROUTING_DEBUG, "update_routing_table {} {} {} {} {} {}", destination, gateway, netmask, flags, adapter, update == UpdateTable::Set ? "Set" : "Delete");
auto route_entry = adopt_ref_if_nonnull(new (nothrow) Route { destination, gateway, netmask, flags, adapter.release_nonnull() });
auto route_entry = adopt_lock_ref_if_nonnull(new (nothrow) Route { destination, gateway, netmask, flags, adapter.release_nonnull() });
if (!route_entry)
return ENOMEM;
@ -178,7 +178,7 @@ static MACAddress multicast_ethernet_address(IPv4Address const& address)
return MACAddress { 0x01, 0x00, 0x5e, (u8)(address[1] & 0x7f), address[2], address[3] };
}
RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through, AllowUsingGateway allow_using_gateway)
RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, LockRefPtr<NetworkAdapter> const through, AllowUsingGateway allow_using_gateway)
{
auto matches = [&](auto& adapter) {
if (!through)
@ -200,8 +200,8 @@ RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, R
auto target_addr = target.to_u32();
auto source_addr = source.to_u32();
RefPtr<NetworkAdapter> local_adapter = nullptr;
RefPtr<Route> chosen_route = nullptr;
LockRefPtr<NetworkAdapter> local_adapter = nullptr;
LockRefPtr<Route> chosen_route = nullptr;
NetworkingManagement::the().for_each([source_addr, &target_addr, &local_adapter, &matches, &through](NetworkAdapter& adapter) {
auto adapter_addr = adapter.ipv4_address().to_u32();
@ -263,7 +263,7 @@ RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, R
return { nullptr, {} };
}
RefPtr<NetworkAdapter> adapter = nullptr;
LockRefPtr<NetworkAdapter> adapter = nullptr;
IPv4Address next_hop_ip;
if (local_adapter) {

View file

@ -7,7 +7,7 @@
#pragma once
#include <AK/IPv4Address.h>
#include <AK/NonnullRefPtr.h>
#include <Kernel/Library/NonnullLockRefPtr.h>
#include <Kernel/Locking/MutexProtected.h>
#include <Kernel/Net/NetworkAdapter.h>
#include <Kernel/Thread.h>
@ -15,7 +15,7 @@
namespace Kernel {
struct Route final : public AtomicRefCounted<Route> {
Route(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, NonnullRefPtr<NetworkAdapter> adapter)
Route(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, NonnullLockRefPtr<NetworkAdapter> adapter)
: destination(destination)
, gateway(gateway)
, netmask(netmask)
@ -38,14 +38,14 @@ struct Route final : public AtomicRefCounted<Route> {
const IPv4Address gateway;
const IPv4Address netmask;
const u16 flags;
NonnullRefPtr<NetworkAdapter> adapter;
NonnullLockRefPtr<NetworkAdapter> adapter;
IntrusiveListNode<Route, RefPtr<Route>> route_list_node {};
IntrusiveListNode<Route, LockRefPtr<Route>> route_list_node {};
using RouteList = IntrusiveList<&Route::route_list_node>;
};
struct RoutingDecision {
RefPtr<NetworkAdapter> adapter;
LockRefPtr<NetworkAdapter> adapter;
MACAddress next_hop;
bool is_zero() const;
@ -57,14 +57,14 @@ enum class UpdateTable {
};
void update_arp_table(IPv4Address const&, MACAddress const&, UpdateTable update);
ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, RefPtr<NetworkAdapter> const adapter, UpdateTable update);
ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, LockRefPtr<NetworkAdapter> const adapter, UpdateTable update);
enum class AllowUsingGateway {
Yes,
No,
};
RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through = nullptr, AllowUsingGateway = AllowUsingGateway::Yes);
RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, LockRefPtr<NetworkAdapter> const through = nullptr, AllowUsingGateway = AllowUsingGateway::Yes);
SpinlockProtected<HashMap<IPv4Address, MACAddress>>& arp_table();
SpinlockProtected<Route::RouteList>& routing_table();

View file

@ -17,7 +17,7 @@
namespace Kernel {
ErrorOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
ErrorOr<NonnullLockRefPtr<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();
}
RefPtr<Socket> Socket::accept()
LockRefPtr<Socket> Socket::accept()
{
MutexLocker locker(mutex());
if (m_pending.is_empty())
@ -63,7 +63,7 @@ RefPtr<Socket> Socket::accept()
return client;
}
ErrorOr<void> Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
ErrorOr<void> Socket::queue_connection_from(NonnullLockRefPtr<Socket> peer)
{
dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this);
MutexLocker locker(mutex());

View file

@ -7,10 +7,10 @@
#pragma once
#include <AK/Error.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefPtr.h>
#include <AK/Time.h>
#include <Kernel/FileSystem/File.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/Library/NonnullLockRefPtrVector.h>
#include <Kernel/Locking/Mutex.h>
#include <Kernel/Net/NetworkAdapter.h>
#include <Kernel/UnixTypes.h>
@ -21,7 +21,7 @@ class OpenFileDescription;
class Socket : public File {
public:
static ErrorOr<NonnullRefPtr<Socket>> create(int domain, int type, int protocol);
static ErrorOr<NonnullLockRefPtr<Socket>> create(int domain, int type, int protocol);
virtual ~Socket() override;
int domain() const { return m_domain; }
@ -68,7 +68,7 @@ public:
void set_connected(bool);
bool can_accept() const { return !m_pending.is_empty(); }
RefPtr<Socket> accept();
LockRefPtr<Socket> accept();
ErrorOr<void> shutdown(int how);
@ -91,7 +91,7 @@ public:
ProcessID acceptor_pid() const { return m_acceptor.pid; }
UserID acceptor_uid() const { return m_acceptor.uid; }
GroupID acceptor_gid() const { return m_acceptor.gid; }
RefPtr<NetworkAdapter> const bound_interface() const { return m_bound_interface; }
LockRefPtr<NetworkAdapter> const bound_interface() const { return m_bound_interface; }
Mutex& mutex() { return m_mutex; }
@ -112,7 +112,7 @@ public:
protected:
Socket(int domain, int type, int protocol);
ErrorOr<void> queue_connection_from(NonnullRefPtr<Socket>);
ErrorOr<void> queue_connection_from(NonnullLockRefPtr<Socket>);
size_t backlog() const { return m_backlog; }
void set_backlog(size_t backlog) { m_backlog = backlog; }
@ -172,7 +172,7 @@ private:
bool m_shut_down_for_reading { false };
bool m_shut_down_for_writing { false };
RefPtr<NetworkAdapter> m_bound_interface { nullptr };
LockRefPtr<NetworkAdapter> m_bound_interface { nullptr };
Time m_receive_timeout {};
Time m_send_timeout {};
@ -180,7 +180,7 @@ private:
ErrorOr<void> m_so_error;
NonnullRefPtrVector<Socket> m_pending;
NonnullLockRefPtrVector<Socket> m_pending;
};
// This is a special variant of TRY() that also updates the socket's SO_ERROR field on error.

View file

@ -88,9 +88,9 @@ void TCPSocket::set_state(State new_state)
evaluate_block_conditions();
}
static Singleton<MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>> s_socket_closing;
static Singleton<MutexProtected<HashMap<IPv4SocketTuple, LockRefPtr<TCPSocket>>>> s_socket_closing;
MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& TCPSocket::closing_sockets()
MutexProtected<HashMap<IPv4SocketTuple, LockRefPtr<TCPSocket>>>& TCPSocket::closing_sockets()
{
return *s_socket_closing;
}
@ -102,9 +102,9 @@ MutexProtected<HashMap<IPv4SocketTuple, TCPSocket*>>& TCPSocket::sockets_by_tupl
return *s_socket_tuples;
}
RefPtr<TCPSocket> TCPSocket::from_tuple(IPv4SocketTuple const& tuple)
LockRefPtr<TCPSocket> TCPSocket::from_tuple(IPv4SocketTuple const& tuple)
{
return sockets_by_tuple().with_shared([&](auto const& table) -> RefPtr<TCPSocket> {
return sockets_by_tuple().with_shared([&](auto const& table) -> LockRefPtr<TCPSocket> {
auto exact_match = table.get(tuple);
if (exact_match.has_value())
return { *exact_match.value() };
@ -122,10 +122,10 @@ RefPtr<TCPSocket> TCPSocket::from_tuple(IPv4SocketTuple const& tuple)
return {};
});
}
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)
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)
{
auto tuple = IPv4SocketTuple(new_local_address, new_local_port, new_peer_address, new_peer_port);
return sockets_by_tuple().with_exclusive([&](auto& table) -> ErrorOr<NonnullRefPtr<TCPSocket>> {
return sockets_by_tuple().with_exclusive([&](auto& table) -> ErrorOr<NonnullLockRefPtr<TCPSocket>> {
if (table.contains(tuple))
return EEXIST;
@ -154,7 +154,7 @@ void TCPSocket::release_to_originator()
m_originator.clear();
}
void TCPSocket::release_for_accept(NonnullRefPtr<TCPSocket> socket)
void TCPSocket::release_for_accept(NonnullLockRefPtr<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<NonnullRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
ErrorOr<NonnullLockRefPtr<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_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
}
ErrorOr<size_t> TCPSocket::protocol_size(ReadonlyBytes raw_ipv4_packet)

View file

@ -10,7 +10,7 @@
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/SinglyLinkedList.h>
#include <AK/WeakPtr.h>
#include <Kernel/Library/LockWeakPtr.h>
#include <Kernel/Locking/MutexProtected.h>
#include <Kernel/Net/IPv4Socket.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<NonnullRefPtr<TCPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
static ErrorOr<NonnullLockRefPtr<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 RefPtr<TCPSocket> from_tuple(IPv4SocketTuple const& tuple);
static LockRefPtr<TCPSocket> from_tuple(IPv4SocketTuple const& tuple);
static MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& closing_sockets();
static MutexProtected<HashMap<IPv4SocketTuple, LockRefPtr<TCPSocket>>>& closing_sockets();
ErrorOr<NonnullRefPtr<TCPSocket>> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port);
ErrorOr<NonnullLockRefPtr<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(NonnullRefPtr<TCPSocket>);
void release_for_accept(NonnullLockRefPtr<TCPSocket>);
void retransmit_packets();
@ -185,11 +185,11 @@ private:
void enqueue_for_retransmit();
void dequeue_for_retransmit();
WeakPtr<TCPSocket> m_originator;
HashMap<IPv4SocketTuple, NonnullRefPtr<TCPSocket>> m_pending_release_for_accept;
LockWeakPtr<TCPSocket> m_originator;
HashMap<IPv4SocketTuple, NonnullLockRefPtr<TCPSocket>> m_pending_release_for_accept;
Direction m_direction { Direction::Unspecified };
Error m_error { Error::None };
RefPtr<NetworkAdapter> m_adapter;
LockRefPtr<NetworkAdapter> m_adapter;
u32 m_sequence_number { 0 };
u32 m_ack_number { 0 };
State m_state { State::Closed };
@ -200,9 +200,9 @@ private:
struct OutgoingPacket {
u32 ack_number { 0 };
RefPtr<PacketWithTimestamp> buffer;
LockRefPtr<PacketWithTimestamp> buffer;
size_t ipv4_payload_offset;
WeakPtr<NetworkAdapter> adapter;
LockWeakPtr<NetworkAdapter> adapter;
int tx_counter { 0 };
};

View file

@ -38,9 +38,9 @@ MutexProtected<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
return *s_map;
}
RefPtr<UDPSocket> UDPSocket::from_port(u16 port)
LockRefPtr<UDPSocket> UDPSocket::from_port(u16 port)
{
return sockets_by_port().with_shared([&](auto const& table) -> RefPtr<UDPSocket> {
return sockets_by_port().with_shared([&](auto const& table) -> LockRefPtr<UDPSocket> {
auto it = table.find(port);
if (it == table.end())
return {};
@ -60,9 +60,9 @@ UDPSocket::~UDPSocket()
});
}
ErrorOr<NonnullRefPtr<UDPSocket>> UDPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
ErrorOr<NonnullLockRefPtr<UDPSocket>> UDPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) UDPSocket(protocol, move(receive_buffer)));
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) UDPSocket(protocol, move(receive_buffer)));
}
ErrorOr<size_t> UDPSocket::protocol_size(ReadonlyBytes raw_ipv4_packet)

View file

@ -14,10 +14,10 @@ namespace Kernel {
class UDPSocket final : public IPv4Socket {
public:
static ErrorOr<NonnullRefPtr<UDPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
static ErrorOr<NonnullLockRefPtr<UDPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
virtual ~UDPSocket() override;
static RefPtr<UDPSocket> from_port(u16);
static LockRefPtr<UDPSocket> from_port(u16);
static void for_each(Function<void(UDPSocket const&)>);
static ErrorOr<void> try_for_each(Function<ErrorOr<void>(UDPSocket const&)>);