7 #include "eventloopinterface.h" 19 constexpr uint64_t USEC_INFINITY =
static_cast<uint64_t
>(-1);
20 constexpr uint64_t USEC_PER_SEC = 1000000ULL;
21 constexpr uint64_t NSEC_PER_USEC = 1000ULL;
23 std::string getErrorMessage(
int error) {
27 return std::format(
"EventLoopException: {0}", std::strerror(error));
33 uint64_t timespec_load(
const struct timespec *ts) {
34 if (ts->tv_sec == (time_t)-1 && ts->tv_nsec == (
long)-1) {
38 if ((uint64_t)ts->tv_sec >
39 (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) {
43 return ((uint64_t)ts->tv_sec * USEC_PER_SEC) +
44 ((uint64_t)ts->tv_nsec / NSEC_PER_USEC);
47 uint64_t now(clockid_t clock_id) {
49 clock_gettime(clock_id, &ts);
51 return timespec_load(&ts);
54 EventLoopException::EventLoopException(
int error)
55 :
std::runtime_error(getErrorMessage(error)), errno_(error) {}
57 void EventSourceTime::setNextInterval(uint64_t time) {
58 setTime(now(clock()) + time);
61 EventSource::~EventSource() =
default;
63 EventLoopInterface::~EventLoopInterface() =
default;