我已经用epoll和timerfd linux API编写了一个定时器eventloop。的timerfd_gettime
的magpage规定如下:检查time_t是否为零
The it_value field returns the amount of time until the timer will
next expire. If both fields of this structure are zero, then the
timer is currently disarmed.
所以要检查是否定时器当前布防或撤防我写了下面的代码:
bool timer_is_running(struct timer *timer)
{
struct itimerspec timerspec;
if(timerfd_gettime(timer->_timer_fd, &timerspec) == -1) {
printf("[TIMER] Could not get timer '%s' running status\n", timer->name);
return false;
}
printf("[TIMER] Checking running state of timer '%s' it_value.tv_sec = %"PRIu64", it_value.tv_nsec = %"PRIu64"\n", timer->name, (uint64_t) timerspec.it_value.tv_sec, (uint64_t) timerspec.it_value.tv_nsec == 0);
return timerspec.it_value.tv_sec != 0 && timerspec.it_value.tv_nsec != 0;
}
这是行不通的,据报道都定时器作为解除武装。当我看到在输出我看到当前解除计时器以下:
[TIMER] Checking running state of timer 'test' it_value.tv_sec = 0, it_value.tv_nsec = 4302591840
经过进一步调查,似乎只有tv_sec
字段设置为0的解除武装定时器。
这是在MIPS架构(OpenWRT)上的内核3.18.23上运行的程序。
在我将其标记为内核实现中的一个bug之前,我想知道通过执行time_t == 0
来检查time_t
是否为0是否正确。任何人都可以确认吗?
此致 大安
在返回的表达式中使用'||',而不是'&&'。 – Peter
你打算第三个参数是'timerspec.it_value.tv_nsec == 0'吗?当计时器有一个值时,这将评估为'0'。我问,因为这不符合格式规格的64位值。 –