From 3c30aa330717955ab9922f9207f4b8ca4043888c Mon Sep 17 00:00:00 2001
From: Nadav Har'El <nyh@cloudius-systems.com>
Date: Sun, 26 Jan 2014 15:35:42 +0200
Subject: [PATCH] clock: Use new clock APIs in callout implementation

Change callout implementation to use the new <osv/clock.hh> APIs and the
monotonic clock.

Since _callout.h now uses the C++ type osv::clock::uptime::time_point,
it can only be used from C++ code. All the relevant code is already C++.

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 bsd/porting/_callout.h |  3 ++-
 bsd/porting/callout.cc | 15 +++++++++------
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/bsd/porting/_callout.h b/bsd/porting/_callout.h
index e1411619a..6953d11cf 100644
--- a/bsd/porting/_callout.h
+++ b/bsd/porting/_callout.h
@@ -39,6 +39,7 @@
 #define	_SYS__CALLOUT_H
 
 #include <osv/mutex.h>
+#include <osv/clock.hh>
 
 struct callout {
 	/* OSv waiter thread for drain (drain) */
@@ -48,7 +49,7 @@ struct callout {
 	uint64_t c_ticks;
 	/* Time when callout will be dispatched, both in ticks and in ns */
 	uint64_t c_time;
-	uint64_t c_to_ns;
+	osv::clock::uptime::time_point c_to_ns;
 	/* Callout Handler */
 	void (*c_fn)(void *);
 	void* c_arg;
diff --git a/bsd/porting/callout.cc b/bsd/porting/callout.cc
index 1eb294a3e..b8621a0ea 100644
--- a/bsd/porting/callout.cc
+++ b/bsd/porting/callout.cc
@@ -7,10 +7,11 @@
 
 #include <mutex>
 #include <set>
-#include "drivers/clock.hh"
 #include "osv/trace.hh"
 #include <osv/debug.hh>
 #include <osv/sched.hh>
+#include <osv/clock.hh>
+using namespace osv::clock::literals;
 
 #include <bsd/porting/rwlock.h>
 #include <bsd/porting/callout.h>
@@ -127,9 +128,9 @@ static void _callout_thread(void)
         // Wait for timeout //
         //////////////////////
 
-        uint64_t cur = clock::get()->time();
+        auto cur = osv::clock::uptime::now();
         bool expired = true;
-        if (cur < c->c_to_ns-TMILISECOND) {
+        if (cur < c->c_to_ns-1_ms) {
             sched::timer t(*sched::thread::current());
             t.set(c->c_to_ns);
 
@@ -213,8 +214,10 @@ static void _callout_thread(void)
 int callout_reset_on(struct callout *c, u64 to_ticks, void (*fn)(void *),
     void *arg, int ignore_cpu)
 {
-    u64 cur = clock::get()->time();
-    int cur_ticks = ns2ticks(cur);
+    auto cur = osv::clock::uptime::now();
+    int cur_ticks = ns2ticks(
+            std::chrono::duration_cast<std::chrono::nanoseconds>
+                (cur.time_since_epoch()).count());
     int result = 0;
     bool queued_first = false;
 
@@ -227,7 +230,7 @@ int callout_reset_on(struct callout *c, u64 to_ticks, void (*fn)(void *),
     // Reset the callout
     c->c_ticks = to_ticks;
     c->c_time = cur_ticks + to_ticks;           // for freebsd compatibility
-    c->c_to_ns = cur + ticks2ns(to_ticks);      // this is what we use
+    c->c_to_ns = cur + ticks2ns(to_ticks) * 1_ns;      // this is what we use
     c->c_fn = fn;
     c->c_arg = arg;
     c->c_flags |= (CALLOUT_PENDING | CALLOUT_ACTIVE);
-- 
GitLab