19a1e991dSShuo Chen#include "Channel.h" 29a1e991dSShuo Chen#include "EventLoop.h" 39a1e991dSShuo Chen 49a1e991dSShuo Chen#include <stdio.h> 59a1e991dSShuo Chen#include <sys/timerfd.h> 69a1e991dSShuo Chen 79a1e991dSShuo Chenmuduo::EventLoop* g_loop; 89a1e991dSShuo Chen 99a1e991dSShuo Chenvoid timeout() 109a1e991dSShuo Chen{ 119a1e991dSShuo Chen printf("Timeout!\n"); 129a1e991dSShuo Chen g_loop->quit(); 139a1e991dSShuo Chen} 149a1e991dSShuo Chen 159a1e991dSShuo Chenint main() 169a1e991dSShuo Chen{ 179a1e991dSShuo Chen muduo::EventLoop loop; 189a1e991dSShuo Chen g_loop = &loop; 199a1e991dSShuo Chen 209a1e991dSShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 219a1e991dSShuo Chen muduo::Channel channel(&loop, timerfd); 229a1e991dSShuo Chen channel.setReadCallback(timeout); 239a1e991dSShuo Chen channel.enableReading(); 249a1e991dSShuo Chen 259a1e991dSShuo Chen struct itimerspec howlong; 269a1e991dSShuo Chen bzero(&howlong, sizeof howlong); 279a1e991dSShuo Chen howlong.it_value.tv_sec = 5; 289a1e991dSShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 299a1e991dSShuo Chen 309a1e991dSShuo Chen loop.loop(); 319a1e991dSShuo Chen 329a1e991dSShuo Chen ::close(timerfd); 339a1e991dSShuo Chen} 34