Skip to content

Commit

Permalink
timer: change mp_sleep_us to mp_sleep_ns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dudemanguy committed Sep 29, 2023
1 parent 4b652ce commit 0290f23
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 28 deletions.
6 changes: 3 additions & 3 deletions audio/out/ao_wasapi_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1028,13 +1028,13 @@ bool wasapi_thread_init(struct ao *ao)
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;
}
Expand Down
5 changes: 2 additions & 3 deletions osdep/timer-darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions osdep/timer-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 6 additions & 6 deletions osdep/timer-win2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions osdep/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ 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);

// Provided by OS specific functions (timer-linux.c)
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
Expand Down
6 changes: 3 additions & 3 deletions video/out/opengl/hwdec_dxva2egl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions video/out/vo_null.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion video/out/vo_x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion video/out/vo_xv.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,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);
}
}
Expand Down

0 comments on commit 0290f23

Please sign in to comment.