diff --git a/trantor/net/EventLoop.cc b/trantor/net/EventLoop.cc index 630d4cbef7..3f8e40e8d8 100755 --- a/trantor/net/EventLoop.cc +++ b/trantor/net/EventLoop.cc @@ -65,14 +65,14 @@ EventLoop::EventLoop() std::bind(&EventLoop::wakeupRead, this)); wakeupChannelPtr_->enableReading(); } - +#ifdef __linux__ void EventLoop::resetTimerQueue() { assertInLoopThread(); assert(!looping_); - timerQueue_ = std::unique_ptr(new TimerQueue(this)); + timerQueue_->reset(); } - +#endif EventLoop::~EventLoop() { assert(!looping_); diff --git a/trantor/net/EventLoop.h b/trantor/net/EventLoop.h index 17727d9390..1ce17ed5d0 100755 --- a/trantor/net/EventLoop.h +++ b/trantor/net/EventLoop.h @@ -46,8 +46,13 @@ class EventLoop : NonCopyable abortNotInLoopThread(); } }; +#ifdef __linux__ void resetTimerQueue(); - bool isInLoopThread() const { return threadId_ == std::this_thread::get_id(); }; +#endif + bool isInLoopThread() const + { + return threadId_ == std::this_thread::get_id(); + }; static EventLoop *getEventLoopOfCurrentThread(); void updateChannel(Channel *chl); void removeChannel(Channel *chl); diff --git a/trantor/net/inner/TimerQueue.cc b/trantor/net/inner/TimerQueue.cc index cbc0cec1b4..25a5c1ee1f 100755 --- a/trantor/net/inner/TimerQueue.cc +++ b/trantor/net/inner/TimerQueue.cc @@ -132,7 +132,26 @@ TimerQueue::TimerQueue(EventLoop *loop) _timerfdChannelPtr->enableReading(); #endif } - +#ifdef __linux__ +void TimerQueue::reset() +{ + _loop->runInLoop([this]() { + _timerfdChannelPtr->disableAll(); + _timerfdChannelPtr->remove(); + close(_timerfd); + _timerfd = createTimerfd(); + _timerfdChannelPtr = std::make_shared(_loop, _timerfd); + _timerfdChannelPtr->setReadCallback(std::bind(&TimerQueue::handleRead, this)); + // we are always reading the timerfd, we disarm it with timerfd_settime. + _timerfdChannelPtr->enableReading(); + if (!_timers.empty()) + { + const Date nextExpire = _timers.top()->when(); + resetTimerfd(_timerfd, nextExpire); + } + }); +} +#endif TimerQueue::~TimerQueue() { #ifdef __linux__ diff --git a/trantor/net/inner/TimerQueue.h b/trantor/net/inner/TimerQueue.h index f6fb603108..6367aef8e9 100755 --- a/trantor/net/inner/TimerQueue.h +++ b/trantor/net/inner/TimerQueue.h @@ -31,14 +31,16 @@ class TimerQueue : NonCopyable TimerId addTimer(TimerCallback &&cb, const Date &when, double interval); void addTimerInLoop(const TimerPtr &timer); void invalidateTimer(TimerId id); -#ifndef __linux__ +#ifdef __linux__ + void reset(); +#else int getTimeout() const; void processTimers(); #endif protected: EventLoop *_loop; #ifdef __linux__ - const int _timerfd; + int _timerfd; std::shared_ptr _timerfdChannelPtr; void handleRead(); #endif