From 74a57376abeda468767903692c8d4efd6c348ef6 Mon Sep 17 00:00:00 2001
From: Nadav Har'El <nyh@cloudius-systems.com>
Date: Sun, 26 Jan 2014 15:38:00 +0200
Subject: [PATCH] clock: Remove old type-less time literals

Drop the s64 literals _ms, _ns, etc., from <drivers/clock.hh>.
Fix a few places which still use the old literals.

The std:chrono::duration version from <osv/clock.hh> remains -
but remember you need to "using namespace osv::clock::literals"
to use them.

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>
---
 core/dhcp.cc               |  4 ++--
 core/poll.cc               |  3 ++-
 drivers/acpi.cc            |  3 +--
 drivers/clock.hh           | 21 ---------------------
 drivers/xenfront-xenbus.cc |  3 ++-
 libc/timerfd.cc            | 22 ++++++++++++----------
 tests/tst-epoll.cc         |  7 ++++---
 tests/tst-timer.hh         | 13 ++++++++++++-
 8 files changed, 35 insertions(+), 41 deletions(-)

diff --git a/core/dhcp.cc b/core/dhcp.cc
index 4b7c960f3..2efea8110 100644
--- a/core/dhcp.cc
+++ b/core/dhcp.cc
@@ -585,8 +585,8 @@ namespace dhcp {
                 _waiter = sched::thread::current();
 
                 sched::timer t(*sched::thread::current());
-                u64 cur_time = clock::get()->time();
-                t.set(cur_time + 3_s);
+                using namespace osv::clock::literals;
+                t.set(3_s);
 
                 sched::thread::wait_until([&]{ return _have_ip || t.expired(); });
             }
diff --git a/core/poll.cc b/core/poll.cc
index 9986dbe78..ca558cebd 100644
--- a/core/poll.cc
+++ b/core/poll.cc
@@ -279,7 +279,8 @@ int do_poll(std::vector<poll_file>& pfd, int _timeout)
     /* Timeout */
     if (p._timeout > 0) {
         /* Convert timeout of ms to ns */
-        tmr.set(clock::get()->time() + p._timeout * 1_ms);
+        using namespace osv::clock::literals;
+        tmr.set(p._timeout * 1_ms);
     }
 
     /* Block  */
diff --git a/drivers/acpi.cc b/drivers/acpi.cc
index 8dc3d3932..d9d584fce 100644
--- a/drivers/acpi.cc
+++ b/drivers/acpi.cc
@@ -16,7 +16,6 @@ extern "C" {
 #include <stdlib.h>
 #include <osv/mmu.hh>
 #include <osv/sched.hh>
-#include "drivers/clock.hh"
 #include "processor.hh"
 #include <osv/align.hh>
 #include "xen.hh"
@@ -138,7 +137,7 @@ ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle,
         return AE_OK;
     default:
         sched::timer timer(*sched::thread::current());
-        timer.set(nanotime() + Timeout * 1_ms);
+        timer.set(std::chrono::milliseconds(Timeout));
         return sem->wait(Units, &timer) ? AE_OK : AE_TIME;
     }
 }
diff --git a/drivers/clock.hh b/drivers/clock.hh
index 14b63aeb7..36e6dbe68 100644
--- a/drivers/clock.hh
+++ b/drivers/clock.hh
@@ -95,27 +95,6 @@ public:
 private:
     static clock* _c;
 };
-
-inline constexpr long long operator"" _ns(unsigned long long t)
-{
-    return t;
-}
-
-inline constexpr long long operator"" _us(unsigned long long t)
-{
-    return t * 1000_ns;
-}
-
-inline constexpr long long operator"" _ms(unsigned long long t)
-{
-    return t * 1000_us;
-}
-
-inline constexpr long long operator"" _s(unsigned long long t)
-{
-    return t * 1000_ms;
-}
-
 static inline s64 nanotime() {
     return clock::get()->time();
 }
