kevin.cianfarini
04/08/2024, 4:49 PMdelay
when an NTP sync occurs.Klitos Kyriacou
04/08/2024, 5:19 PMSystem.currentTimeMillis()
and System.nanoTime()
.Dmitry Khalanskiy [JB]
04/09/2024, 8:43 AMvar lastObservedInstant = Clock.System.now()
while (true) {
delay(1.seconds)
val newInstant = Clock.System.now()
val duration = newInstant - lastObservedInstant
if (/* duration differs too much from one second */) {
notifyAboutDrift()
}
}
Klitos Kyriacou
04/09/2024, 8:58 AMkevin.cianfarini
04/09/2024, 12:02 PMKlitos Kyriacou
04/09/2024, 12:11 PMkevin.cianfarini
04/09/2024, 12:18 PMsleep
uses monotonic time.
``` POSIX.1 specifies that nanosleep() should measure time against
the CLOCK_REALTIME clock. However, Linux measures the time using
the CLOCK_MONOTONIC clock. This probably does not matter, since
the POSIX.1 specification for clock_settime(2) says that
discontinuous changes in CLOCK_REALTIME should not affect
nanosleep():
Setting the value of the CLOCK_REALTIME clock via
clock_settime(2) shall have no effect on threads that are
blocked waiting for a relative time service based upon
this clock, including the nanosleep() function; ...
Consequently, these time services shall expire when the
requested relative interval elapses, independently of the
new or old value of the clock.```https://www.man7.org/linux/man-pages/man2/nanosleep.2.html
Klitos Kyriacou
04/09/2024, 12:26 PMkevin.cianfarini
04/09/2024, 12:27 PMsleep
would also have to contend with NTP sync if they're trying to work against real timekevin.cianfarini
04/09/2024, 12:43 PMkevin.cianfarini
04/09/2024, 12:44 PMkevin.cianfarini
04/09/2024, 12:45 PMint timeDiff;
enum timejump wakeupKind;
/* ... wait for the time (in minutes) to change ... */
do {
cron_sleep(timeRunning + 1, &database);
set_time(FALSE);
} while (!got_sigintterm && clockTime == timeRunning);
if (got_sigintterm)
break;
timeRunning = clockTime;
/*
* Calculate how the current time differs from our virtual
* clock. Classify the change into one of 4 cases.
*/
timeDiff = timeRunning - virtualTime;
check_orphans(&database);
So looks like @Dmitry Khalanskiy [JB]’s suggestion is the way to go!