29 lines
1.1 KiB
C
29 lines
1.1 KiB
C
|
// From https://github.com/veltzer/demos-linux
|
||
|
const long long NSEC_PER_SEC=1000000000;
|
||
|
|
||
|
static inline unsigned long long timespec_diff_nano(struct timespec* x, struct timespec* y) {
|
||
|
return (x->tv_sec-y->tv_sec)*NSEC_PER_SEC+(x->tv_nsec-y->tv_nsec);
|
||
|
}
|
||
|
|
||
|
static inline void timespec_sub(struct timespec* result, struct timespec* x, struct timespec* y) {
|
||
|
/* Perform the carry for the later subtraction by updating Y. */
|
||
|
if (x->tv_nsec < y->tv_nsec) {
|
||
|
int nsec = (y->tv_nsec - x->tv_nsec) / NSEC_PER_SEC + 1;
|
||
|
y->tv_nsec -= NSEC_PER_SEC * nsec;
|
||
|
y->tv_sec += nsec;
|
||
|
}
|
||
|
if (x->tv_nsec - y->tv_nsec > NSEC_PER_SEC) {
|
||
|
int nsec = (x->tv_nsec - y->tv_nsec) / NSEC_PER_SEC;
|
||
|
y->tv_nsec += NSEC_PER_SEC * nsec;
|
||
|
y->tv_sec -= nsec;
|
||
|
}
|
||
|
/* Compute the time remaining to wait. `tv_nsec' is certainly positive. */
|
||
|
result->tv_sec = x->tv_sec - y->tv_sec;
|
||
|
result->tv_nsec = x->tv_nsec - y->tv_nsec;
|
||
|
|
||
|
/* Check that result is not negative */
|
||
|
// CHECK_ASSERT(x->tv_sec >= y->tv_sec);
|
||
|
}
|
||
|
|
||
|
// END From https://github.com/veltzer/demos-linux
|