time-util: introduce jiffies_to_usec()

This commit is contained in:
Yu Watanabe 2019-07-29 23:47:04 +09:00
parent 57894dc989
commit 77372afbe0
2 changed files with 15 additions and 4 deletions

View file

@ -1414,8 +1414,8 @@ struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
}
unsigned long usec_to_jiffies(usec_t u) {
static thread_local unsigned long hz = 0;
static uint32_t sysconf_clock_ticks_cached(void) {
static thread_local uint32_t hz = 0;
long r;
if (hz == 0) {
@ -1425,7 +1425,17 @@ unsigned long usec_to_jiffies(usec_t u) {
hz = r;
}
return DIV_ROUND_UP(u , USEC_PER_SEC / hz);
return hz;
}
uint32_t usec_to_jiffies(usec_t u) {
uint32_t hz = sysconf_clock_ticks_cached();
return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
}
usec_t jiffies_to_usec(uint32_t j) {
uint32_t hz = sysconf_clock_ticks_cached();
return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
}
usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {

View file

@ -136,7 +136,8 @@ int get_timezone(char **timezone);
time_t mktime_or_timegm(struct tm *tm, bool utc);
struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
unsigned long usec_to_jiffies(usec_t usec);
uint32_t usec_to_jiffies(usec_t usec);
usec_t jiffies_to_usec(uint32_t jiffies);
bool in_utc_timezone(void);