static inline int usleep_safe(usec_t usec) {
/* usleep() takes useconds_t that is (typically?) uint32_t. Also, usleep() may only support the
- * range [0, 1000000]. See usleep(3). Let's override usleep() with nanosleep(). */
+ * range [0, 1000000]. See usleep(3). Let's override usleep() with clock_nanosleep().
+ *
+ * ⚠️ Note we are not using plain nanosleep() here, since that operates on CLOCK_REALTIME, not
+ * CLOCK_MONOTONIC! */
// FIXME: use RET_NERRNO() macro here. Currently, this header cannot include errno-util.h.
- return nanosleep(TIMESPEC_STORE(usec), NULL) < 0 ? -errno : 0;
+ return clock_nanosleep(CLOCK_MONOTONIC, 0, TIMESPEC_STORE(usec), NULL) < 0 ? -errno : 0;
}
/* The last second we can format is 31. Dec 9999, 1s before midnight, because otherwise we'd enter 5 digit