From 429905d4e893f91618908773166ba867970c9a17 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 9 Sep 2025 01:43:13 +0800 Subject: Implement SetPoll of event loop. --- src/base/platform/unix/EventLoop.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/base/platform/unix/EventLoop.cpp') diff --git a/src/base/platform/unix/EventLoop.cpp b/src/base/platform/unix/EventLoop.cpp index 53c54866..7c475697 100644 --- a/src/base/platform/unix/EventLoop.cpp +++ b/src/base/platform/unix/EventLoop.cpp @@ -1,5 +1,4 @@ #include "cru/base/platform/unix/EventLoop.h" -#include "cru/base/Base.h" #include "cru/base/Exception.h" #include @@ -174,16 +173,30 @@ bool UnixEventLoop::CheckActionPipe() { return true; } -void UnixEventLoop::AddPoll(int fd, PollHandler action) { - for (const auto &poll_fd : polls_) { +void UnixEventLoop::SetPoll(int fd, PollEvents events, PollHandler action) { + for (auto &poll_fd : polls_) { if (poll_fd.fd == fd) { - throw Exception("The file descriptor is already in poll list."); + poll_fd.events = events; + return; } } - NotImplemented(); + pollfd poll_fd; + poll_fd.fd = fd; + poll_fd.events = events; + poll_fd.revents = 0; + polls_.push_back(poll_fd); + poll_actions_.push_back(std::move(action)); } -void UnixEventLoop::RemovePoll(int fd) { NotImplemented(); } +void UnixEventLoop::RemovePoll(int fd) { + auto iter = std::ranges::find_if( + polls_, [fd](const pollfd &poll_fd) { return poll_fd.fd == fd; }); + if (iter != polls_.cend()) { + auto index = iter - polls_.cbegin(); + polls_.erase(iter); + poll_actions_.erase(poll_actions_.cbegin() + index); + } +} } // namespace cru::platform::unix -- cgit v1.2.3