]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/Timer: add SafeTimer::add_event_at(real_clock::time_point)
authorKefu Chai <kchai@redhat.com>
Thu, 4 Feb 2021 07:05:05 +0000 (15:05 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 4 Feb 2021 11:26:52 +0000 (19:26 +0800)
so we can schedule an event with real_clock::time_point. timer should
wait using a mono_time, but user might want to pass a
real_clock::time_point to it when, for instance, scheduling a task which
should be performed in future at a specified time. in this case, we need
to convert the real_clock::time_point to a "known" time_point.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/Timer.cc
src/common/Timer.h

index eab46661c99e10a65d6e5f9173bd83a31cf06efd..17547962086e691b3e2c7ce181220e95ab0162c8 100644 (file)
@@ -153,6 +153,18 @@ Context* SafeTimer::add_event_at(SafeTimer::clock_t::time_point when, Context *c
   return callback;
 }
 
+Context* SafeTimer::add_event_at(ceph::real_clock::time_point when, Context *callback)
+{
+  ceph_assert(ceph_mutex_is_locked(lock));
+  // convert from real_clock to mono_clock
+  auto mono_now = ceph::mono_clock::now();
+  auto real_now = ceph::real_clock::now();
+  const auto delta = when - real_now;
+  const auto mono_atime = (mono_now +
+                          std::chrono::ceil<clock_t::duration>(delta));
+  return add_event_at(mono_atime, callback);
+}
+
 bool SafeTimer::cancel_event(Context *callback)
 {
   ceph_assert(ceph_mutex_is_locked(lock));
index f543be68a766402836556145c5d057d999e0efa2..32ca733b361e40e561646f92f951f698d7128db0 100644 (file)
@@ -77,7 +77,7 @@ public:
   Context* add_event_after(ceph::timespan duration, Context *callback);
   Context* add_event_after(double seconds, Context *callback);
   Context* add_event_at(clock_t::time_point when, Context *callback);
-
+  Context* add_event_at(ceph::real_clock::time_point when, Context *callback);
   /* Cancel an event.
    * Call with the event_lock LOCKED
    *