aboutsummaryrefslogtreecommitdiff
path: root/jim-eventloop.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2022-12-01 10:13:57 +1000
committerSteve Bennett <steveb@workware.net.au>2023-02-13 10:44:10 +1000
commita5ea6b096e9e9f9913ad860a847e3757580dd9e4 (patch)
treec3e317177151e3ad83e996ee198b28aff4765b03 /jim-eventloop.c
parent4b26f0ec1fa38bcfc8b2bd500a9bef10372bb140 (diff)
downloadjimtcl-a5ea6b096e9e9f9913ad860a847e3757580dd9e4.zip
jimtcl-a5ea6b096e9e9f9913ad860a847e3757580dd9e4.tar.gz
jimtcl-a5ea6b096e9e9f9913ad860a847e3757580dd9e4.tar.bz2
clock millis, time: now use monotonic raw time if possible
Instead of using all time, these commands now use a monotonically increasing system timer so that they are not affected by time (e.g. ntp) adjustments. (But not on Windows since it doesn't work reliably) Fixes #240 Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim-eventloop.c')
-rw-r--r--jim-eventloop.c34
1 files changed, 4 insertions, 30 deletions
diff --git a/jim-eventloop.c b/jim-eventloop.c
index 782d5dd..b9e0e97 100644
--- a/jim-eventloop.c
+++ b/jim-eventloop.c
@@ -237,32 +237,6 @@ void Jim_DeleteFileHandler(Jim_Interp *interp, int fd, int mask)
}
}
-/**
- * Returns the time of day in microseconds.
- * (the time base is not relevant here)
- */
-static jim_wide JimGetTimeUsec(Jim_EventLoop *eventLoop)
-{
- long long now;
- struct timeval tv;
-
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC_RAW)
- struct timespec ts;
-
- if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0) {
- now = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000;
- }
- else
-#endif
- {
- gettimeofday(&tv, NULL);
-
- now = tv.tv_sec * 1000000LL + tv.tv_usec;
- }
-
- return now;
-}
-
jim_wide Jim_CreateTimeHandler(Jim_Interp *interp, jim_wide us,
Jim_TimeProc * proc, void *clientData, Jim_EventFinalizerProc * finalizerProc)
{
@@ -273,7 +247,7 @@ jim_wide Jim_CreateTimeHandler(Jim_Interp *interp, jim_wide us,
te = Jim_Alloc(sizeof(*te));
te->id = id;
te->initialus = us;
- te->when = JimGetTimeUsec(eventLoop) + us;
+ te->when = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) + us;
te->timeProc = proc;
te->finalizerProc = finalizerProc;
te->clientData = clientData;
@@ -374,7 +348,7 @@ jim_wide Jim_DeleteTimeHandler(Jim_Interp *interp, jim_wide id)
if (te) {
jim_wide remain;
- remain = te->when - JimGetTimeUsec(eventLoop);
+ remain = te->when - Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW);
remain = (remain < 0) ? 0 : remain;
Jim_FreeTimeHandler(interp, te);
@@ -432,7 +406,7 @@ int Jim_ProcessEvents(Jim_Interp *interp, int flags)
/* Calculate the time missing for the nearest
* timer to fire. */
- sleep_us = shortest->when - JimGetTimeUsec(eventLoop);
+ sleep_us = shortest->when - Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW);
if (sleep_us < 0) {
sleep_us = 0;
}
@@ -534,7 +508,7 @@ int Jim_ProcessEvents(Jim_Interp *interp, int flags)
te = te->next;
continue;
}
- if (JimGetTimeUsec(eventLoop) >= te->when) {
+ if (Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) >= te->when) {
id = te->id;
/* Remove from the list before executing */
Jim_RemoveTimeHandler(eventLoop, id);