sk6812d/src/utils.c

44 lines
980 B
C
Raw Normal View History

2018-06-13 21:51:30 +02:00
/*
* vim: sw=2 ts=2 expandtab
*
* "THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
* Thomas Kolb <cfr34k@tkolb.de> wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a pizza in return.
* - Thomas Kolb
*/
#include <errno.h>
#include <time.h>
#include <stdint.h>
#include "utils.h"
double get_hires_time(void) {
struct timespec clk;
clock_gettime(CLOCK_REALTIME, &clk);
return (double)clk.tv_sec + 1.0e-9f * clk.tv_nsec;
}
void fsleep(double d) {
struct timespec ts;
ts.tv_sec = (time_t)d;
ts.tv_nsec = (long)(1e9 * (d - (long)d));
nanosleep(&ts, NULL);
}
void sleep_until(double hires_time) {
struct timespec tv;
int ret;
tv.tv_sec = hires_time;
tv.tv_nsec = (uint64_t)(1e9 * hires_time) % 1000000000;
do {
ret = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &tv, NULL);
} while(ret == EINTR);
}