Skip to content

Commit

Permalink
Modify the TimerQueue class
Browse files Browse the repository at this point in the history
  • Loading branch information
an-tao committed Dec 18, 2018
1 parent e0addcc commit 4dade42
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
6 changes: 3 additions & 3 deletions trantor/net/EventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimerQueue>(new TimerQueue(this));
timerQueue_->reset();
}

#endif
EventLoop::~EventLoop()
{
assert(!looping_);
Expand Down
7 changes: 6 additions & 1 deletion trantor/net/EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion trantor/net/inner/TimerQueue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel>(_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__
Expand Down
6 changes: 4 additions & 2 deletions trantor/net/inner/TimerQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> _timerfdChannelPtr;
void handleRead();
#endif
Expand Down

0 comments on commit 4dade42

Please sign in to comment.