From 04c7e52689effe1efe84a333ccee574c6cf2ad69 Mon Sep 17 00:00:00 2001 From: JJ Date: Sun, 31 Oct 2021 12:30:11 +1100 Subject: [PATCH] Kernel: Implement syscalls for epoll --- Kernel/include/Fs/EPoll.h | 31 +++++++++++++++++++++++ Kernel/include/Fs/Filesystem.h | 13 +++++----- Kernel/include/OnCleanup.h | 18 +++++++++++++ LibLemon/include/Lemon/System/ABI/EPoll.h | 31 +++++++++++++++++++++++ 4 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 Kernel/include/Fs/EPoll.h create mode 100644 Kernel/include/OnCleanup.h create mode 100644 LibLemon/include/Lemon/System/ABI/EPoll.h diff --git a/Kernel/include/Fs/EPoll.h b/Kernel/include/Fs/EPoll.h new file mode 100644 index 00000000..fd9f968e --- /dev/null +++ b/Kernel/include/Fs/EPoll.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include +#include + +namespace fs { + +class EPoll : + public FsNode { +public: + EPoll() = default; + + void Close() override { + handleCount--; + + if(handleCount <= 0){ + delete this; + } + } + + bool IsEPoll() const override { + return true; + } + + List> fds; + lock_t epLock = 0; +}; + +} diff --git a/Kernel/include/Fs/Filesystem.h b/Kernel/include/Fs/Filesystem.h index 5a302ed7..89087e9c 100755 --- a/Kernel/include/Fs/Filesystem.h +++ b/Kernel/include/Fs/Filesystem.h @@ -39,12 +39,12 @@ #define FS_NODE_TYPE 0xF000 #define FS_NODE_FILE S_IFREG -#define FS_NODE_DIRECTORY S_IFDIR // 0x2 -#define FS_NODE_MOUNTPOINT S_IFDIR // 0x8 -#define FS_NODE_BLKDEVICE S_IFBLK // 0x10 -#define FS_NODE_SYMLINK S_IFLNK // 0x20 -#define FS_NODE_CHARDEVICE S_IFCHR // 0x40 -#define FS_NODE_SOCKET S_IFSOCK // 0x80 +#define FS_NODE_DIRECTORY S_IFDIR +#define FS_NODE_MOUNTPOINT S_IFDIR +#define FS_NODE_BLKDEVICE S_IFBLK +#define FS_NODE_SYMLINK S_IFLNK +#define FS_NODE_CHARDEVICE S_IFCHR +#define FS_NODE_SOCKET S_IFSOCK #define POLLIN 0x01 #define POLLOUT 0x02 @@ -217,6 +217,7 @@ class FsNode { virtual inline bool IsSymlink() { return (flags & FS_NODE_TYPE) == FS_NODE_SYMLINK; } virtual inline bool IsCharDevice() { return (flags & FS_NODE_TYPE) == FS_NODE_CHARDEVICE; } virtual inline bool IsSocket() { return (flags & FS_NODE_TYPE) == FS_NODE_SOCKET; } + virtual inline bool IsEPoll() const { return false; } void UnblockAll(); diff --git a/Kernel/include/OnCleanup.h b/Kernel/include/OnCleanup.h new file mode 100644 index 00000000..959f6831 --- /dev/null +++ b/Kernel/include/OnCleanup.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +template +concept CleanupFunction = requires(T t) { + t(); +}; + +template class OnCleanup { +public: + ALWAYS_INLINE OnCleanup(F&& func) : m_func(func) {} + + ALWAYS_INLINE ~OnCleanup() { m_func(); } + +private: + F m_func; +}; diff --git a/LibLemon/include/Lemon/System/ABI/EPoll.h b/LibLemon/include/Lemon/System/ABI/EPoll.h new file mode 100644 index 00000000..0f68f3bc --- /dev/null +++ b/LibLemon/include/Lemon/System/ABI/EPoll.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +#define EPOLL_CLOEXEC 1 +#define EPOLL_NONBLOCK 2 + +#define EPOLLIN 0x001 +#define EPOLLPRI 0x002 +#define EPOLLOUT 0x004 +#define EPOLLRDNORM 0x040 +#define EPOLLRDBAND 0x080 +#define EPOLLWRNORM 0x100 +#define EPOLLWRBAND 0x200 +#define EPOLLMSG 0x400 +#define EPOLLERR 0x008 +#define EPOLLHUP 0x010 +#define EPOLLRDHUP 0x2000 +#define EPOLLEXCLUSIVE (1U << 28) +#define EPOLLWAKEUP (1U << 29) +#define EPOLLONESHOT (1U << 30) +#define EPOLLET (1U << 31) + +#define EPOLL_CTL_ADD 1 +#define EPOLL_CTL_DEL 2 +#define EPOLL_CTL_MOD 3 + +struct epoll_event { + uint32_t events; + uint64_t data; +};