diff --git a/drivers/xenfront-xenbus.cc b/drivers/xenfront-xenbus.cc
index 837237eea..64eddf9fb 100644
--- a/drivers/xenfront-xenbus.cc
+++ b/drivers/xenfront-xenbus.cc
@@ -84,7 +84,8 @@ void xenbus::wait_for_devices()
 {
     WITH_LOCK(_children_mutex) {
         while (!_pending_children.empty() || _children.empty()) {
-            condvar_wait(&_pending_devices, &_children_mutex, nanotime() + 1000_ms);
+            using namespace osv::clock::literals;
+            _pending_devices.wait(&_children_mutex, 1000_ms);
         }
         for (auto device : _pending_children) {
             debug("Device %s bringup failed\n", device->get_name());
diff --git a/libc/timerfd.cc b/libc/timerfd.cc
index ab67c08ef..b778b26a3 100644
--- a/libc/timerfd.cc
+++ b/libc/timerfd.cc
@@ -252,6 +252,8 @@ int timerfd_create(int clockid, int flags) {
     }
 }
 
+static constexpr s64 second = 1000000000;
+
 int timerfd_settime(int fd, int flags, const itimerspec *newval,
         itimerspec *oldval)
 {
@@ -274,14 +276,14 @@ int timerfd_settime(int fd, int flags, const itimerspec *newval,
             // oldval is always returned in relative time
             expiration -= now;
         }
-        oldval->it_value.tv_sec = expiration / 1_s;
-        oldval->it_value.tv_nsec = expiration % 1_s;
-        oldval->it_interval.tv_sec = interval / 1_s;
-        oldval->it_interval.tv_nsec = interval % 1_s;
+        oldval->it_value.tv_sec = expiration / second;
+        oldval->it_value.tv_nsec = expiration % second;
+        oldval->it_interval.tv_sec = interval / second;
+        oldval->it_interval.tv_nsec = interval % second;
     }
 
-    expiration = newval->it_value.tv_sec * 1_s + newval->it_value.tv_nsec;
-    interval = newval->it_interval.tv_sec * 1_s + newval->it_interval.tv_nsec;
+    expiration = newval->it_value.tv_sec * second + newval->it_value.tv_nsec;
+    interval = newval->it_interval.tv_sec * second + newval->it_interval.tv_nsec;
     if (flags != TFD_TIMER_ABSTIME && expiration) {
         expiration += now;
     }
@@ -310,9 +312,9 @@ int timerfd_gettime(int fd, itimerspec *val)
         // timerfd_gettime() wants relative time
         expiration -= now;
     }
-    val->it_value.tv_sec = expiration / 1_s;
-    val->it_value.tv_nsec = expiration % 1_s;
-    val->it_interval.tv_sec = interval / 1_s;
-    val->it_interval.tv_nsec = interval % 1_s;
+    val->it_value.tv_sec = expiration / second;
+    val->it_value.tv_nsec = expiration % second;
+    val->it_interval.tv_sec = interval / second;
+    val->it_interval.tv_nsec = interval % second;
     return 0;
 }
diff --git a/tests/tst-epoll.cc b/tests/tst-epoll.cc
index f8de178cd..c39e09012 100644
--- a/tests/tst-epoll.cc
+++ b/tests/tst-epoll.cc
@@ -10,7 +10,7 @@
 #include <unistd.h>
 #include <osv/sched.hh>
 #include <osv/debug.hh>
-#include "drivers/clock.hh"
+#include <osv/clock.hh>
 
 int tests = 0, fails = 0;
 
@@ -90,9 +90,10 @@ int main(int ac, char** av)
     report(r == 1, "write single character");
     t1.join();
 
-    auto ts = clock::get()->time();
+    auto ts = osv::clock::uptime::now();
     r = epoll_wait(ep, events, MAXEVENTS, 300);
-    auto te = clock::get()->time();
+    auto te = osv::clock::uptime::now();
+    using namespace osv::clock::literals;
     report(r == 0 && ((te - ts) > 200_ms), "epoll timeout");
 
 
diff --git a/tests/tst-timer.hh b/tests/tst-timer.hh
index 1588c203d..8038bb3b3 100644
--- a/tests/tst-timer.hh
+++ b/tests/tst-timer.hh
@@ -12,6 +12,17 @@
 #include "drivers/clock.hh"
 #include <osv/debug.hh>
 #include <osv/sched.hh>
+// TODO: change this test to check the newer <osv/clock.hh> APIs and the
+// monotonic clock.
+
+inline constexpr long long operator"" _ms(unsigned long long t)
+{
+    return t * 1000000ULL;
+}
+inline constexpr long long operator"" _s(unsigned long long t)
+{
+    return t * 1000_ms;
+}
 
 class test_timer : public unit_tests::vtest {
 
@@ -47,7 +58,7 @@ public:
         for (int i=0; i<tester_iteration; i++) {
             u64 ns = (random() % 1_s) - 500_ms;
             sched::timer t(*sched::thread::current());
-            t.set(clock::get()->time() + ns);
+            t.set(std::chrono::nanoseconds(ns));
 
             sched::thread::wait_until([&] { return (t.expired()); });
         }
-- 
GitLab