Kernel: Migrate IPv4 socket table locking to ProtectedValue

This commit is contained in:
Jean-Baptiste Boric 2021-07-18 11:59:25 +02:00 committed by Andreas Kling
parent edd6c04024
commit 583abc27d8
Notes: sideshowbarker 2024-07-18 07:20:18 +09:00
4 changed files with 15 additions and 15 deletions

View file

@ -26,11 +26,11 @@
namespace Kernel {
static AK::Singleton<Lockable<HashTable<IPv4Socket*>>> s_table;
static AK::Singleton<ProtectedValue<HashTable<IPv4Socket*>>> s_table;
using BlockFlags = Thread::FileDescriptionBlocker::BlockFlags;
Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
ProtectedValue<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
{
return *s_table;
}
@ -77,14 +77,17 @@ IPv4Socket::IPv4Socket(int type, int protocol, NonnullOwnPtr<DoubleBuffer> recei
if (m_buffer_mode == BufferMode::Bytes) {
VERIFY(m_scratch_buffer);
}
MutexLocker locker(all_sockets().lock());
all_sockets().resource().set(this);
all_sockets().with_exclusive([&](auto& table) {
table.set(this);
});
}
IPv4Socket::~IPv4Socket()
{
MutexLocker locker(all_sockets().lock());
all_sockets().resource().remove(this);
all_sockets().with_exclusive([&](auto& table) {
table.remove(this);
});
}
void IPv4Socket::get_local_address(sockaddr* address, socklen_t* address_size)

View file

@ -10,7 +10,7 @@
#include <AK/SinglyLinkedListWithCount.h>
#include <Kernel/DoubleBuffer.h>
#include <Kernel/KBuffer.h>
#include <Kernel/Locking/Lockable.h>
#include <Kernel/Locking/ProtectedValue.h>
#include <Kernel/Net/IPv4.h>
#include <Kernel/Net/IPv4SocketTuple.h>
#include <Kernel/Net/Socket.h>
@ -31,7 +31,7 @@ public:
static KResultOr<NonnullRefPtr<Socket>> create(int type, int protocol);
virtual ~IPv4Socket() override;
static Lockable<HashTable<IPv4Socket*>>& all_sockets();
static ProtectedValue<HashTable<IPv4Socket*>>& all_sockets();
virtual KResult close() override;
virtual KResult bind(Userspace<const sockaddr*>, socklen_t) override;

View file

@ -223,14 +223,10 @@ void handle_icmp(EthernetFrameHeader const& eth, IPv4Packet const& ipv4_packet,
{
NonnullRefPtrVector<IPv4Socket> icmp_sockets;
{
MutexLocker locker(IPv4Socket::all_sockets().lock(), Mutex::Mode::Shared);
for (auto* socket : IPv4Socket::all_sockets().resource()) {
if (socket->protocol() != (unsigned)IPv4Protocol::ICMP)
continue;
IPv4Socket::all_sockets().for_each_shared([&](const auto& socket) {
if (socket->protocol() == (unsigned)IPv4Protocol::ICMP)
icmp_sockets.append(*socket);
}
}
});
for (auto& socket : icmp_sockets)
socket.did_receive(ipv4_packet.source(), 0, { &ipv4_packet, sizeof(IPv4Packet) + ipv4_packet.payload_size() }, packet_timestamp);
}

View file

@ -11,6 +11,7 @@
#include <AK/SinglyLinkedList.h>
#include <AK/WeakPtr.h>
#include <Kernel/KResult.h>
#include <Kernel/Locking/Lockable.h>
#include <Kernel/Net/IPv4Socket.h>
namespace Kernel {