musiclight2/utils.c

44 lines
969 B
C
Raw Normal View History

2012-07-13 21:18:35 +02:00
/*
* vim: sw=2 ts=2 expandtab
*
2013-02-02 21:54:53 +01:00
* "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
2012-07-13 21:18:35 +02:00
*/
#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);
}