diff options
Diffstat (limited to 'osdep/timer-linux.c')
-rw-r--r-- | osdep/timer-linux.c | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/osdep/timer-linux.c b/osdep/timer-linux.c index 6cd88a253f..559a496636 100644 --- a/osdep/timer-linux.c +++ b/osdep/timer-linux.c @@ -18,40 +18,47 @@ * License along with mpv. If not, see <http://www.gnu.org/licenses/>. */ -#include <unistd.h> +#include <errno.h> #include <stdlib.h> #include <time.h> -#include <sys/time.h> -#include "config.h" + +#include "common/common.h" #include "timer.h" -void mp_sleep_us(int64_t us) +static clockid_t clk_id; + +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 / MP_TIME_S_TO_NS(1); + ts.tv_nsec = ns % MP_TIME_S_TO_NS(1); nanosleep(&ts, NULL); } -#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC) -uint64_t mp_raw_time_us(void) -{ - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts)) - abort(); - return ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; -} -#else -uint64_t mp_raw_time_us(void) +uint64_t mp_raw_time_ns(void) { - struct timeval tv; - gettimeofday(&tv,NULL); - return tv.tv_sec * 1000000LL + tv.tv_usec; + struct timespec tp = {0}; + clock_gettime(clk_id, &tp); + return MP_TIME_S_TO_NS(tp.tv_sec) + tp.tv_nsec; } -#endif void mp_raw_time_init(void) { + static const clockid_t clock_ids[] = { +#ifdef CLOCK_MONOTONIC_RAW + CLOCK_MONOTONIC_RAW, +#endif + CLOCK_MONOTONIC, + }; + + struct timespec tp; + for (int i = 0; i < MP_ARRAY_SIZE(clock_ids); ++i) { + clk_id = clock_ids[i]; + if (!clock_gettime(clk_id, &tp)) + return; + } + fputs("No clock source available!\n", stderr); + abort(); } |