Files
banan-os/kernel/include/kernel/Epoll.h
DcraftBg 16967cd9c0 Kernel: Replace is_* with kind field
Replaced the is_* virtual functions with a kind field instead
2026-05-15 21:23:12 +03:00

102 lines
2.1 KiB
C++

#pragma once
#include <BAN/HashMap.h>
#include <kernel/FS/Inode.h>
#include <kernel/Lock/Mutex.h>
#include <sys/epoll.h>
namespace Kernel
{
class Epoll final : public Inode
{
public:
static BAN::ErrorOr<BAN::RefPtr<Epoll>> create();
~Epoll();
BAN::ErrorOr<void> ctl(int op, int fd, BAN::RefPtr<Inode> inode, epoll_event event);
BAN::ErrorOr<size_t> wait(BAN::Span<epoll_event> events, uint64_t waketime_ns);
void notify(BAN::RefPtr<Inode> inode, uint32_t event);
private:
Epoll() {
m_ino = 0;
m_mode = Mode::IRUSR | Mode::IWUSR;
m_nlink = 0;
m_uid = 0;
m_gid = 0;
m_size = 0;
m_atime = {};
m_mtime = {};
m_ctime = {};
m_blksize = PAGE_SIZE;
m_blocks = 0;
m_dev = 0;
m_rdev = 0;
m_kind = InodeKind::EPOLL;
}
public:
const FileSystem* filesystem() const override { return nullptr; }
bool can_read_impl() const override { return false; }
bool can_write_impl() const override { return false; }
bool has_error_impl() const override { return false; }
bool has_hungup_impl() const override { return false; }
BAN::ErrorOr<void> fsync_impl() override { return {}; }
private:
struct ListenEventList
{
ListenEventList() = default;
ListenEventList(const ListenEventList&) = delete;
ListenEventList& operator=(const ListenEventList&) = delete;
ListenEventList(ListenEventList&& other)
: events(BAN::move(other.events))
{}
ListenEventList& operator=(ListenEventList&& other)
{
events = BAN::move(other.events);
return *this;
}
BAN::HashMap<int, epoll_event> events;
bool has_fd(int fd) const
{
return events.contains(fd);
}
bool empty() const
{
return events.empty();
}
BAN::ErrorOr<void> add_fd(int fd, epoll_event event)
{
TRY(events.insert(fd, event));
return {};
}
void remove_fd(int fd)
{
events.remove(fd);
}
};
private:
Mutex m_mutex;
ThreadBlocker m_thread_blocker;
SpinLock m_ready_lock;
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t> m_ready_events;
BAN::HashMap<BAN::RefPtr<Inode>, uint32_t> m_processing_events;
BAN::HashMap<BAN::RefPtr<Inode>, ListenEventList> m_listening_events;
};
}