[PATCH 1/3] lib/timer: Add current_time_now() function for immediate timestamp

Toke Høiland-Jørgensen toke at toke.dk
Sun Feb 26 23:10:04 CET 2023


Add a current_time_now() function which gets an immediate monotonic
timestamp instead of using the cached value from the event loop. This is
useful for callers that need precise times, such as the Babel RTT
measurement code.

Signed-off-by: Toke Høiland-Jørgensen <toke at toke.dk>
---
 lib/timer.c | 13 +++++++++++++
 lib/timer.h |  1 +
 2 files changed, 14 insertions(+)

diff --git a/lib/timer.c b/lib/timer.c
index c47e0bbc8b78..a96b051aaf9c 100644
--- a/lib/timer.c
+++ b/lib/timer.c
@@ -76,6 +76,19 @@ current_time(void)
   return timeloop_current()->last_time;
 }
 
+btime
+current_time_now(void)
+{
+  struct timespec ts;
+  int rv;
+
+  rv = clock_gettime(CLOCK_MONOTONIC, &ts);
+  if (rv < 0)
+    die("clock_gettime: %m");
+
+  return ts.tv_sec S + ts.tv_nsec NS;
+}
+
 btime
 current_real_time(void)
 {
diff --git a/lib/timer.h b/lib/timer.h
index c5ea430cdb81..bfd9904e04b6 100644
--- a/lib/timer.h
+++ b/lib/timer.h
@@ -44,6 +44,7 @@ static inline timer *timers_first(struct timeloop *loop)
 extern struct timeloop main_timeloop;
 
 btime current_time(void);
+btime current_time_now(void);
 btime current_real_time(void);
 
 //#define now (current_time() TO_S)
-- 
2.39.1



More information about the Bird-users mailing list