diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-09-07 21:57:02 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-09-07 21:57:02 +0800 |
commit | 0b5085db82c06b4dfd9d46dafeee8e3a3e4b21ce (patch) | |
tree | 3f6e90edcab270635aae9c6338490c78cee61b5a | |
parent | 8465655ceac8e1f78e4af2224ed8b8839ec4a35e (diff) | |
download | cru-0b5085db82c06b4dfd9d46dafeee8e3a3e4b21ce.tar.gz cru-0b5085db82c06b4dfd9d46dafeee8e3a3e4b21ce.tar.bz2 cru-0b5085db82c06b4dfd9d46dafeee8e3a3e4b21ce.zip |
Base test for event loop.
-rw-r--r-- | include/cru/base/platform/unix/EventLoop.h | 3 | ||||
-rw-r--r-- | src/base/platform/unix/EventLoop.cpp | 13 |
2 files changed, 3 insertions, 13 deletions
diff --git a/include/cru/base/platform/unix/EventLoop.h b/include/cru/base/platform/unix/EventLoop.h index 9def8c7b..a3fac176 100644 --- a/include/cru/base/platform/unix/EventLoop.h +++ b/include/cru/base/platform/unix/EventLoop.h @@ -48,7 +48,6 @@ class UnixEventLoop : public Object2 { CRU_DEFINE_CLASS_LOG_TAG("cru::platform::unix::UnixEventLoop") public: UnixEventLoop(); - ~UnixEventLoop() override; int Run(); void RequestQuit(int exit_code = 0); @@ -97,7 +96,7 @@ class UnixEventLoop : public Object2 { void RemoveTimer(int id); private: - std::thread::id running_thread_; + std::optional<std::thread::id> running_thread_; std::vector<pollfd> polls_; std::vector< diff --git a/src/base/platform/unix/EventLoop.cpp b/src/base/platform/unix/EventLoop.cpp index 8fe3b7ad..c6a80835 100644 --- a/src/base/platform/unix/EventLoop.cpp +++ b/src/base/platform/unix/EventLoop.cpp @@ -1,6 +1,5 @@ #include "cru/base/platform/unix/EventLoop.h" #include "cru/base/Exception.h" -#include "cru/base/log/Logger.h" #include <poll.h> #include <algorithm> @@ -10,7 +9,7 @@ namespace cru::platform::unix { int UnixTimerFile::GetReadFd() const { return this->read_fd_; } -UnixEventLoop::UnixEventLoop() : timer_tag_(1), polls_(1), poll_actions_(1) { +UnixEventLoop::UnixEventLoop() : polls_(1), poll_actions_(1), timer_tag_(1) { auto timer_pipe = OpenUniDirectionalPipe(UnixPipeFlags::NonBlock); timer_pipe_read_end_ = std::move(timer_pipe.read); timer_pipe_write_end_ = std::move(timer_pipe.write); @@ -19,11 +18,7 @@ UnixEventLoop::UnixEventLoop() : timer_tag_(1), polls_(1), poll_actions_(1) { poll_actions_[0] = [](auto _) {}; } -UnixEventLoop::~UnixEventLoop() { CRU_LOG_TAG_DEBUG("Event loop destroyed."); } - int UnixEventLoop::Run() { - CRU_LOG_TAG_DEBUG("Event loop started."); - running_thread_ = std::this_thread::get_id(); while (!exit_code_) { @@ -135,8 +130,6 @@ bool UnixEventLoop::CheckTimer() { return false; } - CRU_LOG_TAG_INFO("A timer is to be executed."); - auto &timer = *iter; if (timer.repeat) { while (timer.timeout <= std::chrono::milliseconds::zero()) { @@ -156,7 +149,7 @@ bool UnixEventLoop::ReadTimerPipe() { TimerData *pointer; constexpr size_t pointer_size = sizeof(decltype(pointer)); auto rest = pointer_size; - while (true) { + while (rest > 0) { auto result = timer_pipe_read_end_.Read(&pointer, rest); if (result == -1) { // If no data. @@ -178,8 +171,6 @@ bool UnixEventLoop::ReadTimerPipe() { timers_.push_back(std::move(*pointer)); delete pointer; - CRU_LOG_TAG_INFO("A timer from pipe is received."); - return true; } |