Skip to content

Commit 893e653

Browse files
authored
Merge pull request #2466 from oddbookworm/fix_tick_busy_loop_for_slow_cpus
Fix uint64 underflow in `tick_busy_loop`
2 parents d2f45a1 + 75676a3 commit 893e653

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src_c/time.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ static Uint64
308308
accurate_delay(Sint64 ticks)
309309
{
310310
Uint64 funcstart, delay;
311+
// Maximum delay, of the order of hundreds of millions of years. This
312+
// should prevent Uint64 underflow issues with a CPU lag spike during the
313+
// loop
314+
Uint64 maximum_delay = (SDL_MAX_UINT64 >> 1);
311315

312316
if (ticks <= 0)
313317
return 0;
@@ -330,7 +334,7 @@ accurate_delay(Sint64 ticks)
330334
}
331335
do {
332336
delay = ticks - (PG_GetTicks() - funcstart);
333-
} while (delay > 0);
337+
} while (delay > 0 && delay < maximum_delay);
334338

335339
return PG_GetTicks() - funcstart;
336340
}

0 commit comments

Comments
 (0)