Re: Jiffies Wraparound (was Re: interrupt counts)

Linus Torvalds (torvalds@cs.helsinki.fi)
Thu, 22 Aug 1996 09:50:09 +0300 (EET DST)


On Wed, 21 Aug 1996, Richard Henderson wrote:
>
> > IMHO, we either should forget about it and wait for Merced or anything
> > else to move us out of the 32 bit world ... or we should completely avoid
> > comparing "jiffies" to variables. We could do this the following way:
> >
> > #define IS_TIMED_OUT(timeout) (jiffies>timeout) || \
> > (timeout-jiffies>MAX_JIFFIES/2)
>
> Just rewriting tests from
>
> timeout = jiffies + delay;
> while (jifies < timeout) continue;
>
> to
>
> start = jiffies;
> while (jiffies - start < delay) continue;
>
> solves the problem, and is much faster.

In fact, it's even faster to use

end = jiffies + timeout;
while ((signed)(end - jiffies) > 0) continue;

because a comparison against zero generally doesn't need a special compare
instruction. (speed doesn't matter in this case, as we're busy waiting
anyway, but it might matter in the timeout handler itself).

That _does_ require that "timeout" is less or equal to MAX_TIMEOUT:

#define MAX_TIMEOUT ((~0UL)>>1)

but this is generally not a real limitation (it means that you can't have
timeouts longer than 248 days on a x86, tough luck).

Linus