diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-09-07 22:27:17 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-09-07 22:27:17 +0800 |
commit | dedc046844c7e4e7b53cdc6935fc896f64da2fe5 (patch) | |
tree | d62bd84546e7834a1abdc43d9b962570a0a52783 /include | |
parent | 0b5085db82c06b4dfd9d46dafeee8e3a3e4b21ce (diff) | |
download | cru-dedc046844c7e4e7b53cdc6935fc896f64da2fe5.tar.gz cru-dedc046844c7e4e7b53cdc6935fc896f64da2fe5.tar.bz2 cru-dedc046844c7e4e7b53cdc6935fc896f64da2fe5.zip |
More single-threaded event loop.
Diffstat (limited to 'include')
-rw-r--r-- | include/cru/base/platform/unix/EventLoop.h | 25 |
1 files changed, 16 insertions, 9 deletions
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<void(decltype(std::declval<pollfd>().revents) revent)>; + UnixEventLoop(); + /** + * The only thread-safe function. + */ + void QueueAction(std::function<void()> 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<std::thread::id> running_thread_; std::vector<pollfd> polls_; - std::vector< - std::function<void(decltype(std::declval<pollfd>().revents) revent)>> - poll_actions_; + std::vector<PollHandler> poll_actions_; - std::atomic_int timer_tag_; + int timer_tag_; std::vector<TimerData> timers_; - UnixFileDescriptor timer_pipe_read_end_; - UnixFileDescriptor timer_pipe_write_end_; + UnixFileDescriptor action_pipe_read_end_; + UnixFileDescriptor action_pipe_write_end_; std::optional<int> exit_code_; }; |