File tree Expand file tree Collapse file tree 1 file changed +16
-4
lines changed
Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -817,10 +817,22 @@ double OS_Windows::get_unix_time() const {
817817}
818818
819819void OS_Windows::delay_usec (uint32_t p_usec) const {
820- if (p_usec < 1000 ) {
821- Sleep (1 );
822- } else {
823- Sleep (p_usec / 1000 );
820+ constexpr uint32_t tolerance = 1000 + 20 ;
821+
822+ uint64_t t0 = get_ticks_usec ();
823+ uint64_t target_time = t0 + p_usec;
824+
825+ // Calculate sleep duration with a tolerance for fine-tuning.
826+ if (p_usec > tolerance) {
827+ uint32_t coarse_sleep_usec = p_usec - tolerance;
828+ if (coarse_sleep_usec >= 1000 ) {
829+ Sleep (coarse_sleep_usec / 1000 );
830+ }
831+ }
832+
833+ // Spin-wait until we reach the precise target time.
834+ while (get_ticks_usec () < target_time) {
835+ YieldProcessor ();
824836 }
825837}
826838
You can’t perform that action at this time.
0 commit comments