Re: 2.4.23aa2 (bugfixes and important VM improvements for the highend)

From: Magnus Naeslund(t)
Date: Sat Mar 06 2004 - 08:01:03 EST


Jamie Lokier wrote:
[snip]

Any code which is structured like this will break:

time_t timeout = time(0) + TIMEOUT_IN_SECONDS;

do {
/* Do some stuff which takes a little while. */
} while (time(0) <= timeout);

It goes wrong when time() returns a value that is in the past, and
then jumps forward to the correct time suddenly. The timeout of the
above code is reduced by the size of that jump. If the jump is larger
than TIMEOUT_IN_SECONDS, the timeout mechanism is defeated completely.

That sort of code is a prime candidate for the method of using a
worker thread updating a global variable, so it's really important to
to take care when using it.


But isn't this kind of code a known buggy way of implementing timeouts?
Shouldn't it be like:

time_t x = time(0);
do {
...
} while (time(0) - x >= TIMEOUT_IN_SECONDS);

Ofcourse it can't handle times in the past, but it won't get easily hung with regards to leaps or wraparounds (if used with other functions).

Regards

Magnus


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/