Skip to content

Commit

Permalink
Merge pull request #2466 from oddbookworm/fix_tick_busy_loop_for_slow…
Browse files Browse the repository at this point in the history
…_cpus

Fix uint64 underflow in `tick_busy_loop`
  • Loading branch information
ankith26 authored Sep 26, 2023
2 parents d2f45a1 + 75676a3 commit 893e653
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src_c/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ static Uint64
accurate_delay(Sint64 ticks)
{
Uint64 funcstart, delay;
// Maximum delay, of the order of hundreds of millions of years. This
// should prevent Uint64 underflow issues with a CPU lag spike during the
// loop
Uint64 maximum_delay = (SDL_MAX_UINT64 >> 1);

if (ticks <= 0)
return 0;
Expand All @@ -330,7 +334,7 @@ accurate_delay(Sint64 ticks)
}
do {
delay = ticks - (PG_GetTicks() - funcstart);
} while (delay > 0);
} while (delay > 0 && delay < maximum_delay);

return PG_GetTicks() - funcstart;
}
Expand Down

0 comments on commit 893e653

Please sign in to comment.