lkml.org 
[lkml]   [2004]   [Mar]   [6]   [last100]   RSS Feed
Views: [more markup]  [less markup]  [headers]  [forward] 
 
Messages in this thread
/
DateSat, 06 Mar 2004 13:56:57 +0100
From"Magnus Naeslund(t)" <>
SubjectRe: 2.4.23aa2 (bugfixes and important VM improvements for the high end)
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@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:01    [from the cache]
©2003-2008