mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-04 14:49:40 +00:00
SDL_DelayNS() will attempt to sleep exactly the requested amount of time
This provides a highly accurate sleep function for your application, although you are still subject to being switched out occasionally. Fixes https://github.com/libsdl-org/SDL/issues/10210
This commit is contained in:
@@ -643,5 +643,26 @@ Uint64 SDL_GetTicks(void)
|
||||
|
||||
void SDL_Delay(Uint32 ms)
|
||||
{
|
||||
SDL_DelayNS(SDL_MS_TO_NS(ms));
|
||||
SDL_SYS_DelayNS(SDL_MS_TO_NS(ms));
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
{
|
||||
Uint64 current_value = SDL_GetTicksNS();
|
||||
Uint64 target_value = current_value + ns;
|
||||
|
||||
// Sleep for a short number of cycles
|
||||
// We'll use 1 ms as a scheduling timeslice, it's a good value for modern operating systems
|
||||
const int SCHEDULING_TIMESLICE_NS = 1 * SDL_NS_PER_MS;
|
||||
while (current_value < target_value) {
|
||||
Uint64 remaining_ns = (target_value - current_value);
|
||||
if (remaining_ns > (SCHEDULING_TIMESLICE_NS + SDL_NS_PER_US)) {
|
||||
// Sleep for a short time, less than the scheduling timeslice
|
||||
SDL_SYS_DelayNS(SCHEDULING_TIMESLICE_NS - SDL_NS_PER_US);
|
||||
} else {
|
||||
// Spin for any remaining time
|
||||
SDL_CPUPauseInstruction();
|
||||
}
|
||||
current_value = SDL_GetTicksNS();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,6 @@ extern void SDL_QuitTicks(void);
|
||||
extern int SDL_InitTimers(void);
|
||||
extern void SDL_QuitTimers(void);
|
||||
|
||||
extern void SDL_SYS_DelayNS(Uint64 ns);
|
||||
|
||||
#endif /* SDL_timer_c_h_ */
|
||||
|
||||
@@ -35,7 +35,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return SDL_US_PER_SECOND;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
snooze((bigtime_t)SDL_NS_TO_US(ns));
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return SYSCLOCK_ARM11;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
svcSleepThread(ns);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return SDL_US_PER_SECOND;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
const Uint64 max_delay = 0x7fffffffLL * SDL_NS_PER_US;
|
||||
if (ns > max_delay) {
|
||||
|
||||
@@ -39,7 +39,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return kBUSCLK;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
struct timespec tv;
|
||||
tv.tv_sec = (ns / SDL_NS_PER_SECOND);
|
||||
|
||||
@@ -42,7 +42,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return sceRtcGetTickResolution();
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
const Uint64 max_delay = 0xffffffffLL * SDL_NS_PER_US;
|
||||
if (ns > max_delay) {
|
||||
|
||||
@@ -135,7 +135,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return SDL_US_PER_SECOND;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
int was_error;
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return SDL_US_PER_SECOND;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
const Uint64 max_delay = 0xffffffffLL * SDL_NS_PER_US;
|
||||
if (ns > max_delay) {
|
||||
|
||||
@@ -66,7 +66,7 @@ Uint64 SDL_GetPerformanceFrequency(void)
|
||||
return (Uint64)frequency.QuadPart;
|
||||
}
|
||||
|
||||
void SDL_DelayNS(Uint64 ns)
|
||||
void SDL_SYS_DelayNS(Uint64 ns)
|
||||
{
|
||||
/* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag was added in Windows 10 version 1803.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user