timer: change mp_sleep_us to mp_sleep_ns

Linux and macOS already use nanosecond resolution for their sleep
functions. It was just being converted from microseconds before. Since
we have mp_time_ns now, go ahead and bump the precision here. The timer
for windows uses some timeBeginPeriod thing which I'm not sure what it
does really but whatever just convert the units to ms like they were
doing before. There's really no reason to keep the mp_sleep_us helper
around. A multiplication by 1000 is trivial and underlying OS clocks
have nanosecond precision.
This commit is contained in:
Dudemanguy 2023-09-29 17:24:21 -05:00
parent fcebee9080
commit 59dd7d94af
9 changed files with 27 additions and 28 deletions

View File

@ -945,7 +945,7 @@ bool wasapi_thread_init(struct ao *ao)
{
struct wasapi_state *state = ao->priv;
MP_DBG(ao, "Init wasapi thread\n");
int64_t retry_wait = 1;
int64_t retry_wait = 1000;
bool align_hack = false;
HRESULT hr;
@ -1028,13 +1028,13 @@ retry:
goto retry;
case AUDCLNT_E_DEVICE_IN_USE:
case AUDCLNT_E_DEVICE_INVALIDATED:
if (retry_wait > 8) {
if (retry_wait > 8000) {
MP_FATAL(ao, "Bad device retry failed\n");
return false;
}
wasapi_thread_uninit(ao);
MP_WARN(ao, "Retrying in %"PRId64" us\n", retry_wait);
mp_sleep_us(retry_wait);
mp_sleep_ns(retry_wait);
retry_wait *= 2;
goto retry;
}

View File

@ -28,10 +28,9 @@
static double timebase_ratio_ns;
void mp_sleep_us(int64_t us)
void mp_sleep_ns(int64_t ns)
{
uint64_t deadline = us * 1e3 / timebase_ratio_ns + mach_absolute_time();
uint64_t deadline = ns / timebase_ratio_ns + mach_absolute_time();
mach_wait_until(deadline);
}

View File

@ -22,13 +22,13 @@
#include <time.h>
#include "timer.h"
void mp_sleep_us(int64_t us)
void mp_sleep_ns(int64_t ns)
{
if (us < 0)
if (ns < 0)
return;
struct timespec ts;
ts.tv_sec = us / 1000000;
ts.tv_nsec = (us % 1000000) * 1000;
ts.tv_sec = ns / UINT64_C(1000000000);
ts.tv_nsec = ns % UINT64_C(1000000000);
nanosleep(&ts, NULL);
}

View File

@ -52,17 +52,17 @@ void mp_end_hires_timers(int res_ms)
#endif
}
void mp_sleep_us(int64_t us)
void mp_sleep_ns(int64_t ns)
{
if (us < 0)
if (ns < 0)
return;
// Sleep(0) won't sleep for one clocktick as the unix usleep
// instead it will only make the thread ready
// it may take some time until it actually starts to run again
if (us < 1000)
us = 1000;
int hrt = mp_start_hires_timers(us / 1000);
Sleep(us / 1000);
if (ns < 1e6)
ns = 1e6;
int hrt = mp_start_hires_timers(ns / 1e6);
Sleep(ns / 1e6);
mp_end_hires_timers(hrt);
}

View File

@ -29,7 +29,7 @@ int64_t mp_time_us(void);
// Return time in nanoseconds. Never wraps. Never returns 0 or negative values.
int64_t mp_time_ns(void);
// Return time in seconds. Can have down to 1 microsecond resolution, but will
// Return time in seconds. Can have down to 1 nanosecond resolution, but will
// be much worse when casted to float.
double mp_time_sec(void);
@ -37,8 +37,8 @@ double mp_time_sec(void);
void mp_raw_time_init(void);
uint64_t mp_raw_time_ns(void);
// Sleep in microseconds.
void mp_sleep_us(int64_t us);
// Sleep in nanoseconds.
void mp_sleep_ns(int64_t ns);
#ifdef _WIN32
// returns: timer resolution in ms if needed and started successfully, else 0

View File

@ -343,7 +343,7 @@ static int mapper_map(struct ra_hwdec_mapper *mapper)
// of the above StretchRect. Timeout of 8ms is required to reliably
// render 4k on Intel Haswell, Ivybridge and Cherry Trail Atom.
const int max_retries = 8;
const int64_t wait_us = 1000;
const int64_t wait_ns = 1e6;
int retries = 0;
while (true) {
hr = IDirect3DQuery9_GetData(p->query9, NULL, 0, D3DGETDATA_FLUSH);
@ -353,10 +353,10 @@ static int mapper_map(struct ra_hwdec_mapper *mapper)
} else if (hr == S_FALSE) {
if (++retries > max_retries) {
MP_VERBOSE(mapper, "Failed to flush frame after %lld ms\n",
(long long)(wait_us * max_retries) / 1000);
(long long)(wait_ns * max_retries) / 1e6);
break;
}
mp_sleep_us(wait_us);
mp_sleep_ns(wait_ns);
} else {
break;
}

View File

@ -40,14 +40,14 @@ static void flip_page(struct vo *vo)
{
struct priv *p = vo->priv;
if (p->cfg_fps) {
int64_t ft = 1e6 / p->cfg_fps;
int64_t prev_vsync = mp_time_us() / ft;
int64_t ft = 1e9 / p->cfg_fps;
int64_t prev_vsync = mp_time_ns() / ft;
int64_t target_time = (prev_vsync + 1) * ft;
for (;;) {
int64_t now = mp_time_us();
int64_t now = mp_time_ns();
if (now >= target_time)
break;
mp_sleep_us(target_time - now);
mp_sleep_ns(target_time - now);
}
}
}

View File

@ -299,7 +299,7 @@ static void wait_for_completion(struct vo *vo, int max_outstanding)
" for XShm completion events...\n");
ctx->Shm_Warned_Slow = 1;
}
mp_sleep_us(1000);
mp_sleep_ns(1e6);
vo_x11_check_events(vo);
}
}

View File

@ -670,7 +670,7 @@ static void wait_for_completion(struct vo *vo, int max_outstanding)
" for XShm completion events...\n");
ctx->Shm_Warned_Slow = 1;
}
mp_sleep_us(1000);
mp_sleep_ns(1e6);
vo_x11_check_events(vo);
}
}