1. 定时器函数
注:统一的方式处理IO事件和超时事件,参考统一事件源
int timerfd_create(int clockid, int flags);
- clockid:CLOCK_MONOTONIC(如果人为将系统时间向后调整,不会触发定时器)
int timerfd_settime(int fd, int flags,
const struct itimerspec *new_value,
struct itimerspec *old_value);
2. 测试用例 Reactor_test03.cc
#include <muduo/net/Channel.h>
#include <muduo/net/EventLoop.h>
#include <boost/bind.hpp>
#include <stdio.h>
#include <sys/timerfd.h>
using namespace muduo;
using namespace muduo::net;
EventLoop* g_loop;
int timerfd;
void timeout(Timestamp receiveTime)
{
printf("Timeout!\n");
uint64_t howmany;
::read(timerfd, &howmany, sizeof howmany);
g_loop->quit();
}
int main(void)
{
EventLoop loop;
g_loop = &loop;
timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
Channel channel(&loop, timerfd);
channel.setReadCallback(boost::bind(timeout, _1));
channel.enableReading();
struct itimerspec howlong;
bzero(&howlong, sizeof howlong);
howlong.it_value.tv_sec = 1;
::timerfd_settime(timerfd, 0, &howlong, NULL);
loop.loop();
::close(timerfd);
}
运行结果:
EventLoop loop;
首先创建一个EventLoop对象,其构造函数定义如下:
在39行,利用DefaultPoller初始化poller_,newDefaultPoller()定义如下:
可以看出此时使用的是EPollPoller()函数(其内容放在第三部分分析)timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
Channel channel(&loop, timerfd);
channel.setReadCallback(boost::bind(timeout, _1));
- 创建一个定时器,将事件添加到channel中,并设置读回调函数 timeout()。
channel.enableReading();
- 调用时序图如下所示:
struct itimerspec howlong;
bzero(&howlong, sizeof howlong);
howlong.it_value.tv_sec = 1;
::timerfd_settime(timerfd, 0, &howlong, NULL);
- 设置定时器时间在1s后。
loop.loop();
- 1s后定时器超时,对应的文件描述符有事件,Channel调用之前设置的读回调函数(void timeout())。
打印出“Timeout!”,loop退出,其定义如下:
wakeup()唤醒