From 1e1ccbff5cc4f4ec7d870ea1db985b08e37ed3f8 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Mon, 3 Nov 2025 15:51:46 +0800 Subject: Use TimerRegistry in UnixEventLoop. --- include/cru/base/Timer.h | 6 ++--- include/cru/base/platform/unix/EventLoop.h | 40 ++++++++++++++---------------- 2 files changed, 22 insertions(+), 24 deletions(-) (limited to 'include/cru') diff --git a/include/cru/base/Timer.h b/include/cru/base/Timer.h index 80929f17..dac66c6a 100644 --- a/include/cru/base/Timer.h +++ b/include/cru/base/Timer.h @@ -83,13 +83,13 @@ class TimerRegistry : public Object2 { } /** - * Returns 0 if there is no timer. + * Returns nullopt if there is no timer. */ - std::chrono::milliseconds NextTimeout( + std::optional NextTimeout( std::chrono::steady_clock::time_point now) { std::unique_lock lock(mutex_); - if (timers_.empty()) return std::chrono::milliseconds::zero(); + if (timers_.empty()) return std::nullopt; return std::ranges::min( timers_ | std::views::transform([now](const TimerData& timer) { diff --git a/include/cru/base/platform/unix/EventLoop.h b/include/cru/base/platform/unix/EventLoop.h index 4f445fa3..697c6f37 100644 --- a/include/cru/base/platform/unix/EventLoop.h +++ b/include/cru/base/platform/unix/EventLoop.h @@ -6,6 +6,7 @@ #include "../../Base.h" #include "../../Exception.h" +#include "../../Timer.h" #include "UnixFile.h" #include @@ -54,27 +55,43 @@ class UnixEventLoop : public Object2 { UnixEventLoop(); /** - * The only thread-safe function. + * Thread-safe. */ void QueueAction(std::function action); int Run(); void RequestQuit(int exit_code = 0); + /** + * Thread-safe. + */ int SetTimer(std::function action, std::chrono::milliseconds timeout, bool repeat); + + /** + * Thread-safe. + */ void CancelTimer(int id); + /** + * Thread-safe. + */ int SetImmediate(std::function action) { return this->SetTimer(std::move(action), std::chrono::milliseconds::zero(), false); } + /** + * Thread-safe. + */ int SetTimeout(std::function action, std::chrono::milliseconds timeout) { return this->SetTimer(std::move(action), std::move(timeout), false); } + /** + * Thread-safe. + */ int SetInterval(std::function action, std::chrono::milliseconds interval) { return this->SetTimer(std::move(action), std::move(interval), true); @@ -83,26 +100,8 @@ class UnixEventLoop : public Object2 { void SetPoll(int fd, PollEvents events, PollHandler action); void RemovePoll(int fd); - private: - struct TimerData { - int id; - std::chrono::milliseconds original_timeout; - std::chrono::milliseconds timeout; - bool repeat; - std::function action; - - TimerData(int id, std::chrono::milliseconds timeout, bool repeat, - std::function action) - : id(id), - original_timeout(timeout), - timeout(timeout), - repeat(repeat), - action(std::move(action)) {} - }; - private: bool CheckPoll(); - bool CheckTimer(); bool CheckActionPipe(); private: @@ -111,8 +110,7 @@ class UnixEventLoop : public Object2 { std::vector polls_; std::vector poll_actions_; - int timer_tag_; - std::vector timers_; + TimerRegistry> timer_registry_; UnixFileDescriptor action_pipe_read_end_; UnixFileDescriptor action_pipe_write_end_; -- cgit v1.2.3