From 841d15447c18badb4953183a27d420f9c3921b18 Mon Sep 17 00:00:00 2001 From: chenxuan <1607772321@qq.com> Date: Thu, 26 Dec 2024 20:46:11 +0800 Subject: [PATCH] feat:add timefd for mac --- src/cppnet/timer/timer.cpp | 39 ++++++++++++++++++++++++++++++++--- src/cppnet/timer/timer.hpp | 11 ++++++++++ src/test/timer/timer_test.hpp | 6 +----- src/third_party/cpptest | 2 +- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/cppnet/timer/timer.cpp b/src/cppnet/timer/timer.cpp index a7ad524..2ceaef1 100644 --- a/src/cppnet/timer/timer.cpp +++ b/src/cppnet/timer/timer.cpp @@ -1,11 +1,10 @@ - #include "timer.hpp" #include "../utils/const.hpp" #ifdef __linux__ #include #elif __APPLE__ -#include +#include #endif #include @@ -23,8 +22,9 @@ int TimerSocket::Init(int sec, int nsec) { Reset(sec, nsec); status_ = kInit; return kSuccess; + #elif __APPLE__ - return kNotSupport; + return Reset(sec, nsec); #else return kNotSupport; #endif @@ -40,6 +40,39 @@ int TimerSocket::Reset(int sec, int nsec) { timerfd_settime(fd_, 0, &ts, NULL); return kSuccess; + +#elif __APPLE__ + if (send_fd_ > 0) { + Close(); + } + + int pipe_fd[2] = {-1, -1}; + if (pipe(pipe_fd) < 0) { + return kSysErr; + } + + fd_ = pipe_fd[0]; + send_fd_ = pipe_fd[1]; + status_ = kInit; + + std::thread( + [](int send_fd, int sec, int nsec) { + uint64_t exp; + TimerSocket soc(send_fd); + while (true) { + std::this_thread::sleep_for( + std::chrono::nanoseconds((long long)(sec * 1e9) + nsec)); + auto rc = soc.Write(&exp, sizeof(uint64_t)); + if (rc <= 0) { + // close socket + break; + } + } + }, + send_fd_, sec, nsec) + .detach(); + + return kSuccess; #else return kNotSupport; #endif diff --git a/src/cppnet/timer/timer.hpp b/src/cppnet/timer/timer.hpp index 1ebae8d..5f9abeb 100644 --- a/src/cppnet/timer/timer.hpp +++ b/src/cppnet/timer/timer.hpp @@ -36,6 +36,17 @@ class TimerSocket : public Socket { inline int IOWrite(const void *buf, size_t len, int flag = 0) override { return ::write(fd_, buf, len); } + +#ifdef __APPLE__ +public: + int Close() override { + Socket::Close(); + return Socket(send_fd_).Close(); + } + +private: + int send_fd_ = -1; +#endif }; } // namespace cppnet diff --git a/src/test/timer/timer_test.hpp b/src/test/timer/timer_test.hpp index 5a51b6b..a40ff13 100644 --- a/src/test/timer/timer_test.hpp +++ b/src/test/timer/timer_test.hpp @@ -7,10 +7,6 @@ using namespace cppnet; using namespace std; TEST(Timer, CreateTimer) { -#ifdef __APPLE__ - SKIP(); -#endif - #ifdef WIN32 SKIP(); #endif @@ -61,6 +57,6 @@ TEST(Timer, CreateTimer) { // 1e7 = 10ms, 100ms = 1e8 // 1s = 1e9 DEBUG("timer event count: " << count); - MUST_TRUE(count > 90 && count < 110, + MUST_TRUE(count >= 80 && count <= 110, "timer event count wrong " + to_string(count)); } diff --git a/src/third_party/cpptest b/src/third_party/cpptest index 1b9a2db..7844d32 160000 --- a/src/third_party/cpptest +++ b/src/third_party/cpptest @@ -1 +1 @@ -Subproject commit 1b9a2db621ec58c0d155d35c351ee8ac4dc69589 +Subproject commit 7844d32f6efae5be310a4709d66e885355e74949