mirror of https://github.com/mpv-player/mpv
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:
parent
fcebee9080
commit
59dd7d94af
|
@ -945,7 +945,7 @@ bool wasapi_thread_init(struct ao *ao)
|
||||||
{
|
{
|
||||||
struct wasapi_state *state = ao->priv;
|
struct wasapi_state *state = ao->priv;
|
||||||
MP_DBG(ao, "Init wasapi thread\n");
|
MP_DBG(ao, "Init wasapi thread\n");
|
||||||
int64_t retry_wait = 1;
|
int64_t retry_wait = 1000;
|
||||||
bool align_hack = false;
|
bool align_hack = false;
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
|
|
||||||
|
@ -1028,13 +1028,13 @@ retry:
|
||||||
goto retry;
|
goto retry;
|
||||||
case AUDCLNT_E_DEVICE_IN_USE:
|
case AUDCLNT_E_DEVICE_IN_USE:
|
||||||
case AUDCLNT_E_DEVICE_INVALIDATED:
|
case AUDCLNT_E_DEVICE_INVALIDATED:
|
||||||
if (retry_wait > 8) {
|
if (retry_wait > 8000) {
|
||||||
MP_FATAL(ao, "Bad device retry failed\n");
|
MP_FATAL(ao, "Bad device retry failed\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
wasapi_thread_uninit(ao);
|
wasapi_thread_uninit(ao);
|
||||||
MP_WARN(ao, "Retrying in %"PRId64" us\n", retry_wait);
|
MP_WARN(ao, "Retrying in %"PRId64" us\n", retry_wait);
|
||||||
mp_sleep_us(retry_wait);
|
mp_sleep_ns(retry_wait);
|
||||||
retry_wait *= 2;
|
retry_wait *= 2;
|
||||||
goto retry;
|
goto retry;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,10 +28,9 @@
|
||||||
|
|
||||||
static double timebase_ratio_ns;
|
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);
|
mach_wait_until(deadline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,13 +22,13 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "timer.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;
|
return;
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
ts.tv_sec = us / 1000000;
|
ts.tv_sec = ns / UINT64_C(1000000000);
|
||||||
ts.tv_nsec = (us % 1000000) * 1000;
|
ts.tv_nsec = ns % UINT64_C(1000000000);
|
||||||
nanosleep(&ts, NULL);
|
nanosleep(&ts, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,17 +52,17 @@ void mp_end_hires_timers(int res_ms)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_sleep_us(int64_t us)
|
void mp_sleep_ns(int64_t ns)
|
||||||
{
|
{
|
||||||
if (us < 0)
|
if (ns < 0)
|
||||||
return;
|
return;
|
||||||
// Sleep(0) won't sleep for one clocktick as the unix usleep
|
// Sleep(0) won't sleep for one clocktick as the unix usleep
|
||||||
// instead it will only make the thread ready
|
// instead it will only make the thread ready
|
||||||
// it may take some time until it actually starts to run again
|
// it may take some time until it actually starts to run again
|
||||||
if (us < 1000)
|
if (ns < 1e6)
|
||||||
us = 1000;
|
ns = 1e6;
|
||||||
int hrt = mp_start_hires_timers(us / 1000);
|
int hrt = mp_start_hires_timers(ns / 1e6);
|
||||||
Sleep(us / 1000);
|
Sleep(ns / 1e6);
|
||||||
mp_end_hires_timers(hrt);
|
mp_end_hires_timers(hrt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ int64_t mp_time_us(void);
|
||||||
// Return time in nanoseconds. Never wraps. Never returns 0 or negative values.
|
// Return time in nanoseconds. Never wraps. Never returns 0 or negative values.
|
||||||
int64_t mp_time_ns(void);
|
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.
|
// be much worse when casted to float.
|
||||||
double mp_time_sec(void);
|
double mp_time_sec(void);
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ double mp_time_sec(void);
|
||||||
void mp_raw_time_init(void);
|
void mp_raw_time_init(void);
|
||||||
uint64_t mp_raw_time_ns(void);
|
uint64_t mp_raw_time_ns(void);
|
||||||
|
|
||||||
// Sleep in microseconds.
|
// Sleep in nanoseconds.
|
||||||
void mp_sleep_us(int64_t us);
|
void mp_sleep_ns(int64_t ns);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// returns: timer resolution in ms if needed and started successfully, else 0
|
// returns: timer resolution in ms if needed and started successfully, else 0
|
||||||
|
|
|
@ -343,7 +343,7 @@ static int mapper_map(struct ra_hwdec_mapper *mapper)
|
||||||
// of the above StretchRect. Timeout of 8ms is required to reliably
|
// of the above StretchRect. Timeout of 8ms is required to reliably
|
||||||
// render 4k on Intel Haswell, Ivybridge and Cherry Trail Atom.
|
// render 4k on Intel Haswell, Ivybridge and Cherry Trail Atom.
|
||||||
const int max_retries = 8;
|
const int max_retries = 8;
|
||||||
const int64_t wait_us = 1000;
|
const int64_t wait_ns = 1e6;
|
||||||
int retries = 0;
|
int retries = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
hr = IDirect3DQuery9_GetData(p->query9, NULL, 0, D3DGETDATA_FLUSH);
|
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) {
|
} else if (hr == S_FALSE) {
|
||||||
if (++retries > max_retries) {
|
if (++retries > max_retries) {
|
||||||
MP_VERBOSE(mapper, "Failed to flush frame after %lld ms\n",
|
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;
|
break;
|
||||||
}
|
}
|
||||||
mp_sleep_us(wait_us);
|
mp_sleep_ns(wait_ns);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,14 +40,14 @@ static void flip_page(struct vo *vo)
|
||||||
{
|
{
|
||||||
struct priv *p = vo->priv;
|
struct priv *p = vo->priv;
|
||||||
if (p->cfg_fps) {
|
if (p->cfg_fps) {
|
||||||
int64_t ft = 1e6 / p->cfg_fps;
|
int64_t ft = 1e9 / p->cfg_fps;
|
||||||
int64_t prev_vsync = mp_time_us() / ft;
|
int64_t prev_vsync = mp_time_ns() / ft;
|
||||||
int64_t target_time = (prev_vsync + 1) * ft;
|
int64_t target_time = (prev_vsync + 1) * ft;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int64_t now = mp_time_us();
|
int64_t now = mp_time_ns();
|
||||||
if (now >= target_time)
|
if (now >= target_time)
|
||||||
break;
|
break;
|
||||||
mp_sleep_us(target_time - now);
|
mp_sleep_ns(target_time - now);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -299,7 +299,7 @@ static void wait_for_completion(struct vo *vo, int max_outstanding)
|
||||||
" for XShm completion events...\n");
|
" for XShm completion events...\n");
|
||||||
ctx->Shm_Warned_Slow = 1;
|
ctx->Shm_Warned_Slow = 1;
|
||||||
}
|
}
|
||||||
mp_sleep_us(1000);
|
mp_sleep_ns(1e6);
|
||||||
vo_x11_check_events(vo);
|
vo_x11_check_events(vo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -670,7 +670,7 @@ static void wait_for_completion(struct vo *vo, int max_outstanding)
|
||||||
" for XShm completion events...\n");
|
" for XShm completion events...\n");
|
||||||
ctx->Shm_Warned_Slow = 1;
|
ctx->Shm_Warned_Slow = 1;
|
||||||
}
|
}
|
||||||
mp_sleep_us(1000);
|
mp_sleep_ns(1e6);
|
||||||
vo_x11_check_events(vo);
|
vo_x11_check_events(vo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue