5#if defined(HAVE_SYS_TIME_H)
35#define RB_HRTIME_PER_USEC ((rb_hrtime_t)1000)
36#define RB_HRTIME_PER_MSEC (RB_HRTIME_PER_USEC * (rb_hrtime_t)1000)
37#define RB_HRTIME_PER_SEC (RB_HRTIME_PER_MSEC * (rb_hrtime_t)1000)
38#define RB_HRTIME_MAX UINT64_MAX
44#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
45typedef int128_t rb_hrtime_t;
47typedef uint64_t rb_hrtime_t;
52rb_hrtime_t rb_hrtime_now(
void);
58static inline rb_hrtime_t
59rb_hrtime_mul(rb_hrtime_t a, rb_hrtime_t b)
63#ifdef HAVE_BUILTIN___BUILTIN_MUL_OVERFLOW
64 if (__builtin_mul_overflow(a, b, &c))
67 if (b != 0 && a > RB_HRTIME_MAX / b)
78static inline rb_hrtime_t
79rb_hrtime_add(rb_hrtime_t a, rb_hrtime_t b)
83#ifdef HAVE_BUILTIN___BUILTIN_ADD_OVERFLOW
84 if (__builtin_add_overflow(a, b, &c))
97static inline rb_hrtime_t
98rb_timeval2hrtime(
const struct timeval *tv)
100 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)tv->tv_sec, RB_HRTIME_PER_SEC);
101 rb_hrtime_t u = rb_hrtime_mul((rb_hrtime_t)tv->tv_usec, RB_HRTIME_PER_USEC);
103 return rb_hrtime_add(s, u);
109static inline rb_hrtime_t
110rb_timespec2hrtime(
const struct timespec *ts)
112 rb_hrtime_t s = rb_hrtime_mul((rb_hrtime_t)ts->tv_sec, RB_HRTIME_PER_SEC);
114 return rb_hrtime_add(s, (rb_hrtime_t)ts->tv_nsec);
120static inline rb_hrtime_t
121rb_msec2hrtime(
unsigned long msec)
123 return rb_hrtime_mul((rb_hrtime_t)msec, RB_HRTIME_PER_MSEC);
130static inline rb_hrtime_t
131rb_sec2hrtime(time_t sec)
133 if (sec <= 0)
return 0;
135 return rb_hrtime_mul((rb_hrtime_t)sec, RB_HRTIME_PER_SEC);
143rb_hrtime2timespec(
struct timespec *ts,
const rb_hrtime_t *hrt)
146 ts->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
147 ts->tv_nsec = (int32_t)(*hrt % RB_HRTIME_PER_SEC);
158rb_hrtime2timeval(
struct timeval *tv,
const rb_hrtime_t *hrt)
161 tv->tv_sec = (time_t)(*hrt / RB_HRTIME_PER_SEC);
162 tv->tv_usec = (int32_t)((*hrt % RB_HRTIME_PER_SEC)/RB_HRTIME_PER_USEC);