简体   繁体   English

检查time_t是否为零

[英]Check if time_t is zero

I have written a timer eventloop with epoll and the timerfd linux API. 我用epoll和timerfd linux API编写了一个计时器eventloop。 The magpage of timerfd_gettime states the following: timerfd_gettime的timerfd_gettime说明如下:

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.

So to check if a timer is currently armed or disarmed I had written the following code: 因此,要检查计时器当前是否已布防或撤防,我编写了以下代码:

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;
}

This wasn't working and all timers were reported as disarmed. 这不起作用,所有计时器都被报告为撤防状态。 when I looked at the output I saw the following on a currently disarmed timer: 当我查看输出时,在当前撤防的计时器上看到以下内容:

[TIMER] Checking running state of timer 'test' it_value.tv_sec = 0, it_value.tv_nsec = 4302591840

After further investigation it seems only the tv_sec field is set to 0 on disarmed timers. 经过进一步调查,似乎在撤防计时器上只有tv_sec字段设置为0。

This is program runs on kernel 3.18.23 on MIPS architecture (OpenWRT). 该程序在MIPS体系结构(OpenWRT)的内核3.18.23上运行。

Before I flag this as a bug in the kernel implementation I would like to know if it's correct to check if a time_t is 0 by doing time_t == 0 . 在将其标记为内核实现中的错误之前,我想知道通过执行time_t == 0来检查time_t是否为0是否正确。 Can anyone confirm this? 有人可以确认吗?

Kind regards, Daan 亲切的问候,大安

The time_t type alias is an arithmetic or real type. time_t类型别名是算术或实数类型。 Both arithmetic and real types can by implicitly compared with the integer value zero. 算术和实数类型都可以与整数值零进行隐式比较。

Furthermore, on POSIX systems (like Linux) time_t is defined as an integer (see eg this <sys/types.h> reference ). 此外,在POSIX系统(如Linux)上, time_t被定义为整数 (例如,请参见此<sys/types.h>参考 )。

While the C standard doesn't explicitly specify the type of time_t just about all implementations use integers for time_t for compatibility reasons. 尽管C标准没有明确指定time_t的类型,但出于兼容性原因,几乎所有实现都将time_t用作整数。 I don't know any implementation where it's not an integer. 我不知道它不是整数的任何实现。

So the answer to your question is that the comparison is correct. 因此,您的问题的答案是比较是正确的。

It should be noted that it's only the tv_sec member that is of type time_t . 应该注意的是,只有time_t类型的tv_sec成员。 The tv_nsec member is a long . tv_nsec成员是long

This is not a bug in the kernel implementation. 这不是内核实现中的错误。 It is your code that is flawed. 有缺陷的是您的代码。

The it_value field returns the amount of time until the timer will next expire. it_value字段返回直到计时器下一次到期的时间。 If both fields of this structure are zero, then the timer is currently disarmed. 如果此结构的两个字段均为零,则计时器当前处于撤防状态。

The converse of this is that (assuming the call timerfd_gettime() succeeds) the timer is armed if ONE or BOTH of the fields of the structure are non-zero. 相反的是(假设调用timerfd_gettime()成功),如果结构的字段中的一个或两个都不为零,则将计时器设防。

The last return statement of your function is 您的函数的最后一个return语句是

return timerspec.it_value.tv_sec != 0  && timerspec.it_value.tv_nsec != 0;

which returns true only if BOTH the fields are non-zero. 仅当两个字段都不为零时才返回true Instead, you need to use 相反,您需要使用

return timerspec.it_value.tv_sec != 0  || timerspec.it_value.tv_nsec != 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM