summaryrefslogtreecommitdiffstats
path: root/osdep
diff options
context:
space:
mode:
authorDudemanguy <random342@airmail.cc>2023-09-29 17:24:21 -0500
committerDudemanguy <random342@airmail.cc>2023-10-10 19:10:55 +0000
commit59dd7d94af7651baf7e60966c5f8e7d52959d958 (patch)
treec6eb2938d2a63ebb07e377794f28ca708d786abd /osdep
parentfcebee9080a113f3248d218e451345db3f965b47 (diff)
downloadmpv-59dd7d94af7651baf7e60966c5f8e7d52959d958.tar.bz2
mpv-59dd7d94af7651baf7e60966c5f8e7d52959d958.tar.xz
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.
Diffstat (limited to 'osdep')
-rw-r--r--osdep/timer-darwin.c5
-rw-r--r--osdep/timer-linux.c8
-rw-r--r--osdep/timer-win2.c12
-rw-r--r--osdep/timer.h6
4 files changed, 15 insertions, 16 deletions
diff --git a/osdep/timer-darwin.c b/osdep/timer-darwin.c
index a114d0d727..bb8a9b4324 100644
--- a/osdep/timer-darwin.c
+++ b/osdep/timer-darwin.c
@@ -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);
}
diff --git a/osdep/timer-linux.c b/osdep/timer-linux.c
index 0289233695..ff4b137cb2 100644
--- a/osdep/timer-linux.c
+++ b/osdep/timer-linux.c
@@ -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);
}
diff --git a/osdep/timer-win2.c b/osdep/timer-win2.c
index dd7a42f0d1..db4c39d7ef 100644
--- a/osdep/timer-win2.c
+++ b/osdep/timer-win2.c
@@ -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);
}
diff --git a/osdep/timer.h b/osdep/timer.h
index d76ad16129..b273cb405e 100644
--- a/osdep/timer.h
+++ b/osdep/timer.h
@@ -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