一个简单的超时功能 [英] A simple timeout function
问题描述
我碰到一个超时功能,在产品线code这让我跟你真糊涂来了
I came across a timeout function in a product line code which get me really confused:
int TestTimeOut(unsigned long Timed_Val1, unsigned long Timed_Val2)
{
Timed_Val2 = Timed_Val1 + (Timed_Val2 * 200);
if (((Timed_Val1 > Timed_Val2) && (sys_msec < Timed_Val1) && (sys_msec > Timed_Val2)) || ((Timed_Val1 < Timed_Val2) && ((sys_msec < Timed_Val1) || (sys_msec > Timed_Val2))))
return TRUE;
return FALSE;
}
这里是如何使用它:
And here is how it's used:
unsigned long Timeout = sys_msec;
#define MAX_TIMEOUT 15L
while (!com_eot(1)) //to check if some transmission in progress in COM1
if (TestTimeOut(Timeout, MAX_TIMEOUT))
return FALSE;
return TRUE;
它是如何工作的?我完全被3线TestTimeOut混淆()。谢谢,
How does it work? I am totally confused by the 3 lines in TestTimeOut(). Thanks,
推荐答案
首先,我重构了 A
和 B
局部变量。
First I refactor out a
and b
local variables.
int TestTimeOut(unsigned long Timed_Val1, unsigned long Timed_Val2)
{
Timed_Val2 = Timed_Val1 + (Timed_Val2 * 200);
const int a = (Timed_Val1 > Timed_Val2) && (sys_msec < Timed_Val1) && (sys_msec > Timed_Val2);
const int b = (Timed_Val1 < Timed_Val2) && ((sys_msec < Timed_Val1) || (sys_msec > Timed_Val2));
return a || b;
}
现在这个有趣的是, Timed_Val2
是根据 Timed_Val1
,他们都是符号
所以 Timed_Val2
总是&GT; = Timed_Val1
。起初我没有看见什么办法 A
可以是真实的,但马克·威尔金斯指出,它可能如果它环绕。
Now this is interesting, Timed_Val2
is based on Timed_Val1
, they are both unsigned
so Timed_Val2
is always >= Timed_Val1
. At first I could not see no way a
can be true, but as Mark Wilkins points out, it could if it wraps around.
有也仅1例,他们是平等然而,当 Timed_Val2 == 0
我要提取出来作为一种特殊情况,以帮助可读性。那么我可以分解出两个&GT;
/ &LT;
语句到如果
。
There's also only 1 case where they are equal however, that is when Timed_Val2==0
I'm going to extract that out as a special case to help readability. I can then factor out the two >
/<
statements into an if
.
int TestTimeOut(unsigned long Timed_Val1, unsigned long Timed_Val2)
{
if (Timed_Val2==0) return FALSE;
{
Timed_Val2 = Timed_Val1 + (Timed_Val2 * 200);
if (Timed_Val1 > Timed_Val2)
{ //this happens when it wraps around past 2^32
return (sys_msec < Timed_Val1) && (sys_msec > Timed_Val2);
}
else
{
return (sys_msec < Timed_Val1) || (sys_msec > Timed_Val2);
}
}
}
所以,我要说这个返回true,当且仅当(当且仅当) sys_msec
为前 Timed_Val1
或之后 Timed_Val1 + Timed_Val2 *0.2秒
。
作为最后一个阶段,现在我会重新命名变量和评论吧。
As a final stage, now I would rename the variables and comment it.
//Returns true iff time is before startTime_msec or after timeoutPeriods of 0.2 seconds
//startTime_msec - millisecond value compariable to sys_msec
//timeoutPeriods - the number of timeout periods of 0.2 seconds each
int TestTimeOut(const unsigned long startTime_msec, const unsigned long timeoutPeriods)
{
if (timeoutPeriods==0) return FALSE;
{
const unsigned long maxTime_msec = startTime_msec + (timeoutPeriods * 200);
if (startTime_msec > maxTime_msec)
{ //this happens when it wraps around past 2^32
return (sys_msec < startTime_msec) && (sys_msec > maxTime_msec);
}
else
{
return (sys_msec < startTime_msec) || (sys_msec > maxTime_msec);
}
}
}
这并不是说有没有这样做的更好的办法,但至少它现在是可读的。
That's not to say there's not a better way of doing this, but at least it's readable now.
这篇关于一个简单的超时功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!