13b467340SShuo Chen #include "Channel.h"
23b467340SShuo Chen #include "EventLoop.h"
33b467340SShuo Chen 
43b467340SShuo Chen #include <stdio.h>
53b467340SShuo Chen #include <sys/timerfd.h>
63b467340SShuo Chen 
73b467340SShuo Chen muduo::EventLoop* g_loop;
83b467340SShuo Chen 
93b467340SShuo Chen!void timeout(muduo::Timestamp receiveTime)
103b467340SShuo Chen {
113b467340SShuo Chen!  printf("%s Timeout!\n", receiveTime.toFormattedString().c_str());
123b467340SShuo Chen   g_loop->quit();
133b467340SShuo Chen }
143b467340SShuo Chen 
153b467340SShuo Chen int main()
163b467340SShuo Chen {
173b467340SShuo Chen+  printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str());
183b467340SShuo Chen   muduo::EventLoop loop;
193b467340SShuo Chen   g_loop = &loop;
203b467340SShuo Chen 
213b467340SShuo Chen   int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
223b467340SShuo Chen   muduo::Channel channel(&loop, timerfd);
233b467340SShuo Chen   channel.setReadCallback(timeout);
243b467340SShuo Chen   channel.enableReading();
253b467340SShuo Chen 
263b467340SShuo Chen   struct itimerspec howlong;
273b467340SShuo Chen   bzero(&howlong, sizeof howlong);
283b467340SShuo Chen   howlong.it_value.tv_sec = 5;
293b467340SShuo Chen   ::timerfd_settime(timerfd, 0, &howlong, NULL);
303b467340SShuo Chen 
313b467340SShuo Chen   loop.loop();
323b467340SShuo Chen 
333b467340SShuo Chen   ::close(timerfd);
343b467340SShuo Chen }
35