void EventProviderLinux::waitForEvents (EventLoopTime wait_time)
{
AIPSTACK_ASSERT(m_cur_epoll_event == m_num_epoll_events);
namespace chrono = std::chrono;
using Period = EventLoopTime::period;
using Rep = EventLoopTime::rep;
using SecType = decltype(itimerspec().it_value.tv_sec);
using NsecType = decltype(itimerspec().it_value.tv_nsec);
using NsecDuration = chrono::duration<NsecType, std::nano>;
static_assert(Period::num == 1);
static_assert(Period::den <= std::nano::den);
static_assert(std::is_signed_v<Rep>);
static_assert(std::is_signed_v<SecType>);
static_assert(TypeMax<Rep> / Period::den <= TypeMax<SecType>);
static_assert(TypeMin<Rep> / Period::den >= TypeMin<SecType> + 1);
if (wait_time != m_timerfd_time || m_force_timerfd_update) {
m_force_timerfd_update = true;
EventLoopTime::duration time_dur = wait_time.time_since_epoch();
SecType sec = time_dur.count() / Period::den;
Rep subsec = time_dur.count() % Period::den;
if (subsec < 0) {
sec--;
subsec += Period::den;
}
struct itimerspec itspec = {};
itspec.it_value.tv_sec = sec;
itspec.it_value.tv_nsec =
chrono::duration_cast<NsecDuration>(EventLoopTime::duration(subsec)).count();
// Prevent accidentally disarming the timerfd.
if (itspec.it_value.tv_sec == 0 && itspec.it_value.tv_nsec == 0) {
itspec.it_value.tv_nsec = 1;
}
if (::timerfd_settime(*m_timer_fd, TFD_TIMER_ABSTIME, &itspec, nullptr) < 0) {
throw std::runtime_error(formatString(
"EventProviderLinux: timerfd_settime failed, err=%d", errno));
}
m_timerfd_time = wait_time;
m_force_timerfd_update = false;
}
int wait_res;
while (true) {
wait_res = ::epoll_wait(*m_epoll_fd, m_epoll_events, MaxEpollEvents, -1);
if (AIPSTACK_LIKELY(wait_res >= 0)) {
break;
}
int err = errno;
if (err != EINTR) {
throw std::runtime_error(formatString(
"EventProviderLinux: epoll_wait failed, err=%d", err));
}
}
AIPSTACK_ASSERT(wait_res <= MaxEpollEvents);
m_cur_epoll_event = 0;
m_num_epoll_events = wait_res;
}
应该阻塞该功能,直到m_epoll_fd
上发生事件为止。您会看到有一个创建的计时器m_timer_fd
,但从未指定处理程序。在没有处理程序的情况下启动计时器有什么意义?
此外,我在其他任何地方都看不到此计时器的任何用法。由于它是在本地创建的,因此在其类中没有使用。它必须具有本地用途。
函数取自here
答案 0 :(得分:1)
来自timerfd_create man page(重点是我):
这些系统调用创建并在提供计时器的计时器上进行操作 通过文件描述符的过期通知。他们提供了 使用setitimer(2)或timer_create(2)的替代方法, 优点是文件描述符可以由select(2)监视, poll(2)和epoll(7)。
从原始源代码开始:
control_epoll(EPOLL_CTL_ADD, *m_timer_fd, EPOLLIN, &m_timer_fd);
从上面我们可以看到计时器事件已设置为通过epoll
进行监视。