jiffies是一个计数器,用来记录系统启动至今所产生的节拍总数,因此每秒将增加HZ个计数器
在32位系统,jiffies是32位值,对于HZ为1000的情况,大约50天左右jiffies就会达到最在值,因此将会回绕
(wraparound)到0,因此,Linux使用time_after、timer_after_eq、timer_before、
timer_before_eq来处理jiffies回绕问题
因此在使用jiffies作超时判断时要特别注意,以上做法就是没有考虑jiffies值回绕的情况:
如:unsigned long timeout = jiffies + HZ/2
if(timeout > jiffies)
//超时
else
//未超时
现在来看看这些宏的定义(巧妙地利用原码补码规则):
#define time_after(unknown,known) ((long)(known) -
(long)(unknown)<0)
#define time_before(unkonwn,known)
((long)(unknown) - (long)(known)<0)
#define time_after_eq(unknown,known) ((long)(unknown) -
(long)(known)>=0)
#define time_before_eq(unknown,known)
((long)(known) -(long)(unknown)>=0)
阅读(901) | 评论(0) | 转发(0) |