Kernel: Implement linux's eventfd
This commit is contained in:
54
kernel/kernel/FS/EventFD.cpp
Normal file
54
kernel/kernel/FS/EventFD.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <kernel/FS/EventFD.h>
|
||||
|
||||
#include <sys/epoll.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
BAN::ErrorOr<BAN::RefPtr<Inode>> EventFD::create(uint64_t initval, bool semaphore)
|
||||
{
|
||||
auto* eventfd_ptr = new EventFD(initval, semaphore);
|
||||
if (eventfd_ptr == nullptr)
|
||||
return BAN::Error::from_errno(ENOMEM);
|
||||
return BAN::RefPtr<Inode>(BAN::RefPtr<EventFD>::adopt(eventfd_ptr));
|
||||
}
|
||||
|
||||
BAN::ErrorOr<size_t> EventFD::read_impl(off_t, BAN::ByteSpan buffer)
|
||||
{
|
||||
if (buffer.size() < sizeof(uint64_t))
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
while (m_value == 0)
|
||||
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
|
||||
|
||||
const uint64_t read_value = m_is_semaphore ? 1 : m_value;
|
||||
m_value -= read_value;
|
||||
|
||||
buffer.as<uint64_t>() = read_value;
|
||||
|
||||
epoll_notify(EPOLLOUT);
|
||||
|
||||
return sizeof(uint64_t);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<size_t> EventFD::write_impl(off_t, BAN::ConstByteSpan buffer)
|
||||
{
|
||||
if (buffer.size() < sizeof(uint64_t))
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
const uint64_t write_value = buffer.as<const uint64_t>();
|
||||
if (write_value == UINT64_MAX)
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
while (m_value + write_value < m_value)
|
||||
TRY(Thread::current().block_or_eintr_indefinite(m_thread_blocker, &m_mutex));
|
||||
|
||||
m_value += write_value;
|
||||
|
||||
if (m_value > 0)
|
||||
epoll_notify(EPOLLIN);
|
||||
|
||||
return sizeof(uint64_t);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <kernel/ELF.h>
|
||||
#include <kernel/Epoll.h>
|
||||
#include <kernel/FS/DevFS/FileSystem.h>
|
||||
#include <kernel/FS/EventFD.h>
|
||||
#include <kernel/FS/ProcFS/FileSystem.h>
|
||||
#include <kernel/FS/VirtualFileSystem.h>
|
||||
#include <kernel/IDT.h>
|
||||
@@ -28,6 +29,7 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/banan-os.h>
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/futex.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/wait.h>
|
||||
@@ -2160,6 +2162,27 @@ namespace Kernel
|
||||
return TRY(static_cast<Epoll*>(epoll_inode.ptr())->wait(BAN::Span<epoll_event>(events, maxevents), waketime_ns));
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_eventfd(unsigned int initval, int flags)
|
||||
{
|
||||
if (flags & ~(EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE))
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
int oflags = 0;
|
||||
if (flags & EFD_CLOEXEC)
|
||||
oflags |= O_CLOEXEC;
|
||||
if (flags & EFD_NONBLOCK)
|
||||
oflags |= O_NONBLOCK;
|
||||
|
||||
const bool is_semaphore = !!(flags & EFD_SEMAPHORE);
|
||||
|
||||
return TRY(m_open_file_descriptors.open(
|
||||
VirtualFileSystem::File {
|
||||
TRY(EventFD::create(initval, is_semaphore)),
|
||||
"<eventfd>"_sv
|
||||
}, oflags
|
||||
));
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_pipe(int user_fildes[2])
|
||||
{
|
||||
int fildes[2];
|
||||
|
||||
Reference in New Issue
Block a user