From dedc046844c7e4e7b53cdc6935fc896f64da2fe5 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Sun, 7 Sep 2025 22:27:17 +0800 Subject: More single-threaded event loop. --- include/cru/base/platform/unix/EventLoop.h | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/cru/base/platform/unix/EventLoop.h b/include/cru/base/platform/unix/EventLoop.h index a3fac176..1fea6598 100644 --- a/include/cru/base/platform/unix/EventLoop.h +++ b/include/cru/base/platform/unix/EventLoop.h @@ -47,8 +47,16 @@ class UnixTimerFile : public Object2 { class UnixEventLoop : public Object2 { CRU_DEFINE_CLASS_LOG_TAG("cru::platform::unix::UnixEventLoop") public: + using PollHandler = + std::function().revents) revent)>; + UnixEventLoop(); + /** + * The only thread-safe function. + */ + void QueueAction(std::function action); + int Run(); void RequestQuit(int exit_code = 0); @@ -71,6 +79,9 @@ class UnixEventLoop : public Object2 { return this->SetTimer(std::move(action), std::move(interval), true); } + void AddPoll(int fd, PollHandler action); + void RemovePoll(int fd); + private: struct TimerData { int id; @@ -91,23 +102,19 @@ class UnixEventLoop : public Object2 { private: bool CheckPoll(); bool CheckTimer(); - bool ReadTimerPipe(); - - void RemoveTimer(int id); + bool CheckActionPipe(); private: std::optional running_thread_; std::vector polls_; - std::vector< - std::function().revents) revent)>> - poll_actions_; + std::vector poll_actions_; - std::atomic_int timer_tag_; + int timer_tag_; std::vector timers_; - UnixFileDescriptor timer_pipe_read_end_; - UnixFileDescriptor timer_pipe_write_end_; + UnixFileDescriptor action_pipe_read_end_; + UnixFileDescriptor action_pipe_write_end_; std::optional exit_code_; }; -- cgit v1.2.3