diff --git a/util.c b/util.c index f6cb9242..77956e30 100644 --- a/util.c +++ b/util.c @@ -914,6 +914,12 @@ void us_to_timespec(struct timespec *spec, int64_t us) spec->tv_nsec = (us - (spec->tv_sec * 1000000)) * 1000; } +void ms_to_timespec(struct timespec *spec, int64_t ms) +{ + spec->tv_sec = ms / 1000; + spec->tv_nsec = (ms - (spec->tv_sec * 1000)) * 1000000; +} + void timeraddspec(struct timespec *a, const struct timespec *b) { a->tv_sec += b->tv_sec; @@ -924,6 +930,19 @@ void timeraddspec(struct timespec *a, const struct timespec *b) } } +void cgsleep_ms(int ms) +{ + struct timespec ts_start, ts_end; + int ret; + + clock_gettime(CLOCK_MONOTONIC, &ts_start); + ms_to_timespec(&ts_end, ms); + timeraddspec(&ts_end, &ts_start); + do { + ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_end, NULL); + } while (ret == EINTR); +} + /* Returns the microseconds difference between end and start times as a double */ double us_tdiff(struct timeval *end, struct timeval *start) { diff --git a/util.h b/util.h index 12ff7cd2..e5d2e44e 100644 --- a/util.h +++ b/util.h @@ -82,7 +82,9 @@ void timespec_to_val(struct timeval *val, const struct timespec *spec); void timeval_to_spec(struct timespec *spec, const struct timeval *val); void us_to_timeval(struct timeval *val, int64_t us); void us_to_timespec(struct timespec *spec, int64_t us); +void ms_to_timespec(struct timespec *spec, int64_t ms); void timeraddspec(struct timespec *a, const struct timespec *b); +void cgsleep_ms(int ms); double us_tdiff(struct timeval *end, struct timeval *start); double tdiff(struct timeval *end, struct timeval *start); bool stratum_send(struct pool *pool, char *s, ssize_t len);