1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <assert.h>
#include <pthread.h>
#include <time.h>
#include "time-util.h"
void get_monotonic_time(struct timespec *res)
{
struct timespec ts;
int rv = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(rv == 0);
*res = ts;
}
/* Initialize @cond as a condition variable that uses the monotonic clock */
void pthread_cond_init_mono(pthread_cond_t *cond)
{
pthread_condattr_t attr;
int res;
res = pthread_condattr_init(&attr);
assert(res == 0);
res = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
assert(res == 0);
res = pthread_cond_init(cond, &attr);
assert(res == 0);
res = pthread_condattr_destroy(&attr);
assert(res == 0);
}
/* Ensure that 0 <= ts->tv_nsec && ts->tv_nsec < 1000 * 1000 * 1000. */
void normalize_timespec(struct timespec *ts)
{
while (ts->tv_nsec < 0) {
ts->tv_nsec += 1000L * 1000 * 1000;
ts->tv_sec--;
}
while (ts->tv_nsec >= 1000L * 1000 * 1000) {
ts->tv_nsec -= 1000L * 1000 * 1000;
ts->tv_sec++;
}
}
/* Compute *res = *a - *b */
void timespecsub(const struct timespec *a, const struct timespec *b,
struct timespec *res)
{
res->tv_sec = a->tv_sec - b->tv_sec;
res->tv_nsec = a->tv_nsec - b->tv_nsec;
normalize_timespec(res);
}
int timespeccmp(const struct timespec *a, const struct timespec *b)
{
struct timespec tmp;
timespecsub(a, b, &tmp);
if (tmp.tv_sec > 0)
return 1;
if (tmp.tv_sec < 0)
return -1;
return tmp.tv_nsec > 0 ? 1 : (tmp.tv_nsec < 0 ? -1 : 0);
}
|