musiclight2/utils.c

43 рядки
953 B
C

/*
* vim: sw=2 ts=2 expandtab
*
* THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
* <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 clk.tv_sec + 1e-9 * 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);
}