views:

375

answers:

4

Microsoft already says, in the documentation for GetTickCount, that you could never compare tick counts to check if an interval has passed. e.g.:

Incorrect (pseudo-code):

DWORD endTime = GetTickCount + 10000; //10 s from now

...

if (GetTickCount > endTime)
   break;

The above code is bad because it is suceptable to rollover of the tick counter. For example, assume that the clock is near the end of it's range:

endTime = 0xfffffe00 + 10000
        = 0x00002510; //9,488 decimal

Then you perform your check:

if (GetTickCount > endTime)

Which is satisfied immediatly, since GetTickCount is larger than endTime:

if (0xfffffe01 > 0x00002510)

The solution

Instead you should always subtract the two time intervals:

DWORD startTime = GetTickCount;

...

if (GetTickCount - startTime) > 10000 //if it's been 10 seconds
   break;

Looking at the same math:

if (GetTickCount - startTime) > 10000

if (0xfffffe01 - 0xfffffe00) > 10000

if (1 > 10000)

Which is all well and good in C/C++, where the compiler behaves a certain way.

But what about Delphi?

But when i perform the same math in Delphi, with overflow checking on ({Q+}, {$OVERFLOWCHECKS ON}), the subtraction of the two tick counts generates an EIntOverflow exception when the TickCount rolls over:

if (0x00000100 - 0xffffff00) > 10000

0x00000100 - 0xffffff00 = 0x00000200

What is the intended solution for this problem?

Edit: i've tried to temporarily turn off OVERFLOWCHECKS:

{$OVERFLOWCHECKS OFF}]
   delta = GetTickCount - startTime;
{$OVERFLOWCHECKS ON}

But the subtraction still throws an EIntOverflow exception.

Is there a better solution, involving casts and larger intermediate variable types?


Update

Another SO question i asked explained why {$OVERFLOWCHECKS} doesn't work. It apparently only works at the function level, not the line level. So while the following doesn't work:

{$OVERFLOWCHECKS OFF}]
   delta = GetTickCount - startTime;
{$OVERFLOWCHECKS ON}

the following does work:

delta := Subtract(GetTickCount, startTime);

{$OVERFLOWCHECKS OFF}]
   function Subtract(const B, A: DWORD): DWORD;
   begin
      Result := (B - A);
   end;
{$OVERFLOWCHECKS ON}
+1  A: 

You can use the Int64 datatype to avoid overflow:

var
  Start, Delta : Int64;
begin
  Start := GetTickCount;
  ...
  Delta := GetTickCount - start;
  if (Delta > 10000) then
    ...
Ville Krumlinde
Your original code sample won't work correctly with rollover. That code says that $FFFFFF00 - $000000FF = -4294966785, rather than the correct 511. The fix is to **and** the result with $FFFFFFFF.
Ian Boyd
+4  A: 

I have stopped doing these calculations everywhere after writing a few helper functions that are called instead.

To use the new GetTickCount64() function on Vista and later there is the following new type:

type
  TSystemTicks = type int64;

which is used for all such calculations. GetTickCount() is never called directly, the helper function GetSystemTicks() is used instead:

type
  TGetTickCount64 = function: int64; stdcall;
var
  pGetTickCount64: TGetTickCount64;

procedure LoadGetTickCount64;
var
  DllHandle: HMODULE;
begin
  DllHandle := LoadLibrary('kernel32.dll');
  if DllHandle <> 0 then
    pGetTickCount64 := GetProcAddress(DllHandle, 'GetTickCount64');
end;

function GetSystemTicks: TSystemTicks;
begin
  if Assigned(pGetTickCount64) then
    Result := pGetTickCount64
  else
    Result := GetTickCount;
end;

// ...

initialization
  LoadGetTickCount64;
end.

You could even manually track the wrap-around of the GetTickCount() return value and return a true 64 bit system tick count on earlier systems too, which should work fairly well if you call the GetSystemTicks() function at least every few days. [I seem to remember an implementation of that somewhere, but don't remember where it was. gabr posted a link and the implementation.]

Now it's trivial to implement functions like

function GetTicksRemaining(...): TSystemTicks;
function GetElapsedTicks(...): TSystemTicks;
function IsTimeRunning(...): boolean;

that will hide the details. Calling these functions instead of calculating durations in-place serves also as documentation of the code intent, so less comments are necessary.

Edit:

You write in a comment:

But like you said, the fallback on Windows 2000 and XP to GetTickCount still leaves the original problem.

You can fix this easily. First you don't need to fall back to GetTickCount() - you can use the code gabr provided to calculate a 64 bit tick count on older systems as well. (You can replace timeGetTime() with GetTickCount) if you want.)

But if you don't want to do that you can just as well disable range and overflow checks in the helper functions, or check whether the minuend is smaller than the subtrahend and correct for that by adding $100000000 (2^32) to simulate a 64 bit tick count. Or implement the functions in assembler, in which case the code doesn't have the checks (not that I would advise this, but it's a possibility).

mghie
i have something similar (GetTickCountHP), which uses the high-performance timer. i was going to say that the 64-bit number is still susceptible to rollover - but i don't plan on being around to fix the bug in 580 million years. i suppose switching to a 64-bit counter is a good idea going forward. But like you said, the fallback on Windows 2000 and XP to GetTickCount still leaves the original problem.
Ian Boyd
+3  A: 

You can also use DSiTimeGetTime64 from the DSiWin32:

threadvar
  GLastTimeGetTime: DWORD;
  GTimeGetTimeBase: int64;

function DSiTimeGetTime64: int64;
begin
  Result := timeGetTime;
  if Result < GLastTimeGetTime then
    GTimeGetTimeBase := GTimeGetTimeBase + $100000000;
  GLastTimeGetTime := Result;
  Result := Result + GTimeGetTimeBase;
end; { DSiTimeGetTime64 }
gabr
Thanks, that's where I saw it.
mghie
+1  A: 

How about a simple function like this one?

function GetElapsedTime(LastTick : Cardinal) : Cardinal;
var CurrentTick : Cardinal;
begin
  CurrentTick := GetTickCount;
  if CurrentTick >= LastTick then
    Result := CurrentTick - LastTick
  else
    Result := (High(Cardinal) - LastTick) + CurrentTick;
end;

So you have

StartTime := GetTickCount
...
if GetElapsedTime(StartTime) > 10000 then
...

It will work as long as the time between StartTime and the current GetTickCount is less than the infamous 49.7 days range of GetTickCount.

Ken Bourassa
i like this design pattern to solve my specific situation. It also, in a round-about way, answer the question of how to subtract without overflow by using IFs. In the end i got the answer on another SO question that `{$OVERFLOWCHECKS OFF}` define only works at the function level, not at the line level: so i created a function called `Subtract(B,A)`
Ian Boyd